
/*
 * @(#) SaveAsJPEGServiceProvider.java 1.1 99/6/18
 * Copyright (c) 1999 Lawrence Rodrigues
 */

package com.vistech.util;
import java.io.*;
import java.beans.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.lang.reflect.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;
import java.beans.beancontext.*;
  
public class SaveAsJPEGServiceProvider implements SaveAsJPEGService,
                                       BeanContextServiceProvider {
    
    final static int BUF_IMAGE =1;
    final static int IMAGE =2;
    final static int COMPONENT = 4;
  
    BufferedImage destBufImage = null;
    Image destImage = null;
    Component destComp = null;
    int jpegQuality = 75;
    String defaultJpegFileName = "default.jpg";
    JSlider qualSlider; 
    JTextField qualityField; 
    boolean inuse = false;
    int saveType = BUF_IMAGE;
    BeanContextServices bcs;
    Object requestor;
    Class serviceClass;
    Object serviceSelector;


    public Object getService(BeanContextServices bcs, 
                             Object requestor, 
          	                 Class serviceClass, 
                             Object serviceSelector) {
       if(inuse) return null;
       this.bcs = bcs;
       this.requestor = requestor;
       this.serviceClass = serviceClass;
       this.serviceSelector = serviceSelector;
       inuse = true;
       return this;
    }
    
    public void releaseService(BeanContextServices bcs,
	             		 Object requestor, 
                               Object service) {
        System.out.println("release services called");
        destImage = null;
        destBufImage = null;
        destComp = null;
	  inuse = false;
    }

    public Iterator getCurrentServiceSelectors(BeanContextServices bcs,
						  	     Class serviceClass) {
	
		return null;
    }


    public void save() {
       switch(saveType) {
          case BUF_IMAGE:
             JpegUtil.saveImageAsJPEG(destBufImage, 
                                   jpegQuality/(float)100,
                                   defaultJpegFileName);
             break;
          case COMPONENT:
             JpegUtil.saveComponentAsJPEG(destComp,
                                   jpegQuality/(float)100,
                                   defaultJpegFileName);
       }
    }

    public void saveAs() {
       String fname = null; 
       switch(saveType) {
          case BUF_IMAGE: 
            fname = JpegUtil.saveImageAsJPEG(destBufImage, jpegQuality/(float)100);
            break;
          case COMPONENT:
            fname = JpegUtil.saveComponentAsJPEG(destComp, jpegQuality/(float)100);
       }
       if(fname != null) defaultJpegFileName = fname;
    }

    public void saveAs(int quality) {
       jpegQuality = quality;
        saveAs();
    }

    public void save(int quality, String filename) {
       jpegQuality = quality;
       defaultJpegFileName = filename;
       save();
    }

    public Component saveAsJPEG(BufferedImage bi){
       destBufImage = bi;
       saveType = BUF_IMAGE;
       return createUI();
       
    }
    
    public Component saveAsJPEG(Image img){
       destImage = img;
       saveType = IMAGE;
       return createUI();
    }

    public Component saveAsJPEG(Component comp){
       destComp = comp;
       saveType = COMPONENT;
       return createUI();
    }
 
    public Component createUI() {
       JButton save = new JButton("Save");
       save.addActionListener(
           new ActionListener(){
              public void actionPerformed(ActionEvent e){
                 save();
              }
           }
        );

        JButton saveas = new JButton("Save As");
        saveas.addActionListener(
           new ActionListener(){
              public void actionPerformed(ActionEvent e){
                 saveAs();
              }
           }
        );

        int minValue = 10;
        int maxValue =100;
        int qualStartValue = 75;
        qualityField = new JTextField(5);
        qualityField.setText(Integer.toString(qualStartValue));
        qualSlider = new JSlider(minValue,maxValue, qualStartValue);
        int majorTickSpacing = 10;
        //(new Double((maxValueX- minValueX)/5.0)).intValue()+1;
        int minorTickSpacing = 2;//(majorTickSpacing/5)+1;
        qualSlider.setMajorTickSpacing(majorTickSpacing);
        qualSlider.setMinorTickSpacing(minorTickSpacing);
        qualSlider.setExtent(minorTickSpacing);
        qualSlider.setPaintTicks(true);
        qualSlider.setPaintLabels(true);

        qualityField.addActionListener(
           new ActionListener() {
              public void actionPerformed(ActionEvent e){
                  try {
                    String str = ((JTextField)e.getSource()).getText();
                    int value = (Integer.valueOf(str)).intValue();
                    jpegQuality = value;
                    qualSlider.setValue((int)value);
                    System.out.println("qual field quality "+ value);
                  } catch (Exception e1){}
              }
           }
       );

       qualSlider.addChangeListener(
            new ChangeListener(){
              public void stateChanged(ChangeEvent e){
                 Object obj = e.getSource();
                 if(obj instanceof JSlider) {
                    JSlider jsr = (JSlider)obj;
                    //if(!jsr.getValueIsAdjusting()){
                       int quality = ((JSlider)(e.getSource())).getValue();
                       qualityField.setText(Integer.toString(quality));
                       jpegQuality = quality;
                    //}
                 }
              }
            }
       );

       JPanel jpegpan = new JPanel();
       GridBagLayout gb = new GridBagLayout() ;
       GridBagConstraints c = new GridBagConstraints();
       jpegpan.setLayout(gb);
       c.weighty = 1.0; c.weightx = 0.5;
       c.insets = new Insets(0,1, 0,10);
       //c.fill = GridBagConstraints.HORIZONTAL;
       gb.setConstraints(save, c);
       jpegpan.add(save);
       c.gridwidth = GridBagConstraints.REMAINDER;

       gb.setConstraints(saveas, c);
       jpegpan.add(saveas);
       c.gridwidth = 1;
       JLabel lab = new JLabel("Set Quality");
       gb.setConstraints(lab, c);
       jpegpan.add(lab);
       gb.setConstraints(qualSlider, c);
       jpegpan.add(qualSlider);
       gb.setConstraints(qualityField, c);
       jpegpan.add(qualityField);
       jpegpan.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(), "Save Image as JPEG"));
       return jpegpan;
       //launchFrame(jpegpan);
  }

  public static void launchDialog(Component child){
      final JDialog dialog = new JDialog(new Frame(), true);
      JButton jb = new JButton("OK");
      jb.addActionListener(
        new ActionListener() {
           public void actionPerformed(ActionEvent e ){
             if(dialog != null) dialog.dispose();
           }
        }
      );
      dialog.getContentPane().add("Center",child);
      dialog.getContentPane().add("South",jb);
      //dialog.pack();
      Dimension d = child.getPreferredSize(); 
      dialog.setSize(d.width+40, d.height+jb.getPreferredSize().height+40);
      dialog.show();
   }
 }