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