home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / ImageHTMLLink.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  4.1 KB  |  162 lines

  1. package symantec.itools.awt;
  2.  
  3.  
  4. import java.net.URL;
  5. import java.awt.Event;
  6. import java.awt.Container;
  7. import java.applet.*;
  8.  
  9. /**
  10.  * Creates a rectangular button, with an image label, that calls a URL address
  11.  * when clicked. 
  12.  * <p>
  13.  * Specifically, use ImageHTMLLink to:
  14.  * <UL>
  15.  * <DT>╖ display an image instead of text on a button.</DT>
  16.  * <DT>╖ generate a train of action events when the user clicks the button.</DT>
  17.  * </UL>
  18.  * @version 1.0, Nov 26, 1996
  19.  * @author Symantec
  20.  */
  21.  
  22. public class ImageHTMLLink
  23.     extends ImageButton
  24. {
  25.     /**
  26.      * URL to show when the button is clicked.
  27.      */
  28.     protected URL url;
  29.     /**
  30.      * Applet context that shows the URL.
  31.      */
  32.     protected AppletContext context;
  33.     /**
  34.      * Frame specifier for showing a URL document in a browser or applet 
  35.      * viewer. It is interpreted as follows:
  36.      * <UL>
  37.      * <DT>"_self"  show document in the current frame</DT>
  38.      * <DT>"_parent"    show document in the parent frame</DT>
  39.      * <DT>"_top"   show document in the topmost frame</DT>
  40.      * <DT>"_blank" show document in a new unnamed toplevel window</DT>
  41.      * <DT>all others   show document in a new toplevel window with the given name</DT>
  42.      * </UL>
  43.      */
  44.     protected String frame;
  45.  
  46.     /**
  47.      * Constructs a default ImageHTMLLink.
  48.      */
  49.     public ImageHTMLLink()
  50.     {
  51.         frame = null;
  52.     }
  53.  
  54.     /**
  55.      * Returns the URL to show when the button is clicked.
  56.      * @see #setURL
  57.      */
  58.     public URL getURL()
  59.     {
  60.         return url;
  61.     }
  62.  
  63.     /**
  64.      * Sets the URL to show when the button is clicked.
  65.      * @param u the URL to show when the button is clicked
  66.      * @see #getURL
  67.      */
  68.     public void setURL(URL u)
  69.     {
  70.         url = u;
  71.         context = null;
  72.     }
  73.  
  74.     /**
  75.      * Sets the title displayed on the applet frame while showing
  76.      * the URL.
  77.      * @param f applet frame title
  78.      * @see #getFrame
  79.      */
  80.     public void setFrame(String f)
  81.     {
  82.         frame = f;
  83.     }
  84.  
  85.     /**
  86.      * Returns the title that is displayed on the applet frame while showing
  87.      * the URL.
  88.      * @see #setFrame
  89.      */
  90.     public String getFrame()
  91.     {
  92.         return frame;
  93.     }
  94.  
  95.     /**
  96.      * Handles internal actions for this component.
  97.      * This is a standard Java AWT method which usually gets called by the AWT
  98.      * method handleEvent() in response to receiving an ACTION_EVENT event. In those 
  99.      * cases the o parameter contains the value in the event's arg field.
  100.      * 
  101.      * @param event the event that caused this action
  102.      * @param what the action
  103.      * @return true if the action was handled
  104.      * @see java.awt.Component#handleEvent
  105.      */
  106.     public boolean action(Event event, Object what)
  107.     {
  108.         if (context != null)
  109.         {
  110.             if (frame == null || frame.length() == 0)
  111.                 context.showDocument(url);
  112.             else
  113.                 context.showDocument(url, frame);
  114.  
  115.             return true;
  116.         }
  117.  
  118.         return false;
  119.     }
  120.  
  121.     /**
  122.      * Ensures that this component is laid out properly, as needed.
  123.      * This is a standard Java AWT method which gets called by the AWT to 
  124.      * make sure this component and its subcomponents have a valid layout.
  125.      * If this component was made invalid with a call to invalidate(), then 
  126.      * it is laid out again.
  127.      * 
  128.      * It is overridden here to locate the applet containing this component.
  129.      *
  130.      * @see java.awt.Component#invalidate
  131.      */
  132.     public void validate()
  133.     {
  134.         // On validation, try to find the containing applet.  If we can find
  135.         // it, we don't bother doing the link...
  136.         Container c;
  137.  
  138.         c = getParent();
  139.  
  140.         while (c != null)
  141.         {
  142.             if (c instanceof Applet)
  143.             {
  144.                 setAppletContext(((Applet)c).getAppletContext());
  145.                 break;
  146.             }
  147.  
  148.             c = c.getParent();
  149.         }
  150.     }
  151.  
  152.     /**
  153.      * Sets the applet context to use for showing the URL.
  154.      * @param c the applet context
  155.      */
  156.     protected void setAppletContext(AppletContext c)
  157.     {
  158.         context = c;
  159.     }
  160. }
  161.  
  162.