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