/* @(#) ThresholdAdjPanel.java 1.1 00/21/11
 * Copyright (c) 2000 Lawrence Rodrigues
 */
package com.vistech.roi;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;

/** Creates a slider-based GUI for adjusting the threshold.
  * @version 1.0  21 Nov 2000
  * @author Lawrence Rodrigues
  */
public class ThresholdAdjPanel extends JPanel{
   private JTextField threshValueField;
   private JSlider threshSlider;
   protected int minValue =0, maxValue = 255, startValue = 100;
   protected int threshStartValue = 0;

   public ThresholdAdjPanel(){
      JPanel thpan =  createThresholdPanel();
      add(thpan);
   }

   public void addChangeListener(ChangeListener l){threshSlider.addChangeListener(l);}
   public void removeChangeListener(ChangeListener l){ threshSlider.removeChangeListener(l);}

   public void addActionListener(ActionListener l){threshValueField.addActionListener(l);}
   public void removeActionListener(ActionListener l){threshValueField.removeActionListener(l);}

   public void setSliderValues(int min, int max, int start){
      int lower = min%10;
      int higher = max%10;
      this.minValue = min-lower;
      this.maxValue = max+ (10-higher)+10;
      this.startValue = start;

      threshSlider.setMinimum(minValue);
      threshSlider.setMaximum(maxValue);
      threshSlider.setValue(start);
      int major = (int)((maxValue-minValue)/5);
      int minor = (int)(major/4);
      threshSlider.setMajorTickSpacing(major);
      threshSlider.setMinorTickSpacing(minor);
      threshSlider.setExtent(10);
      threshSlider.setPaintTicks(true);
      threshSlider.setPaintLabels(true);
   }

   public void setTextFieldValue(int val){ threshValueField.setText(Integer.toString(val));}
   public void setSliderValue(int val){threshSlider.setValue(val);}

   protected JPanel createThresholdPanel() {
      threshValueField = new JTextField(5);
      threshValueField.addActionListener(
         new ActionListener() {
            public void actionPerformed(ActionEvent e){
               String str = ((JTextField)e.getSource()).getText();
               int threshValue = (Integer.valueOf(str)).intValue();
               setSliderValue(threshValue);
            }
         }
      );
      threshValueField.setText(Integer.toString(threshStartValue));
      JLabel threshLabel = new JLabel("Threshold");

      threshSlider = new JSlider(minValue,maxValue,startValue);
      threshSlider.setMajorTickSpacing(50);
      threshSlider.setMinorTickSpacing(25);
      threshSlider.setExtent(10);
      threshSlider.setPaintTicks(true);
      threshSlider.setPaintLabels(true);
      threshSlider.addChangeListener(
         new ChangeListener() {
             public void stateChanged(ChangeEvent e){
                int threshValue = ((JSlider)(e.getSource())).getValue();
                setTextFieldValue(threshValue);
             }
         }
      );

      JPanel threshpan = new JPanel();
      GridBagLayout gb = new GridBagLayout() ;
      GridBagConstraints c = new GridBagConstraints();
      threshpan.setLayout(gb);
      threshpan.setBackground(Color.lightGray);

      c.weighty = 1.0; c.weightx = 1.0;
      JLabel  rotlab = new JLabel("Threshold ",SwingConstants.RIGHT);
      gb.setConstraints(rotlab, c);
      threshpan.add(rotlab);
      gb.setConstraints(threshValueField, c);
      threshpan.add(threshValueField);
      c.gridwidth = GridBagConstraints.REMAINDER;
      c.fill = GridBagConstraints.HORIZONTAL;
      gb.setConstraints(threshSlider, c);
      threshpan.add(threshSlider);
      return threshpan;
   }
 }
