KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > CmsTabDialog


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/CmsTabDialog.java,v $
3  * Date : $Date: 2006/03/27 14:52:43 $
4  * Version: $Revision: 1.21 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace;
33
34 import org.opencms.i18n.CmsEncoder;
35 import org.opencms.jsp.CmsJspActionElement;
36 import org.opencms.main.CmsLog;
37 import org.opencms.util.CmsStringUtil;
38
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Map JavaDoc;
42
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45 import javax.servlet.jsp.PageContext JavaDoc;
46
47 import org.apache.commons.logging.Log;
48
49 /**
50  * Provides methods for tab styled dialogs.<p>
51  *
52  * Extend this class in order to create a tab styled dialog and provide the methods
53  * getTabs() and getTabParameterOrder() in the new dialog class which should return lists
54  * which represent the tabs of the dialog.<p>
55  *
56  * This class is used for the following dialogs:
57  * <ul>
58  * <li>User preferences (CmsPreferences.java)
59  * </ul>
60  * <p>
61  *
62  * @author Andreas Zahner
63  *
64  * @version $Revision: 1.21 $
65  *
66  * @since 6.0.0
67  */

68 public abstract class CmsTabDialog extends CmsDialog {
69
70     /** Value for the action: switch the tab. */
71     public static final int ACTION_SWITCHTAB = 100;
72
73     /** Request parameter value for the action: switch the tab. */
74     public static final String JavaDoc DIALOG_SWITCHTAB = "switchtab";
75
76     /** Name of the request parameter for the set button pressed flag. */
77     public static final String JavaDoc PARAM_SETPRESSED = "setpressed";
78     /** Name of the request parameter for the current tab. */
79     public static final String JavaDoc PARAM_TAB = "tab";
80
81     /** The log object for this class. */
82     private static final Log LOG = CmsLog.getLog(CmsTabDialog.class);
83
84     /** Stores the currently active tab. */
85     private int m_activeTab = -1;
86     /** Determines if the "set" button was pressed. */
87     private String JavaDoc m_paramSetPressed;
88
89     /** Stores the current tab. */
90     private String JavaDoc m_paramTab;
91
92     /**
93      * Public constructor.<p>
94      *
95      * @param jsp an initialized JSP action element
96      */

97     public CmsTabDialog(CmsJspActionElement jsp) {
98
99         super(jsp);
100     }
101
102     /**
103      * Public constructor with JSP variables.<p>
104      *
105      * @param context the JSP page context
106      * @param req the JSP request
107      * @param res the JSP response
108      */

109     public CmsTabDialog(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
110
111         this(new CmsJspActionElement(context, req, res));
112     }
113
114     /**
115      * Builds the tab content area of the dialog window.<p>
116      *
117      * @param segment the HTML segment (START / END)
118      * @param title the title String for the dialog window
119      * @param attributes additional attributes for the content &lt;div&gt; area of the tab dialog
120      * @return a tab content area start / end segment
121      */

122     public String JavaDoc dialogTabContent(int segment, String JavaDoc title, String JavaDoc attributes) {
123
124         if (segment == HTML_START) {
125             StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
126             // null title is ok, we always want the title headline
127
result.append(dialogHead(title));
128             result.append("<div class=\"dialogtabstart\" unselectable=\"on\">\n");
129             result.append("<!-- dialogtabs start -->\n");
130             result.append(dialogTabRow());
131             result.append("<div class=\"dialogtabcontent\"");
132             if (attributes != null) {
133                 result.append(" " + attributes);
134             }
135             result.append(">\n");
136             result.append("<!-- dialogcontent start -->\n");
137             return result.toString();
138         } else {
139             return "\n<!-- dialogcontent end --></div>\n<!-- dialogtabs end --></div>";
140         }
141     }
142
143     /**
144      * Returns the end html for the tab content area of the dialog window.<p>
145      *
146      * @return the end html for the tab content area of the dialog window
147      */

148     public String JavaDoc dialogTabContentEnd() {
149
150         return dialogTabContent(HTML_END, null, null);
151     }
152
153     /**
154      * Returns the start html for the tab content area of the dialog window.<p>
155      *
156      * @param title the title for the dialog
157      * @return the start html for the tab content area of the dialog window
158      */

159     public String JavaDoc dialogTabContentStart(String JavaDoc title) {
160
161         return dialogTabContent(HTML_START, title, null);
162     }
163
164     /**
165      * Returns the start html for the tab content area of the dialog window.<p>
166      *
167      * @param title the title for the dialog
168      * @param attributes additional attributes for the content &lt;div&gt; area of the tab dialog
169      * @return the start html for the tab content area of the dialog window
170      */

171     public String JavaDoc dialogTabContentStart(String JavaDoc title, String JavaDoc attributes) {
172
173         return dialogTabContent(HTML_START, title, attributes);
174     }
175
176     /**
177      * Builds the html for the tab row of the tab dialog.<p>
178      *
179      * @return the html for the tab row
180      */

181     public String JavaDoc dialogTabRow() {
182
183         StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
184         StringBuffer JavaDoc lineRow = new StringBuffer JavaDoc(256);
185         List JavaDoc tabNames = getTabs();
186         if (tabNames.size() < 2) {
187             // less than 2 tabs present, do not show them and create a border line
188
result.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"maxwidth\" style=\"empty-cells: show;\">\n");
189             result.append("<tr>\n");
190             result.append("\t<td class=\"dialogtabrow\"></td>\n");
191             result.append("</tr>\n");
192             result.append("</table>\n");
193             return result.toString();
194         }
195         Iterator JavaDoc i = tabNames.iterator();
196         int counter = 1;
197         int activeTab = getActiveTab();
198         result.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"maxwidth\" style=\"empty-cells: show;\">\n");
199         result.append("<tr>\n");
200         while (i.hasNext()) {
201             // build a tab entry
202
String JavaDoc curTab = (String JavaDoc)i.next();
203             String JavaDoc curTabLink = "javascript:openTab('" + counter + "');";
204             if (counter == activeTab) {
205                 // create the currently active tab
206
int addDelta = 0;
207                 result.append("\t<td class=\"dialogtabactive\"");
208                 if (counter == 1) {
209                     // for first tab, add special html for correct layout
210
result.append(" style=\"border-left-width: 1px;\"");
211                     addDelta = 1;
212                 }
213                 result.append(">");
214                 result.append("<span class=\"tabactive\" unselectable=\"on\"");
215                 result.append(" style=\"width: " + (curTab.length() * 8 + addDelta) + "px;\"");
216                 result.append(">");
217                 result.append(curTab);
218                 result.append("</span></td>\n");
219                 lineRow.append("\t<td></td>\n");
220             } else {
221                 // create an inactive tab
222
result.append("\t<td class=\"dialogtab\" unselectable=\"on\">");
223                 result.append("<a class=\"tab\" HREF=\"" + curTabLink + "\"");
224                 result.append(" style=\"width: " + (curTab.length() * 8) + "px;\"");
225                 result.append(">");
226                 result.append(curTab);
227                 result.append("</a></td>\n");
228                 lineRow.append("\t<td class=\"dialogtabrow\"></td>\n");
229             }
230
231             counter++;
232         }
233         result.append("\t<td class=\"maxwidth\"></td>\n");
234         result.append("</tr>\n");
235         result.append("<tr>\n");
236         result.append(lineRow);
237         result.append("\t<td class=\"dialogtabrow\"></td>\n");
238         result.append("</tr>\n");
239         result.append("</table>\n");
240         return result.toString();
241     }
242
243     /**
244      * Returns the number of the currently active tab depending on the request parameter.<p>
245      *
246      * This method has to be called once in initWorkplaceRequestValues after filling the request parameters.<p>
247      *
248      * @return the number of the currently active tab
249      */

