KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > A_CmsHtmlIconButton


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/tools/A_CmsHtmlIconButton.java,v $
3  * Date : $Date: 2006/03/27 14:52:51 $
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.tools;
33
34 import org.opencms.i18n.CmsMessageContainer;
35 import org.opencms.jsp.CmsJspActionElement;
36 import org.opencms.util.CmsStringUtil;
37 import org.opencms.workplace.CmsWorkplace;
38
39 import java.io.File JavaDoc;
40
41 /**
42  * Default skeleton for an html icon button.<p>
43  *
44  * @author Michael Moossen
45  *
46  * @version $Revision: 1.21 $
47  *
48  * @since 6.0.0
49  */

50 public abstract class A_CmsHtmlIconButton implements I_CmsHtmlIconButton {
51
52     /** Constant for an empty message. */
53     public static final CmsMessageContainer EMPTY_MESSAGE = Messages.get().container(Messages.GUI_EMPTY_MESSAGE_0);
54
55     /** unique id. */
56     protected String JavaDoc m_id;
57
58     /** Enabled flag. */
59     private boolean m_enabled = true;
60
61     /** Help text or description. */
62     private CmsMessageContainer m_helpText;
63
64     /** Path to the icon. */
65     private String JavaDoc m_iconPath;
66
67     /** Display name. */
68     private CmsMessageContainer m_name;
69
70     /** Visibility flag. */
71     private boolean m_visible = true;
72
73     /**
74      * Default Constructor.<p>
75      *
76      * @param id the id
77      */

78     public A_CmsHtmlIconButton(String JavaDoc id) {
79
80         m_id = id;
81     }
82
83     /**
84      * Full Constructor.<p>
85      *
86      * @param id the id
87      * @param name the name
88      * @param helpText the help text
89      * @param iconPath the path to the icon
90      * @param enabled if enabled or not
91      */

92     public A_CmsHtmlIconButton(
93         String JavaDoc id,
94         CmsMessageContainer name,
95         CmsMessageContainer helpText,
96         String JavaDoc iconPath,
97         boolean enabled) {
98
99         this(id);
100         setName(name);
101         setHelpText(helpText);
102         setIconPath(iconPath);
103         setEnabled(enabled);
104     }
105
106     /**
107      * Generates a default html code for icon buttons.<p>
108      *
109      * @param jsp the jsp context
110      * @param style the style of the button
111      * @param id the id
112      * @param name the name
113      * @param helpText the help text
114      * @param enabled if enabled or not
115      * @param iconPath the path to the icon
116      * @param confirmationMessage the optional confirmation message
117      * @param onClick the js code to execute
118      *
119      * @return html code
120      */

121     public static String JavaDoc defaultButtonHtml(
122         CmsJspActionElement jsp,
123         CmsHtmlIconButtonStyleEnum style,
124         String JavaDoc id,
125         String JavaDoc name,
126         String JavaDoc helpText,
127         boolean enabled,
128         String JavaDoc iconPath,
129         String JavaDoc confirmationMessage,
130         String JavaDoc onClick) {
131
132         return defaultButtonHtml(jsp, style, id, id, name, helpText, enabled, iconPath, confirmationMessage, onClick, false);
133     }
134
135     /**
136      * Generates a default html code where several buttons can have the same help text.<p>
137      *
138      * @param jsp the cms context, can be null
139      * @param style the style of the button
140      * @param id the id
141      * @param helpId the id of the helptext div tag
142      * @param name the name, if empty only the icon is displayed
143      * @param helpText the help text, if empty no mouse events are generated
144      * @param enabled if enabled or not, if not set be sure to take an according helptext
145      * @param iconPath the path to the icon, if empty only the name is displayed
146      * @param confirmationMessage the confirmation message
147      * @param onClick the js code to execute, if empty no link is generated
148      * @param singleHelp if set, no helptext is written, you have to use the defaultHelpHtml() method later
149      *
150      * @return html code
151      */

152     public static String JavaDoc defaultButtonHtml(
153         CmsJspActionElement jsp,
154         CmsHtmlIconButtonStyleEnum style,
155         String JavaDoc id,
156         String JavaDoc helpId,
157         String JavaDoc name,
158         String JavaDoc helpText,
159         boolean enabled,
160         String JavaDoc iconPath,
161         String JavaDoc confirmationMessage,
162         String JavaDoc onClick,
163         boolean singleHelp) {
164
165         StringBuffer JavaDoc html = new StringBuffer JavaDoc(1024);
166         if (style == CmsHtmlIconButtonStyleEnum.BIG_ICON_TEXT) {
167             html.append("<div class='bigLink' id='img");
168             html.append(id);
169             html.append("'>\n");
170         }
171         html.append("\t<span class=\"link");
172         if (enabled) {
173             html.append("\"");
174         } else {
175             html.append(" linkdisabled\"");
176         }
177         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(helpText)) {
178             if (!singleHelp) {
179                 html.append(" onMouseOver=\"sMH('");
180                 html.append(id);
181                 html.append("');\" onMouseOut=\"hMH('");
182                 html.append(id);
183                 html.append("');\"");
184             } else {
185                 html.append(" onMouseOver=\"sMHS('");
186                 html.append(id);
187                 html.append("', '");
188                 html.append(helpId);
189                 html.append("');\" onMouseOut=\"hMH('");
190                 html.append(id);
191                 html.append("', '");
192                 html.append(helpId);
193                 html.append("');\"");
194             }
195         }
196         if (enabled && CmsStringUtil.isNotEmptyOrWhitespaceOnly(onClick)) {
197             html.append(" onClick=\"");
198             if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(confirmationMessage)) {
199                 html.append("if (confirm('" + CmsStringUtil.escapeJavaScript(confirmationMessage) + "')) {");
200             }
201             html.append(onClick);
202             if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(confirmationMessage)) {
203                 html.append(" }");
204             }
205             html.append("\"");
206         }
207         if (style == CmsHtmlIconButtonStyleEnum.SMALL_ICON_ONLY) {
208             html.append(" title='");
209             html.append(name);
210             html.append("'");
211         }
212         html.append(">");
213         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(iconPath)) {
214             html.append("<img SRC='");
215             html.append(CmsWorkplace.getSkinUri());
216             if (!enabled) {
217                 StringBuffer JavaDoc icon = new StringBuffer JavaDoc(128);
218                 icon.append(iconPath.substring(0, iconPath.lastIndexOf('.')));
219                 icon.append("_disabled");
220                 icon.append(iconPath.substring(iconPath.lastIndexOf('.')));
221                 if (jsp != null) {
222                     String JavaDoc resorcesRoot = jsp.getJspContext().getServletConfig().getServletContext().getRealPath(
223                         "/resources/");
224                     File JavaDoc test = new File JavaDoc(resorcesRoot + "/" + icon.toString());
225                     if (test.exists()) {
226                         html.append(icon);
227                     } else {
228                         html.append(iconPath);
229                     }
230                 } else {
231                     html.append(iconPath);
232                 }
233             } else {
234                 html.append(iconPath);
235             }
236             html.append("'");
237             if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(name)) {
238                 html.append(" alt='");
239                 html.append(name);
240                 html.append("'");
241                 html.append(" title='");
242                 html.append(name);
243                 html.append("'");
244             }
245             html.append(">");
246             if (style == CmsHtmlIconButtonStyleEnum.BIG_ICON_TEXT) {
247                 html.append("<br>");
248             }
249         }
250         if (style != CmsHtmlIconButtonStyleEnum.SMALL_ICON_ONLY && CmsStringUtil.isNotEmptyOrWhitespaceOnly(name)) {
251             if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(iconPath) && style != CmsHtmlIconButtonStyleEnum.BIG_ICON_TEXT) {
252                 html.append("&nbsp;");
253             }
254             if (enabled) {
255                 html.append("<a HREF='#'>");
256             }
257             html.append(name);
258             if (enabled) {
259                 html.append("</a>");
260             }
261         }
262         html.append("</span>");
263         if (style == CmsHtmlIconButtonStyleEnum.BIG_ICON_TEXT) {
264             html.append("</div>\n");
265         }
266         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(helpText) && !singleHelp) {
267             html.append("<div class='help' id='help");
268             html.append(helpId);
269             html.append("' onMouseOver=\"sMH('");
270             html.append(id);
271             html.append("');\" onMouseOut=\"hMH('");
272             html.append(id);
273             html.append("');\">");
274             html.append(helpText);
275             html.append("</div>\n");
276         }
277         return html.toString();
278     }
279
280     /**
281      * Generates html for the helptext when having one helptext for several buttons.<p>
282      *
283      * @param helpId the id of the help text
284      * @param helpText the help text
285      *
286      * @return html code
287      */

