home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / Source.bin / ImageViewer.java < prev    next >
Text File  |  1998-03-18  |  15KB  |  566 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.      * @exception PropertyVetoException
  209.      * if the specified property value is unacceptable
  210.      */
  211.     public void setURL(URL aUrl) throws PropertyVetoException
  212.     {
  213.         setImageURL(aUrl);
  214.     }
  215.  
  216.    /**
  217.      * @deprecated
  218.      * @see #getImageURL
  219.      */
  220.     public URL getURL()
  221.     {
  222.         return getImageURL();
  223.     }
  224.  
  225.     /**
  226.      * @deprecated
  227.      * @see #setStyle
  228.      * @exception PropertyVetoException
  229.      * if the specified property value is unacceptable
  230.      */
  231.     public void setCenterMode(boolean flag) throws PropertyVetoException
  232.     {
  233.         if(flag)
  234.         {
  235.             if(getStyle() != IMAGE_CENTERED)
  236.                 setStyle(IMAGE_CENTERED);
  237.         }
  238.         else
  239.         {
  240.             if(getStyle() != IMAGE_NORMAL)
  241.                 setStyle(IMAGE_NORMAL);
  242.         }
  243.     }
  244.  
  245.     /**
  246.      * @deprecated
  247.      * @see #getStyle
  248.      */
  249.     public boolean getCenterMode()
  250.     {
  251.         return (getStyle() == IMAGE_CENTERED);
  252.     }
  253.  
  254.     /**
  255.      * Sets the new panel image style.
  256.      * @param newStyle the new panel image style, one of
  257.      * IMAGE_TILED, IMAGE_CENTERED, or IMAGE_SCALED_TO_FIT
  258.      * @exception PropertyVetoException
  259.      * if the specified property value is unacceptable
  260.      * @see #getStyle
  261.      * @see #IMAGE_TILED
  262.      * @see #IMAGE_CENTERED
  263.      * @see #IMAGE_SCALED_TO_FIT
  264.      * @see #IMAGE_NORMAL
  265.      */
  266.     public void setStyle(int newStyle) throws PropertyVetoException
  267.     {
  268.         if (newStyle != imageStyle)
  269.         {
  270.             Integer oldValue = new Integer(imageStyle);
  271.             Integer newValue = new Integer(newStyle);
  272.  
  273.             vetos.fireVetoableChange("style", oldValue, newValue);
  274.  
  275.             imageStyle = newStyle;
  276.             repaint();
  277.  
  278.             changes.firePropertyChange("style", oldValue, newValue);
  279.         }
  280.     }
  281.  
  282.     /**
  283.      * Gets the current panel image style.
  284.      * @return the current panel image style, one of
  285.      * IMAGE_TILED, IMAGE_CENTERED, or IMAGE_SCALED_TO_FIT
  286.      * @see #setStyle
  287.      * @see #IMAGE_TILED
  288.      * @see #IMAGE_CENTERED
  289.      * @see #IMAGE_SCALED_TO_FIT
  290.      */
  291.     public int getStyle()
  292.     {
  293.         return imageStyle;
  294.     }
  295.  
  296.     /**
  297.      * Set or change the current image.  Call this method if you want to
  298.      * specify directly the image to display.
  299.      *
  300.      * @param img the image to be displayed
  301.      *
  302.      * @exception PropertyVetoException
  303.      * if the specified property value is unacceptable
  304.      */
  305.     public void setImage(Image img) throws PropertyVetoException
  306.     {
  307.         fileName = null;
  308.         Image oldValue = image;
  309.  
  310.         vetos.fireVetoableChange( "image", oldValue, img );
  311.  
  312.         image    = img;
  313.  
  314.         if (img != null)
  315.         {
  316.             MediaTracker tracker;
  317.  
  318.             try
  319.             {
  320.                 tracker = new MediaTracker(this);
  321.                 tracker.addImage(image, 0);
  322.                 tracker.waitForID(0);
  323.             }
  324.             catch(InterruptedException e) { }
  325.         }
  326.         else
  327.               repaint();
  328.  
  329.         changes.firePropertyChange( "image", oldValue, img );
  330.     }
  331.  
  332.     /**
  333.      * Obtain the image currently being displayed.
  334.      *
  335.      * @return the image currently displayed or null if no image
  336.      */
  337.     public Image getImage()
  338.     {
  339.         return image;
  340.     }
  341.  
  342.     /**
  343.      * Paints this component using the given graphics context.
  344.      * This is a standard Java AWT method which typically gets called
  345.      * by the AWT to handle painting this component. It paints this component
  346.      * using the given graphics context. The graphics context clipping region
  347.      * is set to the bounding rectangle of this component and its [0,0]
  348.      * coordinate is this component's top-left corner.
  349.      *
  350.      * @param g the graphics context used for painting
  351.      * @see java.awt.Component#repaint
  352.      * @see java.awt.Component#update
  353.      */
  354.     public void paint(Graphics g)
  355.     {
  356.         super.paint(g);
  357.  
  358.         Dimension dim = size();
  359.         if (image != null)
  360.         {
  361.  
  362.             int imageWidth = image.getWidth(this);
  363.             int imageHeight = image.getHeight(this);
  364.  
  365.             switch(imageStyle)
  366.             {
  367.                 case IMAGE_CENTERED:
  368.                 default:
  369.                 {
  370.                     g.drawImage
  371.                         (image,
  372.                          (dim.width - imageWidth) / 2,
  373.                          (dim.height - imageHeight) / 2,
  374.                          imageWidth,
  375.                          imageHeight,
  376.                          this);
  377.  
  378.                     break;
  379.                 }
  380.  
  381.                 case IMAGE_TILED:
  382.                 {
  383.                     //Calculate number of images that should be drawn horizontally
  384.                     int numHImages = dim.width / imageWidth;
  385.  
  386.                     //Don't forget remainders
  387.                     if (dim.width % imageWidth != 0)
  388.                         numHImages++;
  389.  
  390.                     //Calculate number of images that should be drawn vertically
  391.                     int numVImages = dim.height / imageHeight;
  392.  
  393.                     //Don't forget remainders
  394.                     if (dim.height % imageHeight != 0)
  395.                         numVImages++;
  396.  
  397.                     int h;
  398.                     int v = 0;
  399.                     for (int vCount = 0;vCount < numVImages;vCount++)
  400.                     {
  401.                         h = 0;
  402.                         for (int hCount = 0;hCount < numHImages;hCount++)
  403.                         {
  404.                             g.drawImage(image, h, v, imageWidth, imageHeight, this);
  405.  
  406.                             //Increment to next column
  407.                             h += imageWidth;
  408.                         }
  409.  
  410.                         //Increment to next row
  411.                         v += imageHeight;
  412.                     }
  413.  
  414.                     break;
  415.                 }
  416.  
  417.  
  418.                 case IMAGE_SCALED_TO_FIT:
  419.                 {
  420.                     g.drawImage(image, 0, 0, dim.width, dim.height, this);
  421.  
  422.                     break;
  423.                 }
  424.  
  425.                 case IMAGE_NORMAL:
  426.                 {
  427.                     g.drawImage(image, 0, 0, this);
  428.                     break;
  429.                 }
  430.             }//switch
  431.         }
  432.         else
  433.         {
  434.             g.clearRect(0, 0, dim.width, dim.height);
  435.         }
  436.     }
  437.  
  438.     /**
  439.      * Returns the recommended dimensions to properly display this component.
  440.      * This is a standard Java AWT method which gets called to determine
  441.      * the recommended size of this component.
  442.      *
  443.      * @return  If no image has been loaded, a dimension of 10 by 10 is returned.
  444.      *          If an image has been loaded, the height and width of the image
  445.      *          is returned.
  446.      *
  447.      * @see #minimumSize
  448.      */
  449.     public Dimension preferredSize()
  450.     {
  451.         if (image != null)
  452.             return (new Dimension(image.getWidth(this), image.getHeight(this)));
  453.         else
  454.             return new Dimension(10, 10);
  455.     }
  456.  
  457.  
  458.     /**
  459.      * Returns the minimum dimensions to properly display this component.
  460.      * This is a standard Java AWT method which gets called to determine
  461.      * the minimum size of this component.
  462.      *
  463.      * @return  If no image has been loaded, a dimension of 10 by 10 is returned.
  464.      *          If an image has been loaded, the height and width of the image
  465.      *          is returned.
  466.      * @see #preferredSize
  467.      */
  468.     public Dimension minimumSize()
  469.     {
  470.         return preferredSize();
  471.     }
  472.  
  473.     /**
  474.      * Adds a listener for all event changes.
  475.      * @param PropertyChangeListener listener the listener to add.
  476.      * @see #removePropertyChangeListener
  477.      */
  478.     public void addPropertyChangeListener(PropertyChangeListener listener)
  479.     {
  480.         //super.addPropertyChangeListener(listener);
  481.         changes.addPropertyChangeListener(listener);
  482.     }
  483.  
  484.     /**
  485.      * Removes a listener for all event changes.
  486.      * @param PropertyChangeListener listener the listener to remove.
  487.      * @see #addPropertyChangeListener
  488.      */
  489.     public void removePropertyChangeListener(PropertyChangeListener listener)
  490.     {
  491.         //super.removePropertyChangeListener(listener);
  492.         changes.removePropertyChangeListener(listener);
  493.     }
  494.  
  495.     /**
  496.      * Adds a vetoable listener for all event changes.
  497.      * @param VetoableChangeListener listener the listener to add.
  498.      * @see #removeVetoableChangeListener
  499.      */
  500.     public void addVetoableChangeListener(VetoableChangeListener listener)
  501.     {
  502.          //super.addVetoableChangeListener(listener);
  503.         vetos.addVetoableChangeListener(listener);
  504.     }
  505.  
  506.     /**
  507.      * Removes a vetoable listener for all event changes.
  508.      * @param VetoableChangeListener listener the listener to remove.
  509.      * @see #addVetoableChangeListener
  510.      */
  511.     public void removeVetoableChangeListener(VetoableChangeListener listener)
  512.     {
  513.         //super.removeVetoableChangeListener(listener);
  514.         vetos.removeVetoableChangeListener(listener);
  515.     }
  516.  
  517.     private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
  518.         stream.defaultReadObject();
  519.  
  520.         errors = ResourceBundle.getBundle("symantec.itools.resources.ErrorsBundle");
  521.  
  522.         if (url != null) {
  523.             image = getToolkit().getImage(url);
  524.  
  525.             MediaTracker tracker = new MediaTracker(this);
  526.             tracker.addImage(image, 0);
  527.             try {
  528.                 tracker.waitForAll();
  529.             }
  530.             catch(InterruptedException e) {
  531.             Object[] args = { url };
  532.             throw new IOException(MessageFormat.format(errors.getString("ErrorLoadingImageForURL"), args));
  533.             }
  534.         }
  535.     }
  536.  
  537.    /**
  538.      * Image that this viewer is displaying.
  539.      */
  540.     transient protected Image image;
  541.  
  542.     /**
  543.      * Name of file, if any, associated with this image.
  544.      */
  545.     protected String fileName;
  546.  
  547.     /**
  548.      * URL of the image being displayed.
  549.      */
  550.     protected URL url;
  551.  
  552.     /**
  553.      * Determines how to draw the image.
  554.      */
  555.     protected int imageStyle;
  556.  
  557.     /**
  558.      * Error strings.
  559.      */
  560.     transient protected ResourceBundle errors;
  561.  
  562.     private symantec.itools.beans.VetoableChangeSupport vetos   = new symantec.itools.beans.VetoableChangeSupport(this);
  563.     private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  564.  
  565. }
  566.