/* @(#) ViewerPanel.java 1.0 99/10/4
 * Copyright (c) 1999 Larry Rodrigues
 */
package com.vistech.imageviewer;
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.*;

/** A container class to hold multiple image canvases.
 * @version 1.0 4 October 1999
 * @author Lawrence Rodrigues
 **/
public class ViewerPanel extends com.vistech.util.ApplicationFrame implements Printable {
   protected int curIndex =0;
   protected int numImages= 0;
   protected Image[] imageset;
   protected  int cellWidth = 100;
   protected int cellHeight = 80;
   protected int rows =1;
   protected int cols =1;
   protected int maxWidth = 600, minWidth =340;
   protected int maxHeight= 400, minHeight =220;
   transient protected ImageCanvas vp[] = null;
   protected JPanel vpGrid;
   String defaultFileName = "defaultimage";
   public ViewerPanel() {
      init();
   }
   public void init(){
      setViewerSize();
      createUI();
   }

   public void setImageSet(Image 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);
   }
   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 ImageCanvas[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++){
              int ind = (i*cols)+j;
              if(vp[ind] == null){
                 try{
                   vp[ind] = new ImageCanvas();
                   if(ind < imageset.length){
                     vp[ind].setAWTImage(imageset[ind]);
                     vp[ind].repaint();
                   }
                 } catch(Exception e){}
              }
              vp[ind].setSize(new Dimension(cellWidth,cellHeight));
              vpGrid.add(vp[ind]);
          }
      }
      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 ImageCanvas[] getViewports(){
      return vp;
   }

   public void paintImages(){
      for(int i=0;i<numImages;i++){
          if(imageset[i] == null) continue;
          vp[i].setAWTImage(imageset[i]);
          vp[i].repaint();
          curIndex++;
          curIndex %= numImages;
      }
   }
   public void display(int imageIndex){
      if((imageIndex <0) ||
         (vp == null) ||
         (imageset[imageIndex] == null)) return;
      vp[curIndex].setAWTImage(imageset[imageIndex]);
      vp[curIndex++].repaint();
      curIndex %= numImages;
   }
   public Dimension getCellSize(){
      if(vp[0] != null){
         return vp[0].getSize();
      }
      else return (new Dimension(cellWidth, cellHeight));
   }

   public BufferedImage createBufferedImage() {
     Dimension dim = getCellSize();
     int imWid = cols*dim.width;
     int imHt =  rows*dim.height;
     BufferedImage bi = new BufferedImage((int)imWid,
                                          (int)(imHt),
                                           BufferedImage.TYPE_INT_RGB);
     Graphics2D g2d = bi.createGraphics();
     int x = 0; int y=0;
     for(int i=0; i< rows; i++) {
         for(int j=0;j<cols; j++) {
             if(i*cols+j >= imageset.length) break;
             Image img = imageset[i*cols+j];
             if(img != null)
                g2d.drawImage(img, x+1,y+1, dim.width-2, dim.height-2,this);
             x += dim.width;
         }
         x =0;
         y += dim.height;
      }
     return bi;
  }

  protected void saveAs(){
     JpegUtil.saveImageAsJPEG(createBufferedImage(), .9f);
  }

  protected void save(){
     JpegUtil.saveImageAsJPEG(createBufferedImage(), .9f, defaultFileName);
  }

  protected void printContents() {
     PrinterJob pj = PrinterJob.getPrinterJob();
     pj.setPrintable(this);
     pj.printDialog();
     try{
        pj.print();
     }catch(Exception e){System.out.println(e);}
 }

  public int print(Graphics g, PageFormat f, int pageIndex){
      if(pageIndex >= 1) return   Printable.NO_SUCH_PAGE;
      Graphics2D g2d = (Graphics2D) g;
      g2d.translate(f.getImageableX(), f.getImageableY());
      ImagePrint cmp[] = (ImagePrint[])getViewports();
      if(cmp == null) return   Printable.NO_SUCH_PAGE;
      for(int i=0;i<cmp.length;i++){
          cmp[i].setPrintLocation(((Component)cmp[i]).getLocation());
          cmp[i].setPrintSize(((Component)cmp[i]).getSize());
          cmp[i].printImage(g2d);
      }
      return Printable.PAGE_EXISTS;
  }
}
