KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HrefButtonArea


1 /*
2  * @(#)HrefButtonArea.java 1.15 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)HrefButtonArea.java 1.15 05/11/17
39  */

40
41 import java.awt.Graphics JavaDoc;
42 import java.awt.Image JavaDoc;
43 import java.net.URL JavaDoc;
44 import java.net.MalformedURLException JavaDoc;
45
46 /**
47  * An improved "Fetch a URL" ImageArea class.
48  * This class extends the basic ImageArea Class to fetch a URL when
49  * the user clicks in the area. In addition, special custom highlights
50  * are used to make the area look and feel like a 3-D button.
51  *
52  * @author Jim Graham
53  * @version 1.15, 11/17/05
54  */

55 class HrefButtonArea extends ImageMapArea {
56     /** The URL to be fetched when the user clicks on this area. */
57     URL JavaDoc anchor;
58     /** The highlight image for when the button is "UP". */
59     Image JavaDoc upImage;
60     /** The highlight image for when the button is "DOWN". */
61     Image JavaDoc downImage;
62     /** This flag indicates if the "button" is currently pressed. */
63     boolean pressed = false;
64     /** The border size for the 3-D effect. */
65     int border = 5;
66
67     /**
68      * The argument string is the URL to be fetched.
69      * This method also constructs the various highlight images needed
70      * to achieve the 3-D effect.
71      */

72     public void handleArg(String JavaDoc arg) {
73     try {
74         anchor = new URL JavaDoc(parent.getDocumentBase(), arg);
75     } catch (MalformedURLException JavaDoc e) {
76         anchor = null;
77     }
78     if (border * 2 > W || border * 2 > H) {
79         border = Math.min(W, H) / 2;
80     }
81     }
82
83     public void makeImages() {
84     upImage = parent.getHighlight(X, Y, W, H,
85                       new ButtonFilter(false,
86                                parent.hlpercent,
87                                border, W, H));
88     downImage = parent.getHighlight(X, Y, W, H,
89                     new ButtonFilter(true,
90                              parent.hlpercent,
91                              border, W, H));
92     }
93
94     public boolean imageUpdate(Image JavaDoc img, int infoflags,
95                    int x, int y, int width, int height) {
96     if (img == (pressed ? downImage : upImage)) {
97         return parent.imageUpdate(img, infoflags, x + X, y + Y,
98                       width, height);
99     } else {
100         return (img == downImage || img == upImage);
101     }
102     }
103
104     /**
105      * The isTerminal method indicates whether events should propagate
106      * to the areas underlying this one.
107      */

108     public boolean isTerminal() {
109     return true;
110     }
111
112     /**
113      * The status message area is updated to show the destination URL.
114      * The graphical highlight is achieved using the ButtonFilter.
115      */

116     public void highlight(Graphics JavaDoc g) {
117     if (entered) {
118         g.drawImage(pressed ? downImage : upImage, X, Y, this);
119     }
120     }
121
122     public void enter() {
123     showStatus((anchor != null)
124            ? "Go To " + anchor.toExternalForm()
125            : null);
126     repaint();
127     }
128
129     public void exit() {
130     showStatus(null);
131     repaint();
132     }
133
134     /**
135      * Since the highlight changes when the button is pressed, we need
136      * to record the "pressed" state and induce a repaint.
137      */

138     public boolean press() {
139     pressed = true;
140     repaint();
141     return true;
142     }
143
144     /**
145      * The new URL is fetched when the user releases the mouse button
146      * only if they are still in the area.
147      */

148     public boolean lift(int x, int y) {
149     pressed = false;
150     repaint();
151     if (inside(x, y) && anchor != null) {
152         showDocument(anchor);
153     }
154     return true;
155     }
156 }
157
158
Popular Tags