| 
001 /*002  * Copyright 2014-2017 the original author or authors.
 003  *
 004  * Licensed under the Apache License, Version 2.0 (the "License");
 005  * you may not use this file except in compliance with the License.
 006  * You may obtain a copy of the License at
 007  *
 008  *     http://www.apache.org/licenses/LICENSE-2.0
 009  *
 010  * Unless required by applicable law or agreed to in writing, software
 011  * distributed under the License is distributed on an "AS IS" BASIS,
 012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 013  * See the License for the specific language governing permissions and
 014  * limitations under the License.
 015  */
 016 package griffon.swing.support.tangoicons;
 017
 018 import griffon.plugins.tangoicons.Tango;
 019
 020 import javax.annotation.Nonnull;
 021 import javax.swing.ImageIcon;
 022 import java.awt.Toolkit;
 023 import java.net.URL;
 024
 025 import static griffon.plugins.tangoicons.Tango.invalidDescription;
 026 import static griffon.plugins.tangoicons.Tango.requireValidSize;
 027 import static griffon.util.GriffonNameUtils.requireNonBlank;
 028 import static java.util.Objects.requireNonNull;
 029
 030 /**
 031  * @author Andres Almiray
 032  */
 033 public class TangoIcon extends ImageIcon {
 034     private static final String ERROR_TANGO_NULL = "Argument 'tango' must not be null";
 035     private Tango tango;
 036     private int size;
 037
 038     public TangoIcon() {
 039         this(Tango.findByDescription("apps:help-browser:16"));
 040     }
 041
 042     public TangoIcon(@Nonnull Tango tango) {
 043         this(tango, 16);
 044     }
 045
 046     public TangoIcon(@Nonnull Tango tango, int size) {
 047         super(toURL(tango, size));
 048         this.tango = requireNonNull(tango, ERROR_TANGO_NULL);
 049         this.size = size;
 050     }
 051
 052     public TangoIcon(@Nonnull String description) {
 053         this(Tango.findByDescription(description));
 054         setTango(description);
 055     }
 056
 057     @Nonnull
 058     private static URL toURL(@Nonnull Tango tango, int size) {
 059         requireNonNull(tango, ERROR_TANGO_NULL);
 060         String resource = tango.asResource(size);
 061         URL url = Thread.currentThread().getContextClassLoader().getResource(resource);
 062         if (url == null) {
 063             throw new IllegalArgumentException("Icon " + tango + " does not exist");
 064         }
 065         return url;
 066     }
 067
 068     @Nonnull
 069     public Tango getTango() {
 070         return tango;
 071     }
 072
 073     public void setTango(@Nonnull Tango tango) {
 074         this.tango = requireNonNull(tango, ERROR_TANGO_NULL);
 075         setImage(Toolkit.getDefaultToolkit().getImage(toURL(tango, size)));
 076     }
 077
 078     public void setTango(@Nonnull String description) {
 079         requireNonBlank(description, "Argument 'description' must not be blank");
 080
 081         String[] parts = description.split(":");
 082         if (parts.length == 3) {
 083             try {
 084                 int s = Integer.parseInt(parts[2]);
 085                 this.size = requireValidSize(s);
 086             } catch (NumberFormatException e) {
 087                 throw invalidDescription(description, e);
 088             }
 089         } else if (size == 0) {
 090             size = 16;
 091         }
 092
 093         tango = Tango.findByDescription(description);
 094         setImage(Toolkit.getDefaultToolkit().getImage(toURL(tango, size)));
 095     }
 096
 097     public int getSize() {
 098         return size;
 099     }
 100
 101     public void setSize(int size) {
 102         this.size = requireValidSize(size);
 103         setImage(Toolkit.getDefaultToolkit().getImage(toURL(tango, size)));
 104     }
 105 }
 |