/*
 * @(#) WindowLevelGUI.java 1.0 99/11/4
 * Copyright (c) 1999 Larry Rodrigues
 */
package com.vistech.imageviewer;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.beans.*;
/** Implements sliders for adjusting window width and level.
  * @version 1.0  4 Nov 1999
  * @author Lawrence Rodrigues
  **/
public class WindowLevelGUI extends JPanel {
   JSlider windowSlider;
   JSlider levelSlider;
   JTextField windowField;
   JTextField levelField;
   int minValue =0;
   int maxValue =255;
   int window, level;
   WindowLevel wl;

   public WindowLevelGUI(WindowLevel wl){
       this.wl = wl;
       init();
   }

   void init(){ add(createWindowLevelPanel());}

    public JPanel createWindowLevelPanel (){
        int brightStartValue = 50;
        windowField = new JTextField(5);
        levelField = new JTextField(5);

        windowField.setText(Integer.toString(brightStartValue));
        windowSlider = new JSlider(minValue,maxValue, brightStartValue);
        levelSlider = new JSlider(minValue,maxValue, brightStartValue);

        int majorTickSpacing = 100;
        int minorTickSpacing = 25;
        windowSlider.setMajorTickSpacing(majorTickSpacing);
        windowSlider.setMinorTickSpacing(minorTickSpacing);
        windowSlider.setExtent(minorTickSpacing);
        windowSlider.setPaintTicks(true);
        windowSlider.setPaintLabels(true);

        levelSlider.setMajorTickSpacing(majorTickSpacing);
        levelSlider.setMinorTickSpacing(minorTickSpacing);
        levelSlider.setExtent(minorTickSpacing);
        levelSlider.setPaintTicks(true);
        levelSlider.setPaintLabels(true);


        windowField.addActionListener(
           new ActionListener() {
              public void actionPerformed(ActionEvent e){
                  try {
                    String str = ((JTextField)e.getSource()).getText();
                    int value = (Integer.valueOf(str)).intValue();
                    windowSlider.setValue((int)value);
                    wl.setWindowLevel(window,level);

                  } catch (Exception e1){}
              }
           }
       );


       levelField.addActionListener(
           new ActionListener() {
              public void actionPerformed(ActionEvent e){
                  try {
                    String str = ((JTextField)e.getSource()).getText();
                    int value = (Integer.valueOf(str)).intValue();
                    levelSlider.setValue((int)value);
                    wl.setWindowLevel(window,level);
                  } catch (Exception e1){}
              }
           }
       );


       windowSlider.addChangeListener(
            new ChangeListener(){
              public void stateChanged(ChangeEvent e){
                 Object obj = e.getSource();
                 if(obj instanceof JSlider) {
                    JSlider jsr = (JSlider)obj;
                    window = ((JSlider)(e.getSource())).getValue();
                    wl.setWindowLevel(window, level);
                    windowField.setText(Integer.toString(window));
                 }
              }
            }
       );

      levelSlider.addChangeListener(
            new ChangeListener(){
              public void stateChanged(ChangeEvent e){
                 Object obj = e.getSource();
                 if(obj instanceof JSlider) {
                    JSlider jsr = (JSlider)obj;
                    level = ((JSlider)(e.getSource())).getValue();
                    wl.setWindowLevel(window, level);
                    levelField.setText(Integer.toString(level));
                 }
              }
            }
       );

      
       JPanel brightpan = new JPanel();
       GridBagLayout gb = new GridBagLayout() ;
       GridBagConstraints c = new GridBagConstraints();
       brightpan.setLayout(gb);
       c.weighty = 0.0; c.weightx = 1.0;
       c.fill = GridBagConstraints.HORIZONTAL;
       JLabel lab = new JLabel("Window");
       gb.setConstraints(lab, c);
       brightpan.add(lab);
       gb.setConstraints(windowSlider, c);
       brightpan.add(windowSlider);
       c.gridwidth = GridBagConstraints.REMAINDER;
       gb.setConstraints(windowField, c);
       brightpan.add(windowField);
       c.gridwidth =1;
       JLabel lab1 = new JLabel("Level");
       gb.setConstraints(lab1, c);
       brightpan.add(lab1);
       c.gridwidth =1;
       gb.setConstraints(levelSlider, c);
       brightpan.add(levelSlider);
       c.gridwidth = GridBagConstraints.REMAINDER;
       gb.setConstraints(levelField, c);
       brightpan.add(levelField);
       return brightpan;
   }

   public void initSliders(int[] minmax){
      int min = minmax[0];
      int max = minmax[1];
      minValue = (int)min; maxValue=(int) max;
      windowSlider.setMinimum(minValue);
      windowSlider.setMaximum(maxValue);
      levelSlider.setMinimum(minValue);
      levelSlider.setMaximum(maxValue);
   }
 }
