home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / Source.bin / ImageViewer.java < prev    next >
Encoding:
Java Source  |  1998-09-14  |  14.9 KB  |  568 lines

  1. package symantec.itools.multimedia;
  2.  
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.awt.MediaTracker;
  8. import java.awt.Toolkit;
  9. import java.net.URL;
  10. import java.net.MalformedURLException;
  11. import java.beans.PropertyVetoException;
  12. import java.beans.PropertyChangeListener;
  13. import java.beans.VetoableChangeListener;
  14. import java.io.ObjectInputStream;
  15. import java.io.IOException;
  16. import java.util.ResourceBundle;
  17. import java.text.MessageFormat;
  18.  
  19. //    01/29/97    TWB    Integrated changes from Windows
  20. //    02/19/97    RKM    Changed default for centerMode to correspond with DES file
  21. //                    Changed setCenterMode to call repaint instead of invalidate
  22. //                    Changed setCenterMode to repaint only if value had changed
  23. //
  24. //    05/29/97    MSH    Updated to support Java 1.1
  25. //  07/16/97    CAR marked fields transient as needed
  26. //                  implemented readObject to load an Image after deserialization
  27. //  07/17/97    CAR added add/remove property/vetoable ChangeListner methods
  28. //  08/04/97    LAB Made lightweight.  Implemented setStyle and getStyle.  Deprecated
  29. //                    setCentered and getCentered.
  30. //  08/27/97    CAR update setURL and setFileName to "erase" image if URL or String is null
  31. //  08/04/97    LAB Deprecated URL property and replaced it with ImageURL property (Addresses
  32. //                    Mac Bug #7689).  Changed names of strings in PropertyChangeEvent handling
  33. //                    to follow Bean Spec naming conventions.
  34.  
  35. /**
  36.  * ImageViewer component. Provides a platform-independent display of an Image.
  37.  * @version 1.1, August 4, 1997
  38.  * @author    Symantec
  39.  */
  40. public class ImageViewer extends Component implements java.io.Serializable
  41. {
  42.     /**
  43.      * A constant indicating the image is to be tiled in the size of this component.
  44.      */
  45.     public static final int IMAGE_TILED = 0;
  46.     /**
  47.      * A constant indicating the image is to be centered in the size of this component.
  48.      */
  49.     public static final int IMAGE_CENTERED = 1;
  50.     /**
  51.      * A constant indicating the image is to be scaled to fit the size of this component.
  52.      */
  53.     public static final int IMAGE_SCALED_TO_FIT = 2;
  54.     /**
  55.      * A constant indicating the image is to be drawn normally in the upper left corner.
  56.      */
  57.     public static final int IMAGE_NORMAL = 3;
  58.  
  59.     /**
  60.      * Create default image viewer.
  61.      */
  62.     public ImageViewer()
  63.     {
  64.         image        = null;
  65.         fileName    = null;
  66.         url            = null;
  67.         imageStyle    = IMAGE_CENTERED;
  68.     }
  69.  
  70.     /**
  71.      * Crate image viewer with filename.  The specified filename is used as
  72.      * the image source.
  73.      *
  74.      * @param str name of file containing the image source
  75.      * @exception MalformedURLException Thrown if URL cannot be generated from filename
  76.      *
  77.      */
  78.     public ImageViewer(String str) throws MalformedURLException
  79.     {
  80.         this();
  81.  
  82.         try
  83.         {
  84.             setFileName(str);
  85.         }
  86.         catch(PropertyVetoException e){}
  87.     }
  88.  
  89.     /**
  90.      * Create image viewer with URL.  The specified URL is used as
  91.      * the image source.
  92.      *
  93.      * @param url the URL of the image to be displayed
  94.      */
  95.     public ImageViewer(URL url)
  96.     {
  97.         this();
  98.  
  99.         try
  100.         {
  101.              setURL(url);
  102.         }
  103.         catch ( PropertyVetoException e ) { }
  104.     }
  105.  
  106.     /**
  107.      * Create image viewer with image.  The specified image is used as
  108.      * the image source
  109.      *
  110.      * @param img the image to be displayed
  111.      */
  112.     public ImageViewer(Image img)
  113.     {
  114.         this();
  115.  
  116.         try
  117.         {
  118.             setImage(img);
  119.         }
  120.         catch ( PropertyVetoException e ) { }
  121.     }
  122.  
  123.     /**
  124.      * Specify or change the image filename.
  125.      *
  126.      * @param str name of file containing image source
  127.      *
  128.      * @exception PropertyVetoException
  129.      * if the specified property value is unacceptable
  130.      */
  131.     public void setFileName(String str) throws PropertyVetoException
  132.     {
  133.         String oldValue = fileName;
  134.  
  135.         try
  136.         {
  137.             vetos.fireVetoableChange("fileName", oldValue, str );
  138.  
  139.            fileName = str;
  140.            if(fileName != null && fileName != "")
  141.                setURL(new URL(fileName));
  142.            else
  143.                setURL(null);
  144.  
  145.         }
  146.         catch(MalformedURLException e)
  147.         {
  148.             //System.out.println("malformed URL");
  149.             fileName = oldValue;
  150.         }
  151.         repaint();
  152.  
  153.         changes.firePropertyChange("fileName", oldValue, str );
  154.     }
  155.  
  156.     /**
  157.      * Obtain the filename associated with the current image.
  158.      *
  159.      * @return the name of the file, if any, associated with this image.  If
  160.      * no file is associated with this image, returns null
  161.      */
  162.     public String getFileName()
  163.     {
  164.         return fileName;
  165.     }
  166.  
  167.     /**
  168.      * Specify or change the image URL.
  169.      *
  170.      * @param aUrl the URL of the image to be displayed
  171.      *
  172.      * @exception PropertyVetoException
  173.      * if the specified property value is unacceptable
  174.      */
  175.     public void setImageURL(URL aUrl) throws PropertyVetoException
  176.     {
  177.         URL oldValue = url;
  178.         vetos.fireVetoableChange("imageURL", oldValue, aUrl );
  179.  
  180.         url = aUrl;
  181.         fileName = null;
  182.         Image loadedImage = null;
  183.         if(url != null){
  184.             loadedImage = getToolkit().getImage(url);
  185.         }
  186.         setImage(loadedImage);
  187.         repaint();
  188.         changes.firePropertyChange("imageURL", oldValue, aUrl );
  189.     }
  190.  
  191.    /**
  192.      * Obtain the URL associated with the current image.  If the image
  193.      * was specified by file name, or URL, it will have a URL which
  194.      * indicates its source.  Images created using the constructor
  195.      * with an Image parameter will have no associated URL.
  196.      *
  197.      * @return the name of the URL, if any, associated with this image.  If
  198.                no URL is associated with this image, returns null
  199.      */
  200.     public URL getImageURL()
  201.     {
  202.         return url;
  203.     }
  204.  
  205.     /**
  206.      * @deprecated
  207.      * @see #setImageURL
  208.      */
  209.     public void setURL(URL aUrl) throws PropertyVetoException
  210.     {
  211.         setImageURL(aUrl);
  212.     }
  213.  
  214.    /**
  215.      * @deprecated
  216.      * @see #getImageURL
  217.      */
  218.     public URL getURL()
  219.     {
  220.         return getImageURL();
  221.     }
  222.  
  223.     /**
  224.      * @deprecated
  225.      * @see #setStyle
  226.      * @exception PropertyVetoException
  227.      * if the specified property value is unacceptable
  228.      */
  229.     public void setCenterMode(boolean flag) throws PropertyVetoException
  230.     {
  231.         if(flag)
  232.         {
  233.             if(getStyle() != IMAGE_CENTERED)
  234.                 setStyle(IMAGE_CENTERED);
  235.         }
  236.         else
  237.         {
  238.             if(getStyle() != IMAGE_NORMAL)
  239.                 setStyle(IMAGE_NORMAL);
  240.         }
  241.     }
  242.  
  243.     /**
  244.      * @deprecated
  245.      * @see #getStyle
  246.      */
  247.     public boolean getCenterMode()
  248.     {
  249.         return (getStyle() == IMAGE_CENTERED);
  250.     }
  251.  
  252.     /**
  253.      * Sets the new panel image style.
  254.      * @param newStyle the new panel image style, one of
  255.      * IMAGE_TILED, IMAGE_CENTERED, or IMAGE_SCALED_TO_FIT
  256.      * @exception PropertyVetoException
  257.      * if the specified property value is unacceptable
  258.      * @see #getStyle
  259.      * @see #IMAGE_TILED
  260.      * @see #IMAGE_CENTERED
  261.      * @see #IMAGE_SCALED_TO_FIT
  262.      * @see #IMAGE_NORMAL
  263.      */
  264.     public void setStyle(int newStyle) throws PropertyVetoException
  265.     {
  266.         if (newStyle != imageStyle)
  267.         {
  268.             Integer oldValue = new Integer(imageStyle);
  269.             Integer newValue = new Integer(newStyle);
  270.  
  271.             vetos.fireVetoableChange("style", oldValue, newValue);
  272.  
  273.             imageStyle = newStyle;
  274.             repaint();
  275.  
  276.             changes.firePropertyChange("style", oldValue, newValue);
  277.         }
  278.     }
  279.  
  280.     /**
  281.      * Gets the current panel image style.
  282.      * @return the current panel image style, one of
  283.      * IMAGE_TILED, IMAGE_CENTERED, or IMAGE_SCALED_TO_FIT
  284.      * @see #setStyle
  285.      * @see #IMAGE_TILED
  286.      * @see #IMAGE_CENTERED
  287.      * @see #IMAGE_SCALED_TO_FIT
  288.      */
  289.     public int getStyle()
  290.     {
  291.         return imageStyle;
  292.     }
  293.  
  294.     /**
  295.      * Set or change the current image.  Call this method if you want to
  296.      * specify directly the image to display.
  297.      *
  298.      * @param img the image to be displayed
  299.      *
  300.      * @exception PropertyVetoException
  301.      * if the specified property value is unacceptable
  302.      */
  303.     public void setImage(Image img) throws PropertyVetoException
  304.     {
  305.         fileName = null;
  306.         Image oldValue = image;
  307.  
  308.         vetos.fireVetoableChange( "image", oldValue, img );
  309.  
  310.         image    = img;
  311.  
  312.         if (img != null)
  313.         {
  314.             MediaTracker tracker;
  315.  
  316.             try
  317.             {
  318.                 tracker = new MediaTracker(this);
  319.                 tracker.addImage(image, 0);
  320.                 tracker.waitForID(0);
  321.             }
  322.             catch(InterruptedException e) { }
  323.         }
  324.         else
  325.               repaint();
  326.  
  327.         changes.firePropertyChange( "image", oldValue, img );
  328.     }
  329.  
  330.     /**
  331.      * Obtain the image currently being displayed.
  332.      *
  333.      * @return the image currently displayed or null if no image
  334.      */
  335.     public Image getImage()
  336.     {
  337.         return image;
  338.     }
  339.  
  340.     /**
  341.      * Paints this component using the given graphics context.
  342.      * This is a standard Java AWT method which typically gets called
  343.      * by the AWT to handle painting this component. It paints this component
  344.      * using the given graphics context. The graphics context clipping region
  345.      * is set to the bounding rectangle of this component and its [0,0]
  346.      * coordinate is this component's top-left corner.
  347.      *
  348.      * @param g the graphics context used for painting
  349.      * @see java.awt.Component#repaint
  350.      * @see java.awt.Component#update
  351.      */
  352.     public void paint(Graphics g)
  353.     {
  354.         super.paint(g);
  355.  
  356.         Dimension dim = size();
  357.         if (image != null)
  358.         {
  359.  
  360.             int imageWidth = image.getWidth(this);
  361.             int imageHeight = image.getHeight(this);
  362.  
  363.             switch(imageStyle)
  364.             {
  365.                 case IMAGE_CENTERED:
  366.                 default:
  367.                 {
  368.                     g.drawImage
  369.                         (image,
  370.                          (dim.width - imageWidth) / 2,
  371.                          (dim.height - imageHeight) / 2,
  372.                          imageWidth,
  373.                          imageHeight,
  374.                          this);
  375.  
  376.                     break;
  377.                 }
  378.  
  379.                 case IMAGE_TILED:
  380.                 {
  381.                     //Calculate number of images that should be drawn horizontally
  382.                     int numHImages = dim.width / imageWidth;
  383.  
  384.                     //Don't forget remainders
  385.                     if (dim.width % imageWidth != 0)
  386.                         numHImages++;
  387.  
  388.                     //Calculate number of images that should be drawn vertically
  389.                     int numVImages = dim.height / imageHeight;
  390.  
  391.                     //Don't forget remainders
  392.                     if (dim.height % imageHeight != 0)
  393.                         numVImages++;
  394.  
  395.                     int h;
  396.                     int v = 0;
  397.                     for (int vCount = 0;vCount < numVImages;vCount++)
  398.                     {
  399.                         h = 0;
  400.                         for (int hCount = 0;hCount < numHImages;hCount++)
  401.                         {
  402.                             g.drawImage(image, h, v, imageWidth, imageHeight, this);
  403.  
  404.                             //Increment to next column
  405.                             h += imageWidth;
  406.                         }
  407.  
  408.                         //Increment to next row
  409.                         v += imageHeight;
  410.                     }
  411.  
  412.                     break;
  413.                 }
  414.  
  415.  
  416.                 case IMAGE_SCALED_TO_FIT:
  417.                 {
  418.                     g.drawImage(image, 0, 0, dim.width, dim.height, this);
  419.  
  420.                     break;
  421.                 }
  422.  
  423.                 case IMAGE_NORMAL:
  424.                 {
  425.                     g.drawImage(image, 0, 0, this);
  426.                     break;
  427.                 }
  428.             }//switch
  429.         }
  430.         else
  431.         {
  432.             g.clearRect(0, 0, dim.width, dim.height);
  433.         }
  434.     }
  435.  
  436.     /**
  437.      * Returns the recommended dimensions to properly display this component.
  438.      * This is a standard Java AWT method which gets called to determine
  439.      * the recommended size of this component.
  440.      *
  441.      * @return  If no image has been loaded, a dimension of 10 by 10 is returned.
  442.      *          If an image has been loaded, the height and width of the image
  443.      *          is returned.
  444.      *
  445.      * @see #minimumSize
  446.      */
  447.     public Dimension preferredSize()
  448.     {
  449.         if (image != null)
  450.             return (new Dimension(image.getWidth(this), image.getHeight(this)));
  451.         else
  452.             return new Dimension(10, 10);
  453.     }
  454.  
  455.  
  456.     /**
  457.      * Returns the minimum dimensions to properly display this component.
  458.      * This is a standard Java AWT method which gets called to determine
  459.      * the minimum size of this component.
  460.      *
  461.      * @return  If no image has been loaded, a dimension of 10 by 10 is returned.
  462.      *          If an image has been loaded, the height and width of the image
  463.      *          is returned.
  464.      * @see #preferredSize
  465.      */
  466.     public Dimension minimumSize()
  467.     {
  468.         return preferredSize();
  469.     }
  470.  
  471.     /**
  472.      * Adds a listener for all event changes.
  473.      * @param PropertyChangeListener listener the listener to add.
  474.      * @see #removePropertyChangeListener
  475.      */
  476.     public void addPropertyChangeListener(PropertyChangeListener listener)
  477.     {
  478.         //super.addPropertyChangeListener(listener);
  479.         changes.addPropertyChangeListener(listener);
  480.     }
  481.  
  482.     /**
  483.      * Removes a listener for all event changes.
  484.      * @param PropertyChangeListener listener the listener to remove.
  485.      * @see #addPropertyChangeListener
  486.      */
  487.     public void removePropertyChangeListener(PropertyChangeListener listener)
  488.     {
  489.         //super.removePropertyChangeListener(listener);
  490.         changes.removePropertyChangeListener(listener);
  491.     }
  492.  
  493.     /**
  494.      * Adds a vetoable listener for all event changes.
  495.      * @param VetoableChangeListener listener the listener to add.
  496.      * @see #removeVetoableChangeListener
  497.      */
  498.     public void addVetoableChangeListener(VetoableChangeListener listener)
  499.     {
  500.          //super.addVetoableChangeListener(listener);
  501.         vetos.addVetoableChangeListener(listener);
  502.     }
  503.  
  504.     /**
  505.      * Removes a vetoable listener for all event changes.
  506.      * @param VetoableChangeListener listener the listener to remove.
  507.      * @see #addVetoableChangeListener
  508.      */
  509.     public void removeVetoableChangeListener(VetoableChangeListener listener)
  510.     {
  511.         //super.removeVetoableChangeListener(listener);
  512.         vetos.removeVetoableChangeListener(listener);
  513.     }
  514.  
  515.     private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
  516.         stream.defaultReadObject();
  517.         
  518.         try
  519.         {
  520.             errors = ResourceBundle.getBundle("symantec.itools.resources.ErrorsBundle");
  521.         }
  522.         catch(Throwable ex)
  523.         {
  524.             errors = new symantec.itools.resources.ErrorsBundle();
  525.         }
  526.  
  527.         if (url != null) {
  528.             image = getToolkit().getImage(url);
  529.  
  530.             MediaTracker tracker = new MediaTracker(this);
  531.             tracker.addImage(image, 0);
  532.             try {
  533.                 tracker.waitForAll();
  534.             }
  535.             catch(InterruptedException e) {
  536.             Object[] args = { url };
  537.             throw new IOException(MessageFormat.format(errors.getString("ErrorLoadingImageForURL"), args));
  538.             }
  539.         }
  540.     }
  541.  
  542.    /**
  543.      * Image that this viewer is displaying.
  544.      */
  545.     transient protected Image image;
  546.  
  547.     /**
  548.      * Name of file, if any, associated with this image.
  549.      */
  550.     protected String fileName;
  551.  
  552.     /**
  553.      * URL of the image being displayed.
  554.      */
  555.     protected URL url;
  556.  
  557.     /**
  558.      * Determines how to draw the image.
  559.      */
  560.     protected int imageStyle;
  561.  
  562.     transient protected ResourceBundle errors;
  563.  
  564.     private symantec.itools.beans.VetoableChangeSupport vetos   = new symantec.itools.beans.VetoableChangeSupport(this);
  565.     private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  566.  
  567. }
  568.