PolygonPropertyEditor.java
001 /*
002  * SPDX-License-Identifier: Apache-2.0
003  *
004  * Copyright 2008-2017 the original author or authors.
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 package griffon.swing.editors;
019 
020 import griffon.core.editors.AbstractPropertyEditor;
021 import griffon.metadata.PropertyEditorFor;
022 
023 import java.awt.Polygon;
024 import java.util.List;
025 
026 import static griffon.util.GriffonNameUtils.isBlank;
027 
028 /**
029  @author Andres Almiray
030  @since 2.0.0
031  */
032 @PropertyEditorFor(Polygon.class)
033 public class PolygonPropertyEditor extends AbstractPropertyEditor {
034     @Override
035     public String getAsText() {
036         if (null == getValue()) return null;
037         Polygon p = (PolygongetValue();
038         StringBuilder b = new StringBuilder();
039         for (int i = 0; i < p.npoints; i++) {
040             if (i != 0) {
041                 b.append(", ");
042             }
043             b.append(p.xpoints[i])
044                 .append(", ")
045                 .append(p.ypoints[i]);
046         }
047         return b.toString();
048     }
049 
050     @Override
051     protected void setValueInternal(Object value) {
052         if (null == value) {
053             super.setValueInternal(null);
054         else if (value instanceof CharSequence) {
055             handleAsString(String.valueOf(value));
056         else if (value instanceof List) {
057             handleAsList((Listvalue);
058         else if (value instanceof Polygon) {
059             super.setValueInternal(value);
060         else {
061             throw illegalValue(value, Polygon.class);
062         }
063     }
064 
065     protected void handleAsString(String str) {
066         if (isBlank(str)) {
067             super.setValueInternal(null);
068             return;
069         }
070 
071         String[] parts = str.split(",");
072         if (parts.length % == 1) {
073             throw illegalValue(str, Polygon.class);
074         }
075 
076         int npoints = parts.length / 2;
077         int[] xpoints = new int[npoints];
078         int[] ypoints = new int[npoints];
079 
080         for (int i = 0; i < npoints; i++) {
081             xpoints[i= parse(parts[* i]);
082             ypoints[i= parse(parts[(* i1]);
083         }
084         super.setValueInternal(new Polygon(xpoints, ypoints, npoints));
085     }
086 
087     protected void handleAsList(List<?> list) {
088         if (list.isEmpty()) {
089             super.setValueInternal(null);
090             return;
091         }
092 
093         if (list.size() == 1) {
094             throw illegalValue(list, Polygon.class);
095         }
096 
097         int npoints = list.size() 2;
098         int[] xpoints = new int[npoints];
099         int[] ypoints = new int[npoints];
100 
101         for (int i = 0; i < npoints; i++) {
102             xpoints[i= parseValue(list.get(* i));
103             ypoints[i= parseValue(list.get((* i1));
104         }
105         super.setValueInternal(new Polygon(xpoints, ypoints, npoints));
106     }
107 
108     protected int parseValue(Object value) {
109         if (value instanceof CharSequence) {
110             return parse(String.valueOf(value));
111         else if (value instanceof Number) {
112             return parse((Numbervalue);
113         }
114         throw illegalValue(value, Polygon.class);
115     }
116 
117     protected int parse(String val) {
118         try {
119             return Integer.parseInt(val.trim());
120         catch (NumberFormatException e) {
121             throw illegalValue(val, Polygon.class, e);
122         }
123     }
124 
125     protected int parse(Number val) {
126         return val.intValue();
127     }
128 }