home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 14.5 KB | 520 lines |
- package symantec.itools.multimedia;
-
- import java.awt.Dimension;
- import java.awt.Event;
- import java.awt.Graphics;
- import java.awt.Container;
- import java.net.MalformedURLException;
- import java.util.Vector;
- import java.awt.Image;
- import java.net.URL;
- import java.beans.PropertyVetoException;
- import java.beans.PropertyChangeListener;
- import java.beans.VetoableChangeListener;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.awt.AWTEvent;
- import java.awt.AWTEventMulticaster;
- import java.util.ResourceBundle;
- import java.text.MessageFormat;
-
- // 05/30/97 RKM Made it compile with some of the 1.1 component changes (added catches)
- // 06/02/97 LAB Updated to Java 1.1. !!! LAB !!! This class should be rewritten as a bean!
- // 08/04/97 LAB Updated version to 1.1. Make sourceActionEvent protected. Deprecated preferredSize
- // and added getPreferredSize. Added getMinimumSize which returns results from
- // getPreferredSize. Made lightwieght. Removed update, and repaint. Made some private
- // members protected. Deprecated display().
-
- /**
- * This is a basic graphical image "slide show" component. Displays a series of images
- * with descriptive text.
- * @version 1.1, August 1, 1997
- * @author Symantec
- */
- public class SlideShow extends Container
- {
- /**
- * Constructs a new SlideShow.
- */
- public SlideShow()
- {
- errors = ResourceBundle.getBundle("symantec.itools.resources.ErrorsBundle");
-
- imageViewer = new ImageViewer();
- try
- {
- imageViewer.setStyle(ImageViewer.IMAGE_CENTERED);
- }
- catch (PropertyVetoException exc) {}
-
- imageIndex = 0;
- setLayout(null);
-
- //!!! LAB !!! Remove this hack when reshape is truely removed from the AWT.
- isReshapeHack = false;
- }
-
- /**
- * Return the number of images in the slide show set.
- */
- public int getNumberOfImages()
- {
- if(urlList == null)
- return 0;
-
- return urlList.size();
- }
-
- /**
- * Return the current image index being displayed.
- */
- public int getCurrentImageIndex()
- {
- return imageIndex;
- }
-
- /**
- * Return the URL of the image at the given index.
- * @param index index of image to retrieve URL of
- * @return URL - URL of image at given index
- */
- public URL getURL(int index)
- {
- if(urlList == null)
- return null;
- return (URL)urlList.elementAt(index);
- }
-
- /**
- * Set the description of the image at the given index.
- * @param index index of description to set
- * @param str description string
- * @see #getDescription
- *
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setDescription(int index, String str) throws PropertyVetoException
- {
- String oldValue = getDescription(index);
-
- vetos.fireVetoableChange("Description", oldValue, str);
-
- descriptions.setElementAt(str, index);
-
- changes.firePropertyChange("Description", oldValue, str);
- }
-
- /**
- * Return the description of the image at the given index.
- * @param index index of image to retrieve description of
- * @return String - description of image at given index
- * @see #setDescription
- */
- public String getDescription(int index)
- {
- return (String)descriptions.elementAt(index);
- }
-
- /**
- * Query if displaying first image in slide show set.
- * @return boolean - true if displaying first image; false
- * otherwise
- * @see #isAtLastImage
- */
- public boolean isAtFirstImage()
- {
- return imageIndex == 0;
- }
-
- /**
- * Query if displaying last image in slide show set.
- * @return boolean - true if displaying last image; false
- * otherwise
- * @see #isAtFirstImage
- */
- public boolean isAtLastImage()
- {
- return imageIndex == (urlList.size() - 1);
- }
-
- /**
- * Display the image at the given index.
- * @param index index of the image to display
- * @param str description string
- * @return int - image index being displayed
- */
- public int setImage(int index)
- {
- if (index >= 0 && index < urlList.size())
- {
- imageIndex = index;
- try
- {
- imageViewer.setImage((Image)images.elementAt(imageIndex));
- }
- catch(java.beans.PropertyVetoException veto) {}
-
- repaint();
- }
- return imageIndex;
- }
-
- /**
- * Reset slide show and add first image.
- *
- * @param url url of first slide image, if null slide show is reinitialized
- */
- public void setFirstImage(URL url)
- {
- if (url == null)
- {
- try
- {
- imageViewer.setImage(null);
- }
- catch(java.beans.PropertyVetoException veto) {}
- imageIndex = 0;
- urlList = new Vector();
- images = new Vector();
- descriptions = new Vector();
- }
- else
- {
- if (urlList.size() != 0)
- {
- urlList.setElementAt(url, 0);
- descriptions.setElementAt("", 0);
- images.setElementAt(loadImageFromURL(url), 0);
- setImage(0);
- }
- else
- {
- addImageAndDescription(url, "");
- }
- }
- }
-
- /**
- * @deprecated
- */
- //!!! LAB !!! This should go away.
- public void display()
- {
- imageIndex = 0;
-
- setImage(imageIndex);
- add(imageViewer);
- repaint();
- validate();
- }
-
- /**
- * Add an image URL and associated description to the slide
- * show image set.
- * @param url URL of image file
- * @param description description of image
- * @return int - index of added image in slide show set
- */
- public int addImageAndDescription(URL url, String description)
- {
- urlList.addElement(url);
- descriptions.addElement(description);
- images.addElement(loadImageFromURL(url));
-
- int index = urlList.size() - 1;
- if (index == 0)
- {
- imageIndex = 0;
-
- setImage(imageIndex);
- add(imageViewer);
- repaint();
- validate();
- }
-
- return index;
- }
-
- /**
- * Display the next image in the slide show set.
- * Fires an ActionEvent with the actionCommand of "nextImage" to it's listeners.
- * @see #previousImage
- */
- public int nextImage()
- {
- if (! isAtLastImage())
- {
- int rv;
- ++imageIndex;
- rv = setImage(imageIndex);
- sourceActionEvent("nextImage");
- return rv;
- }
- return imageIndex;
- }
-
- /**
- * Display the previous image in the slide show set.
- * Fires an ActionEvent with the actionCommand of "previousImage" to it's listeners.
- * @see #nextImage
- */
- public int previousImage()
- {
- if (! isAtFirstImage())
- {
- int rv;
- --imageIndex;
- rv = setImage(imageIndex);
- sourceActionEvent("previousImage");
- return rv;
- }
- return imageIndex;
- }
-
- /**
- * Returns the recommended dimensions to properly display this component.
- * This is a standard Java AWT method which gets called to determine
- * the recommended size of this component.
- *
- * @return If no image has been loaded, a dimension of 10 by 10 is returned.
- * If an image has been loaded, the height and width of the image
- * is returned.
- * @see #getMinimumSize
- */
- public Dimension getPreferredSize()
- {
- Dimension d = new Dimension(10, 10);
- Dimension imDim;
-
- if(images != null && imageViewer != null)
- {
- //Save the current image
- Image oldImage = imageViewer.getImage();
- for(int i = 0; i < images.size(); i++)
- {
- if(images.elementAt(i) != null)
- {
- try
- {
- imageViewer.setImage((Image)images.elementAt(i));
- }
- catch(java.beans.PropertyVetoException veto) {}
-
- imDim = imageViewer.getPreferredSize();
- d.width = Math.max(imDim.width, d.width);
- d.height = Math.max(imDim.height, d.height);
- }
- }
- //Restore the old Image
- try
- {
- imageViewer.setImage(oldImage);
- }
- catch(java.beans.PropertyVetoException veto) {}
- }
-
- return d;
- }
-
- /**
- * @deprecated
- * @see #getPreferredSize
- */
- public Dimension preferredSize()
- {
- return getPreferredSize();
- }
-
- /**
- * Returns the minimum dimensions to properly display this component.
- * This is a standard Java AWT method which gets called to determine
- * the minimum size of this component.
- *
- * @return the results from getPreferredSize()
- * @see #getPreferredSize
- */
- public Dimension getMinimumSize()
- {
- return getPreferredSize();
- }
-
- /**
- * Reshapes the Component to the specified bounding box.
- * @param x the x coordinate
- * @param y the y coordinate
- * @param width the width of the component
- * @param height the height of the component
- * @see java.awt.Component#getBounds
- * @see java.awt.Component#setLocation
- * @see java.awt.Component#setSize
- */
- public void setBounds(int x, int y, int width, int height)
- {
- //!!! LAB !!! Remove this hack when reshape is truely removed from the AWT.
- isReshapeHack = true;
- super.setBounds(x, y, width, height);
- imageViewer.setBounds(0, 0, width, height);
- isReshapeHack = false;
- }
-
- /**
- * @deprecated
- * @see #setBounds
- */
- public void reshape(int x, int y, int width, int height)
- {
- //!!! LAB !!! Remove this hack when reshape is truely removed from the AWT.
- super.reshape(x, y, width, height);
- if(! isReshapeHack)
- imageViewer.setBounds(0, 0, width, height);
- }
-
- /**
- * Adds the specified action listener to receive action events
- * from this button.
- * @param l the action listener
- */
- public void addActionListener(ActionListener l)
- {
- actionListener = AWTEventMulticaster.add(actionListener, l);
- }
-
- /**
- * Removes the specified action listener so it no longer receives
- * action events from this button.
- * @param l the action listener
- */
- public void removeActionListener(ActionListener l)
- {
- actionListener = AWTEventMulticaster.remove(actionListener, l);
- }
-
-
- /**
- * Adds a listener for all event changes.
- * @param PropertyChangeListener listener the listener to add.
- * @see #removePropertyChangeListener
- */
- public void addPropertyChangeListener(PropertyChangeListener listener)
- {
- changes.addPropertyChangeListener(listener);
- }
-
- /**
- * Removes a listener for all event changes.
- * @param PropertyChangeListener listener the listener to remove.
- * @see #addPropertyChangeListener
- */
- public void removePropertyChangeListener(PropertyChangeListener listener)
- {
- changes.removePropertyChangeListener(listener);
- }
-
- /**
- * Adds a vetoable listener for all event changes.
- * @param VetoableChangeListener listener the listener to add.
- * @see #removeVetoableChangeListener
- */
- public void addVetoableChangeListener(VetoableChangeListener listener)
- {
- vetos.addVetoableChangeListener(listener);
- }
-
- /**
- * Removes a vetoable listener for all event changes.
- * @param VetoableChangeListener listener the listener to remove.
- * @see #addVetoableChangeListener
- */
- public void removeVetoableChangeListener(VetoableChangeListener listener)
- {
- vetos.removeVetoableChangeListener(listener);
- }
-
- java.awt.event.ActionListener actionListener = null;
-
- /**
- * Fire an action event to the listeners
- * @param actionCommand the command name associated with the ActionEvent to fire.
- */
- protected void sourceActionEvent(String actionCommand)
- {
- if (actionListener != null)
- actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand));
- }
-
- /**
- * Loads an image from a given URL
- * System.err's the exception if there was a problem loading the Image.
- * @param url the url referencing the image to load
- * @return the loaded image
- */
- protected Image loadImageFromURL(URL url)
- {
- Image image = null;
-
- try
- {
- image = getToolkit().getImage(url);
- }
- catch (Exception e)
- {
- //throw new MalformedURLException("Error loading image");
- Object[] args = { url };
- System.err.println("Error in SlideShow: " +
- MessageFormat.format(errors.getString("ErrorLoadingImageForURL"), args));
- }
-
- return image;
- }
-
-
- //!!! LAB !!! Remove this hack when reshape is truely removed from the AWT.
- /**
- * Internal utility flag.
- */
- protected boolean isReshapeHack;
-
- /**
- * The zero-relative index of the currently displayed image.
- * @see #getCurrentImageIndex
- * @see #setImage
- * @see #nextImage
- * @see #previousImage
- */
- protected int imageIndex;
- /**
- * The sub-component that displays the images.
- */
- protected ImageViewer imageViewer;
-
- /**
- * The URLs of the images to display.
- * @see #getURL
- * @see #setFirstImage
- * @see #addImageAndDescription
- */
- protected Vector urlList = new Vector();
- /**
- * The displayed images, loaded from the image URL list.
- * @see #setFirstImage
- * @see #addImageAndDescription
- */
- protected Vector images = new Vector();
- /**
- * The displayed image descriptions.
- * @see #getDescription
- * @see #setDescription
- * @see #addImageAndDescription
- */
- protected Vector descriptions = new Vector();
-
- /**
- * Error strings.
- */
- transient protected ResourceBundle errors;
-
- private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
- private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
- }
-