1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 package com.ivata.mask.web.javascript;
99 import java.net.MalformedURLException;
100 import java.util.Map;
101 import javax.servlet.jsp.PageContext;
102 import org.apache.struts.taglib.TagUtils;
103 /***
104 * <p>
105 * <b>IMPORTANT: </b> the code generated by this class uses double quoted
106 * strings. That means, if you include the output in an <i>HTML </i> tag
107 * attribute, you should use single quotes for the HTML attribute.
108 * </p>
109 *
110 * <p>
111 * Creates a new window using JavaScript. To use this class, set the attributes
112 * of the window by using the setter methods of this class, then call
113 * {@link #toString() toString()}to generate the JavaScript.
114 * </p>
115 *
116 * <p>
117 * You must set the URL and the frame name before calling {@link#toString()
118 * toString()}.
119 * </p>
120 *
121 * @since ivata masks 0.4 (2002-05-18)
122 * @author Colin MacLeod
123 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
124 * @version $Revision: 1.3 $
125 */
126 public final class JavaScriptWindow {
127 /***
128 * <p>
129 * Stores the fame name of the new window.
130 * </p>
131 */
132 private String frameName = null;
133 /***
134 * <p>
135 * Stores whether or not the window has a menu bar. The default setting is
136 * <code>false</code>.
137 * </p>
138 */
139 private boolean hasMenuBar = false;
140 /***
141 * <p>
142 * Stores whether or not the window has scroll bars. The default setting is
143 * <code>true</code>.
144 * </p>
145 */
146 private boolean hasScrollBars = true;
147 /***
148 * <p>
149 * Stores whether or not the window has a toolbar. The default setting is
150 * <code>false</code>.
151 * </p>
152 */
153 private boolean hasToolBar = false;
154 /***
155 * <p>
156 * Stores the height of the window.
157 * </p>
158 */
159 private int height;
160 /***
161 * <p>
162 * Stores whether or not the window can be resized. The default setting is
163 * <code>true</code>.
164 * </p>
165 */
166 private boolean isResizable = true;
167 /***
168 * <p>
169 * Set this value to specify the application-specific path (beginning with a
170 * '/') which the new window will show.
171 * </p>
172 */
173 private String page = null;
174 /***
175 * <p>
176 * Used to calculate the URL with the <code>SecureRequestUtils</code> from
177 * <strong>Struts </strong>.
178 * </p>
179 */
180 private PageContext pageContext = null;
181 /***
182 * <p>
183 * Specifies parameters for the page this window shows.
184 * </p>
185 *
186 * @see TagUtils.getInstance().computeURL
187 */
188 private Map params = null;
189 /***
190 * <p>
191 * Specifies the name of a <code>java.util.Map</code> instance in the page
192 * context which is used to specify parameters for the page this window
193 * shows.
194 * </p>
195 *
196 * @see TagUtils.getInstance().computeURL.
197 */
198 private String paramsName = null;
199 /***
200 * <p>
201 * Stores the width of the window.
202 * </p>
203 */
204 private int width;
205 /***
206 * <p>
207 * Stores the name of a JavaScript object name for the new window.
208 * </p>
209 */
210 private String windowName = null;
211 /***
212 * Private helper to create a pop-up (alert) window with a message.
213 *
214 * @param message
215 * message to display in a pop-up window.
216 * @return JavaScript for appropriate pop-up.
217 */
218 private String alertError(final String message) {
219 return "alert(\"ERROR in JavaScriptWindow: " + message + "\");";
220 }
221 /***
222 * <p>
223 * Get the fame name of the new window.
224 * </p>
225 *
226 * @return the fame name of the new window.
227 */
228 public String getFrameName() {
229 return frameName;
230 }
231 /***
232 * <p>
233 * Get whether or not the window has a menu bar. The default setting is
234 * <code>false</code>.
235 * </p>
236 *
237 * @return <code>true</code> if the window has a menu bar, otherwise
238 * <code>false</code>.
239 */
240 public boolean getHasMenuBar() {
241 return hasMenuBar;
242 }
243 /***
244 * <p>
245 * Get whether or not the window has scroll bars. The default setting is
246 * <code>true</code>.
247 * </p>
248 *
249 * @return <code>true</code> if the window has scroll bars, otherwise
250 * <code>false</code>.
251 */
252 public boolean getHasScrollBars() {
253 return hasScrollBars;
254 }
255 /***
256 * <p>
257 * Get whether or not the window has a tool bar. The default setting is
258 * <code>true</code>.
259 * </p>
260 *
261 * @return <code>true</code> if the window has a tool bar, otherwise
262 * <code>false</code>.
263 */
264 public boolean getHasToolBar() {
265 return hasToolBar;
266 }
267 /***
268 * <p>
269 * Get the height of the window.
270 * </p>
271 *
272 * @return the height of the window.
273 */
274 public int getHeight() {
275 return height;
276 }
277 /***
278 * <p>
279 * Get whether or not the window can be resized. The default setting is
280 * <code>true</code>.
281 * </p>
282 *
283 * @return <code>true</code> if the window has can be resized, otherwise
284 * <code>false</code>.
285 */
286 public boolean getIsResizable() {
287 return isResizable;
288 }
289 /***
290 * <p>
291 * Set this value to specify the application-specific path (beginning with a
292 * '/') which the new window will show.
293 * </p>
294 *
295 * @return the current value of page.
296 */
297 public String getPage() {
298 return page;
299 }
300 /***
301 * <p>
302 * Specifies parameters for the page this window shows.
303 * </p>
304 *
305 * @see TagUtils.getInstance().computeURL
306 * @return the current value of parameters.
307 */
308 public Map getParams() {
309 return params;
310 }
311 /***
312 * <p>
313 * Specifies the name of a <code>java.util.Map</code> instance in the page
314 * context which is used to specify parameters for the page this window
315 * shows.
316 * </p>
317 *
318 * @see TagUtils.getInstance().computeURL.
319 *
320 * @return the current value of paramsName.
321 */
322 public String getParamsName() {
323 return paramsName;
324 }
325 /***
326 * <p>
327 * Get the width of the window.
328 * </p>
329 *
330 * @return the width of the window.
331 */
332 public int getWidth() {
333 return width;
334 }
335 /***
336 * <p>
337 * Get a JavaScript object name for the new window.
338 * </p>
339 *
340 * @return a JavaScript object name for the new window.
341 */
342 public String getWindowName() {
343 return windowName;
344 }
345 /***
346 * <p>
347 * Set the fame name of the new window.
348 * </p>
349 *
350 * @param frameNameParam
351 * the new value of the fame name of the new window.
352 */
353 public void setFrameName(final String frameNameParam) {
354 this.frameName = frameNameParam;
355 }
356 /***
357 * <p>
358 * Set whether or not the window has a menu bar. The default setting is
359 * <code>false</code>.
360 * </p>
361 *
362 * @param hasMenuBarParam
363 * set to <code>true</code> if the window has a menu bar,
364 * otherwise <code>false</code>.
365 */
366 public void setHasMenuBar(final boolean hasMenuBarParam) {
367 this.hasMenuBar = hasMenuBarParam;
368 }
369 /***
370 * <p>
371 * Set whether or not the window has scroll bars. The default setting is
372 * <code>true</code>.
373 * </p>
374 *
375 * @param hasScrollBarsParam
376 * set to <code>true</code> if the window has scroll bars,
377 * otherwise <code>false</code>.
378 */
379 public void setHasScrollBars(final boolean hasScrollBarsParam) {
380 this.hasScrollBars = hasScrollBarsParam;
381 }
382 /***
383 * <p>
384 * Set whether or not the window has a tool bar. The default setting is
385 * <code>true</code>.
386 * </p>
387 *
388 * @param hasToolBarParam
389 * set to <code>true</code> if the window has a tool bar,
390 * otherwise <code>false</code>.
391 */
392 public void setHasToolBar(final boolean hasToolBarParam) {
393 this.hasToolBar = hasToolBarParam;
394 }
395 /***
396 * <p>
397 * Set the height of the window. The default setting is <code>450</code>.
398 * </p>
399 *
400 * @param heightParam
401 * the new height of the window.
402 */
403 public void setHeight(final int heightParam) {
404 this.height = heightParam;
405 }
406 /***
407 * <p>
408 * Set whether or not the window can be resized. The default setting is
409 * <code>true</code>.
410 * </p>
411 *
412 * @param isResizableParam
413 * set to <code>true</code> if the window has can be resized,
414 * otherwise <code>false</code>.
415 */
416 public void setIsResizable(final boolean isResizableParam) {
417 this.isResizable = isResizableParam;
418 }
419 /***
420 * <p>
421 * Set this value to specify the application-specific path (beginning with a
422 * '/') which the new window will show.
423 * </p>
424 *
425 * @param pageParam
426 * the new value of page.
427 */
428 public void setPage(final String pageParam) {
429 this.page = pageParam;
430 }
431 /***
432 * <p>
433 * Set the page context used to calculate the window <i>URL </i>.
434 * </p>
435 *
436 * @param pageContextParam
437 * new value of page context.
438 */
439 public void setPageContext(final PageContext pageContextParam) {
440 this.pageContext = pageContextParam;
441 }
442 /***
443 * <p>
444 * Specifies parameters for the page this window shows.
445 * </p>
446 *
447 * @see TagUtils.getInstance().computeURL
448 *
449 * @param paramsParam
450 * the new value of the parameters.
451 */
452 public void setParams(final Map paramsParam) {
453 this.params = paramsParam;
454 }
455 /***
456 * <p>
457 * Specifies the name of a <code>java.util.Map</code> instance in the page
458 * context which is used to specify parameters for the page this window
459 * shows.
460 * </p>
461 *
462 * @see TagUtils.getInstance().computeURL.
463 * @param paramsNameParam
464 * the new value of paramsName.
465 */
466 public void setParamsName(final String paramsNameParam) {
467 this.paramsName = paramsNameParam;
468 }
469 /***
470 * <p>
471 * Set the width of the window. The default setting is <code>700</code>.
472 * </p>
473 *
474 * @param widthParam
475 * the new value for the width of the window.
476 */
477 public void setWidth(final int widthParam) {
478 this.width = widthParam;
479 }
480 /***
481 * <p>
482 * Set a JavaScript object name for the new window.
483 * </p>
484 *
485 * @param windowNameParam
486 * a JavaScript object name for the new window.
487 */
488 public void setWindowName(final String windowNameParam) {
489 this.windowName = windowNameParam;
490 }
491 /***
492 * <p>
493 * Create the JavaScript that makes the window and return it as a string.
494 * </p>
495 *
496 * @return the JavaScript that makes the window.
497 */
498 public String toString() {
499 StringBuffer buffer = new StringBuffer();
500
501 if (windowName != null) {
502 buffer.append("var ");
503 buffer.append(windowName);
504 buffer.append(" = ");
505 }
506
507 if (page == null) {
508 return alertError("No page parameter was set.");
509 }
510 if (pageContext == null) {
511 return alertError("No pageContext parameter was set.");
512 }
513
514 if (paramsName != null) {
515 params = (Map) pageContext.findAttribute(paramsName);
516 }
517 try {
518 String uRL = TagUtils.getInstance().computeURL(pageContext, null,
519 null, page, null, null, params, null, false);
520 if (frameName == null) {
521 return alertError("No frameName parameter was set.");
522 }
523 buffer.append("window.open(\"");
524 buffer.append(uRL);
525 buffer.append("\", \"");
526 buffer.append(frameName);
527 buffer.append("\", \"toolbar=");
528 if (hasToolBar) {
529 buffer.append("yes");
530 } else {
531 buffer.append("no");
532 }
533 buffer.append(",menubar=");
534 if (hasMenuBar) {
535 buffer.append("yes");
536 } else {
537 buffer.append("no");
538 }
539 buffer.append(",scrollbars=");
540 if (hasScrollBars) {
541 buffer.append("auto");
542 } else {
543 buffer.append("no");
544 }
545 buffer.append(",height=");
546 buffer.append(height);
547 buffer.append(",width=");
548 buffer.append(width);
549 buffer.append(",top=200");
550 buffer.append(",left=200");
551 buffer.append(",resizable=");
552 if (isResizable) {
553 buffer.append("yes");
554 } else {
555 buffer.append("no");
556 }
557 buffer.append("\");");
558 return buffer.toString();
559 } catch (MalformedURLException e) {
560 return alertError("Malformed URL: " + e.getMessage());
561 }
562 }
563 }
564