Package org.geoforge.guillc.panel

Source Code of org.geoforge.guillc.panel.GfrPnlGrpSngTfdSld

/*
*  Copyright (C) 2011-2014 GeoForge Project
*
*  This program 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
*  of the License, or (at your option) any later version.
*
*  This program 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 program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

package org.geoforge.guillc.panel;

import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.geoforge.guillc.slider.GfrSld;
import org.geoforge.guillc.textfield.GfrTfd;
import org.geoforge.guillc.textfield.GfrTfdAbs;

/**
*
* @author Amadeus.Sowerby
*
* email: Amadeus.Sowerby_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*
* Should be refactored => remove "Sng"
*/
public class GfrPnlGrpSngTfdSld extends GfrPnlGrpAbs implements ChangeListener
{

   //--
   //-- beg private fields
   private GfrTfd _tfdLeft_ = null;

   private GfrSld _sldRight_ = null;
   //--

   private int _intRatio_ = -1;
  
   private String _strDoubleFormatter_ = null;
   //-- end private fields
   //--

   public double getValue()
   {
      return ((double) this._sldRight_.getValue())/this._intRatio_;
   }


   public GfrPnlGrpSngTfdSld(
           String strWhat,
           int intMin,
           int intMax,
           int intInit)
   {
      this(
              strWhat,
              (double) intMin,
              (double) intMax,
              (double) intInit,
              1,
              "%.2f");
   }
  
  
   public GfrPnlGrpSngTfdSld(
           String strWhat,
           int intRatio)
   {
      this(
               strWhat,
            -1,//default
            1,//default
            0d,//default
            intRatio);
   }
  
   public GfrPnlGrpSngTfdSld(
           String strWhat,
           int intMin,
           int intMax,
           double dblInit,
           int intRatio)
   {
      this(
              strWhat,
              (double) intMin,
              (double) intMax,
              dblInit,
              intRatio,
              "%.2f");
   }

   public GfrPnlGrpSngTfdSld(
           String strWhat,
           double dblMin,
           double dblMax,
           double dblInit,
           int intRatio,
           String strDoubleFormatter)
   {
      super(
              strWhat,
              GfrTfdAbs.CMP_HEIGHT);

           
      this._strDoubleFormatter_ = strDoubleFormatter;
     
     
      this._intRatio_ = intRatio;

      this._tfdLeft_ = new GfrTfd();
      this._tfdLeft_.setText(String.format(this._strDoubleFormatter_, dblInit));


      this._sldRight_ = new GfrSld(
              JSlider.HORIZONTAL,
              (int)(dblMin*intRatio),
              (int)(dblMax*intRatio),
              (int)(dblInit*intRatio));
     

      this._sldRight_.addChangeListener((ChangeListener) this);
   }
  
   public void setSliderValueMinimum(double dblValue)
   {
      this._sldRight_.setMinimum((int)(this._intRatio_ * dblValue));
   }
  
   public void setSliderValueMaximum(double dblValue)
   {
      this._sldRight_.setMaximum((int)(this._intRatio_ * dblValue));
   }
  
   public void setSliderValue(double dblValue)
   {
      this._sldRight_.setValue((int)(this._intRatio_ * dblValue));
   }


   public void setToolTip(String strToolTip)
   {
      this._sldRight_.setToolTipText(strToolTip);
   }

   @Override
   public boolean init()
   {
      if (!super.init())
         return false;

      if (!this._sldRight_.init())
         return false;

      if (!this._tfdLeft_.init())
         return false;


      this._sldRight_.setPaintTicks(false);
      this._sldRight_.setPaintLabels(false);

      this._tfdLeft_.setSize(new Dimension(35, GfrTfdAbs.CMP_HEIGHT));
      this._tfdLeft_.setPreferredSize(new Dimension(35, GfrTfdAbs.CMP_HEIGHT));
      this._tfdLeft_.setMinimumSize(new Dimension(35, GfrTfdAbs.CMP_HEIGHT));
      this._tfdLeft_.setMaximumSize(new Dimension(35, GfrTfdAbs.CMP_HEIGHT));
      this._tfdLeft_.setEditable(false);
      GfrPnl pnl = new GfrPnl();

      if (!pnl.init())
         return false;

      pnl.setLayout(new BoxLayout(pnl, BoxLayout.LINE_AXIS));

      pnl.add(this._tfdLeft_);
      pnl.add(Box.createRigidArea(new Dimension(5, 0)));
      pnl.add(this._sldRight_);

      super.add(pnl);

      return true;
   }

   @Override
   public void destroy()
   {
      super.destroy();

      if (this._tfdLeft_ != null)
      {
         this._tfdLeft_.destroy();
         this._tfdLeft_ = null;
      }

      if (this._sldRight_ != null)
      {
         this._sldRight_.destroy();
         this._sldRight_ = null;
      }
   }

   @Override
   public void stateChanged(ChangeEvent e)
   {
      try
      {
         double dbl = (double) this._sldRight_.getValue();
         dbl /= this._intRatio_;
        
        
        
         this._tfdLeft_.setText(String.format(this._strDoubleFormatter_, dbl));
         super.firePanelUpdate();
      }
      catch (Exception ee)
      {
         //dont care for the moment
         //TODO : avoid exception
         //ee.printStackTrace();
      }
   }

   @Override
   public void setEnabled(boolean enabled)
   {
      super.setEnabled(enabled);
     
      this._sldRight_.setEnabled(enabled);
      this._tfdLeft_.setEnabled(enabled);
   }

   public void setPaintTicks(boolean bln)
   {
      this._sldRight_.setPaintTicks(bln);
   }

   public void setPaintLabels(boolean bln)
   {
      this._sldRight_.setPaintLabels(bln);
   }

   public void setMajorTickSpacing(int value)
   {
      this._sldRight_.setMajorTickSpacing(value);
   }

   public void setMinorTickSpacing(int value)
   {
      this._sldRight_.setMinorTickSpacing(value);
   }
  
}
TOP

Related Classes of org.geoforge.guillc.panel.GfrPnlGrpSngTfdSld

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.