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 / SlideShow.java < prev    next >
Encoding:
Java Source  |  1998-09-14  |  14.6 KB  |  524 lines

  1. package symantec.itools.multimedia;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.Event;
  5. import java.awt.Graphics;
  6. import java.awt.Container;
  7. import java.net.MalformedURLException;
  8. import java.util.Vector;
  9. import java.awt.Image;
  10. import java.net.URL;
  11. import java.beans.PropertyVetoException;
  12. import java.beans.PropertyChangeListener;
  13. import java.beans.VetoableChangeListener;
  14. import java.awt.event.ActionListener;
  15. import java.awt.event.ActionEvent;
  16. import java.awt.AWTEvent;
  17. import java.awt.AWTEventMulticaster;
  18. import java.util.ResourceBundle;
  19. import java.text.MessageFormat;
  20.  
  21. //    05/30/97    RKM    Made it compile with some of the 1.1 component changes (added catches)
  22. //    06/02/97    LAB    Updated to Java 1.1.  !!! LAB !!! This class should be rewritten as a bean!
  23. //    08/04/97    LAB    Updated version to 1.1. Make sourceActionEvent protected.  Deprecated preferredSize
  24. //                    and added getPreferredSize.  Added getMinimumSize which returns results from
  25. //                    getPreferredSize. Made lightwieght.  Removed update, and repaint.  Made some private
  26. //                    members protected.  Deprecated display().
  27.  
  28. /**
  29.  * This is a basic graphical image "slide show" component. Displays a series of images
  30.  * with descriptive text.
  31.  * @version 1.1, August 1, 1997
  32.  * @author Symantec
  33.  */
  34. public class SlideShow extends Container
  35. {
  36.     /**
  37.      * Constructs a new SlideShow.
  38.      */
  39.     public SlideShow()
  40.     {        
  41.         try
  42.         {
  43.             errors = ResourceBundle.getBundle("symantec.itools.resources.ErrorsBundle");
  44.         }
  45.         catch(Throwable ex)
  46.         {
  47.             errors = new symantec.itools.resources.ErrorsBundle();
  48.         }
  49.  
  50.         imageViewer = new ImageViewer();
  51.         try
  52.         {
  53.             imageViewer.setStyle(ImageViewer.IMAGE_CENTERED);
  54.         }
  55.         catch (PropertyVetoException exc) {}
  56.  
  57.         imageIndex = 0;
  58.         setLayout(null);
  59.  
  60.         //!!! LAB !!! Remove this hack when reshape is truely removed from the AWT.
  61.         isReshapeHack = false;
  62.     }
  63.  
  64.     /**
  65.      * Return the number of images in the slide show set.
  66.      */
  67.     public int getNumberOfImages()
  68.     {
  69.         if(urlList == null)
  70.             return 0;
  71.  
  72.         return urlList.size();
  73.     }
  74.  
  75.     /**
  76.      * Return the current image index being displayed.
  77.      */
  78.     public int getCurrentImageIndex()
  79.     {
  80.         return imageIndex;
  81.     }
  82.  
  83.     /**
  84.      * Return the URL of the image at the given index.
  85.      * @param index index of image to retrieve URL of
  86.      * @return URL - URL of image at given index
  87.      */
  88.     public URL getURL(int index)
  89.     {
  90.         if(urlList == null)
  91.             return null;
  92.         return (URL)urlList.elementAt(index);
  93.     }
  94.  
  95.     /**
  96.      * Set the description of the image at the given index.
  97.      * @param index index of description to set
  98.      * @param str description string
  99.      * @see #getDescription
  100.      *
  101.      * @exception PropertyVetoException
  102.      * if the specified property value is unacceptable
  103.      */
  104.     public void setDescription(int index, String str) throws PropertyVetoException
  105.     {
  106.         String oldValue = getDescription(index);
  107.  
  108.         vetos.fireVetoableChange("Description", oldValue, str);
  109.  
  110.         descriptions.setElementAt(str, index);
  111.  
  112.         changes.firePropertyChange("Description", oldValue, str);
  113.     }
  114.  
  115.     /**
  116.      * Return the description of the image at the given index.
  117.      * @param index index of image to retrieve description of
  118.      * @return String - description of image at given index
  119.      * @see #setDescription
  120.      */
  121.     public String getDescription(int index)
  122.     {
  123.         return (String)descriptions.elementAt(index);
  124.     }
  125.  
  126.     /**
  127.      * Query if displaying first image in slide show set.
  128.      * @return boolean - true if displaying first image; false
  129.      * otherwise
  130.      * @see #isAtLastImage
  131.      */
  132.     public boolean isAtFirstImage()
  133.     {
  134.         return imageIndex == 0;
  135.     }
  136.  
  137.     /**
  138.      * Query if displaying last image in slide show set.
  139.      * @return boolean - true if displaying last image; false
  140.      * otherwise
  141.      * @see #isAtFirstImage
  142.      */
  143.     public boolean isAtLastImage()
  144.     {
  145.         return imageIndex == (urlList.size() - 1);
  146.     }
  147.  
  148.     /**
  149.      * Display the image at the given index.
  150.      * @param index index of the image to display
  151.      * @param str description string
  152.      * @return int - image index being displayed
  153.      */
  154.     public int setImage(int index)
  155.     {
  156.         if (index >= 0 && index < urlList.size())
  157.         {
  158.             imageIndex = index;
  159.             try
  160.             {
  161.                 imageViewer.setImage((Image)images.elementAt(imageIndex));
  162.             }
  163.             catch(java.beans.PropertyVetoException veto) {}
  164.  
  165.             repaint();
  166.         }
  167.         return imageIndex;
  168.     }
  169.  
  170.     /**
  171.      * Reset slide show and add first image.
  172.      *
  173.      * @param url url of first slide image, if null slide show is reinitialized
  174.      */
  175.     public void setFirstImage(URL url)
  176.     {
  177.         if (url == null)
  178.         {
  179.             try
  180.             {
  181.                 imageViewer.setImage(null);
  182.             }
  183.             catch(java.beans.PropertyVetoException veto) {}
  184.             imageIndex = 0;
  185.             urlList = new Vector();
  186.             images = new Vector();
  187.             descriptions = new Vector();
  188.         }
  189.         else
  190.         {
  191.             if (urlList.size() != 0)
  192.             {
  193.                 urlList.setElementAt(url, 0);
  194.                 descriptions.setElementAt("", 0);
  195.                 images.setElementAt(loadImageFromURL(url), 0);
  196.                 setImage(0);
  197.             }
  198.             else
  199.             {
  200.                 addImageAndDescription(url, "");
  201.             }
  202.         }
  203.     }
  204.  
  205.     /**
  206.      * @deprecated
  207.      */
  208.     //!!! LAB !!! This should go away.
  209.     public void display()
  210.     {
  211.         imageIndex = 0;
  212.  
  213.         setImage(imageIndex);
  214.         add(imageViewer);
  215.         repaint();
  216.         validate();
  217.     }
  218.  
  219.    /**
  220.      * Add an image URL and associated description to the slide
  221.      * show image set.
  222.      * @param url URL of image file
  223.      * @param description description of image
  224.      * @return int - index of added image in slide show set
  225.      */
  226.     public int addImageAndDescription(URL url, String description)
  227.     {
  228.         urlList.addElement(url);
  229.         descriptions.addElement(description);
  230.         images.addElement(loadImageFromURL(url));
  231.  
  232.         int index = urlList.size() - 1;
  233.         if (index == 0)
  234.         {
  235.             imageIndex = 0;
  236.  
  237.             setImage(imageIndex);
  238.             add(imageViewer);
  239.             repaint();
  240.             validate();
  241.         }
  242.  
  243.         return index;
  244.     }
  245.  
  246.     /**
  247.      * Display the next image in the slide show set.
  248.      * Fires an ActionEvent with the actionCommand of "nextImage" to it's listeners.
  249.      * @see #previousImage
  250.      */
  251.     public int nextImage()
  252.     {
  253.         if (! isAtLastImage())
  254.         {
  255.             int rv;
  256.             ++imageIndex;
  257.             rv = setImage(imageIndex);
  258.             sourceActionEvent("nextImage");
  259.             return rv;
  260.         }
  261.         return imageIndex;
  262.     }
  263.  
  264.     /**
  265.      * Display the previous image in the slide show set.
  266.      * Fires an ActionEvent with the actionCommand of "previousImage" to it's listeners.
  267.      * @see #nextImage
  268.      */
  269.     public int previousImage()
  270.     {
  271.         if (! isAtFirstImage())
  272.         {
  273.             int rv;
  274.             --imageIndex;
  275.             rv = setImage(imageIndex);
  276.             sourceActionEvent("previousImage");
  277.             return rv;
  278.         }
  279.         return imageIndex;
  280.     }
  281.  
  282.     /**
  283.      * Returns the recommended dimensions to properly display this component.
  284.      * This is a standard Java AWT method which gets called to determine
  285.      * the recommended size of this component.
  286.      *
  287.      * @return  If no image has been loaded, a dimension of 10 by 10 is returned.
  288.      *          If an image has been loaded, the height and width of the image
  289.      *          is returned.
  290.      * @see #getMinimumSize
  291.      */
  292.     public Dimension getPreferredSize()
  293.     {
  294.         Dimension d = new Dimension(10, 10);
  295.         Dimension imDim;
  296.  
  297.         if(images != null && imageViewer != null)
  298.         {
  299.             //Save the current image
  300.             Image oldImage = imageViewer.getImage();
  301.             for(int i = 0; i < images.size(); i++)
  302.             {
  303.                 if(images.elementAt(i) != null)
  304.                 {
  305.                     try
  306.                     {
  307.                         imageViewer.setImage((Image)images.elementAt(i));
  308.                     }
  309.                     catch(java.beans.PropertyVetoException veto) {}
  310.  
  311.                     imDim = imageViewer.getPreferredSize();
  312.                     d.width = Math.max(imDim.width, d.width);
  313.                     d.height = Math.max(imDim.height, d.height);
  314.                 }
  315.             }
  316.             //Restore the old Image
  317.             try
  318.             {
  319.                 imageViewer.setImage(oldImage);
  320.             }
  321.             catch(java.beans.PropertyVetoException veto) {}
  322.         }
  323.  
  324.         return d;
  325.     }
  326.  
  327.     /**
  328.      * @deprecated
  329.      * @see #getPreferredSize
  330.      */
  331.     public Dimension preferredSize()
  332.     {
  333.         return getPreferredSize();
  334.     }
  335.  
  336.     /**
  337.      * Returns the minimum dimensions to properly display this component.
  338.      * This is a standard Java AWT method which gets called to determine
  339.      * the minimum size of this component.
  340.      *
  341.      * @return  the results from getPreferredSize()
  342.      * @see #getPreferredSize
  343.      */
  344.     public Dimension getMinimumSize()
  345.     {
  346.         return getPreferredSize();
  347.     }
  348.  
  349.     /**
  350.      * Reshapes the Component to the specified bounding box.
  351.      * @param x the x coordinate
  352.      * @param y the y coordinate
  353.      * @param width the width of the component
  354.      * @param height the height of the component
  355.      * @see java.awt.Component#getBounds
  356.      * @see java.awt.Component#setLocation
  357.      * @see java.awt.Component#setSize
  358.      */
  359.     public void setBounds(int x, int y, int width, int height)
  360.     {
  361.         //!!! LAB !!! Remove this hack when reshape is truely removed from the AWT.
  362.         isReshapeHack = true;
  363.         super.setBounds(x, y, width, height);
  364.         imageViewer.setBounds(0, 0, width, height);
  365.         isReshapeHack = false;
  366.     }
  367.  
  368.     /**
  369.      * @deprecated
  370.      * @see #setBounds
  371.      */
  372.     public void reshape(int x, int y, int width, int height)
  373.     {
  374.         //!!! LAB !!! Remove this hack when reshape is truely removed from the AWT.
  375.         super.reshape(x, y, width, height);
  376.         if(! isReshapeHack)
  377.             imageViewer.setBounds(0, 0, width, height);
  378.     }
  379.  
  380.     /**
  381.      * Adds the specified action listener to receive action events
  382.      * from this button.
  383.      * @param l the action listener
  384.      */
  385.     public void addActionListener(ActionListener l)
  386.     {
  387.         actionListener = AWTEventMulticaster.add(actionListener, l);
  388.     }
  389.  
  390.     /**
  391.      * Removes the specified action listener so it no longer receives
  392.      * action events from this button.
  393.      * @param l the action listener
  394.      */
  395.     public void removeActionListener(ActionListener l)
  396.     {
  397.         actionListener = AWTEventMulticaster.remove(actionListener, l);
  398.     }
  399.  
  400.  
  401.     /**
  402.      * Adds a listener for all event changes.
  403.      * @param PropertyChangeListener listener the listener to add.
  404.      * @see #removePropertyChangeListener
  405.      */
  406.     public void addPropertyChangeListener(PropertyChangeListener listener)
  407.     {
  408.         changes.addPropertyChangeListener(listener);
  409.     }
  410.  
  411.     /**
  412.      * Removes a listener for all event changes.
  413.      * @param PropertyChangeListener listener the listener to remove.
  414.      * @see #addPropertyChangeListener
  415.      */
  416.     public void removePropertyChangeListener(PropertyChangeListener listener)
  417.     {
  418.         changes.removePropertyChangeListener(listener);
  419.     }
  420.  
  421.     /**
  422.      * Adds a vetoable listener for all event changes.
  423.      * @param VetoableChangeListener listener the listener to add.
  424.      * @see #removeVetoableChangeListener
  425.      */
  426.     public void addVetoableChangeListener(VetoableChangeListener listener)
  427.     {
  428.         vetos.addVetoableChangeListener(listener);
  429.     }
  430.  
  431.     /**
  432.      * Removes a vetoable listener for all event changes.
  433.      * @param VetoableChangeListener listener the listener to remove.
  434.      * @see #addVetoableChangeListener
  435.      */
  436.     public void removeVetoableChangeListener(VetoableChangeListener listener)
  437.     {
  438.         vetos.removeVetoableChangeListener(listener);
  439.     }
  440.  
  441.     java.awt.event.ActionListener actionListener = null;
  442.  
  443.     /**
  444.      * Fire an action event to the listeners
  445.      * @param actionCommand the command name associated with the ActionEvent to fire.
  446.      */
  447.     protected void sourceActionEvent(String actionCommand)
  448.     {
  449.         if (actionListener != null)
  450.             actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand));
  451.     }
  452.  
  453.     /**
  454.      * Loads an image from a given URL
  455.      * System.err's the exception if there was a problem loading the Image.
  456.      * @param url the url referencing the image to load
  457.      * @return the loaded image
  458.      */
  459.     protected Image loadImageFromURL(URL url)
  460.     {
  461.         Image image = null;
  462.  
  463.         try
  464.         {
  465.             image = getToolkit().getImage(url);
  466.         }
  467.         catch (Exception e)
  468.         {
  469.             //throw new MalformedURLException("Error loading image");
  470.             Object[] args = { url };
  471.             System.err.println("Error in SlideShow: " +
  472.                 MessageFormat.format(errors.getString("ErrorLoadingImageForURL"), args));
  473.         }
  474.  
  475.         return image;
  476.     }
  477.  
  478.  
  479.     //!!! LAB !!! Remove this hack when reshape is truely removed from the AWT.
  480.     /**
  481.      * Internal utility flag.
  482.      */
  483.     protected boolean isReshapeHack;
  484.  
  485.     /**
  486.      * The zero-relative index of the currently displayed image.
  487.      * @see #getCurrentImageIndex
  488.      * @see #setImage
  489.      * @see #nextImage
  490.      * @see #previousImage
  491.      */
  492.     protected int imageIndex;
  493.     /**
  494.      * The sub-component that displays the images.
  495.      */
  496.     protected ImageViewer imageViewer;
  497.  
  498.     /**
  499.      * The URLs of the images to display.
  500.      * @see #getURL
  501.      * @see #setFirstImage
  502.      * @see #addImageAndDescription
  503.      */
  504.     protected Vector urlList     = new Vector();
  505.     /**
  506.      * The displayed images, loaded from the image URL list.
  507.      * @see #setFirstImage
  508.      * @see #addImageAndDescription
  509.      */
  510.     protected Vector images      = new Vector();
  511.     /**
  512.      * The displayed image descriptions.
  513.      * @see #getDescription
  514.      * @see #setDescription
  515.      * @see #addImageAndDescription
  516.      */
  517.     protected Vector descriptions = new Vector();
  518.  
  519.     transient protected ResourceBundle errors;
  520.  
  521.     private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  522.     private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  523. }
  524.