KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > html > Rollover


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

15 package org.apache.tapestry.html;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.hivemind.Resource;
22 import org.apache.tapestry.AbstractComponent;
23 import org.apache.tapestry.IAsset;
24 import org.apache.tapestry.IEngine;
25 import org.apache.tapestry.IMarkupWriter;
26 import org.apache.tapestry.IRequestCycle;
27 import org.apache.tapestry.IScript;
28 import org.apache.tapestry.PageRenderSupport;
29 import org.apache.tapestry.Tapestry;
30 import org.apache.tapestry.TapestryUtils;
31 import org.apache.tapestry.components.ILinkComponent;
32 import org.apache.tapestry.components.LinkEventType;
33 import org.apache.tapestry.engine.IScriptSource;
34
35 /**
36  * Combines a link component (such as {@link org.apache.tapestry.link.DirectLink}) with an
37  * <img> and JavaScript code to create a rollover effect that works with both Netscape
38  * Navigator and Internet Explorer. [ <a
39  * HREF="../../../../../ComponentReference/Rollover.html">Component Reference </a>]
40  *
41  * @author Howard Lewis Ship
42  */

43
44 public abstract class Rollover extends AbstractComponent
45 {
46     private IScript _parsedScript;
47
48     /**
49      * Converts an {@link IAsset}binding into a usable URL. Returns null if the binding does not
50      * exist or the binding's value is null.
51      */

52
53     protected String JavaDoc getAssetURL(IAsset asset, IRequestCycle cycle)
54     {
55         if (asset == null)
56             return null;
57
58         return asset.buildURL(cycle);
59     }
60
61     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
62     {
63         // No body, so we skip it all if not rewinding (assumes no side effects on
64
// accessors).
65

66         if (cycle.isRewinding())
67             return;
68
69         String JavaDoc imageURL = null;
70         String JavaDoc focusURL = null;
71         String JavaDoc blurURL = null;
72         boolean dynamic = false;
73         String JavaDoc imageName = null;
74
75         PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
76
77         ILinkComponent serviceLink = (ILinkComponent) cycle
78                 .getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME);
79
80         if (serviceLink == null)
81             throw new ApplicationRuntimeException(Tapestry
82                     .getMessage("Rollover.must-be-contained-by-link"), this, null, null);
83
84         boolean linkDisabled = serviceLink.isDisabled();
85
86         if (linkDisabled)
87         {
88             imageURL = getAssetURL(getDisabled(), cycle);
89
90             if (imageURL == null)
91                 imageURL = getAssetURL(getImage(), cycle);
92         }
93         else
94         {
95             imageURL = getAssetURL(getImage(), cycle);
96             focusURL = getAssetURL(getFocus(), cycle);
97             blurURL = getAssetURL(getBlur(), cycle);
98
99             dynamic = (focusURL != null) || (blurURL != null);
100         }
101
102         if (imageURL == null)
103             throw Tapestry.createRequiredParameterException(this, "image");
104
105         writer.beginEmpty("img");
106
107         writer.attribute("src", imageURL);
108
109         writer.attribute("border", 0);
110
111         if (dynamic)
112         {
113             if (focusURL == null)
114                 focusURL = imageURL;
115
116             if (blurURL == null)
117                 blurURL = imageURL;
118
119             imageName = writeScript(cycle, pageRenderSupport, serviceLink, focusURL, blurURL);
120
121             writer.attribute("name", imageName);
122         }
123
124         renderInformalParameters(writer, cycle);
125
126         writer.closeTag();
127
128     }
129
130     private IScript getParsedScript()
131     {
132         if (_parsedScript == null)
133         {
134             IEngine engine = getPage().getEngine();
135             IScriptSource source = engine.getScriptSource();
136
137             Resource scriptLocation = getSpecification().getSpecificationLocation()
138                     .getRelativeResource("Rollover.script");
139
140             _parsedScript = source.getScript(scriptLocation);
141         }
142
143         return _parsedScript;
144     }
145
146     private String JavaDoc writeScript(IRequestCycle cycle, PageRenderSupport pageRenderSupport,
147             ILinkComponent link, String JavaDoc focusURL, String JavaDoc blurURL)
148     {
149         String JavaDoc imageName = pageRenderSupport.getUniqueString(getId());
150         String JavaDoc focusImageURL = pageRenderSupport.getPreloadedImageReference(focusURL);
151         String JavaDoc blurImageURL = pageRenderSupport.getPreloadedImageReference(blurURL);
152
153         Map JavaDoc symbols = new HashMap JavaDoc();
154
155         symbols.put("imageName", imageName);
156         symbols.put("focusImageURL", focusImageURL);
157         symbols.put("blurImageURL", blurImageURL);
158
159         getParsedScript().execute(cycle, pageRenderSupport, symbols);
160
161         // Add attributes to the link to control mouse over/out.
162
// Because the script is written before the <body> tag,
163
// there won't be any timing issues (such as cause
164
// bug #113893).
165

166         link.addEventHandler(LinkEventType.MOUSE_OVER, (String JavaDoc) symbols.get("onMouseOverName"));
167         link.addEventHandler(LinkEventType.MOUSE_OUT, (String JavaDoc) symbols.get("onMouseOutName"));
168
169         return imageName;
170     }
171
172     public abstract IAsset getBlur();
173
174     public abstract IAsset getDisabled();
175
176     public abstract IAsset getFocus();
177
178     public abstract IAsset getImage();
179 }
Popular Tags