001: /*
002: * $Id: Point2DPropertyEditor.java,v 1.2 2006/03/11 02:50:42 rbair Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021:
022: package org.jdesktop.swingx.editors;
023:
024: import java.awt.geom.Point2D;
025: import java.beans.PropertyEditorSupport;
026:
027: /**
028: *
029: * @author rbair
030: */
031: public class Point2DPropertyEditor extends PropertyEditorSupport {
032:
033: /** Creates a new instance of Point2DPropertyEditor */
034: public Point2DPropertyEditor() {
035: }
036:
037: public Point2D getValue() {
038: return (Point2D.Double) super .getValue();
039: }
040:
041: public String getJavaInitializationString() {
042: Point2D point = getValue();
043: return point == null ? "null"
044: : "new java.awt.geom.Point2D.Double(" + point.getX()
045: + ", " + point.getY() + ")";
046: }
047:
048: public void setAsText(String text) throws IllegalArgumentException {
049: //the text could be in many different formats. All of the supported formats are as follows:
050: //(where x and y are doubles of some form)
051: //[x,y]
052: //[x y]
053: //x,y]
054: //[x,y
055: //[ x , y ] or any other arbitrary whitespace
056: // x , y ] or any other arbitrary whitespace
057: //[ x , y or any other arbitrary whitespace
058: //x,y
059: // x , y (or any other arbitrary whitespace)
060: //x y
061: // (empty space)
062: //null
063: //[]
064: //[ ]
065: //any other value throws an IllegalArgumentException
066:
067: String originalParam = text;
068:
069: if (text != null) {
070: //remove any opening or closing brackets
071: text = text.replace('[', ' ');
072: text = text.replace(']', ' ');
073: text = text.replace(',', ' ');
074: //trim whitespace
075: text = text.trim();
076: }
077:
078: //test for the simple case
079: if (text == null || text.equals("") || text.equals("null")) {
080: setValue(null);
081: return;
082: }
083:
084: //the first sequence of characters must now be a number. So, parse it out
085: //ending at the first whitespace. Then trim and the remaining value must
086: //be the second number. If there are any problems, throw and IllegalArgumentException
087: try {
088: int index = text.indexOf(' ');
089: String firstNumber = text.substring(0, index).trim();
090: String secondNumber = text.substring(index).trim();
091: Point2D.Double val = new Point2D.Double(Double
092: .parseDouble(firstNumber), Double
093: .parseDouble(secondNumber));
094: setValue(val);
095: } catch (Exception e) {
096: throw new IllegalArgumentException(
097: "The input value "
098: + originalParam
099: + " is not formatted correctly. Please "
100: + "try something of the form [x,y] or [x , y] or [x y]",
101: e);
102: }
103: }
104:
105: public String getAsText() {
106: Point2D point = getValue();
107: return point == null ? "[]" : "[" + point.getX() + ", "
108: + point.getY() + "]";
109: }
110:
111: public static void main(String... args) {
112: test("[1.5,1.2]");
113: test("1.5,1.2]");
114: test("[1.5,1.2");
115: test("[ 1.5 , 1.2 ]");
116: test(" 1.5 , 1.2 ]");
117: test("[ 1.5 , 1.2");
118: test("1.5,1.2");
119: test(" 1.5 , 1.2 ");
120: test("1.5 1.2");
121: test("");
122: test("null");
123: test("[]");
124: test("[ ]");
125: test("[1.5 1.2]");
126: }
127:
128: private static void test(String input) {
129: System.out.print("Input '" + input + "'");
130: try {
131: Point2DPropertyEditor ed = new Point2DPropertyEditor();
132: ed.setAsText(input);
133: Point2D point = ed.getValue();
134: System.out.println(" succeeded: " + point);
135: } catch (Exception e) {
136: System.out.println(" failed: " + e.getMessage());
137: }
138: }
139: }
|