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" : "new java.awt.geom.Point2D.Double(" + point.getX() + ", " + point.getY() + ")"; 044 } 045 046 public void setAsText(String text) throws IllegalArgumentException { 047 //the text could be in many different formats. All of the supported formats are as follows: 048 //(where x and y are doubles of some form) 049 //[x,y] 050 //[x y] 051 //x,y] 052 //[x,y 053 //[ x , y ] or any other arbitrary whitespace 054 // x , y ] or any other arbitrary whitespace 055 //[ x , y or any other arbitrary whitespace 056 //x,y 057 // x , y (or any other arbitrary whitespace) 058 //x y 059 // (empty space) 060 //null 061 //[] 062 //[ ] 063 //any other value throws an IllegalArgumentException 064 065 String originalParam = text; 066 067 if (text != null) { 068 //remove any opening or closing brackets 069 text = text.replace('[', ' '); 070 text = text.replace(']', ' '); 071 text = text.replace(',', ' '); 072 //trim whitespace 073 text = text.trim(); 074 } 075 076 //test for the simple case 077 if (text == null || text.equals("") || text.equals("null")) { 078 setValue(null); 079 return; 080 } 081 082 //the first sequence of characters must now be a number. So, parse it out 083 //ending at the first whitespace. Then trim and the remaining value must 084 //be the second number. If there are any problems, throw and IllegalArgumentException 085 try { 086 int index = text.indexOf(' '); 087 String firstNumber = text.substring(0, index).trim(); 088 String secondNumber = text.substring(index).trim(); 089 Point2D.Double val = new Point2D.Double(Double.parseDouble(firstNumber), Double.parseDouble(secondNumber)); 090 setValue(val); 091 } catch (Exception e) { 092 throw new IllegalArgumentException("The input value " + originalParam + " is not formatted correctly. Please " + 093 "try something of the form [x,y] or [x , y] or [x y]", e); 094 } 095 } 096 097 public String getAsText() { 098 Point2D point = getValue(); 099 return point == null ? "[]" : "[" + point.getX() + ", " + point.getY() + "]"; 100 } 101 102 public static void main(String... args) { 103 test("[1.5,1.2]"); 104 test("1.5,1.2]"); 105 test("[1.5,1.2"); 106 test("[ 1.5 , 1.2 ]"); 107 test(" 1.5 , 1.2 ]"); 108 test("[ 1.5 , 1.2"); 109 test("1.5,1.2"); 110 test(" 1.5 , 1.2 "); 111 test("1.5 1.2"); 112 test(""); 113 test("null"); 114 test("[]"); 115 test("[ ]"); 116 test("[1.5 1.2]"); 117 } 118 119 private static void test(String input) { 120 System.out.print("Input '" + input + "'"); 121 try { 122 Point2DPropertyEditor ed = new Point2DPropertyEditor(); 123 ed.setAsText(input); 124 Point2D point = ed.getValue(); 125 System.out.println(" succeeded: " + point); 126 } catch (Exception e) { 127 System.out.println(" failed: " + e.getMessage()); 128 } 129 } 130 }