001    /*
002     * RectanglePropertyEditor.java
003     *
004     * Created on August 16, 2006, 12:13 AM
005     *
006     * To change this template, choose Tools | Template Manager
007     * and open the template in the editor.
008     */
009    
010    package org.jdesktop.swingx.editors;
011    
012    import java.awt.Rectangle;
013    import java.awt.geom.Rectangle2D;
014    import java.beans.PropertyEditorSupport;
015    import java.lang.reflect.Constructor;
016    import java.lang.reflect.InvocationTargetException;
017    
018    
019    /**
020     *
021     * @author joshy
022     */
023    public class RectanglePropertyEditor extends PropertyEditorSupport {
024        
025        /** Creates a new instance of Rectangle2DPropertyEditor */
026        public RectanglePropertyEditor() {
027        }
028        
029        public Rectangle getValue() {
030            return (Rectangle)super.getValue();
031        }
032        
033        public String getJavaInitializationString() {
034            Rectangle rect = getValue();
035            return rect == null ? "null" : "new java.awt.Rectangle(" + rect.getX() + ", " + rect.getY() + ", " + rect.getWidth() + ", " + rect.getHeight() + ")";
036        }
037        
038        public void setAsText(String text) throws IllegalArgumentException {
039            String originalParam = text;
040            try {
041                Rectangle val = (Rectangle)PropertyEditorUtil.createValueFromString(text, 4, Rectangle.class, int.class);
042                setValue(val);
043            } catch (Throwable e) {
044                System.out.println(e.getMessage());
045                e.printStackTrace();
046                throw new IllegalArgumentException("The input value " + originalParam + " is not formatted correctly. Please " +
047                        "try something of the form [x,y,w,h] or [x , y , w , h] or [x y w h]", e);
048            }
049        }
050        
051        
052        public String getAsText() {
053            Rectangle rect = getValue();
054            return rect == null ? "[]" : "[" + rect.x + ", " + rect.y + ", " + rect.width + ", " + rect.height + "]";
055        }
056        
057    }