PolygonPropertyEditor.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.swing.editors;
017 
018 import griffon.core.editors.AbstractPropertyEditor;
019 import griffon.metadata.PropertyEditorFor;
020 
021 import java.awt.Polygon;
022 import java.util.List;
023 
024 import static griffon.util.GriffonNameUtils.isBlank;
025 
026 /**
027  @author Andres Almiray
028  @since 2.0.0
029  */
030 @PropertyEditorFor(Polygon.class)
031 public class PolygonPropertyEditor extends AbstractPropertyEditor {
032     public String getAsText() {
033         if (null == getValue()) return null;
034         Polygon p = (PolygongetValue();
035         StringBuilder b = new StringBuilder();
036         for (int i = 0; i < p.npoints; i++) {
037             if (i != 0) {
038                 b.append(", ");
039             }
040             b.append(p.xpoints[i])
041                 .append(", ")
042                 .append(p.ypoints[i]);
043         }
044         return b.toString();
045     }
046 
047     protected void setValueInternal(Object value) {
048         if (null == value) {
049             super.setValueInternal(null);
050         else if (value instanceof CharSequence) {
051             handleAsString(String.valueOf(value));
052         else if (value instanceof List) {
053             handleAsList((Listvalue);
054         else if (value instanceof Polygon) {
055             super.setValueInternal(value);
056         else {
057             throw illegalValue(value, Polygon.class);
058         }
059     }
060 
061     protected void handleAsString(String str) {
062         if (isBlank(str)) {
063             super.setValueInternal(null);
064             return;
065         }
066 
067         String[] parts = str.split(",");
068         if (parts.length % == 1) {
069             throw illegalValue(str, Polygon.class);
070         }
071 
072         int npoints = parts.length / 2;
073         int[] xpoints = new int[npoints];
074         int[] ypoints = new int[npoints];
075 
076         for (int i = 0; i < npoints; i++) {
077             xpoints[i= parse(parts[* i]);
078             ypoints[i= parse(parts[(* i1]);
079         }
080         super.setValueInternal(new Polygon(xpoints, ypoints, npoints));
081     }
082 
083     protected void handleAsList(List<?> list) {
084         if (list.isEmpty()) {
085             super.setValueInternal(null);
086             return;
087         }
088 
089         if (list.size() == 1) {
090             throw illegalValue(list, Polygon.class);
091         }
092 
093         int npoints = list.size() 2;
094         int[] xpoints = new int[npoints];
095         int[] ypoints = new int[npoints];
096 
097         for (int i = 0; i < npoints; i++) {
098             xpoints[i= parseValue(list.get(* i));
099             ypoints[i= parseValue(list.get((* i1));
100         }
101         super.setValueInternal(new Polygon(xpoints, ypoints, npoints));
102     }
103 
104     protected int parseValue(Object value) {
105         if (value instanceof CharSequence) {
106             return parse(String.valueOf(value));
107         else if (value instanceof Number) {
108             return parse((Numbervalue);
109         }
110         throw illegalValue(value, Polygon.class);
111     }
112 
113     protected int parse(String val) {
114         try {
115             return Integer.parseInt(val.trim());
116         catch (NumberFormatException e) {
117             throw illegalValue(val, Polygon.class, e);
118         }
119     }
120 
121     protected int parse(Number val) {
122         return val.intValue();
123     }
124 }