/**  ROIAnalyzer Class
   * @version 1.0  18 apr 2000
   * @author Lawrence Rodrigues
   **/
package app;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import com.vistech.util.*;
import com.vistech.jai.imageio.*;
import com.vistech.jai.render.*;
import javax.media.jai.*;

import java.awt.image.renderable.*;
import com.vistech.imageviewer.*;
import com.vistech.roi.*;
import com.vistech.listselect.*;
import com.vistech.jai.roi.*;

/** An ROI application developed using the JAI operators and classes. 
   * @version 1.0  10 April 1999
   * @author Lawrence Rodrigues
   */

public class ROIAnalyzer extends TiledImageViewer{
   protected JButton resetb;
   protected ManipUI manippan;
   protected ROIManagerJAI roiManager;
   protected  JPanel roipan;

   public void createUI() {
      int width, height;
      Dimension sd= Toolkit.getDefaultToolkit().getScreenSize();
      width = (int)(sd.width *7/8.0);
      height = (int)(sd.height*7/8.0);

      setTitle("ROI Analyzer");
      roiManager = new ROIManagerJAI((int)(width*4/5.0), height);
      JPanel repPanel = roiManager.createUI();
      roipan = roiManager.createROIPanel();
      Dimension d = new Dimension(width, height);

      flBrowser = new FileBrowser();
      loader = new ImageLoaderJAI();
      flBrowser.addListSelectListener(loader);
      loader.addPlanarImageLoadedListener(
          new PlanarImageLoadedListener() {
             public void imageLoaded(PlanarImageLoadedEvent e) {
                PlanarImage image = e.getImage();
                if(image == null) return;
                roiManager.setROIContext(new ROIContextJAI(image));
            }
         }
      );

      twidth = new JTextField(10);
      theight = new JTextField(10);
      viewer = roiManager.getSourceCanvas();
      renderGrid = new RenderGrid();
      theight.setText(Integer.toString(roiManager.getSourceCanvas().getTileHeight()));
      twidth.setText(Integer.toString(roiManager.getSourceCanvas().getTileWidth()));

      GeomManip manip =  new GeomManip((ImageManipulator)viewer);
      manippan = new ManipUI(manip);

      JPanel gridpan = new JPanel();
      JPanel tilesetpan = createTileSetPanel();
      //DisplayModePanel dispmodepan = new DisplayModePanel((ImageManipulator)viewer);
      //FlipModePanel flipmodepan = new FlipModePanel(manip);
      InterpModePanel intermodepan = new InterpModePanel((ImageManipulator)viewer);

      JTabbedPane jtab = new JTabbedPane();
      jtab.add("Tile Dimension",tilesetpan);
      jtab.add("Interp Mode", intermodepan);

      //jtab.add("Flip Mode",flipmodepan);

      JTabbedPane roitab = new JTabbedPane();
      roitab.add("ROI", roipan);
      roitab.add("Manip",manippan);
      changeEventhandler(roitab);

      GridBagLayout gb = new GridBagLayout() ;
      GridBagConstraints c = new GridBagConstraints();
      gridpan.setLayout(gb);
      c.fill = GridBagConstraints.BOTH;
      c.gridwidth = GridBagConstraints.REMAINDER;
      c.weighty = 1.0; c.weightx = 1.0;
      gb.setConstraints(jtab, c);
      gridpan.add(jtab);
      gb.setConstraints(roitab, c);
      gridpan.add(roitab);
      resetb = new JButton("Reset");
      resetEventHandler();
      c.weighty = 0.1; c.weightx = 1.0;
      gb.setConstraints(resetb, c);
      gridpan.add(resetb);
      saver = roiManager.getImageSaver(); 
      saver.setBorder(BorderFactory.createTitledBorder("Image Save"));

      JTabbedPane jtp = new JTabbedPane();
      jtp.add("Load", flBrowser);
      jtp.add("Save",  saver);
      JSplitPane hsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jtp,gridpan);
      JSplitPane vsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, hsp,repPanel);
      getContentPane().setLayout(new GridLayout(1,1));
      getContentPane().add(vsp);
      pack();
      setSize(new Dimension(d.width+170, d.height));
      show();
      addEventAdapters();

      hsp.setDividerLocation(0.5);
      vsp.setDividerLocation(0.3);
      hsp.setDividerSize(5);
      vsp.setDividerSize(6);
      viewer.repaint();
   }

   protected void changeEventhandler(JTabbedPane jt) {
      final JTabbedPane jtp = jt;
      jtp.addChangeListener(
         new ChangeListener() {
               public void stateChanged(ChangeEvent e) {
                  Component cmp = jtp.getSelectedComponent();
                  if(cmp == null) { return;}
                  if(cmp instanceof TabPanel) {
                     TabPanel tp = (TabPanel)cmp;
                     tp.openPanel();
                  }
                  int ind = jtp.getSelectedIndex();
                  String tabname = jtp.getTitleAt(ind);
                  if(tabname.equals("ROI")) roiManager.setRoiOn(true);
                  else  roiManager.setRoiOn(false);
               }
         }
      );
   }

   protected void resetEventHandler(){
      resetb.addActionListener(
         new ActionListener(){
            public void actionPerformed(ActionEvent e) {
               ((ImageManipulator)viewer).resetManipulation();
               manippan.resetManipulation();
               ((ROIPanel)roipan).reset();
             }
          }
       );
   }

   public static void  main(String[] args){
      ROIAnalyzer ip = new ROIAnalyzer();
      if(args.length <1) {
         ip.createUI();
      }else ip.loadAndDisplay(args[0]);
  }
}

