001    /*
002     * $Id: Rectangle2DPropertyEditor.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.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            String originalParam = text;
048            try {
049                Rectangle2D val = (Rectangle2D)PropertyEditorUtil.createValueFromString(
050                        text, 4, Rectangle2D.Double.class, double.class);
051                setValue(val);
052            } catch (Exception e) {
053                throw new IllegalArgumentException("The input value " + originalParam + " is not formatted correctly. Please " +
054                        "try something of the form [x,y,w,h] or [x , y , w , h] or [x y w h]", e);
055            }
056        }
057    
058        public String getAsText() {
059            Rectangle2D rect = getValue();
060            return rect == null ? "[]" : "[" + rect.getX() + ", " + rect.getY() + ", " + rect.getWidth() + ", " + rect.getHeight() + "]";
061        }
062    
063        public static void main(String... args) {
064            test("[1.5,1.2,10,35]");
065            test("1.5,1.2,10,35]");
066            test("[1.5,1.2,10,35");
067            test("[ 1.5 , 1.2 ,10,35]");
068            test(" 1.5 , 1.2 ,10,35]");
069            test("[ 1.5 , 1.2,10,35");
070            test("1.5,1.2,10,35");
071            test(" 1.5 , 1.2 10 35");
072            test("1.5 1.2, 10 35");
073            test("");
074            test("null");
075            test("[]");
076            test("[ ]");
077            test("[1.5 1.2 10 35]");
078        }
079    
080        private static void test(String input) {
081            System.out.print("Input '" + input + "'");
082            try {
083                Rectangle2DPropertyEditor ed = new Rectangle2DPropertyEditor();
084                ed.setAsText(input);
085                Rectangle2D rect = ed.getValue();
086                System.out.println(" succeeded: " + rect);
087            } catch (Exception e) {
088                System.out.println(" failed: " + e.getMessage());
089            }
090        }
091    }