PaintPropertyEditor.java
01 /*
02  * Copyright 2008-2016 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.paint.Color;
21 import javafx.scene.paint.LinearGradient;
22 import javafx.scene.paint.Paint;
23 import javafx.scene.paint.RadialGradient;
24 
25 import java.beans.PropertyEditor;
26 import java.util.List;
27 import java.util.Map;
28 
29 import static griffon.core.editors.PropertyEditorResolver.findEditor;
30 
31 /**
32  @author Andres Almiray
33  @since 2.4.0
34  */
35 @PropertyEditorFor(Paint.class)
36 public class PaintPropertyEditor extends AbstractPropertyEditor {
37 
38     @Override
39     public String getAsText() {
40         if (null == getValue()) return null;
41         return getValueInternal().toString();
42     }
43 
44     protected void setValueInternal(Object value) {
45         if (null == value) {
46             super.setValueInternal(null);
47         else if (value instanceof CharSequence) {
48             handleInternal(String.valueOf(value).trim());
49         else if (value instanceof List || value instanceof Map) {
50             handleInternal(value);
51         else if (value instanceof Paint) {
52             super.setValueInternal(value);
53         else {
54             throw illegalValue(value, Paint.class);
55         }
56     }
57 
58     private void handleInternal(Object value) {
59         for (Class<?> type : new Class[]{Color.class, RadialGradient.class, LinearGradient.class}) {
60             try {
61                 PropertyEditor propertyEditor = findEditor(type);
62                 propertyEditor.setValue(value);
63                 super.setValueInternal(propertyEditor.getValue());
64                 return;
65             catch (Exception e) {
66                 // ignore
67             }
68         }
69         throw illegalValue(value, Paint.class);
70     }
71 }