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