/* ===========================================================
 
* JFreeChart : a free chart library for the Java(tm) platform
 
* ===========================================================
 
*
 
* (C) Copyright 2000-2016, by Object Refinery Limited and Contributors.
 
*
 
* Project Info:
  
http://www.jfree.org/jfreechart/index.html
 
*
 
* This library is free software; you can redistribute it and/or modify it
 
* under the terms of the GNU Lesser General Public License as published by
 
* the Free Software Foundation; either version 2.1 of the License, or
 
* (at your option) any later version.
 
*
 
* This library is distributed in the hope that it will be useful, but
 
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 
* License for more details.
 
*
 
* You should have received a copy of the GNU Lesser General Public
 
* License along with this library; if not, write to the Free Software
 
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  
02110-1301,
 
* USA.
 
*
 
* [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 
* Other names may be trademarks of their respective owners.]
 
*
 
* ----------------------------
 
* DefaultNumberAxisEditor.java
 
* ----------------------------
 
* (C) Copyright 2005-2009, Object Refinery Limited and Contributors.
 
*
 
* Original Author:
  
David Gilbert (for Object Refinery Limited);
 
* Contributor(s):
   
Arnaud Lelievre;
 
*
 
* Changes:
 
* --------
 
* 24-Nov-2005 : Version 1, based on NumberAxisPropertyEditor (DG);
 
* 18-Dec-2008 : Use ResourceBundleWrapper - see patch 1607918 by
 
*
               
Jess Thrysoee (DG);
 
* 27-Feb-2009 : Fixed bug 2612649, NullPointerException (DG);
 
* 03-Nov-2011 : Refactoring to use new DefaultValueAxisEditor (MH);
 
*/

package org.jfree.chart.editor;

import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jfree.chart.axis.Axis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.ui.LCBLayout;

/**
 
* A panel for editing the properties of a value axis.
 
*/

class DefaultNumberAxisEditor extends DefaultValueAxisEditor
    
implements FocusListener {

    
private double manualTickUnitValue;

    
private JTextField manualTickUnit;


    
/**
     
* Standard constructor: builds a property panel for the specified axis.
     
*
     
* @param axis
  
the axis, which should be changed.
     
*/

    
public DefaultNumberAxisEditor(NumberAxis axis) {

        
super(axis);

        
this.manualTickUnitValue = axis.getTickUnit().getSize();
        
validateTickUnit();
    
}

    
@Override
    
protected JPanel createTickUnitPanel()
    
{
        
JPanel tickUnitPanel = new JPanel(new LCBLayout(3));
        
tickUnitPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));

        
tickUnitPanel.add(new JPanel());
        
JCheckBox autoTickUnitSelectionCheckBox = new JCheckBox(
                
localizationResources.getString("Auto-TickUnit_Selection"),
                
isAutoTickUnitSelection());
        
autoTickUnitSelectionCheckBox.setActionCommand("AutoTickOnOff");
        
autoTickUnitSelectionCheckBox.addActionListener(this);
        
setAutoTickUnitSelectionCheckBox(autoTickUnitSelectionCheckBox);
        
tickUnitPanel.add(getAutoTickUnitSelectionCheckBox());
        
tickUnitPanel.add(new JPanel());

        
tickUnitPanel.add(new JLabel(localizationResources.getString(
                
"Manual_TickUnit_value")));
        
this.manualTickUnit = new JTextField(Double.toString(
                
this.manualTickUnitValue));
        
this.manualTickUnit.setEnabled(!isAutoTickUnitSelection());
        
this.manualTickUnit.setActionCommand("TickUnitValue");
        
this.manualTickUnit.addActionListener(this);
        
this.manualTickUnit.addFocusListener(this);
        
tickUnitPanel.add(this.manualTickUnit);
        
tickUnitPanel.add(new JPanel());

        
return tickUnitPanel;
    
}

    
/**
     
* Handles actions from within the property panel.
     
* @param event an event.
     
*/

    
@Override
    
public void actionPerformed(ActionEvent event) {
        
String command = event.getActionCommand();
        
if (command.equals("TickUnitValue")) {
            
validateTickUnit();
        
}
        
else {
            
// pass to the super-class for handling
            
super.actionPerformed(event);
        
}
    
}

    
@Override
    
public void focusLost(FocusEvent event) {
        
super.focusLost(event);
        
if (event.getSource() == this.manualTickUnit) {
            
validateTickUnit();
        
}
    
}

    
@Override
    
public void toggleAutoTick() {
        
super.toggleAutoTick();
        
if (isAutoTickUnitSelection()) {
            
this.manualTickUnit.setText(Double.toString(this.manualTickUnitValue));
            
this.manualTickUnit.setEnabled(false);
        
}
        
else {
            
this.manualTickUnit.setEnabled(true);
        
}
    
}

    
public void validateTickUnit() {
        
double newTickUnit;
        
try {
            
newTickUnit = Double.parseDouble(this.manualTickUnit.getText());
        
}
        
catch (NumberFormatException e) {
            
newTickUnit = this.manualTickUnitValue;
        
}

        
if (newTickUnit > 0.0) {
            
this.manualTickUnitValue = newTickUnit;
        
}
        
this.manualTickUnit.setText(Double.toString(this.manualTickUnitValue));
    
}

    
/**
     
* Sets the properties of the specified axis to match the properties
     
* defined on this panel.
     
*
     
* @param axis
  
the axis.
     
*/

    
@Override
    
public void setAxisProperties(Axis axis) {
        
super.setAxisProperties(axis);
        
NumberAxis numberAxis = (NumberAxis) axis;
        
if (!isAutoTickUnitSelection()) {
            
numberAxis.setTickUnit(new NumberTickUnit(manualTickUnitValue));
        
}
    
}
}