001    /*
002     * DimensionPropertyEditor.java
003     *
004     * Created on August 16, 2006, 12:18 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.Dimension;
013    import java.beans.PropertyEditorSupport;
014    
015    /**
016     *
017     * @author joshy
018     */
019    public class DimensionPropertyEditor extends PropertyEditorSupport {
020        
021        public DimensionPropertyEditor() {
022        }
023        
024        public Dimension getValue() {
025            return (Dimension)super.getValue();
026        }
027        
028        public String getJavaInitializationString() {
029            Dimension point = getValue();
030            return point == null ? "null" : "new java.awt.Dimension(" + point.width + ", " + point.height + ")";
031        }
032        
033        public void setAsText(String text) throws IllegalArgumentException {
034            String originalParam = text;
035            try {
036                Dimension val = (Dimension)PropertyEditorUtil.createValueFromString(
037                        text, 2, Dimension.class, int.class);
038                setValue(val);
039            } catch (Exception ex) {
040                System.out.println(ex.getMessage());
041                ex.printStackTrace();
042                throw new IllegalArgumentException("The input value " + originalParam + " is not formatted correctly. Please " +
043                        "try something of the form [w,h] or [w , h] or [w h]", ex);
044            }
045        }
046        
047        public String getAsText() {
048            Dimension dim = getValue();
049            return dim == null ? "[]" : "[" + dim.width + ", " + dim.height + "]";
050        }
051        
052    }
053