001    /*
002     * $Id: PointPropertyEditor.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.Point;
025    import java.beans.PropertyEditorSupport;
026    
027    /**
028     *
029     * @author rbair
030     */
031    public class PointPropertyEditor extends PropertyEditorSupport {
032    
033        /** Creates a new instance of Point2DPropertyEditor */
034        public PointPropertyEditor() {
035        }
036    
037        public Point getValue() {
038            return (Point)super.getValue();
039        }
040    
041        public String getJavaInitializationString() {
042            Point point = getValue();
043            return point == null ? "null" : "new java.awt.Point(" + point.getX() + ", " + point.getY() + ")";
044        }
045    
046        public void setAsText(String text) throws IllegalArgumentException {
047            String originalParam = text;
048            try {
049                Point val = (Point)PropertyEditorUtil.createValueFromString(
050                        text, 2, Point.class, int.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] or [x , y] or [x y]", e);
055            }
056        }
057    
058        public String getAsText() {
059            Point point = getValue();
060            return point == null ? "[]" : "[" + point.x + ", " + point.y + "]";
061        }
062    } 
063