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