PaintPropertyEditor.java
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2017 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package griffon.javafx.editors;
19 
20 import griffon.core.editors.AbstractPropertyEditor;
21 import griffon.metadata.PropertyEditorFor;
22 import javafx.scene.paint.Color;
23 import javafx.scene.paint.LinearGradient;
24 import javafx.scene.paint.Paint;
25 import javafx.scene.paint.RadialGradient;
26 
27 import java.beans.PropertyEditor;
28 import java.util.List;
29 import java.util.Map;
30 
31 import static griffon.core.editors.PropertyEditorResolver.findEditor;
32 
33 /**
34  @author Andres Almiray
35  @since 2.4.0
36  */
37 @PropertyEditorFor(Paint.class)
38 public class PaintPropertyEditor extends AbstractPropertyEditor {
39     @Override
40     public String getAsText() {
41         if (null == getValue()) return null;
42         return getValueInternal().toString();
43     }
44 
45     @Override
46     protected void setValueInternal(Object value) {
47         if (null == value) {
48             super.setValueInternal(null);
49         else if (value instanceof CharSequence) {
50             handleInternal(String.valueOf(value).trim());
51         else if (value instanceof List || value instanceof Map) {
52             handleInternal(value);
53         else if (value instanceof Paint) {
54             super.setValueInternal(value);
55         else {
56             throw illegalValue(value, Paint.class);
57         }
58     }
59 
60     private void handleInternal(Object value) {
61         for (Class<?> type : new Class[]{Color.class, RadialGradient.class, LinearGradient.class}) {
62             try {
63                 PropertyEditor propertyEditor = findEditor(type);
64                 propertyEditor.setValue(value);
65                 super.setValueInternal(propertyEditor.getValue());
66                 return;
67             catch (Exception e) {
68                 // ignore
69             }
70         }
71         throw illegalValue(value, Paint.class);
72     }
73 }