KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > SVGAElementBridge


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.bridge;
19
20 import java.awt.Cursor JavaDoc;
21
22 import org.apache.batik.dom.events.AbstractEvent;
23 import org.apache.batik.dom.util.XLinkSupport;
24 import org.apache.batik.gvt.GraphicsNode;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.events.Event JavaDoc;
27 import org.w3c.dom.events.EventListener JavaDoc;
28 import org.w3c.dom.events.EventTarget JavaDoc;
29 import org.w3c.dom.events.UIEvent JavaDoc;
30 import org.w3c.dom.svg.SVGAElement;
31
32 /**
33  * Bridge class for the <a> element.
34  *
35  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
36  * @version $Id: SVGAElementBridge.java,v 1.27 2004/08/18 07:12:32 vhardy Exp $
37  */

38 public class SVGAElementBridge extends SVGGElementBridge {
39
40     /**
41      * Constructs a new bridge for the &lt;a> element.
42      */

43     public SVGAElementBridge() {}
44
45     /**
46      * Returns 'a'.
47      */

48     public String JavaDoc getLocalName() {
49         return SVG_A_TAG;
50     }
51
52     /**
53      * Returns a new instance of this bridge.
54      */

55     public Bridge getInstance() {
56         return new SVGAElementBridge();
57     }
58
59     /**
60      * Builds using the specified BridgeContext and element, the
61      * specified graphics node.
62      *
63      * @param ctx the bridge context to use
64      * @param e the element that describes the graphics node to build
65      * @param node the graphics node to build
66      */

67     public void buildGraphicsNode(BridgeContext ctx,
68                                   Element JavaDoc e,
69                                   GraphicsNode node) {
70
71         super.buildGraphicsNode(ctx, e, node);
72
73         if (ctx.isInteractive()) {
74             EventTarget JavaDoc target = (EventTarget JavaDoc)e;
75             EventListener JavaDoc l = new AnchorListener(ctx.getUserAgent());
76             target.addEventListener(SVG_EVENT_CLICK, l, false);
77             ctx.storeEventListener(target, SVG_EVENT_CLICK, l, false);
78
79             l = new CursorMouseOverListener(ctx.getUserAgent());
80             target.addEventListener(SVG_EVENT_MOUSEOVER, l, false);
81             ctx.storeEventListener(target, SVG_EVENT_MOUSEOVER, l, false);
82
83             l = new CursorMouseOutListener(ctx.getUserAgent());
84             target.addEventListener(SVG_EVENT_MOUSEOUT, l, false);
85             ctx.storeEventListener(target, SVG_EVENT_MOUSEOUT, l, false);
86         }
87     }
88
89     /**
90      * Returns true as the &lt;a> element is a container.
91      */

92     public boolean isComposite() {
93         return true;
94     }
95
96     /**
97      * To handle a click on an anchor.
98      */

99     public static class AnchorListener implements EventListener JavaDoc {
100
101         protected UserAgent userAgent;
102
103         public AnchorListener(UserAgent ua) {
104             userAgent = ua;
105         }
106
107         public void handleEvent(Event evt) {
108             if (AbstractEvent.getEventPreventDefault(evt))
109                 return;
110             SVGAElement elt = (SVGAElement)evt.getCurrentTarget();
111             Cursor JavaDoc cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
112             userAgent.setSVGCursor(cursor);
113             userAgent.openLink(elt);
114             evt.stopPropagation();
115         }
116     }
117
118     /**
119      * To handle a mouseover on an anchor and set the cursor.
120      */

121     public static class CursorMouseOverListener implements EventListener JavaDoc {
122
123         protected UserAgent userAgent;
124
125         public CursorMouseOverListener(UserAgent ua) {
126             userAgent = ua;
127         }
128
129         public void handleEvent(Event evt) {
130             if (AbstractEvent.getEventPreventDefault(evt))
131                 return;
132             //
133
// Only modify the cursor if the target's cursor property is
134
// 'auto'. Note that we do not need to check the value of
135
// anchor element as the target's cursor value is resulting
136
// from the CSS cascade which has accounted for inheritance.
137
// This means that our behavior is to set the cursor to a
138
// hand cursor for any content on which the cursor property is
139
// 'auto' inside an anchor element. If, for example, the
140
// content was:
141
// <a cusor="wait">
142
// <g cursor="auto">
143
// <rect />
144
// </g>
145
// </a>
146
//
147
// The cursor on the inside rect will be set to the hand cursor and
148
// not the wait cursor
149
//
150
Element JavaDoc target = (Element JavaDoc)evt.getTarget();
151             
152             if (CSSUtilities.isAutoCursor(target)) {
153                 // The target's cursor value is 'auto': use the hand cursor
154
userAgent.setSVGCursor(CursorManager.ANCHOR_CURSOR);
155             }
156             
157             //
158
// In all cases, display the href in the userAgent
159
//
160

161             SVGAElement elt = (SVGAElement)evt.getCurrentTarget();
162             if (elt != null) {
163                 String JavaDoc href = XLinkSupport.getXLinkHref(elt);
164                 userAgent.displayMessage(href);
165             }
166         }
167     }
168
169     /**
170      * To handle a mouseout on an anchor and set the cursor.
171      */

172     public static class CursorMouseOutListener implements EventListener JavaDoc {
173
174         protected UserAgent userAgent;
175
176         public CursorMouseOutListener(UserAgent ua) {
177             userAgent = ua;
178         }
179
180         public void handleEvent(Event evt) {
181             if (AbstractEvent.getEventPreventDefault(evt))
182                 return;
183             // No need to set the cursor on out events: this is taken care of
184
// by the BridgeContext
185

186             // Hide the href in the userAgent
187
SVGAElement elt = (SVGAElement)evt.getCurrentTarget();
188             if (elt != null) {
189                 userAgent.displayMessage("");
190             }
191         }
192     }
193 }
194
Popular Tags