001: /*
002: * $Id: Rectangle2DPropertyEditor.java,v 1.1 2006/03/11 03:01:26 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.Rectangle2D;
025: import java.beans.PropertyEditorSupport;
026:
027: /**
028: *
029: * @author rbair
030: */
031: public class Rectangle2DPropertyEditor extends PropertyEditorSupport {
032:
033: /** Creates a new instance of Rectangle2DPropertyEditor */
034: public Rectangle2DPropertyEditor() {
035: }
036:
037: public Rectangle2D getValue() {
038: return (Rectangle2D.Double) super .getValue();
039: }
040:
041: public String getJavaInitializationString() {
042: Rectangle2D rect = getValue();
043: return rect == null ? "null"
044: : "new java.awt.geom.Rectangle2D.Double(" + rect.getX()
045: + ", " + rect.getY() + ", " + rect.getWidth()
046: + ", " + rect.getHeight() + ")";
047: }
048:
049: public void setAsText(String text) throws IllegalArgumentException {
050: //the text could be in many different formats. All of the supported formats are as follows:
051: //(where x and y are doubles of some form)
052: //[x,y,w,h]
053: //[x y w h]
054: //x,y,w,h]
055: //[x,y w,h
056: //[ x , y w,h] or any other arbitrary whitespace
057: // x , y w h] or any other arbitrary whitespace
058: //[ x , y w, h or any other arbitrary whitespace
059: //x,y w, h
060: // x , y w,h (or any other arbitrary whitespace)
061: //x y w h
062: //or any other such permutation
063: // (empty space)
064: //null
065: //[]
066: //[ ]
067: //any other value throws an IllegalArgumentException
068:
069: String originalParam = text;
070:
071: if (text != null) {
072: //remove any opening or closing brackets
073: text = text.replace('[', ' ');
074: text = text.replace(']', ' ');
075: text = text.replace(',', ' ');
076: //trim whitespace
077: text = text.trim();
078: }
079:
080: //test for the simple case
081: if (text == null || text.equals("") || text.equals("null")) {
082: setValue(null);
083: return;
084: }
085:
086: //the first sequence of characters must now be a number. So, parse it out
087: //ending at the first whitespace. Then trim and the remaining value must
088: //be the second number. If there are any problems, throw and IllegalArgumentException
089: try {
090: int index = text.indexOf(' ');
091: String x = text.substring(0, index).trim();
092: text = text.substring(index).trim();
093: index = text.indexOf(' ');
094: String y = text.substring(0, index).trim();
095: text = text.substring(index).trim();
096: index = text.indexOf(' ');
097: String w = text.substring(0, index).trim();
098: String h = text.substring(index).trim();
099: Rectangle2D.Double val = new Rectangle2D.Double(Double
100: .parseDouble(x), Double.parseDouble(y), Double
101: .parseDouble(w), Double.parseDouble(h));
102: setValue(val);
103: } catch (Exception e) {
104: throw new IllegalArgumentException(
105: "The input value "
106: + originalParam
107: + " is not formatted correctly. Please "
108: + "try something of the form [x,y,w,h] or [x , y , w , h] or [x y w h]",
109: e);
110: }
111: }
112:
113: public String getAsText() {
114: Rectangle2D rect = getValue();
115: return rect == null ? "[]" : "[" + rect.getX() + ", "
116: + rect.getY() + ", " + rect.getWidth() + ", "
117: + rect.getHeight() + "]";
118: }
119:
120: public static void main(String... args) {
121: test("[1.5,1.2,10,35]");
122: test("1.5,1.2,10,35]");
123: test("[1.5,1.2,10,35");
124: test("[ 1.5 , 1.2 ,10,35]");
125: test(" 1.5 , 1.2 ,10,35]");
126: test("[ 1.5 , 1.2,10,35");
127: test("1.5,1.2,10,35");
128: test(" 1.5 , 1.2 10 35");
129: test("1.5 1.2, 10 35");
130: test("");
131: test("null");
132: test("[]");
133: test("[ ]");
134: test("[1.5 1.2 10 35]");
135: }
136:
137: private static void test(String input) {
138: System.out.print("Input '" + input + "'");
139: try {
140: Rectangle2DPropertyEditor ed = new Rectangle2DPropertyEditor();
141: ed.setAsText(input);
142: Rectangle2D rect = ed.getValue();
143: System.out.println(" succeeded: " + rect);
144: } catch (Exception e) {
145: System.out.println(" failed: " + e.getMessage());
146: }
147: }
148: }
|