001    /* ======================================================
002     * Orson : a free chart beans library based on JFreeChart
003     * ======================================================
004     *
005     * (C) Copyright 2007, by Object Refinery Limited.
006     *
007     * Project Info:  not-yet-released
008     *
009     * This library is free software; you can redistribute it and/or modify it 
010     * under the terms of the GNU Lesser General Public License as published by 
011     * the Free Software Foundation; either version 2.1 of the License, or 
012     * (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but 
015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017     * License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this library; if not, write to the Free Software
021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022     * USA.  
023     *
024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025     * in the United States and other countries.]
026     */
027    
028    package org.jfree.beans.editors;
029    
030    import java.beans.PropertyEditorSupport;
031    
032    import org.jfree.beans.LegendPosition;
033    
034    /**
035     * A JavaBeans property editor for the {@link LegendPosition} class.
036     */
037    public class LegendPositionEditor extends PropertyEditorSupport {
038    
039        /**
040         * Returns a string representing the current value.  This will be one of
041         * <code>TOP</code>, <code>BOTTOM</code>, <code>LEFT</code>, 
042         * <code>RIGHT</code> and <code>NULL</code>.
043         * 
044         * @return A string representing the current value.
045         */
046        public String getAsText() {
047            LegendPosition lp = (LegendPosition) getValue();
048            if (lp.equals(LegendPosition.TOP)) {
049                return "TOP";
050            }
051            else if (lp.equals(LegendPosition.BOTTOM)) {
052                return "BOTTOM";
053            }
054            else if (lp.equals(LegendPosition.LEFT)) {
055                return "LEFT";
056            }
057            else if (lp.equals(LegendPosition.RIGHT)) {
058                return "RIGHT";
059            }
060            else if (lp.equals(LegendPosition.NONE)) {
061                return "NONE";
062            }
063            throw new IllegalStateException("Bad LegendPosition.");
064        }
065        
066        /**
067         * Sets the current value by parsing the supplied string.
068         * 
069         * @param text  the string value.
070         * 
071         * @throws IllegalArgumentException if <code>text</code> is not one of
072         *     the values listed in {@link #getAsText()}.
073         */
074        public void setAsText(String text) throws IllegalArgumentException {
075            if ("TOP".equals(text)) {
076                setValue(LegendPosition.TOP);
077            }
078            else if ("BOTTOM".equals(text)) {
079                setValue(LegendPosition.BOTTOM);
080            }
081            else if ("LEFT".equals(text)) {
082                setValue(LegendPosition.LEFT);
083            }
084            else if ("RIGHT".equals(text)) {
085                setValue(LegendPosition.RIGHT);
086            }
087            else if ("NONE".equals(text)) {
088                setValue(LegendPosition.NONE);
089            }
090            else {
091                throw new IllegalArgumentException("Unrecognised 'text' argument.");
092            }
093        }
094        
095        /**
096         * Returns the valid string values for this property.
097         * 
098         * @return The valid string values for this property.
099         */
100        public String[] getTags() {
101            return new String[] {"TOP", "BOTTOM", "LEFT", "RIGHT", "NONE"};
102        }
103    
104        /**
105         * Returns a string for the property value.
106         * 
107         * @return A string for the property value.
108         */
109        public String getJavaInitializationString() {
110            LegendPosition lp = (LegendPosition) getValue();
111            return "org.jfree.beans." + lp.toString();
112        }
113    
114    }