/*
 * @(#) WindowLevelGUI.java 1.0 2000/9/27
 * Copyright (c) 1999 Larry Rodrigues
 */
package com.vistech.jai.render;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.beans.*;
import com.vistech.jai.render.*;
import javax.media.jai.*;

/** Implements sliders for adjusting window width and level.
  * @version 1.0  27 Sept 2000
  * @author Lawrence Rodrigues
  **/

public class WindowLevelGUI extends JPanel {
   protected JSlider windowSlider;
   protected JSlider levelSlider;
   protected JTextField windowField;
   protected JTextField levelField;
   protected int minValue =0;
   protected int maxValue =255;
   protected int window, level;
   protected JAIWindowLevel wl;

   public WindowLevelGUI(JAIWindowLevel wl ){
      this.wl = wl;
      init();
   }
   protected  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(double[][] minmax){
      double min = minmax[0][0];
      double max = minmax[1][0];
      for(int j=0; j<minmax[0].length;j++){
          if(min > minmax[0][j]) min =  minmax[0][j];
          if(max <  minmax[1][j]) max = minmax[1][j];
      }
      minValue = (int)min; maxValue=(int) max;
      windowSlider.setMinimum(minValue);
      windowSlider.setMaximum(maxValue);
      levelSlider.setMinimum(minValue);
      levelSlider.setMaximum(maxValue);
   }
}
