ImagePropertyEditor.java
01 /*
02  * Copyright 2008-2014 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *     http://www.apache.org/licenses/LICENSE-2.0
09  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package griffon.javafx.editors;
17 
18 import griffon.core.editors.AbstractPropertyEditor;
19 import griffon.metadata.PropertyEditorFor;
20 import javafx.scene.image.Image;
21 
22 import java.io.File;
23 import java.io.InputStream;
24 import java.net.MalformedURLException;
25 import java.net.URI;
26 import java.net.URL;
27 
28 import static griffon.util.GriffonNameUtils.isBlank;
29 
30 /**
31  @author Andres Almiray
32  */
33 @PropertyEditorFor(Image.class)
34 public class ImagePropertyEditor extends AbstractPropertyEditor {
35     protected void setValueInternal(Object value) {
36         if (null == value) {
37             super.setValueInternal(null);
38         else if (value instanceof CharSequence) {
39             handleAsString(String.valueOf(value));
40         else if (value instanceof File) {
41             handleAsFile((Filevalue);
42         else if (value instanceof URL) {
43             handleAsURL((URLvalue);
44         else if (value instanceof URI) {
45             handleAsURI((URIvalue);
46         else if (value instanceof InputStream) {
47             handleAsInputStream((InputStreamvalue);
48         else if (value instanceof Image) {
49             super.setValueInternal(value);
50         else {
51             throw illegalValue(value, Image.class);
52         }
53     }
54 
55     private void handleAsString(String str) {
56         if (isBlank(str)) {
57             super.setValueInternal(null);
58         else {
59             handleAsURL(getClass().getClassLoader().getResource(str));
60         }
61     }
62 
63     private void handleAsFile(File file) {
64         try {
65             handleAsURL(file.toURI().toURL());
66         catch (MalformedURLException e) {
67             throw illegalValue(file, URL.class, e);
68         }
69     }
70 
71     private void handleAsURL(URL url) {
72         try {
73             super.setValueInternal(new Image(url.toString()));
74         catch (Exception e) {
75             throw illegalValue(url, URL.class, e);
76         }
77     }
78 
79     private void handleAsURI(URI uri) {
80         try {
81             handleAsURL(uri.toURL());
82         catch (MalformedURLException e) {
83             throw illegalValue(uri, URL.class, e);
84         }
85     }
86 
87     private void handleAsInputStream(InputStream stream) {
88         try {
89             super.setValueInternal(new Image(stream));
90         catch (Exception e) {
91             throw illegalValue(stream, URL.class, e);
92         }
93     }
94 }