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