288     public static String JavaDoc defaultHelpHtml(String JavaDoc helpId, String JavaDoc helpText) {
289
290         StringBuffer JavaDoc html = new StringBuffer JavaDoc(1024);
291         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(helpText)) {
292             html.append("<div class='help' id='help");
293             html.append(helpId);
294             html.append("' onMouseOut=\"hMH('");
295             html.append(helpId);
296             html.append("');\">");
297             html.append(helpText);
298             html.append("</div>\n");
299         }
300         return html.toString();
301     }
302
303     /**
304      * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#getHelpText()
305      */

306     public CmsMessageContainer getHelpText() {
307
308         if (m_helpText == null) {
309             m_helpText = EMPTY_MESSAGE;
310         }
311         return m_helpText;
312     }
313
314     /**
315      * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#getIconPath()
316      */

317     public String JavaDoc getIconPath() {
318
319         return m_iconPath;
320     }
321
322     /**
323      * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#getId()
324      */

325     public String JavaDoc getId() {
326
327         return m_id;
328     }
329
330     /**
331      * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#getName()
332      */

333     public CmsMessageContainer getName() {
334
335         return m_name;
336     }
337
338     /**
339      * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#isEnabled()
340      */

341     public boolean isEnabled() {
342
343         return m_enabled;
344     }
345
346     /**
347      * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#isVisible()
348      */

349     public boolean isVisible() {
350
351         return m_visible;
352     }
353
354     /**
355      * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#setEnabled(boolean)
356      */

357     public void setEnabled(boolean enabled) {
358
359         m_enabled = enabled;
360     }
361
362     /**
363      * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#setHelpText(org.opencms.i18n.CmsMessageContainer)
364      */

365     public void setHelpText(CmsMessageContainer helpText) {
366
367         if (helpText == null) {
368             helpText = EMPTY_MESSAGE;
369         }
370         m_helpText = helpText;
371     }
372
373     /**
374      * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#setIconPath(java.lang.String)
375      */

376     public void setIconPath(String JavaDoc iconPath) {
377
378         m_iconPath = iconPath;
379     }
380
381     /**
382      * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#setName(org.opencms.i18n.CmsMessageContainer)
383      */

384     public void setName(CmsMessageContainer name) {
385
386         if (name == null) {
387             name = EMPTY_MESSAGE;
388         }
389         m_name = name;
390     }
391
392     /**
393      * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#setVisible(boolean)
394      */

395     public void setVisible(boolean visible) {
396
397         m_visible = visible;
398     }
399 }
Popular Tags