/**  ROIPanel Class
   * @version 1.0  18 apr 2000
   * @author Lawrence Rodrigues
   **/
package com.vistech.jai.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.roi.*;
import com.vistech.imageviewer.*;

/** Creates a GUI panel with radio buttons for selecting the shape type.
  * @version 1.0  3 Oct 2000
  * @author Lawrence Rodrigues
  */

public class ROIPanel extends JPanel implements TabPanel {
   ROIManagerJAI roiManager;
   JPanel thresholdPanel;
   public ROIPanel(ROIManagerJAI roiManager){
      this.roiManager = roiManager;
      thresholdPanel= new ThresholdAdjPanel();
      createUI();
   }

   public JPanel getThresholdAdjuster(){
       return thresholdPanel;
   }

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

   protected void createUI() {
      ButtonGroup bg = new ButtonGroup();
      JRadioButton fh = new JRadioButton("Free hand");
      JRadioButton rect = new JRadioButton("Rectangle");
      JRadioButton elli = new JRadioButton("Ellipse");

      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 jp = new JPanel();
      jp.add(fh);
      jp.add(rect);
      jp.add(elli);
      JButton erase = new JButton("Erase Shapes ");
      erase.addActionListener(
         new ActionListener(){
           public void actionPerformed(ActionEvent e){
              roiManager.eraseShapes();
           }
         }
      );
      JButton deposit = new JButton("Deposit ROI ");
      deposit.addActionListener(
         new ActionListener(){
           public void actionPerformed(ActionEvent e){
              roiManager.depositCurrentROI();
           }
         }
      );

      thresholdPanel.setBorder(BorderFactory.createTitledBorder("Adjust Threshold"));

      JPanel jpl = new JPanel(new GridLayout(2,1));
      jpl.add(jp);
      jpl.add(thresholdPanel);

      GridBagLayout gb = new GridBagLayout() ;
      GridBagConstraints c = new GridBagConstraints();
      setLayout(gb);
      c.fill = GridBagConstraints.BOTH;
      c.gridwidth = GridBagConstraints.REMAINDER;
      c.weighty = 1.0; c.weightx = 1.0;
      gb.setConstraints(jpl, c);
      add(jpl);
       c.gridwidth = 1;
      c.weighty = 0.1; c.weightx = 1.0;
      gb.setConstraints(erase, c);
      add(erase);
      c.gridwidth = GridBagConstraints.REMAINDER;
      c.weighty = 0.1; c.weightx = 1.0;
      gb.setConstraints(deposit, c);
      add(deposit);
   }
   public void reset() {
      roiManager.eraseShapes();
   }

   class ShapeAdapter implements ActionListener {
      int mode;
      public ShapeAdapter( int type) {mode = type;}
      public void actionPerformed(ActionEvent e) {
         roiManager.setShapeType(mode);
      }
   }
}