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.crystalicons;
017
018 import griffon.plugins.crystalicons.Crystal;
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.crystalicons.Crystal.invalidDescription;
026 import static griffon.plugins.crystalicons.Crystal.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 CrystalIcon extends ImageIcon {
034 private static final String ERROR_CRYSTAL_NULL = "Argument 'crystal' must not be null";
035 private Crystal crystal;
036 private int size;
037
038 public CrystalIcon() {
039 this(Crystal.findByDescription("actions:bookmark"));
040 }
041
042 public CrystalIcon(@Nonnull Crystal crystal) {
043 this(crystal, 16);
044 }
045
046 public CrystalIcon(@Nonnull Crystal crystal, int size) {
047 super(toURL(crystal, size));
048 this.crystal = requireNonNull(crystal, ERROR_CRYSTAL_NULL);
049 this.size = size;
050 }
051
052 public CrystalIcon(@Nonnull String description) {
053 this(Crystal.findByDescription(description));
054 setCrystal(description);
055 }
056
057 @Nonnull
058 private static URL toURL(@Nonnull Crystal crystal, int size) {
059 requireNonNull(crystal, ERROR_CRYSTAL_NULL);
060 String resource = crystal.asResource(size);
061 URL url = Thread.currentThread().getContextClassLoader().getResource(resource);
062 if (url == null) {
063 throw new IllegalArgumentException("Icon " + crystal.formatted() + " does not exist");
064 }
065 return url;
066 }
067
068 @Nonnull
069 public Crystal getCrystal() {
070 return crystal;
071 }
072
073 public void setCrystal(@Nonnull Crystal crystal) {
074 this.crystal = requireNonNull(crystal, ERROR_CRYSTAL_NULL);
075 setImage(Toolkit.getDefaultToolkit().getImage(toURL(crystal, size)));
076 }
077
078 public void setCrystal(@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 size = requireValidSize(s);
086 } catch (NumberFormatException e) {
087 throw invalidDescription(description, e);
088 }
089 } else if (size == 0) {
090 size = 16;
091 }
092
093 crystal = Crystal.findByDescription(description);
094 setImage(Toolkit.getDefaultToolkit().getImage(toURL(crystal, 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(crystal, size)));
104 }
105 }
|