001 /*
002 * Copyright 2008-2017 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 @Override
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 super.setValueInternal(value);
060 } else {
061 throw illegalValue(value, Image.class);
062 }
063 }
064
065 protected void handleAsString(String str) {
066 if (isBlank(str)) {
067 super.setValueInternal(null);
068 return;
069 }
070 handleAsURL(getClass().getClassLoader().getResource(str));
071 }
072
073 protected void handleAsFile(File file) {
074 try {
075 super.setValueInternal(ImageIO.read(file));
076 } catch (IOException e) {
077 throw illegalValue(file, Image.class, e);
078 }
079 }
080
081 protected void handleAsURL(URL url) {
082 try {
083 super.setValueInternal(ImageIO.read(url));
084 } catch (IOException e) {
085 throw illegalValue(url, Image.class, e);
086 }
087 }
088
089 protected void handleAsURI(URI uri) {
090 try {
091 handleAsURL(uri.toURL());
092 } catch (MalformedURLException e) {
093 throw illegalValue(uri, Image.class, e);
094 }
095 }
096
097 protected void handleAsInputStream(InputStream stream) {
098 try {
099 super.setValueInternal(ImageIO.read(stream));
100 } catch (IOException e) {
101 throw illegalValue(stream, Image.class, e);
102 }
103 }
104
105 protected void handleAsImageInputStream(ImageInputStream stream) {
106 try {
107 super.setValueInternal(ImageIO.read(stream));
108 } catch (IOException e) {
109 throw illegalValue(stream, Image.class, e);
110 }
111 }
112
113 protected void handleAsByteArray(byte[] bytes) {
114 super.setValueInternal(Toolkit.getDefaultToolkit().createImage(bytes));
115 }
116 }
|