MOA 12.03
Real Time Analytics for Data Streams
FloatOptionEditComponent.java
Go to the documentation of this file.
00001 /*
00002  *    FloatOptionEditComponent.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 package moa.gui;
00021 
00022 import java.awt.GridLayout;
00023 
00024 import javax.swing.JPanel;
00025 import javax.swing.JSlider;
00026 import javax.swing.JSpinner;
00027 import javax.swing.SpinnerNumberModel;
00028 import javax.swing.event.ChangeEvent;
00029 import javax.swing.event.ChangeListener;
00030 
00031 import moa.options.FloatOption;
00032 import moa.options.Option;
00033 
00040 public class FloatOptionEditComponent extends JPanel implements
00041         OptionEditComponent {
00042 
00043     private static final long serialVersionUID = 1L;
00044 
00045     public static final int SLIDER_RESOLUTION = 100000;
00046 
00047     protected FloatOption editedOption;
00048 
00049     protected JSpinner spinner;
00050 
00051     protected JSlider slider;
00052 
00053     public FloatOptionEditComponent(FloatOption option) {
00054         this.editedOption = option;
00055         double minVal = option.getMinValue();
00056         double maxVal = option.getMaxValue();
00057         setLayout(new GridLayout(1, 0));
00058         this.spinner = new JSpinner(new SpinnerNumberModel(option.getValue(),
00059                 minVal, maxVal, 0.001));
00060         add(this.spinner);
00061         if ((minVal > Double.NEGATIVE_INFINITY)
00062                 && (maxVal < Double.POSITIVE_INFINITY)) {
00063             this.slider = new JSlider(0, SLIDER_RESOLUTION,
00064                     floatValueToSliderValue(option.getValue()));
00065             add(this.slider);
00066             this.slider.addChangeListener(new ChangeListener() {
00067 
00068                 @Override
00069                 public void stateChanged(ChangeEvent e) {
00070                     FloatOptionEditComponent.this.spinner.setValue(sliderValueToFloatValue(FloatOptionEditComponent.this.slider.getValue()));
00071                 }
00072             });
00073             this.spinner.addChangeListener(new ChangeListener() {
00074 
00075                 @Override
00076                 public void stateChanged(ChangeEvent e) {
00077                     FloatOptionEditComponent.this.slider.setValue(floatValueToSliderValue(((Double) FloatOptionEditComponent.this.spinner.getValue()).doubleValue()));
00078                 }
00079             });
00080         }
00081     }
00082 
00083     protected int floatValueToSliderValue(double floatValue) {
00084         double minVal = this.editedOption.getMinValue();
00085         double maxVal = this.editedOption.getMaxValue();
00086         return (int) Math.round((floatValue - minVal) / (maxVal - minVal)
00087                 * SLIDER_RESOLUTION);
00088     }
00089 
00090     protected double sliderValueToFloatValue(int sliderValue) {
00091         double minVal = this.editedOption.getMinValue();
00092         double maxVal = this.editedOption.getMaxValue();
00093         return minVal
00094                 + (((double) sliderValue / SLIDER_RESOLUTION) * (maxVal - minVal));
00095     }
00096 
00097     @Override
00098     public void applyState() {
00099         this.editedOption.setValue(((Double) this.spinner.getValue()).doubleValue());
00100         // this.editedOption.setValue(Double.parseDouble(this.spinner.getValue().toString()));
00101     }
00102 
00103     @Override
00104     public Option getEditedOption() {
00105         return this.editedOption;
00106     }
00107 
00108     @Override
00109     public void setEditState(String cliString) {
00110         this.spinner.setValue(FloatOption.cliStringToDouble(cliString));
00111     }
00112 }
 All Classes Namespaces Files Functions Variables Enumerations