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