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