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