/**  ShapesGUI Class
   * @version 1.0  18 apr 2000
   * @author Lawrence Rodrigues
   **/
package com.vistech.roi;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.geom.*;
import com.vistech.util.*;
import com.vistech.imageviewer.*;

/** Creates a panel for selecting the shape types, and the colors for
  * filling the ROI and drawing the outilne.
  * @version 1.0  10 March 2000
  * @author Lawrence Rodrigues
  */
public class ShapesGUI extends JPanel implements TabPanel {
   protected ROIManager roiManager;
   protected String colorStr[] = {"White", "Black", "Red", "Green", "Blue"};
   protected Color[] color = {Color.white, Color.black, Color.red, Color.green, Color.blue};

   private JRadioButton fh = new JRadioButton("Free hand");
   private JRadioButton rect = new JRadioButton("Rectangle");
   private JRadioButton elli = new JRadioButton("Ellipse");

   public static int PAN_ZOOM = 1;
   public static int SHAPES = 2;
   public ShapesGUI(ROIManager roiManager){
      this.roiManager = roiManager;
      createUI();
   }

   public void openPanel() { }
   public void closePanel(){ }

   protected void createUI() {
      ButtonGroup bg = new ButtonGroup();
      ShapeAdapter fhada = new ShapeAdapter(ShapeMarker.FREEHAND);
      ShapeAdapter rectada = new ShapeAdapter(ShapeMarker.RECTANGLE);
      ShapeAdapter elliada = new ShapeAdapter(ShapeMarker.ELLIPSE);
      fh.setSelected(true);

      fh.addActionListener(fhada);
      rect.addActionListener(rectada);
      elli.addActionListener(elliada);

      bg.add(fh);
      bg.add(rect);
      bg.add(elli);

      JPanel jp1 = new JPanel();
      jp1.add(fh);
      jp1.add(rect);
      jp1.add(elli);
      JLabel lab1 = new JLabel("Outline Color");
      JLabel lab2 = new JLabel("Mask Value");

      JComboBox jc1 = new JComboBox(colorStr);
      jc1.addItemListener(
         new ItemListener(){
           public void itemStateChanged(ItemEvent e){
              JComboBox jcb = (JComboBox)e.getSource();
              int index = jcb.getSelectedIndex();
              roiManager.setDrawingColor(color[index]);
           }
         }
      );
      JTextField jtx = new JTextField(3);
      jtx.setText("255");
      jtx.addActionListener(
         new ActionListener(){
           public void actionPerformed(ActionEvent e){
              try {
                JTextField jt = (JTextField)e.getSource();
                String maskstr = jt.getText();
                int mask = Integer.parseInt(maskstr);
                roiManager.setMaskValue(mask);
               }catch(Exception e1) {}
           }
         }
      );
      JPanel jp2 = new JPanel();
      jp2.add(lab1);
      jp2.add(jc1);
      jp2.add(lab2);
      jp2.add(jtx);
      JPanel jpr = new JPanel(new GridLayout(2,1));
      jpr.add(jp1);
      jpr.add(jp2);
      jpr.setBorder(BorderFactory.createTitledBorder("Select Shapes"));
      JButton erase = new JButton("Erase Shapes ");
      erase.addActionListener(
         new ActionListener(){
           public void actionPerformed(ActionEvent e){
              roiManager.eraseShapes();
           }
         }
      );

      JButton reset = new JButton("Reset ");
      reset.addActionListener(
         new ActionListener(){
           public void actionPerformed(ActionEvent e){
              roiManager.reset();
           }
         }
      );

      JPanel jpbl = new JPanel();
      jpbl.add(erase);
      jpbl.add(reset);

      ButtonGroup bg1 = new ButtonGroup();
      JRadioButton jb = new JRadioButton("Pan/Zoom");
      jb.addActionListener(new StateAdapter(PAN_ZOOM));

      JRadioButton sh = new JRadioButton("Draw Shape");
      sh.addActionListener(new StateAdapter(SHAPES));
      bg1.add(jb);
      bg1.add(sh);
      sh.setSelected(true);
      JPanel jptl = new JPanel();
      jptl.add(jb);
      jptl.add(sh);

      JPanel jpl = new JPanel(new GridLayout(2,1));
      jpl.add(jptl);
      jpl.add(jpbl);
      jpl.setBorder(BorderFactory.createTitledBorder("Set Commands"));

      add(jpl);
      add(jpr);
   }
   public void reset() { roiManager.eraseShapes();}

   class StateAdapter implements ActionListener {
      int mode;
      public StateAdapter( int type) {mode = type;}
      public void actionPerformed(ActionEvent e) {
         if(mode == PAN_ZOOM) {
            roiManager.setPanZoom(true);
            fh.setEnabled(false);
            rect.setEnabled(false);
            elli.setEnabled(false);
         }
         else {
           roiManager.setPanZoom(false);
           fh.setEnabled(true);
           rect.setEnabled(true);
           elli.setEnabled(true);
        }
      }
   }
   class ShapeAdapter implements ActionListener {
      int mode;
      public ShapeAdapter( int type) {mode = type;}
      public void actionPerformed(ActionEvent e) {
         roiManager.setShapeType(mode);
      }
   }
}