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

/** An application that displays renderable images. This application uses the RenderableImageCanvas
  * as the component to display a renderable image.
  * @version 1.0 10 Apr 2000
  * @author Lawrence Rodrigues
  **/
public class RenderableImageViewer extends JAIImageViewer{
   RenderableImageCanvas viewer;
   public static void  main(String[] args){
      RenderableImageViewer ip = new RenderableImageViewer();
      if(args.length <1) {
         ip.createUI();
      }else ip.loadAndDisplay(args[0]);
   }
   public void displayImage(PlanarImage img) {
      int wid = img.getWidth();
      int ht = img.getHeight();
      ParameterBlock pb = new ParameterBlock();
      pb.addSource(img);
      pb.add(null).add(null).add(null).add(null).add(null);
      RenderableOp op = JAI.createRenderable("renderable", pb);
      viewer.setImage(op);
      viewer.setOrigImageSize(new Dimension(wid, ht));
      saver.setImage(img);
      viewer.repaint();
   }

   public void createUI() {
      Dimension dim= Toolkit.getDefaultToolkit().getScreenSize();
      int width = (int)(dim.width *3/4.0);
      int height = (int)(dim.height*3/4.0);

      setTitle("Renderable Image Viewer");
      viewer = new RenderableImageCanvas();
      Dimension d = ApplicationFrame.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;
                SwingUtilities.invokeLater(new ImagePaint(image));
             }
         }
      );

      JTabbedPane jtp = new JTabbedPane();
      jtp.add("Load", flBrowser);
      jtp.add("Save",  saver);
      JPanel leftpan = new JPanel();

      JPanel dispmodepan = createDisplayModePanel();

      JSplitPane hsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jtp,dispmodepan);

      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();
      hsp.setDividerLocation(0.85);
      vsp.setDividerLocation(0.32);
      hsp.setDividerSize(5);
      vsp.setDividerSize(6);
      initializeScroll();
      viewer.repaint();
   }

   protected void initializeScroll(){
      scroll = new ScrollGUI(viewer);
      viewer.addMouseListener(scroll);
      viewer.addMouseMotionListener(scroll);
   }

   protected JPanel createDisplayModePanel() {
      ButtonGroup bg = new ButtonGroup();
      JRadioButton normal = new JRadioButton("Scaled");
      JRadioButton tofit = new JRadioButton("To Fit");
      JRadioButton orig = new JRadioButton("ORIG_SIZE");

      DisplayModeAdapter danormal = new DisplayModeAdapter(RenderableImageCanvas.SCALED);
      DisplayModeAdapter datofit = new DisplayModeAdapter(RenderableImageCanvas.TO_FIT);
      DisplayModeAdapter daorig = new DisplayModeAdapter(RenderableImageCanvas.MAX_SIZE);
      normal.setSelected(true);

      normal.addActionListener(danormal);
      tofit.addActionListener(datofit);
      orig.addActionListener(daorig);

      bg.add(normal);
      bg.add(tofit);
      bg.add(orig);
      JPanel jp = new JPanel();

      jp.add(normal);
      jp.add(tofit);
      jp.add(orig);
      jp.setBorder(BorderFactory.createTitledBorder("Display Modes"));
      return jp;
   }

   class DisplayModeAdapter implements ActionListener {
      int mode;
      public DisplayModeAdapter( int type) { mode = type;}
      public void actionPerformed(ActionEvent e) {
         viewer.setDisplayMode(mode);
      }
   }

   class ImagePaint implements Runnable {
      PlanarImage image;
      boolean firstTime = true;
      public ImagePaint(PlanarImage image){this.image = image;}
      public void run() {
         if(firstTime) {
            try {
              setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
              displayImage(image);
              firstTime = false;
              SwingUtilities.invokeLater(this);
            } catch(Exception e){SwingUtilities.invokeLater(this);}
         }
         else {
            if(!viewer.isImageDrawn()) SwingUtilities.invokeLater(this);
            else{
                setCursor(Cursor.getDefaultCursor());
            }
         }
      }
   }
 }
