
/*
 * @(#) Viewport.java 1.0 2000/11/13 Larry Rodrigues
 * Copyright 2000 Larry Rodrigues
 */
package vis.beans.plotter;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import com.vistech.util.*;
import java.beans.beancontext.*;
import java.beans.*;

/** The Viewport class provides the basic functionality to draw on a
  * viewport.
  * @version 1.2  13 Nov 1999
  * @author Larry Rodrigues
  **/
public abstract class Viewport extends JComponent implements Serializable,
                            BeanContextChild, BeanContextServicesListener{
   protected int width, height;
   protected double xScaleFac=1.0, yScaleFac= 1.0;
   protected double xMagFac =1.0, yMagFac =1.0;
   transient protected BufferedImage offScrImage, destImage;
   transient protected Graphics2D offScrGc, destGc;
   transient protected Image pixImage;
   private int curWid, curHt;

   protected BeanContext plBeanContext;
   /** Viewport Constructor.
     * @param wd the width of the viewport.
     * @param ht the height of the viewport.
     */
   public  Viewport(int wid, int ht){
      width = wid;
      height = ht;
      setSize(wid, ht);
   }
   public  Viewport(){}
   /** Sets width and height.
     * @param wd the width of the viewport.
     * @param ht the height of the viewport.
     **/
   public void setWidHt(int wd, int hgt){
      width  = wd; height = hgt;
   }

   /** Creates an OffScreen image. **/
   public  void createOffScrImage(){
      Rectangle r = getBounds();
      if((r.width ==0) || (r.height ==0)){
          return;
      }
      setWidHt(r.width, r.height);
      offScrImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
      destImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
      setOffScrGc();
   }

   /** Sets off screen graphics context. */
   public  void setOffScrGc(){
      if(offScrImage!= null){
         offScrGc = offScrImage.createGraphics();
         //Default Attributes
         offScrGc.setColor(Color.cyan);
         Font fnt = new Font("Helivetica", Font.BOLD,9);
         offScrGc.setFont(fnt);
         offScrGc.setColor(Color.black);
         offScrGc.fillRect(0,0, width, height);
      }

   }
   /** Paints an image within a given rectangle which can be placed
     * anywhere in the viewport
     * It is the responsibilty of the caller to check whether
     * the image is loaded.
     * @param img the image to  be painted
     * @param st the point on the image corresponding to
     * upper left hand corner(ULHC) of the rectangle
     * @param d the dimension of the rectangle in which the image is drawn
     **/
    public  void imagePaint(Image img, Point st, Dimension d){
        offScrGc.setColor(Color.black);
        offScrGc.drawImage(img, st.x, st.y, d.width, d.height, this);
        repaint();
    }

    /** Called by the system whenever the visibility status
      * of the viewport changes.
      **/
    public void paintComponent(Graphics paintGc){
       Graphics2D g2d = (Graphics2D)paintGc;
       Rectangle  border = getBounds();
       int wid = border.width;
       int ht = border.height;
       if((width != wid)|| (height != ht)){
           createOffScrImage();
           draw();
       }
       if(offScrImage != null){
          g2d.drawImage(offScrImage,0,0,wid,ht, this);
       }
    }
    /** All sub classes have to implement this method. */
    public abstract void draw();

    public void serviceRevoked( BeanContextServiceRevokedEvent bcsre) {}
    public void serviceAvailable( BeanContextServiceAvailableEvent sae) {}




    //------------------ Bean Context Methods ---------------------------
    public void setBeanContext(BeanContext bc)
                    throws PropertyVetoException {
       plBeanContext = bc;
    }

    public BeanContext getBeanContext(){
       return plBeanContext;
    }
    public Object invokeSaveImageService() {
       BeanContextServices bcs = (BeanContextServices)plBeanContext;
        Object save = null;
        try {
          save = bcs.getService(this,
                                this,
                                SaveAsJPEGService.class,
                                offScrImage,this);

        } catch (Exception e) {
            System.out.println("getService exe");
        }
        return save;
    }

    public Object invokePrinterService() {
        BeanContextServices bcs = (BeanContextServices)plBeanContext;
        Object print = null;
        try {
          print = bcs.getService(this,
                                this,
                                java.awt.print.PrinterJob.class,
                                this,this);

        } catch (Exception e) {}
        return print;
    }
    public void addPropertyChangeListener(String name, PropertyChangeListener p){
       if(plBeanContext!= null) plBeanContext.addPropertyChangeListener(name, p);
       else addPropertyChangeListener(p);
    }

    public void removePropertyChangeListener(String name, PropertyChangeListener p){
       if(plBeanContext!= null) plBeanContext.removePropertyChangeListener(name, p);
       else removePropertyChangeListener(p);
    }

    public void addVetoableChangeListener(String name, VetoableChangeListener p){
       if(plBeanContext!= null) plBeanContext.addVetoableChangeListener(name,p);
       else addVetoableChangeListener(p);
    }

    public void removeVetoableChangeListener(String name, VetoableChangeListener p){
       if(plBeanContext!= null) plBeanContext.removeVetoableChangeListener(name,p);
       else removeVetoableChangeListener(p);
    }
}












































