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