001 /*
002 * $Id: Point2DPropertyEditor.java 1815 2007-03-14 19:46:32Z joshy $
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)super.getValue();
039 }
040
041 public String getJavaInitializationString() {
042 Point2D point = getValue();
043 return point == null ? "null" : "new java.awt.geom.Point2D.Double(" + point.getX() + ", " + point.getY() + ")";
044 }
045
046 public void setAsText(String text) throws IllegalArgumentException {
047
048 String originalParam = text;
049 try {
050 Point2D val = (Point2D)PropertyEditorUtil.createValueFromString(
051 text, 2, Point2D.Double.class, double.class);
052 setValue(val);
053 } catch (Exception e) {
054 System.out.println(e.getMessage());
055 e.printStackTrace();
056 throw new IllegalArgumentException("The input value " + originalParam + " is not formatted correctly. Please " +
057 "try something of the form [x,y] or [x , y] or [x y]", e);
058 }
059 }
060
061 public String getAsText() {
062 Point2D point = getValue();
063 return point == null ? "[]" : "[" + point.getX() + ", " + point.getY() + "]";
064 }
065
066 public static void main(String... args) {
067 test("[1.5,1.2]");
068 test("1.5,1.2]");
069 test("[1.5,1.2");
070 test("[ 1.5 , 1.2 ]");
071 test(" 1.5 , 1.2 ]");
072 test("[ 1.5 , 1.2");
073 test("1.5,1.2");
074 test(" 1.5 , 1.2 ");
075 test("1.5 1.2");
076 test("");
077 test("null");
078 test("[]");
079 test("[ ]");
080 test("[1.5 1.2]");
081 }
082
083 private static void test(String input) {
084 System.out.print("Input '" + input + "'");
085 try {
086 Point2DPropertyEditor ed = new Point2DPropertyEditor();
087 ed.setAsText(input);
088 Point2D point = ed.getValue();
089 System.out.println(" succeeded: " + point);
090 } catch (Exception e) {
091 System.out.println(" failed: " + e.getMessage());
092 }
093 }
094 }