 /*
 * @(#) ViewerPanel.java 1.0 200/10/4
 * Copyright (c) 1999 Larry Rodrigues
 */
  package com.vistech.jai.render;
  import java.io.*;
  import java.awt.*;
  import java.awt.image.*;
  import java.awt.event.*;
  import java.util.*;
  import javax.swing.*;
  import javax.swing.event.*;
  import java.util.jar.*;
  import com.vistech.util.*;
  import java.awt.print.*;
  import java.beans.*;
  import java.beans.beancontext.*;
  import com.sun.image.codec.jpeg.*;
  import java.awt.geom.*;
  import java.awt.image.RenderedImage;
  import javax.media.jai.JAI;
  import javax.media.jai.*;
  import com.sun.media.jai.codec.*;
  import  com.vistech.jai.imageio.*;
  import com.vistech.jai.util.*;

  /** A container class for holding multiple image canvases, each of which
  * displays a planar image.
  * @version 1.0 4 Oct 2000
  * @author Lawrence Rodrigues
  **/

  public class ViewerPanel extends com.vistech.util.ApplicationFrame {
     protected int curIndex =0;
     protected int numImages= 0;
     protected PlanarImage[] imageset;
     protected int cellWidth = 100,cellHeight = 80;
     protected int rows =1,cols =1;
     protected int maxWidth = 600, minWidth =340;
     protected int maxHeight= 400, minHeight =220;
     transient protected ImagePanel vp[] = null;
     protected JPanel vpGrid;
     protected String defaultFileName = "defaultimage";
     protected JFrame saveFrame;
     protected ImageSaverPanel savePane;
     public ViewerPanel() { init();}
     public void init(){
        setViewerSize();
        createUI();
     }

     public void setImageSet(PlanarImage images[]){
        imageset = images;
        createNewPanel();
     }

     private void setViewerSize(){
        Dimension d= Toolkit.getDefaultToolkit().getScreenSize();
        maxWidth = (int)(d.width *2/3.0);
        maxHeight = (int)(d.height*3/4.0);
        minWidth = d.width/4;
        minHeight = d.height/4;
     }

     public Dimension getViewerSize(){
        return new Dimension(maxWidth, maxHeight);
     }

     private void createUI (){
        vpGrid = createViewportGrid();
        getContentPane().setLayout(new GridLayout(1,1));
        getContentPane().add(vpGrid);
        pack();
        setSize(maxWidth, maxHeight);
        addComponentListener(
            new ComponentAdapter() {
               public void componentResized(ComponentEvent e)  {
                  maxWidth = vpGrid.getSize().width;
                  maxHeight = vpGrid.getSize().height;
                  cellWidth = maxWidth/cols;
                  cellHeight= maxHeight/rows;
               }
            }
        );
     }
     protected JPanel createViewportGrid() {
        JPanel vpGrid = new JPanel();
        vpGrid.setBackground(Color.black);
        vpGrid.setSize(maxWidth, maxHeight);
        if(imageset == null) return vpGrid;
        numImages = imageset.length;
        if(numImages ==0) return vpGrid;
        int numvp = (int)(Math.ceil( Math.sqrt((double)numImages)));
        vp = new ImagePanel[numvp*numvp];
        cols = numvp;
        rows = (int)(Math.ceil(numImages/(double)numvp));
        vpGrid.setLayout(new GridLayout(rows, cols));
        cellWidth = maxWidth/cols;
        cellHeight= maxHeight/rows;
        for(int i=0;i<rows;i++){
            for(int j=0;j<cols;j++){
                if(vp[(i*cols)+j] == null){
                   try{
                     if(((i*cols)+j) < imageset.length){
                       vp[(i*cols)+j] = new ImagePanel(imageset[(i*cols)+j]);
                       vp[(i*cols)+j].repaint();
                     } else vp[(i*cols)+j] = new ImagePanel();
                   } catch(Exception e){}
                }
                vp[(i*cols)+j].setSize(new Dimension(cellWidth,cellHeight));
                vpGrid.add(vp[(i*cols)+j]);
            }
        }
        vpGrid.setSize(maxWidth, maxHeight);
        return vpGrid;
     }

     public Dimension getPreferredSize(){
        return new Dimension(maxHeight, maxWidth);
     }

     private void createNewPanel() {
        remove(vpGrid);
        invalidate();
        vpGrid = createViewportGrid();
        getContentPane().add(vpGrid);
        validate();
        paintImages();
     }

     public void paintImages(){
        for(int i=0;i<numImages;i++){
            if(imageset[i] == null) continue;
            vp[i].setImage(imageset[i]);
            vp[i].repaint();
            curIndex++;
            curIndex %= numImages;
        }
     }

     public void display(int imageIndex){
        if((imageIndex <0) ||
           (vp == null) ||
           (imageset[imageIndex] == null)) return;
        vp[curIndex].setImage(imageset[imageIndex]);
        vp[curIndex++].repaint();
        curIndex %= numImages;
     }

     public Dimension getCellSize(){
        if(vp[0] != null){
           return vp[0].getSize();
        }
        else return (new Dimension(cellWidth, cellHeight));
    }

    protected void save(){
       try{
         PlanarImage img = (PlanarImage) JAIUtil.aggregateImages(imageset,
                                          new Dimension(cellWidth, cellHeight),
                                          cols,rows);
         ImageSaverJAI.saveAsJPEG(img, "aggr.jpg");
       } catch(Exception e){}
    }
    protected void saveAs(){
       if((imageset == null)  || (imageset.length <=0)) return;
       PlanarImage img = (PlanarImage) JAIUtil.aggregateImages(imageset,
                                          new Dimension(cellWidth, cellHeight),
                                          cols,rows);
       if(saveFrame== null){
          saveFrame = new JFrame();
          savePane = new ImageSaverPanel();
          saveFrame.getContentPane().add(savePane);
          saveFrame.pack();
       }
       savePane.setImage(imageset[0]);
       savePane.setDisplayImage(img);
       saveFrame.show();
    }

    class ImagePanel extends JComponent {
      PlanarImage image;
      AffineTransform atx = new AffineTransform();
      int width, height;
      Dimension border = new Dimension(2,2);
      public ImagePanel(){ }
      public ImagePanel(PlanarImage img){
         RenderedOp op = JAIUtil.setConstantBorder(img,border,255);
         image = op.createInstance();
         width = image.getWidth();
         height = image.getHeight();
      }
      public void setImage(PlanarImage img){image = img;}

      public void paintComponent(Graphics gc){
         Graphics2D g = (Graphics2D)gc;
         Rectangle rect = this.getBounds();
         if((width != rect.width) || (height != rect.height)){
             double magx = rect.width/(double)width ;
             double magy = rect.height/(double)height ;
             atx.setToTranslation(border.width, border.height);
             atx.scale(magx, magy);
         }
         if(image != null) g.drawRenderedImage(image, atx);
      }
   }
 }
