001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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 * DefaultLogAxisEditor.java
029 * -------------------------
030 * (C) Copyright 2005-2013, 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
041package org.jfree.chart.editor;
042
043import java.awt.event.ActionEvent;
044import java.awt.event.FocusEvent;
045
046import javax.swing.JLabel;
047import javax.swing.JPanel;
048import javax.swing.JTextField;
049
050import org.jfree.chart.axis.Axis;
051import org.jfree.chart.axis.LogAxis;
052import org.jfree.chart.axis.NumberTickUnit;
053
054/**
055 * A panel for editing properties of a {@link LogAxis}.
056 */
057public class DefaultLogAxisEditor extends DefaultValueAxisEditor {
058
059    private double manualTickUnitValue;
060
061    private JTextField manualTickUnit;
062
063    /**
064     * Standard constructor: builds a property panel for the specified axis.
065     *
066     * @param axis  the axis, which should be changed.
067     */
068    public DefaultLogAxisEditor(LogAxis axis) {
069        super(axis);
070        this.manualTickUnitValue = axis.getTickUnit().getSize();
071        manualTickUnit.setText(Double.toString(this.manualTickUnitValue));
072    }
073    
074    /**
075     * Creates a panel for editing the tick unit.
076     * 
077     * @return A panel.
078     */
079    @Override
080    protected JPanel createTickUnitPanel() {
081        JPanel tickUnitPanel = super.createTickUnitPanel();
082
083        tickUnitPanel.add(new JLabel(localizationResources.getString(
084                "Manual_TickUnit_value")));
085        this.manualTickUnit = new JTextField(Double.toString(
086                this.manualTickUnitValue));
087        this.manualTickUnit.setEnabled(!isAutoTickUnitSelection());
088        this.manualTickUnit.setActionCommand("TickUnitValue");
089        this.manualTickUnit.addActionListener(this);
090        this.manualTickUnit.addFocusListener(this);
091        tickUnitPanel.add(this.manualTickUnit);
092        tickUnitPanel.add(new JPanel());
093
094        return tickUnitPanel;
095    }
096
097    /**
098     * Handles actions from within the property panel.
099     * 
100     * @param event an event.
101     */
102    @Override
103    public void actionPerformed(ActionEvent event) {
104        String command = event.getActionCommand();
105        if (command.equals("TickUnitValue")) {
106            validateTickUnit();
107        }
108        else {
109            // pass to the super-class for handling
110            super.actionPerformed(event);
111        }
112    }
113
114    @Override
115    public void focusLost(FocusEvent event) {
116        super.focusLost(event);
117        if (event.getSource() == this.manualTickUnit) {
118            validateTickUnit();
119        }
120    }
121
122    /**
123     * Toggles the auto-tick-unit setting.
124     */
125    @Override
126    public void toggleAutoTick() {
127        super.toggleAutoTick();
128        if (isAutoTickUnitSelection()) {
129            this.manualTickUnit.setText(Double.toString(this.manualTickUnitValue));
130            this.manualTickUnit.setEnabled(false);
131        }
132        else {
133            this.manualTickUnit.setEnabled(true);
134        }
135    }
136
137    /**
138     * Validates the tick unit entered.
139     */
140    public void validateTickUnit() {
141        double newTickUnit;
142        try {
143            newTickUnit = Double.parseDouble(this.manualTickUnit.getText());
144        }
145        catch (NumberFormatException e) {
146            newTickUnit = this.manualTickUnitValue;
147        }
148
149        if (newTickUnit > 0.0) {
150            this.manualTickUnitValue = newTickUnit;
151        }
152        this.manualTickUnit.setText(Double.toString(this.manualTickUnitValue));
153    }
154
155    /**
156     * Sets the properties of the specified axis to match the properties
157     * defined on this panel.
158     *
159     * @param axis  the axis.
160     */
161    @Override
162    public void setAxisProperties(Axis axis) {
163        super.setAxisProperties(axis);
164        LogAxis logAxis = (LogAxis) axis;
165        if (!isAutoTickUnitSelection()) {
166            logAxis.setTickUnit(new NumberTickUnit(manualTickUnitValue));
167        }
168    }
169}