001 /*
002 * Copyright 2008-2014 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.net.MalformedURLException;
030 import java.net.URI;
031 import java.net.URL;
032
033 import static griffon.util.GriffonNameUtils.isBlank;
034
035 /**
036 * @author Andres Almiray
037 * @since 2.0.0
038 */
039 @PropertyEditorFor(Icon.class)
040 public class IconPropertyEditor extends AbstractPropertyEditor {
041 protected void setValueInternal(Object value) {
042 if (null == value) {
043 super.setValueInternal(null);
044 } else if (value instanceof CharSequence) {
045 handleAsString(String.valueOf(value));
046 } else if (value instanceof File) {
047 handleAsFile((File) value);
048 } else if (value instanceof URL) {
049 handleAsURL((URL) value);
050 } else if (value instanceof URI) {
051 handleAsURI((URI) value);
052 } else if (value instanceof InputStream) {
053 handleAsInputStream((InputStream) value);
054 } else if (value instanceof ImageInputStream) {
055 handleAsImageInputStream((ImageInputStream) value);
056 } else if (value instanceof byte[]) {
057 handleAsByteArray((byte[]) value);
058 } else if (value instanceof Image) {
059 handleAsImage((Image) value);
060 } else if (value instanceof Icon) {
061 super.setValueInternal(value);
062 } else {
063 throw illegalValue(value, Icon.class);
064 }
065 }
066
067 private void handleAsString(String str) {
068 if (isBlank(str)) {
069 super.setValueInternal(null);
070 return;
071 }
072 handleAsURL(getClass().getClassLoader().getResource(str));
073 }
074
075 private void handleAsFile(File file) {
076 try {
077 handleAsImage(ImageIO.read(file));
078 } catch (IOException e) {
079 throw illegalValue(file, Icon.class, e);
080 }
081 }
082
083 private void handleAsURL(URL url) {
084 try {
085 handleAsImage(ImageIO.read(url));
086 } catch (IOException e) {
087 throw illegalValue(url, Icon.class, e);
088 }
089 }
090
091 private void handleAsURI(URI uri) {
092 try {
093 handleAsURL(uri.toURL());
094 } catch (MalformedURLException e) {
095 throw illegalValue(uri, Icon.class, e);
096 }
097 }
098
099 private void handleAsInputStream(InputStream stream) {
100 try {
101 handleAsImage(ImageIO.read(stream));
102 } catch (IOException e) {
103 throw illegalValue(stream, Icon.class, e);
104 }
105 }
106
107 private void handleAsImageInputStream(ImageInputStream stream) {
108 try {
109 handleAsImage(ImageIO.read(stream));
110 } catch (IOException e) {
111 throw illegalValue(stream, Icon.class, e);
112 }
113 }
114
115 private void handleAsByteArray(byte[] bytes) {
116 super.setValueInternal(new ImageIcon(bytes));
117 }
118
119 private void handleAsImage(Image img) {
120 super.setValueInternal(new ImageIcon(img));
121 }
122 }
|