/**  RenderedImageManipulator 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 java.awt.geom.*;
import com.vistech.util.*;
import com.vistech.jai.imageio.*;
import com.vistech.jai.render.*;

import java.awt.image.RenderedImage;
import java.beans.*;
import javax.media.jai.*;
import com.sun.media.jai.codec.*;
import java.awt.image.renderable.*;
import com.vistech.imageviewer.*;

/** An application that allows interactive image manipulation.
  * @version 1.0 18 Apr 2000
  * @author Lawrence Rodrigues
  **/
public class RenderedImageManipulator extends TiledImageViewer{
   protected PanZoom panzoom;
   protected JButton resetb;
   protected ManipUI manippan;
   protected GeomManip manip;
   public void createUI() {
      int width, height;
      Dimension sd= Toolkit.getDefaultToolkit().getScreenSize();
      width = (int)(sd.width *3/4.0);
      height = (int)(sd.height*3/4.0);

      setTitle("JAI Image Manipulator");
      viewer = new ImageManipulatorCanvas();
      Dimension d = new Dimension(width, height);//getViewerSize(width/(double)height);
      //viewer.setPreferredSize(new Dimension(d.width, d.height));
      createImageLoaderAndSaver();
      loader.addPlanarImageLoadedListener(
          new PlanarImageLoadedListener() {
             public void imageLoaded(PlanarImageLoadedEvent e) {
                PlanarImage image = e.getImage();
                if(image == null) return;
                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                SwingUtilities.invokeLater(new ImagePaint(image));
             }
         }
      );

      twidth = new JTextField(10);
      theight = new JTextField(10);

      theight.setText(Integer.toString(viewer.getTileHeight()));
      twidth.setText(Integer.toString(viewer.getTileWidth()));

      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);
      jtab.add("Manip",manippan);

      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);
      resetb = new JButton("Reset");
      resetEventHandler();
      c.weighty = 0.1; c.weightx = 1.0;
      gb.setConstraints(resetb, c);
      gridpan.add(resetb);

      saver = new ImageSaverPanel();
      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,viewer);
      getContentPane().setLayout(new GridLayout(1,1));
      getContentPane().add(vsp);
      pack();
      setSize(new Dimension(d.width+170, d.height));
      show();
      addTileParamsEventAdapters();
      initializeScroll();
      panzoom = new PanZoom((ImageManipulator)viewer);
      hsp.setDividerLocation(0.6);
      vsp.setDividerLocation(0.35);
      hsp.setDividerSize(5);
      vsp.setDividerSize(6);
      viewer.repaint();
   }

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

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

