ImagePropertyEditor.java
001 /*
002  * Copyright 2008-2016 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.javafx.editors;
017 
018 import griffon.core.editors.AbstractPropertyEditor;
019 import griffon.metadata.PropertyEditorFor;
020 import javafx.scene.image.Image;
021 import javafx.scene.image.ImageView;
022 
023 import java.io.File;
024 import java.io.InputStream;
025 import java.lang.reflect.Constructor;
026 import java.lang.reflect.InvocationTargetException;
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  */
036 @PropertyEditorFor(Image.class)
037 public class ImagePropertyEditor extends AbstractPropertyEditor {
038     protected void setValueInternal(Object value) {
039         if (null == value) {
040             super.setValueInternal(null);
041         else if (value instanceof CharSequence) {
042             handleAsString(String.valueOf(value));
043         else if (value instanceof File) {
044             handleAsFile((Filevalue);
045         else if (value instanceof URL) {
046             handleAsURL((URLvalue);
047         else if (value instanceof URI) {
048             handleAsURI((URIvalue);
049         else if (value instanceof InputStream) {
050             handleAsInputStream((InputStreamvalue);
051         else if (value instanceof Image) {
052             super.setValueInternal(value);
053         else {
054             throw illegalValue(value, Image.class);
055         }
056     }
057 
058     protected void handleAsString(String str) {
059         if (isBlank(str)) {
060             super.setValueInternal(null);
061         else if (str.contains("|")) {
062             handleAsClassWithArg(str);
063         else {
064             handleAsURL(getClass().getClassLoader().getResource(str));
065         }
066     }
067 
068     protected void handleAsFile(File file) {
069         try {
070             handleAsURL(file.toURI().toURL());
071         catch (MalformedURLException e) {
072             throw illegalValue(file, URL.class, e);
073         }
074     }
075 
076     protected void handleAsURL(URL url) {
077         try {
078             super.setValueInternal(new Image(url.toString()));
079         catch (Exception e) {
080             throw illegalValue(url, URL.class, e);
081         }
082     }
083 
084     protected void handleAsURI(URI uri) {
085         try {
086             handleAsURL(uri.toURL());
087         catch (MalformedURLException e) {
088             throw illegalValue(uri, URL.class, e);
089         }
090     }
091 
092     protected void handleAsInputStream(InputStream stream) {
093         try {
094             super.setValueInternal(new Image(stream));
095         catch (Exception e) {
096             throw illegalValue(stream, URL.class, e);
097         }
098     }
099 
100     protected void handleAsClassWithArg(String str) {
101         String[] args = str.split("\\|");
102         if (args.length == 2) {
103             Class<?> iconClass = null;
104             try {
105                 iconClass = ImagePropertyEditor.class.getClassLoader().loadClass(args[0]);
106             catch (ClassNotFoundException e) {
107                 throw illegalValue(str, Image.class, e);
108             }
109 
110             Constructor<?> constructor = null;
111             try {
112                 constructor = iconClass.getConstructor(String.class);
113             catch (NoSuchMethodException e) {
114                 throw illegalValue(str, Image.class, e);
115             }
116 
117             try {
118                 Object o = constructor.newInstance(args[1]);
119                 if (instanceof Image) {
120                     super.setValueInternal(o);
121                 else if (instanceof ImageView) {
122                     super.setValueInternal(((ImageViewo).getImage());
123                 else {
124                     throw illegalValue(str, Image.class);
125                 }
126             catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
127                 throw illegalValue(str, Image.class, e);
128             }
129         else {
130             throw illegalValue(str, Image.class);
131         }
132     }
133 }