IconPropertyEditor.java
001 /*
002  * Copyright 2008-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.editors;
017 
018 import griffon.core.editors.AbstractPropertyEditor;
019 import griffon.metadata.PropertyEditorFor;
020 
021 import javax.imageio.ImageIO;
022 import javax.imageio.stream.ImageInputStream;
023 import javax.swing.Icon;
024 import javax.swing.ImageIcon;
025 import java.awt.Image;
026 import java.io.File;
027 import java.io.IOException;
028 import java.io.InputStream;
029 import java.lang.reflect.Constructor;
030 import java.lang.reflect.InvocationTargetException;
031 import java.net.MalformedURLException;
032 import java.net.URI;
033 import java.net.URL;
034 
035 import static griffon.util.GriffonNameUtils.isBlank;
036 
037 /**
038  @author Andres Almiray
039  @since 2.0.0
040  */
041 @PropertyEditorFor(Icon.class)
042 public class IconPropertyEditor extends AbstractPropertyEditor {
043     @Override
044     protected void setValueInternal(Object value) {
045         if (null == value) {
046             super.setValueInternal(null);
047         else if (value instanceof CharSequence) {
048             handleAsString(String.valueOf(value));
049         else if (value instanceof File) {
050             handleAsFile((Filevalue);
051         else if (value instanceof URL) {
052             handleAsURL((URLvalue);
053         else if (value instanceof URI) {
054             handleAsURI((URIvalue);
055         else if (value instanceof InputStream) {
056             handleAsInputStream((InputStreamvalue);
057         else if (value instanceof ImageInputStream) {
058             handleAsImageInputStream((ImageInputStreamvalue);
059         else if (value instanceof byte[]) {
060             handleAsByteArray((byte[]) value);
061         else if (value instanceof Image) {
062             handleAsImage((Imagevalue);
063         else if (value instanceof Icon) {
064             super.setValueInternal(value);
065         else {
066             throw illegalValue(value, Icon.class);
067         }
068     }
069 
070     protected void handleAsString(String str) {
071         if (isBlank(str)) {
072             super.setValueInternal(null);
073             return;
074         }
075         if (str.contains("|")) {
076             // assume classname|arg format
077             handleAsClassWithArg(str);
078         else {
079             handleAsURL(getClass().getClassLoader().getResource(str));
080         }
081     }
082 
083     @SuppressWarnings("unchecked")
084     protected void handleAsClassWithArg(String str) {
085         String[] args = str.split("\\|");
086         if (args.length == 2) {
087             Class<? extends Icon> iconClass = null;
088             try {
089                 iconClass = (Class<? extends Icon>IconPropertyEditor.class.getClassLoader().loadClass(args[0]);
090             catch (ClassNotFoundException e) {
091                 throw illegalValue(str, Icon.class, e);
092             }
093 
094             Constructor<? extends Icon> constructor = null;
095             try {
096                 constructor = iconClass.getConstructor(String.class);
097             catch (NoSuchMethodException e) {
098                 throw illegalValue(str, Icon.class, e);
099             }
100 
101             try {
102                 super.setValueInternal(constructor.newInstance(args[1]));
103             catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
104                 throw illegalValue(str, Icon.class, e);
105             }
106         else {
107             throw illegalValue(str, Icon.class);
108         }
109     }
110 
111     protected void handleAsFile(File file) {
112         try {
113             handleAsImage(ImageIO.read(file));
114         catch (IOException e) {
115             throw illegalValue(file, Icon.class, e);
116         }
117     }
118 
119     protected void handleAsURL(URL url) {
120         try {
121             handleAsImage(ImageIO.read(url));
122         catch (IOException e) {
123             throw illegalValue(url, Icon.class, e);
124         }
125     }
126 
127     protected void handleAsURI(URI uri) {
128         try {
129             handleAsURL(uri.toURL());
130         catch (MalformedURLException e) {
131             throw illegalValue(uri, Icon.class, e);
132         }
133     }
134 
135     protected void handleAsInputStream(InputStream stream) {
136         try {
137             handleAsImage(ImageIO.read(stream));
138         catch (IOException e) {
139             throw illegalValue(stream, Icon.class, e);
140         }
141     }
142 
143     protected void handleAsImageInputStream(ImageInputStream stream) {
144         try {
145             handleAsImage(ImageIO.read(stream));
146         catch (IOException e) {
147             throw illegalValue(stream, Icon.class, e);
148         }
149     }
150 
151     protected void handleAsByteArray(byte[] bytes) {
152         super.setValueInternal(new ImageIcon(bytes));
153     }
154 
155     protected void handleAsImage(Image img) {
156         super.setValueInternal(new ImageIcon(img));
157     }
158 }