 /** ImageSaverPanel Class
   * @version 1.0  18 apr 2000
   * @author Lawrence Rodrigues
   **/
package com.vistech.jai.imageio;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.RenderedImage;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import com.sun.media.jai.codec.*;

public class ImageSaverPanel extends JPanel{
   String[] tags = {"JPEG", "BMP","TIFF","PNG_GRAY","PNG_RGB","PNG_PALLETTE", "PNM"};
   String fileName = "default";
   protected RenderedImage plImage;
   protected RenderedImage dispImage;
   JTextField fnField = null;
   JList saveTypeList;

   public ImageSaverPanel(){createUI();}

   public void setImage(PlanarImage pImage){ this.plImage = pImage;}

   public void setDisplayImage(PlanarImage pImage){
      this.dispImage = pImage.getAsBufferedImage();
   }

   public void saveDisplay(int ind){
      if(fnField.getText() != null)fileName = fnField.getText();
      save(dispImage, ind, fileName);
   }

   public void saveOrig(int ind){
      if(fnField.getText() != null)fileName = fnField.getText();
      save(plImage, ind, fileName);
   }

   private void createUI() {
      ListModel  saveTypeModel = new AbstractListModel(){
         public int getSize(){
            if(tags == null) return 0;
            else return tags.length;
         }
         public Object getElementAt(int index){
            if(tags == null) return null;
            if((index <0) || (index>tags.length)) return null;
            return tags[index];
         }
      };
      saveTypeList = new JList(saveTypeModel);
      saveTypeList.setForeground(Color.blue);
      saveTypeList.setSelectedIndex(0);
      MouseListener mouseListener = new MouseAdapter() {
         public void mouseClicked(MouseEvent e) {
            if(e.getClickCount() == 2) {
               int ind = saveTypeList.getSelectedIndex();
               String str = tags[ind];
               if(plImage == null) return;
               if(fnField.getText() != null)fileName = fnField.getText();
            }
         }
      };

      saveTypeList.addMouseListener(mouseListener);
      saveTypeList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
      fnField = new JTextField(12);
      fnField.setText(fileName);
      fnField.addActionListener(
           new ActionListener() {
              public void actionPerformed(ActionEvent e){
                 int ind = saveTypeList.getSelectedIndex();
                 if(fnField.getText() != null)fileName = fnField.getText();
              }
           }
      );
      JButton saveb = new JButton("Save Orig Image");
      saveb.addActionListener(
           new ActionListener() {
              public void actionPerformed(ActionEvent e){
                  int ind = saveTypeList.getSelectedIndex();
                  if(fnField.getText() != null)fileName = fnField.getText();
                  saveOrig(ind);
              }
           }
      );
      JButton saved = new JButton("Save Display Image");
      saved.addActionListener(
           new ActionListener() {
              public void actionPerformed(ActionEvent e){
                  int ind = saveTypeList.getSelectedIndex();
                  if(fnField.getText() != null)fileName = fnField.getText();
                  saveDisplay(ind);
              }
           }
      );

      JLabel flab = new JLabel("Enter file", SwingConstants.LEFT);
      JLabel typelab = new JLabel("Format ", SwingConstants.LEFT);
      GridBagLayout   gb = new GridBagLayout() ;
      GridBagConstraints c = new GridBagConstraints();
      setLayout(gb);
      c.insets = new Insets(10,1,0,0);
      c.anchor = GridBagConstraints.NORTHEAST;
      c.weighty = 1.0; c.weightx = 0.1;
      c.gridwidth = 1;
      gb.setConstraints(flab,c);
      add(flab,c);
      c.weighty = 1.0; c.weightx = 1.0;
      c.gridwidth = GridBagConstraints.REMAINDER;
      gb.setConstraints(fnField,c);
      add(fnField);
      c.gridwidth = 1;
      c.weighty = 1.0; c.weightx = 0.1;
      add(typelab);
      gb.setConstraints(typelab,c);
      c.fill = GridBagConstraints.BOTH;
      c.gridwidth = GridBagConstraints.REMAINDER;
      c.weighty = 1.0; c.weightx = 1.0;
      add(saveTypeList);
      gb.setConstraints(saveTypeList,c);
      c.gridwidth = 1;
      c.fill = GridBagConstraints.NONE;
      c.weighty = 0.1; c.weightx = 0.1;
      add(saveb);
      gb.setConstraints(saveb,c);
      add(saved);
      gb.setConstraints(saved,c);
   }
   protected void save(RenderedImage image, int ind, String filename) {
      try {
      switch(ind) {
        case 0:
          ImageSaverJAI.saveAsJPEG(image, fileName);
          break;
       case 1:
          ImageSaverJAI.saveAsBMP(image, fileName);
          break;
        case 2:
          ImageSaverJAI.saveAsTIFF(image, fileName);
          break;
        case 3:
          ImageSaverJAI.saveAsPNGGray(image, fileName);
          break;
        case 4:
          ImageSaverJAI.saveAsPNGRGB(image, fileName);
          break;
        case 5:
          ImageSaverJAI.saveAsPNGPalette(image, fileName);
          break;
        case 6:
          ImageSaverJAI.saveAsPNM(image, fileName, true);
          break;
      }
      } catch(Exception e) {}
   }
}