250     public int getActiveTab() {
251
252         if (m_activeTab < 0) {
253             String JavaDoc paramTab = getParamTab();
254             int tab = 1;
255             if (CmsStringUtil.isNotEmpty(paramTab)) {
256                 try {
257                     tab = Integer.parseInt(paramTab);
258                 } catch (NumberFormatException JavaDoc e) {
259                     // do nothing, the first tab is returned
260
if (LOG.isInfoEnabled()) {
261                         LOG.info(e.getLocalizedMessage());
262                     }
263                 }
264             }
265             setParamTab("" + tab);
266             m_activeTab = tab;
267             return tab;
268         } else {
269             return m_activeTab;
270         }
271     }
272
273     /**
274      * Returns the localized name of the currently active tab.<p>
275      *
276      * @return the localized name of the currently active tab or null if no tab name was found
277      */

278     public String JavaDoc getActiveTabName() {
279
280         if (m_activeTab < 0) {
281             getActiveTab();
282         }
283         List JavaDoc tabNames = getTabs();
284         try {
285             return (String JavaDoc)tabNames.get(m_activeTab - 1);
286         } catch (IndexOutOfBoundsException JavaDoc e) {
287             // should usually never happen
288
if (LOG.isInfoEnabled()) {
289                 LOG.info(e.getLocalizedMessage());
290             }
291             return null;
292         }
293     }
294
295     /**
296      * Returns the value of the setpressed parameter.<p>
297      *
298      * @return the value of the setpressed parameter
299      */

300     public String JavaDoc getParamSetPressed() {
301
302         return m_paramSetPressed;
303     }
304
305     /**
306      * Returns the value of the tab parameter.<p>
307      *
308      * @return the value of the tab parameter
309      */

310     public String JavaDoc getParamTab() {
311
312         return m_paramTab;
313     }
314
315     /**
316      * Returns the order of the parameter prefixes for each tab.<p>
317      *
318      * For example, all parameters stored in tab 1 have the prefix "Tab1", i.e.
319      * the getter and setter methods must be getParam<b>Tab1</b>MyParameterName().<p>
320      *
321      * To change the tab order, simply change the order in the String array
322      * and in the generated tab list.<p>
323      *
324      * @return the ordered parameter prefix List
325      * @see org.opencms.workplace.CmsTabDialog#getTabs()
326      */

