View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: JavaScriptWindow.java,v $
31   * Revision 1.3  2005/04/09 18:04:18  colinmacleod
32   * Changed copyright text to GPL v2 explicitly.
33   *
34   * Revision 1.2  2005/03/10 10:28:26  colinmacleod
35   * Fixed for tomcat4.
36   * Now uses getAttribute and only in page context.
37   * (Was findAttribute before.)
38   *
39   * Revision 1.1  2005/01/06 23:00:31  colinmacleod
40   * Moved up a version number.
41   * Changed copyright notices to 2005.
42   * Updated the documentation:
43   *   - started working on multiproject:site docu.
44   *   - changed the logo.
45   * Added checkstyle and fixed LOADS of style issues.
46   * Added separate thirdparty subproject.
47   * Added struts (in web), util and webgui (in webtheme) from ivata op.
48   *
49   * Revision 1.4  2004/12/23 21:01:31  colinmacleod
50   * Updated Struts to v1.2.4.
51   * Changed base classes to use ivata masks.
52   *
53   * Revision 1.3  2004/11/12 15:57:21  colinmacleod
54   * Removed dependencies on SSLEXT.
55   * Moved Persistence classes to ivata masks.
56   *
57   * Revision 1.2  2004/03/21 21:16:35  colinmacleod
58   * Shortened name to ivata op.
59   *
60   * Revision 1.1.1.1  2004/01/27 20:59:38  colinmacleod
61   * Moved ivata op to SourceForge.
62   *
63   * Revision 1.3  2004/01/19 21:14:23  colin
64   * Updated for Struts/SSLEXT v1.10
65   *
66   * Revision 1.2  2003/10/16 15:43:03  jano
67   * Fixes problems with building and some problems with splitting to subprojects
68   *
69   * Revision 1.1.1.1  2003/10/13 20:49:27  colin
70   * Restructured portal into subprojects
71   *
72   * Revision 1.3  2003/06/10 06:07:18  peter
73   * last change was a mistake... changed to normal quotes
74   *
75   * Revision 1.1  2003/02/24 19:33:33  colin
76   * Moved to new subproject.
77   *
78   * Revision 1.6  2003/02/18 13:13:23  colin
79   * reverted to single quotes for pop-ups
80   *
81   * Revision 1.5  2003/02/18 11:13:15  colin
82   * switched double/single quotes
83   *
84   * Revision 1.4  2003/02/04 17:43:46  colin
85   * copyright notice
86   *
87   * Revision 1.3  2003/01/27 08:19:08  colin
88   * updated for SSLEXT
89   *
90   * Revision 1.2  2003/01/18 20:20:38  colin
91   * changed error messages to alerts to make them easier to find.
92   *
93   * Revision 1.1  2002/06/21 11:58:37  colin
94   * restructured com.ivata.mask.jsp into separate
95   * sub-categories: format, JavaScript, theme and tree.
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         // if the window name was set, begin with that
501         if (windowName != null) {
502             buffer.append("var ");
503             buffer.append(windowName);
504             buffer.append(" = ");
505         }
506         // we need a URL and a frameName
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         // see if we were specified parameters
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