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