327     public abstract List JavaDoc getTabParameterOrder();
328
329     /**
330      * Returns a list with localized Strings representing the names of the tabs.<p>
331      *
332      * @return list with localized String for the tabs
333      */

334     public abstract List JavaDoc getTabs();
335
336     /**
337      * Builds the start html of the page, including setting of DOCTYPE and
338      * inserting a header with the content-type.<p>
339      *
340      * This overloads the default method of the parent class.<p>
341      *
342      * @return the start html of the page
343      */

344     public String JavaDoc htmlStart() {
345
346         return htmlStart(null);
347     }
348
349     /**
350      * Builds the start html of the page, including setting of DOCTYPE and
351      * inserting a header with the content-type.<p>
352      *
353      * This overloads the default method of the parent class.<p>
354      *
355      * @param helpUrl the key for the online help to include on the page
356      * @return the start html of the page
357      */

358     public String JavaDoc htmlStart(String JavaDoc helpUrl) {
359
360         String JavaDoc stylesheet = null;
361         if (isPopup()) {
362             stylesheet = "popup.css";
363         }
364         StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.pageHtmlStyle(HTML_START, null, stylesheet));
365         if (getSettings().isViewExplorer()) {
366             result.append("<script type=\"text/javascript\" SRC=\"");
367             result.append(getSkinUri());
368             result.append("commons/explorer.js\"></script>\n");
369         }
370         result.append("<script type=\"text/javascript\">\n");
371         if (helpUrl != null) {
372             result.append("top.head.helpUrl=\"");
373             result.append(helpUrl + "\";\n");
374
375         }
376         // js to switch the dialog tabs
377
result.append("function openTab(tabValue) {\n");
378         result.append("\tdocument.forms[0]." + PARAM_TAB + ".value = tabValue;\n");
379         result.append("\tdocument.forms[0]." + PARAM_ACTION + ".value = \"" + DIALOG_SWITCHTAB + "\";\n");
380         result.append("\tdocument.forms[0].submit();\n");
381         result.append("}\n");
382         // js for the button actions, overwrites CmsDialog.dialogScriptSubmit() js method
383
result.append("function submitAction(actionValue, theForm, formName) {\n");
384         result.append("\tif (theForm == null) {\n");
385         result.append("\t\ttheForm = document.forms[formName];\n");
386         result.append("\t}\n");
387         result.append("\ttheForm." + PARAM_FRAMENAME + ".value = window.name;\n");
388         result.append("\tif (actionValue == \"" + DIALOG_SET + "\") {\n");
389         result.append("\t\ttheForm." + PARAM_ACTION + ".value = \"" + DIALOG_SET + "\";\n");
390         result.append("\t} else if (actionValue == \"" + DIALOG_CANCEL + "\") {\n");
391         result.append("\t\ttheForm." + PARAM_ACTION + ".value = \"" + DIALOG_CANCEL + "\";\n");
392         result.append("\t}\n");
393         result.append("\ttheForm.submit();\n");
394         result.append("\treturn false;\n");
395         result.append("}\n");
396         result.append("//-->\n</script>\n");
397         return result.toString();
398     }
399
400     /**
401      * Returns all initialized parameters of the current workplace class
402      * as hidden field tags that can be inserted in a form.<p>
403      *
404      * This overwrites the method in CmsWorkplace because for each tab,
405      * only the hidden parameters of the non displayed tabs are added.<p>
406      *
407      * @return all initialized parameters of the current workplace class
408      * as hidden field tags that can be inserted in a html form
409      */

410     public String JavaDoc paramsAsHidden() {
411
412         StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
413         String JavaDoc activeTab = (String JavaDoc)getTabParameterOrder().get(getActiveTab() - 1);
414         Map JavaDoc params = paramValues();
415         Iterator JavaDoc i = params.keySet().iterator();
416         while (i.hasNext()) {
417             String JavaDoc param = (String JavaDoc)i.next();
418             if (!param.startsWith(activeTab)) {
419                 // add only parameters which are not displayed in currently active tab
420
Object JavaDoc value = params.get(param);
421                 result.append("<input type=\"hidden\" name=\"");
422                 result.append(param);
423                 result.append("\" value=\"");
424                 result.append(CmsEncoder.encode(value.toString(), getCms().getRequestContext().getEncoding()));
425                 result.append("\">\n");
426             }
427         }
428         return result.toString();
429     }
430
431     /**
432      * Sets the value of the setpressed parameter.<p>
433      *
434      * @param value the value to set
435      */

436     public void setParamSetPressed(String JavaDoc value) {
437
438         m_paramSetPressed = value;
439     }
440
441     /**
442      * Sets the value of the tab parameter.<p>
443      *
444      * @param value the value to set
445      */

446     public void setParamTab(String JavaDoc value) {
447
448         m_paramTab = value;
449     }
450
451 }
452
Popular Tags