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.BufferedImage;
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(BufferedImage.class)
040 public class BufferedImagePropertyEditor extends AbstractPropertyEditor {
041 @Override
042 protected void setValueInternal(Object value) {
043 if (null == value) {
044 super.setValueInternal(null);
045 } else if (value instanceof CharSequence) {
046 handleAsString(String.valueOf(value));
047 } else if (value instanceof File) {
048 handleAsFile((File) value);
049 } else if (value instanceof URL) {
050 handleAsURL((URL) value);
051 } else if (value instanceof URI) {
052 handleAsURI((URI) value);
053 } else if (value instanceof InputStream) {
054 handleAsInputStream((InputStream) value);
055 } else if (value instanceof ImageInputStream) {
056 handleAsImageInputStream((ImageInputStream) value);
057 } else if (value instanceof BufferedImage) {
058 super.setValueInternal(value);
059 } else {
060 throw illegalValue(value, BufferedImage.class);
061 }
062 }
063
064 protected void handleAsString(String str) {
065 if (isBlank(str)) {
066 super.setValueInternal(null);
067 } else {
068 handleAsURL(getClass().getClassLoader().getResource(str));
069 }
070 }
071
072 protected void handleAsFile(File file) {
073 try {
074 super.setValueInternal(ImageIO.read(file));
075 } catch (IOException e) {
076 throw illegalValue(file, BufferedImage.class, e);
077 }
078 }
079
080 protected void handleAsURL(URL url) {
081 try {
082 super.setValueInternal(ImageIO.read(url));
083 } catch (IOException e) {
084 throw illegalValue(url, BufferedImage.class, e);
085 }
086 }
087
088 protected void handleAsURI(URI uri) {
089 try {
090 handleAsURL(uri.toURL());
091 } catch (MalformedURLException e) {
092 throw illegalValue(uri, BufferedImage.class, e);
093 }
094 }
095
096 protected void handleAsInputStream(InputStream stream) {
097 try {
098 super.setValueInternal(ImageIO.read(stream));
099 } catch (IOException e) {
100 throw illegalValue(stream, BufferedImage.class, e);
101 }
102 }
103
104 protected void handleAsImageInputStream(ImageInputStream stream) {
105 try {
106 super.setValueInternal(ImageIO.read(stream));
107 } catch (IOException e) {
108 throw illegalValue(stream, BufferedImage.class, e);
109 }
110 }
111 }
|