home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / HrefButtonArea.java < prev    next >
Encoding:
Java Source  |  1997-07-30  |  4.8 KB  |  149 lines

  1. // $Header: z:/admin/metro_examples/java/demo/ImageMap/rcs/HrefButtonArea.java 1.1 1997/02/06 00:30:09 IPGIntel-2 Exp $ 
  2. /*
  3.  * @(#)HrefButtonArea.java    1.6 96/12/06
  4.  *
  5.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  6.  *
  7.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  8.  * modify and redistribute this software in source and binary code form,
  9.  * provided that i) this copyright notice and license appear on all copies of
  10.  * the software; and ii) Licensee does not utilize the software in a manner
  11.  * which is disparaging to Sun.
  12.  *
  13.  * This software is provided "AS IS," without a warranty of any kind. ALL
  14.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  15.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  16.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  17.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  18.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  19.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  20.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  21.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  22.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  23.  * POSSIBILITY OF SUCH DAMAGES.
  24.  *
  25.  * This software is not designed or intended for use in on-line control of
  26.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  27.  * the design, construction, operation or maintenance of any nuclear
  28.  * facility. Licensee represents and warrants that it will not use or
  29.  * redistribute the Software for such purposes.
  30.  */
  31.  
  32. import java.awt.Graphics;
  33. import java.awt.Image;
  34. import java.net.URL;
  35. import java.net.MalformedURLException;
  36.  
  37. /**
  38.  * An improved "Fetch a URL" ImageArea class.
  39.  * This class extends the basic ImageArea Class to fetch a URL when
  40.  * the user clicks in the area.  In addition, special custom highlights
  41.  * are used to make the area look and feel like a 3-D button.
  42.  *
  43.  * @author     Jim Graham
  44.  * @version     1.6, 12/06/96
  45.  */
  46. class HrefButtonArea extends ImageMapArea {
  47.     /** The URL to be fetched when the user clicks on this area. */
  48.     URL anchor;
  49.     /** The highlight image for when the button is "UP". */
  50.     Image upImage;
  51.     /** The highlight image for when the button is "DOWN". */
  52.     Image downImage;
  53.     /** This flag indicates if the "button" is currently pressed. */
  54.     boolean pressed = false;
  55.     /** The border size for the 3-D effect. */
  56.     int border = 5;
  57.  
  58.     /**
  59.      * The argument string is the URL to be fetched.
  60.      * This method also constructs the various highlight images needed
  61.      * to achieve the 3-D effect.
  62.      */
  63.     public void handleArg(String arg) {
  64.     try {
  65.         anchor = new URL(parent.getDocumentBase(), arg);
  66.     } catch (MalformedURLException e) {
  67.         anchor = null;
  68.     }
  69.     if (border * 2 > W || border * 2 > H) {
  70.         border = Math.min(W, H) / 2;
  71.     }
  72.     }
  73.  
  74.     public void makeImages() {
  75.     upImage = parent.getHighlight(X, Y, W, H,
  76.                       new ButtonFilter(false,
  77.                                parent.hlpercent,
  78.                                border, W, H));
  79.     downImage = parent.getHighlight(X, Y, W, H,
  80.                     new ButtonFilter(true,
  81.                              parent.hlpercent,
  82.                              border, W, H));
  83.     }
  84.  
  85.     public boolean imageUpdate(Image img, int infoflags,
  86.                    int x, int y, int width, int height) {
  87.     if (img == (pressed ? downImage : upImage)) {
  88.         return parent.imageUpdate(img, infoflags, x + X, y + Y,
  89.                       width, height);
  90.     } else {
  91.         return (img == downImage || img == upImage);
  92.     }
  93.     }
  94.  
  95.     /**
  96.      * The isTerminal method indicates whether events should propagate
  97.      * to the areas underlying this one.
  98.      */
  99.     public boolean isTerminal() {
  100.     return true;
  101.     }
  102.  
  103.     /**
  104.      * The status message area is updated to show the destination URL.
  105.      * The graphical highlight is achieved using the ButtonFilter.
  106.      */
  107.     public void highlight(Graphics g) {
  108.     if (entered) {
  109.         g.drawImage(pressed ? downImage : upImage, X, Y, this);
  110.     }
  111.     }
  112.  
  113.     public void enter() {
  114.     showStatus((anchor != null)
  115.            ? "Go To " + anchor.toExternalForm()
  116.            : null);
  117.     repaint();
  118.     }
  119.  
  120.     public void exit() {
  121.     showStatus(null);
  122.     repaint();
  123.     }
  124.  
  125.     /**
  126.      * Since the highlight changes when the button is pressed, we need
  127.      * to record the "pressed" state and induce a repaint.
  128.      */
  129.     public boolean press() {
  130.     pressed = true;
  131.     repaint();
  132.     return true;
  133.     }
  134.  
  135.     /**
  136.      * The new URL is fetched when the user releases the mouse button
  137.      * only if they are still in the area.
  138.      */
  139.     public boolean lift(int x, int y) {
  140.     pressed = false;
  141.     repaint();
  142.     if (inside(x, y) && anchor != null) {
  143.         showDocument(anchor);
  144.     }
  145.     return true;
  146.     }
  147. }
  148.  
  149.