001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ----------------------
028 * DefaultPolarPlotEditor.java
029 * ----------------------
030 * (C) Copyright 2005-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  Martin Hoeller;
033 * Contributor(s):   -;
034 *
035 */
036
037
038package org.jfree.chart.editor;
039
040import java.awt.event.ActionEvent;
041import java.awt.event.FocusEvent;
042import java.awt.event.FocusListener;
043
044import javax.swing.BorderFactory;
045import javax.swing.JLabel;
046import javax.swing.JPanel;
047import javax.swing.JTabbedPane;
048import javax.swing.JTextField;
049
050import org.jfree.chart.axis.NumberTickUnit;
051import org.jfree.chart.plot.Plot;
052import org.jfree.chart.plot.PolarPlot;
053import org.jfree.chart.ui.LCBLayout;
054
055/**
056 * A panel for editing the properties of a {@link PolarPlot}.
057 */
058public class DefaultPolarPlotEditor extends DefaultPlotEditor
059    implements FocusListener {
060
061    /** A text field to enter a manual TickUnit. */
062    private JTextField manualTickUnit;
063
064    /** A text field to enter the angleOffset. */
065    private JTextField angleOffset;
066
067    /** The size for the manual TickUnit. */
068    private double manualTickUnitValue;
069
070    /** The value for the plot's angle offset. */
071    private double angleOffsetValue;
072
073    
074    /**
075     * Standard constructor - constructs a panel for editing the properties of
076     * the specified plot.
077     *
078     * @param plot  the plot, which should be changed.
079     */
080    public DefaultPolarPlotEditor(PolarPlot plot) {
081        super(plot);
082        this.angleOffsetValue = plot.getAngleOffset();
083        this.angleOffset.setText(Double.toString(this.angleOffsetValue));
084        this.manualTickUnitValue = plot.getAngleTickUnit().getSize();
085        this.manualTickUnit.setText(Double.toString(this.manualTickUnitValue));
086    }
087
088    /**
089     * Creates a tabbed pane for editing the plot attributes.
090     * 
091     * @param plot  the plot.
092     * 
093     * @return A tabbed pane. 
094     */
095    @Override
096    protected JTabbedPane createPlotTabs(Plot plot) {
097        JTabbedPane tabs = super.createPlotTabs(plot);
098        // TODO find a better localization key
099        tabs.insertTab(localizationResources.getString("General1"), null, 
100                createPlotPanel(), null, 0);
101        tabs.setSelectedIndex(0);
102        return tabs;
103    }
104
105    private JPanel createPlotPanel() {
106        JPanel plotPanel = new JPanel(new LCBLayout(3));
107        plotPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
108
109        plotPanel.add(new JLabel(localizationResources.getString(
110                "AngleOffset")));
111        this.angleOffset = new JTextField(Double.toString(
112                this.angleOffsetValue));
113        this.angleOffset.setActionCommand("AngleOffsetValue");
114        this.angleOffset.addActionListener(this);
115        this.angleOffset.addFocusListener(this);
116        plotPanel.add(this.angleOffset);
117        plotPanel.add(new JPanel());
118
119        plotPanel.add(new JLabel(localizationResources.getString(
120                "Manual_TickUnit_value")));
121        this.manualTickUnit = new JTextField(Double.toString(
122                this.manualTickUnitValue));
123        this.manualTickUnit.setActionCommand("TickUnitValue");
124        this.manualTickUnit.addActionListener(this);
125        this.manualTickUnit.addFocusListener(this);
126        plotPanel.add(this.manualTickUnit);
127        plotPanel.add(new JPanel());
128
129        return plotPanel;
130    }
131
132    /**
133     * Does nothing.
134     *
135     * @param event  the event.
136     */
137    @Override
138    public void focusGained(FocusEvent event) {
139        // don't need to do anything
140    }
141
142    /**
143     *  Revalidates minimum/maximum range.
144     *
145     *  @param event  the event.
146     */
147    @Override
148    public void focusLost(FocusEvent event) {
149        if (event.getSource() == this.angleOffset) {
150            validateAngleOffset();
151        }
152        else if (event.getSource() == this.manualTickUnit) {
153            validateTickUnit();
154        }
155    }
156
157    /**
158     * Handles actions from within the property panel.
159     * @param event an event.
160     */
161    @Override
162    public void actionPerformed(ActionEvent event) {
163        String command = event.getActionCommand();
164        if (command.equals("AngleOffsetValue")) {
165            validateAngleOffset();
166        }
167        else if (command.equals("TickUnitValue")) {
168            validateTickUnit();
169        }
170    }
171
172    /**
173     * Validates the angle offset entered by the user.
174     */
175    public void validateAngleOffset() {
176        double newOffset;
177        try {
178            newOffset = Double.parseDouble(this.angleOffset.getText());
179        }
180        catch (NumberFormatException e) {
181            newOffset = this.angleOffsetValue;
182        }
183        this.angleOffsetValue = newOffset;
184        this.angleOffset.setText(Double.toString(this.angleOffsetValue));
185    }
186
187    /**
188     * Validates the tick unit entered by the user.
189     */
190    public void validateTickUnit() {
191        double newTickUnit;
192        try {
193            newTickUnit = Double.parseDouble(this.manualTickUnit.getText());
194        }
195        catch (NumberFormatException e) {
196            newTickUnit = this.manualTickUnitValue;
197        }
198
199        if (newTickUnit > 0.0 && newTickUnit < 360.0) {
200            this.manualTickUnitValue = newTickUnit;
201        }
202        this.manualTickUnit.setText(Double.toString(this.manualTickUnitValue));
203    }
204
205    @Override
206    public void updatePlotProperties(Plot plot) {
207        super.updatePlotProperties(plot);
208        PolarPlot pp = (PolarPlot) plot;
209        pp.setAngleTickUnit(new NumberTickUnit(this.manualTickUnitValue));
210        pp.setAngleOffset(this.angleOffsetValue);
211    }
212}