001    /* $Id: JSliderDouble.java 12 2007-07-27 09:48:53Z ringler $
002     * This class is part of the de.mospace.swing library.
003     * Copyright (C) 2005-2006 Moritz Ringler
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License
007     * as published by the Free Software Foundation; either version 2
008     * of the License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
018     */
019    package de.mospace.swing;
020    
021    import java.util.Hashtable;
022    import javax.swing.JLabel;
023    import javax.swing.JSlider;
024    
025    /** A JSlider that accepts and returns double values.
026    *
027    * @author Moritz Ringler
028    * @version $Revision: 12 $ ($Date: 2007-07-27 11:48:53 +0200 (Fr, 27 Jul 2007) $)
029    * @see javax.swing.JSlider
030    **/
031    public class JSliderDouble extends JSlider{
032        private final double mmin;
033        private final double sstep;
034    
035        /** Constructs a new instance of this class.
036        * @param min the minimum value of the slider
037        * @param max the maximum value of the slider
038        * @param step the minimum stepsize of the slider
039        * @param labelStep the tick spacing of the slider
040        * @param initialValue the initial value of the slider
041        **/
042        public JSliderDouble(double min, double max, double step,
043                double labelStep, double initialValue){
044            super(0, (int) ((max-min)/step), (int) ((initialValue - min)/step));
045            final int numSteps   = (int) ((max-min)/step);
046            final int iLabelStep = (int) (labelStep/step);
047            Hashtable labels = new Hashtable();
048            labelStep = iLabelStep * step;
049            double d = truncate(min,step);
050            for(int i = 0; i <= numSteps; i+=iLabelStep){
051                int ival = (int) d;
052                labels.put(new Integer(i),
053                    new JLabel((Math.abs(d - ival) < step/10.)?
054                            String.valueOf(ival):
055                            String.valueOf(d)
056                        ));
057                d = truncate (d+labelStep, step);
058            }
059            super.setLabelTable(labels);
060            labels = null;
061            super.setMajorTickSpacing(iLabelStep);
062            setPaintTicks(true);
063            setPaintLabels(true);
064            mmin = min;
065            sstep = step;
066        }
067    
068        private double truncate(double val, double step){
069            double xval = val;
070            double xstep = step;
071            int i = 1;
072            while (xstep<1){
073                xval *= 10;
074                i    *= 10;
075                if (Math.abs(Math.round(xval) - xval) < xstep/10.){
076                    return ((int) Math.round(xval))/((double) i);
077                }
078                xstep *= 10;
079            }
080            return val;
081        }
082    
083        /** Disabled. **/
084        public void setLabelTable(Hashtable ht){
085            //disabled
086        }
087    
088        /** Disabled. **/
089        public void setMajorTickSpacing(int i){
090            //disabled
091        }
092    
093        /** Returns the current value of the slider.
094        * @return the current value
095        **/
096        public double getDoubleValue(){
097            return super.getValue()*sstep + mmin;
098        }
099    }