IconPropertyEditor.java
001 /*
002  * SPDX-License-Identifier: Apache-2.0
003  *
004  * Copyright 2008-2017 the original author or authors.
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 package griffon.swing.editors;
019 
020 import griffon.core.editors.AbstractPropertyEditor;
021 import griffon.metadata.PropertyEditorFor;
022 
023 import javax.imageio.ImageIO;
024 import javax.imageio.stream.ImageInputStream;
025 import javax.swing.Icon;
026 import javax.swing.ImageIcon;
027 import java.awt.Image;
028 import java.io.File;
029 import java.io.IOException;
030 import java.io.InputStream;
031 import java.lang.reflect.Constructor;
032 import java.lang.reflect.InvocationTargetException;
033 import java.net.MalformedURLException;
034 import java.net.URI;
035 import java.net.URL;
036 
037 import static griffon.util.GriffonNameUtils.isBlank;
038 
039 /**
040  @author Andres Almiray
041  @since 2.0.0
042  */
043 @PropertyEditorFor(Icon.class)
044 public class IconPropertyEditor extends AbstractPropertyEditor {
045     @Override
046     protected void setValueInternal(Object value) {
047         if (null == value) {
048             super.setValueInternal(null);
049         else if (value instanceof CharSequence) {
050             handleAsString(String.valueOf(value));
051         else if (value instanceof File) {
052             handleAsFile((Filevalue);
053         else if (value instanceof URL) {
054             handleAsURL((URLvalue);
055         else if (value instanceof URI) {
056             handleAsURI((URIvalue);
057         else if (value instanceof InputStream) {
058             handleAsInputStream((InputStreamvalue);
059         else if (value instanceof ImageInputStream) {
060             handleAsImageInputStream((ImageInputStreamvalue);
061         else if (value instanceof byte[]) {
062             handleAsByteArray((byte[]) value);
063         else if (value instanceof Image) {
064             handleAsImage((Imagevalue);
065         else if (value instanceof Icon) {
066             super.setValueInternal(value);
067         else {
068             throw illegalValue(value, Icon.class);
069         }
070     }
071 
072     protected void handleAsString(String str) {
073         if (isBlank(str)) {
074             super.setValueInternal(null);
075             return;
076         }
077         if (str.contains("|")) {
078             // assume classname|arg format
079             handleAsClassWithArg(str);
080         else {
081             handleAsURL(getClass().getClassLoader().getResource(str));
082         }
083     }
084 
085     @SuppressWarnings("unchecked")
086     protected void handleAsClassWithArg(String str) {
087         String[] args = str.split("\\|");
088         if (args.length == 2) {
089             Class<? extends Icon> iconClass = null;
090             try {
091                 iconClass = (Class<? extends Icon>IconPropertyEditor.class.getClassLoader().loadClass(args[0]);
092             catch (ClassNotFoundException e) {
093                 throw illegalValue(str, Icon.class, e);
094             }
095 
096             Constructor<? extends Icon> constructor = null;
097             try {
098                 constructor = iconClass.getConstructor(String.class);
099             catch (NoSuchMethodException e) {
100                 throw illegalValue(str, Icon.class, e);
101             }
102 
103             try {
104                 super.setValueInternal(constructor.newInstance(args[1]));
105             catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
106                 throw illegalValue(str, Icon.class, e);
107             }
108         else {
109             throw illegalValue(str, Icon.class);
110         }
111     }
112 
113     protected void handleAsFile(File file) {
114         try {
115             handleAsImage(ImageIO.read(file));
116         catch (IOException e) {
117             throw illegalValue(file, Icon.class, e);
118         }
119     }
120 
121     protected void handleAsURL(URL url) {
122         try {
123             handleAsImage(ImageIO.read(url));
124         catch (IOException e) {
125             throw illegalValue(url, Icon.class, e);
126         }
127     }
128 
129     protected void handleAsURI(URI uri) {
130         try {
131             handleAsURL(uri.toURL());
132         catch (MalformedURLException e) {
133             throw illegalValue(uri, Icon.class, e);
134         }
135     }
136 
137     protected void handleAsInputStream(InputStream stream) {
138         try {
139             handleAsImage(ImageIO.read(stream));
140         catch (IOException e) {
141             throw illegalValue(stream, Icon.class, e);
142         }
143     }
144 
145     protected void handleAsImageInputStream(ImageInputStream stream) {
146         try {
147             handleAsImage(ImageIO.read(stream));
148         catch (IOException e) {
149             throw illegalValue(stream, Icon.class, e);
150         }
151     }
152 
153     protected void handleAsByteArray(byte[] bytes) {
154         super.setValueInternal(new ImageIcon(bytes));
155     }
156 
157     protected void handleAsImage(Image img) {
158         super.setValueInternal(new ImageIcon(img));
159     }
160 }