001 /*
002 * Copyright 2008-2015 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 protected void setValueInternal(Object value) {
044 if (null == value) {
045 super.setValueInternal(null);
046 } else if (value instanceof CharSequence) {
047 handleAsString(String.valueOf(value));
048 } else if (value instanceof File) {
049 handleAsFile((File) value);
050 } else if (value instanceof URL) {
051 handleAsURL((URL) value);
052 } else if (value instanceof URI) {
053 handleAsURI((URI) value);
054 } else if (value instanceof InputStream) {
055 handleAsInputStream((InputStream) value);
056 } else if (value instanceof ImageInputStream) {
057 handleAsImageInputStream((ImageInputStream) value);
058 } else if (value instanceof byte[]) {
059 handleAsByteArray((byte[]) value);
060 } else if (value instanceof Image) {
061 handleAsImage((Image) value);
062 } else if (value instanceof Icon) {
063 super.setValueInternal(value);
064 } else {
065 throw illegalValue(value, Icon.class);
066 }
067 }
068
069 protected void handleAsString(String str) {
070 if (isBlank(str)) {
071 super.setValueInternal(null);
072 return;
073 }
074 if (str.contains("|")) {
075 // assume classname|arg format
076 handleAsClassWithArg(str);
077 } else {
078 handleAsURL(getClass().getClassLoader().getResource(str));
079 }
080 }
081
082 @SuppressWarnings("unchecked")
083 protected void handleAsClassWithArg(String str) {
084 String[] args = str.split("\\|");
085 if (args.length == 2) {
086 Class<? extends Icon> iconClass = null;
087 try {
088 iconClass = (Class<? extends Icon>) IconPropertyEditor.class.getClassLoader().loadClass(args[0]);
089 } catch (ClassNotFoundException e) {
090 throw illegalValue(str, Icon.class, e);
091 }
092
093 Constructor<? extends Icon> constructor = null;
094 try {
095 constructor = iconClass.getConstructor(String.class);
096 } catch (NoSuchMethodException e) {
097 throw illegalValue(str, Icon.class, e);
098 }
099
100 try {
101 super.setValueInternal(constructor.newInstance(args[1]));
102 } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
103 throw illegalValue(str, Icon.class, e);
104 }
105 } else {
106 throw illegalValue(str, Icon.class);
107 }
108 }
109
110 protected void handleAsFile(File file) {
111 try {
112 handleAsImage(ImageIO.read(file));
113 } catch (IOException e) {
114 throw illegalValue(file, Icon.class, e);
115 }
116 }
117
118 protected void handleAsURL(URL url) {
119 try {
120 handleAsImage(ImageIO.read(url));
121 } catch (IOException e) {
122 throw illegalValue(url, Icon.class, e);
123 }
124 }
125
126 protected void handleAsURI(URI uri) {
127 try {
128 handleAsURL(uri.toURL());
129 } catch (MalformedURLException e) {
130 throw illegalValue(uri, Icon.class, e);
131 }
132 }
133
134 protected void handleAsInputStream(InputStream stream) {
135 try {
136 handleAsImage(ImageIO.read(stream));
137 } catch (IOException e) {
138 throw illegalValue(stream, Icon.class, e);
139 }
140 }
141
142 protected void handleAsImageInputStream(ImageInputStream stream) {
143 try {
144 handleAsImage(ImageIO.read(stream));
145 } catch (IOException e) {
146 throw illegalValue(stream, Icon.class, e);
147 }
148 }
149
150 protected void handleAsByteArray(byte[] bytes) {
151 super.setValueInternal(new ImageIcon(bytes));
152 }
153
154 protected void handleAsImage(Image img) {
155 super.setValueInternal(new ImageIcon(img));
156 }
157 }
|