/* @(#) ThresholdAdjuster.java 1.1 99/21/11
 * Copyright (c) 1999 Lawrence Rodrigues
 */
package com.vistech.roi;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
//import com.vistech.imagedata.*;
import java.util.*;

public class ThresholdAdjuster implements  MouseListener{
   JTextField threshValueField;
   int threshStartValue = 0;
   JSlider threshSlider;
   JFrame threshFrame;
   int minValue =0, maxValue = 255, startValue = 100;

   public ThresholdAdjuster(){
      createThresholdFrame();
   }

   protected void launchThresholdPanel() {
      if( threshFrame != null){
          threshFrame.show();
          return;
      }
      threshFrame = createThresholdFrame();
   }

   protected JFrame createThresholdFrame() {
      threshFrame = new JFrame();
      JPanel thpan =  createThresholdPanel();
      threshFrame.getContentPane().add(thpan);
      threshFrame.pack();
      return threshFrame;
   }

   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 setSlideValues(int min, int max, int start){
      this.minValue = min;
      this.maxValue = max;
      this.startValue = start;
   }
   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.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);

      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;
   }

   public void mousePressed(MouseEvent e) {
      if(SwingUtilities.isRightMouseButton(e))
         popupMenu((JComponent)e.getSource(), e.getX(), e.getY());
   }
   public void mouseReleased(MouseEvent e) { }
   public void mouseClicked(MouseEvent e){}
   public void mouseEntered(MouseEvent e){}
   public void mouseExited(MouseEvent e){}

   protected void popupMenu(JComponent comp,int x, int y){
      JPopupMenu jp = new JPopupMenu("");
      jp.setLightWeightPopupEnabled(true);
      comp.add(jp);
      JMenuItem th= new JMenuItem("Adjust Threshold");
      th.addActionListener(
         new ActionListener(){
           public void actionPerformed(ActionEvent e){
              launchThresholdPanel();
           }
         }
      );
      jp.add(th);
      jp.show(comp,x,y);
   }
}
