KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > util > JavaScriptUtil


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

16 package com.blandware.atleap.webapp.taglib.core.util;
17
18 import com.blandware.atleap.common.util.StringUtil;
19 import com.blandware.atleap.webapp.util.core.WebappConstants;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25
26 /**
27  * <p>Contains some helper methods usable by tags, which produce some JavaScript code</p>
28  * <p><a HREF="JavaScriptUtil.java.htm"><i>View Source</i></a></p>
29  *
30  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
31  * @version $Revision: 1.10 $ $Date: 2005/10/22 09:28:33 $
32  */

33 public class JavaScriptUtil {
34
35     /**
36      * JavaScript event handler that is executed when this element receives a mouse click.
37      */

38     public static final String JavaDoc EVENT_TYPE_ONCLICK = "onclick";
39
40     /**
41      * JavaScript event handler that is executed when this element receives a mouse double click.
42      */

43     public static final String JavaDoc EVENT_TYPE_ONDBLCLICK = "ondblick";
44
45     /**
46      * JavaScript event handler that is executed when this element receives a key up event.
47      */

48     public static final String JavaDoc EVENT_TYPE_ONKEYUP = "onkeyup";
49
50     /**
51      * JavaScript event handler that is executed when this element receives a key down event.
52      */

53     public static final String JavaDoc EVENT_TYPE_ONKEYDOWN = "onkeydown";
54
55     /**
56      * JavaScript event handler that is executed when this element receives a key press event.
57      */

58     public static final String JavaDoc EVENT_TYPE_ONKEYPRESS = "onkeypress";
59
60     /**
61      * JavaScript event handler that is executed when this element receives a mouse move event.
62      */

63     public static final String JavaDoc EVENT_TYPE_ONMOUSEMOVE = "onmousemove";
64
65     /**
66      * JavaScript event handler that is executed when this element receives a mouse up event.
67      */

68     public static final String JavaDoc EVENT_TYPE_ONMOUSEUP = "onmouseup";
69
70     /**
71      * JavaScript event handler that is executed when this element receives a mouse down event.
72      */

73     public static final String JavaDoc EVENT_TYPE_ONMOUSEDOWN = "onmousedown";
74
75     /**
76      * JavaScript event handler that is executed when this element receives a mouse over event.
77      */

78     public static final String JavaDoc EVENT_TYPE_ONMOUSEOVER = "onmouseover";
79
80     /**
81      * JavaScript event handler that is executed when this element receives a mouse out event.
82      */

83     public static final String JavaDoc EVENT_TYPE_ONMOUSEOUT = "onmouseout";
84
85     /**
86      * JavaScript event handler that is executed when this element receives input focus.
87      */

88     public static final String JavaDoc EVENT_TYPE_ONFOCUS = "onfocus";
89
90     /**
91      * JavaScript event handler that is executed when this element loses input focus.
92      */

93     public static final String JavaDoc EVENT_TYPE_ONBLUR = "onblur";
94
95     /**
96      * Creates an HTML <code>DIV</code> element which behaves like hyperlink. Usable, for example, in context menus
97      *
98      * @param url Location of link
99      * @param body Body of link
100      * @return String representation of generated HTML element
101      */

102     public static String JavaDoc createLinkAsLayer(String JavaDoc url, String JavaDoc body,
103                                            String JavaDoc styleClass, String JavaDoc highlightedStyleClass) {
104         HTMLElement div = new HTMLElement("div");
105         div.putAttribute("class", WebappConstants.CONTEXT_MENU_ITEM_STYLE_CLASS);
106         div.putAttribute("onmouseover", "activateContextMenuItem(this, '" + highlightedStyleClass + "');");
107         div.putAttribute("onmouseout", "deactivateContextMenuItem(this, '" + styleClass + "');");
108         div.putAttribute("url", url);
109         div.putAttribute(EVENT_TYPE_ONCLICK, "onMenuItemClick(this);");
110         div.setBodyContent(body);
111         return div.toString();
112     }
113
114     /**
115      * Private constructor, because this class has static methods only
116      */

117     private JavaScriptUtil() {
118     }
119
120     /**
121      * Represents an HTML element. For example, <code>A</code> or <code>DIV</code>.
122      *
123      * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
124      */

125     public static class HTMLElement implements Serializable JavaDoc {
126
127         /**
128          * Name of HTML element
129          */

130         protected String JavaDoc name;
131
132         /**
133          * Map of attributes
134          */

135         protected Map JavaDoc attributes = new HashMap JavaDoc();
136
137         /**
138          * Body content to insert between opening and closing tags
139          */

140         protected String JavaDoc bodyContent;
141
142         /**
143          * Creates new instance of HTML element
144          *
145          * @param name Name of element
146          */

147         public HTMLElement(String JavaDoc name) {
148             this(name, new HashMap JavaDoc());
149         }
150
151         /**
152          * Creates new instance of HTML element with predefined set of attributes
153          *
154          * @param name Name of element
155          * @param attributes Set of attributes
156          */

157         public HTMLElement(String JavaDoc name, Map JavaDoc attributes) {
158             if ( name == null || name.trim().length() == 0 ) {
159                 throw new IllegalArgumentException JavaDoc("Name must be non-null and non-empty string");
160             }
161             this.name = name.trim();
162             if ( attributes != null ) {
163                 this.attributes = attributes;
164             }
165         }
166
167         /**
168          * Adds new attribute. If attribute with the same name already exists, replaces its value
169          *
170          * @param name Name of attribute
171          * @param value Value of attribute
172          */

173         public void putAttribute(String JavaDoc name, String JavaDoc value) {
174             attributes.put(name, value);
175         }
176
177         /**
178          * Removes attribute with specified name
179          *
180          * @param name Name of attribute to remove
181          */

182         public void removeAttribute(String JavaDoc name) {
183             attributes.remove(name);
184         }
185
186         /**
187          * Sets body content
188          *
189          * @param bodyContent Body content to set
190          */

191         public void setBodyContent(String JavaDoc bodyContent) {
192             this.bodyContent = bodyContent;
193         }
194
195         /**
196          * Puts all attributes together, inserts body content and returns element, represented as string.
197          * No escaping is made.
198          *
199          * @return String representation of this element
200          */

201         public String JavaDoc toString() {
202             return toString(false);
203         }
204
205         /**
206          * Puts all attributes together, inserts body content and returns element, represented as string.
207          * Escapes all values if requested
208          *
209          * @param escapeValues Whether or not to escape values
210          * @return String representation of this element
211          */

212         public String JavaDoc toString(boolean escapeValues) {
213             StringBuffer JavaDoc element = new StringBuffer JavaDoc();
214             element.append("<").append(name);
215
216             // append attributes
217
for ( Iterator JavaDoc i = attributes.entrySet().iterator(); i.hasNext(); ) {
218                 Map.Entry JavaDoc attribute = (Map.Entry JavaDoc) i.next();
219                 String JavaDoc name = (String JavaDoc) attribute.getKey();
220                 String JavaDoc value = (String JavaDoc) attribute.getValue();
221                 if ( escapeValues ) {
222                     value = StringUtil.escape(value);
223                 }
224                 element.append(" ").append(name).append("=\"").append(value).append("\"");
225             }
226
227             element.append(">");
228
229             // append body content if any
230
if ( bodyContent != null ) {
231                 element.append(bodyContent);
232             }
233
234             // close tag
235
element.append("</").append(name).append(">");
236
237             return element.toString();
238         }
239     }
240 }
241
Popular Tags