001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.workplace.tools;
029
030import org.opencms.i18n.CmsMessageContainer;
031import org.opencms.main.OpenCms;
032import org.opencms.util.CmsStringUtil;
033import org.opencms.workplace.CmsWorkplace;
034
035import java.io.File;
036
037/**
038 * Default skeleton for an html icon button.<p>
039 *
040 * @since 6.0.0
041 */
042public abstract class A_CmsHtmlIconButton implements I_CmsHtmlIconButton {
043
044    /** Constant for an empty message. */
045    public static final CmsMessageContainer EMPTY_MESSAGE = Messages.get().container(Messages.GUI_EMPTY_MESSAGE_0);
046
047    /** unique id. */
048    protected String m_id;
049
050    /** Enabled flag. */
051    private boolean m_enabled = true;
052
053    /** Help text or description. */
054    private CmsMessageContainer m_helpText;
055
056    /** Path to the icon. */
057    private String m_iconPath;
058
059    /** Display name. */
060    private CmsMessageContainer m_name;
061
062    /** Visibility flag. */
063    private boolean m_visible = true;
064
065    /**
066     * Default Constructor.<p>
067     *
068     * @param id the id
069     */
070    public A_CmsHtmlIconButton(String id) {
071
072        m_id = id;
073    }
074
075    /**
076     * Full Constructor.<p>
077     *
078     * @param id the id
079     * @param name the name
080     * @param helpText the help text
081     * @param iconPath the path to the icon
082     * @param enabled if enabled or not
083     */
084    public A_CmsHtmlIconButton(
085        String id,
086        CmsMessageContainer name,
087        CmsMessageContainer helpText,
088        String iconPath,
089        boolean enabled) {
090
091        this(id);
092        setName(name);
093        setHelpText(helpText);
094        setIconPath(iconPath);
095        setEnabled(enabled);
096    }
097
098    /**
099     * Generates a default html code for icon buttons.<p>
100     *
101     * @param style the style of the button
102     * @param id the id
103     * @param name the name
104     * @param helpText the help text
105     * @param enabled if enabled or not
106     * @param iconPath the path to the icon
107     * @param confirmationMessage the optional confirmation message
108     * @param onClick the js code to execute
109     *
110     * @return html code
111     */
112    public static String defaultButtonHtml(
113        CmsHtmlIconButtonStyleEnum style,
114        String id,
115        String name,
116        String helpText,
117        boolean enabled,
118        String iconPath,
119        String confirmationMessage,
120        String onClick) {
121
122        return defaultButtonHtml(
123            style,
124            id,
125            id,
126            name,
127            helpText,
128            enabled,
129            iconPath,
130            confirmationMessage,
131            onClick,
132            false,
133            null);
134    }
135
136    /**
137     * Generates a default html code where several buttons can have the same help text.<p>
138     *
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     * @param rightHtml optional html code that should come direct after the button
150     *
151     * @return html code
152     */
153    public static String defaultButtonHtml(
154        CmsHtmlIconButtonStyleEnum style,
155        String id,
156        String helpId,
157        String name,
158        String helpText,
159        boolean enabled,
160        String iconPath,
161        String confirmationMessage,
162        String onClick,
163        boolean singleHelp,
164        String rightHtml) {
165
166        StringBuffer html = new StringBuffer(1024);
167        if (style == CmsHtmlIconButtonStyleEnum.BIG_ICON_TEXT) {
168            html.append("<div class='bigLink' id='img");
169            html.append(id);
170            html.append("'>\n");
171        }
172        html.append("\t<span class=\"link");
173        if (enabled) {
174            html.append("\"");
175        } else {
176            html.append(" linkdisabled\"");
177        }
178        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(helpText)) {
179            if (!singleHelp) {
180                html.append(" onMouseOver=\"sMH('");
181                html.append(id);
182                html.append("');\" onMouseOut=\"hMH('");
183                html.append(id);
184                html.append("');\"");
185            } else {
186                html.append(" onMouseOver=\"sMHS('");
187                html.append(id);
188                html.append("', '");
189                html.append(helpId);
190                html.append("');\" onMouseOut=\"hMH('");
191                html.append(id);
192                html.append("', '");
193                html.append(helpId);
194                html.append("');\"");
195            }
196        }
197        if (enabled && CmsStringUtil.isNotEmptyOrWhitespaceOnly(onClick)) {
198            html.append(" onClick=\"");
199            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(confirmationMessage)) {
200                html.append("if (confirm('" + CmsStringUtil.escapeJavaScript(confirmationMessage) + "')) {");
201            }
202            html.append(onClick);
203            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(confirmationMessage)) {
204                html.append(" }");
205            }
206            html.append("\"");
207        }
208        if (style == CmsHtmlIconButtonStyleEnum.SMALL_ICON_ONLY) {
209            html.append(" title='");
210            html.append(name);
211            html.append("'");
212        }
213
214        html.append(">");
215        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(iconPath)) {
216            html.append("<img src='");
217            html.append(CmsWorkplace.getSkinUri());
218            if (!enabled) {
219                StringBuffer icon = new StringBuffer(128);
220                icon.append(iconPath.substring(0, iconPath.lastIndexOf('.')));
221                icon.append("_disabled");
222                icon.append(iconPath.substring(iconPath.lastIndexOf('.')));
223                String resourcesRoot = OpenCms.getSystemInfo().getWebApplicationRfsPath() + "resources/";
224                File test = new File(resourcesRoot + 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            html.append("'");
234            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(helpText)) {
235                html.append(" alt='");
236                html.append(helpText);
237                html.append("'");
238                html.append(" title='");
239                html.append(helpText);
240                html.append("'");
241            }
242            html.append(">");
243            if (style == CmsHtmlIconButtonStyleEnum.BIG_ICON_TEXT) {
244                html.append("<br>");
245            }
246        }
247        if ((style != CmsHtmlIconButtonStyleEnum.SMALL_ICON_ONLY) && CmsStringUtil.isNotEmptyOrWhitespaceOnly(name)) {
248            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(iconPath)
249                && (style != CmsHtmlIconButtonStyleEnum.BIG_ICON_TEXT)) {
250                html.append("&nbsp;");
251            }
252            if (enabled) {
253                if (style != CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT) {
254                    html.append("<a href='#'>");
255                } else {
256                    html.append("<a href='#' style='white-space: nowrap;'>");
257                }
258            }
259            html.append(name);
260            if (enabled) {
261                html.append("</a>");
262            }
263
264            // doesn't work in new dialog for the radio button cols
265            // couldn't find a place where this is needed
266            // if (style != CmsHtmlIconButtonStyleEnum.BIG_ICON_TEXT && name.length() > 1) {
267            //  html.append("&nbsp;");
268            // }
269        }
270        html.append("</span>");
271        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(rightHtml)) {
272            html.append(rightHtml);
273        }
274        if (style == CmsHtmlIconButtonStyleEnum.BIG_ICON_TEXT) {
275            html.append("</div>\n");
276        }
277        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(helpText) && !singleHelp) {
278            html.append("<div class='help' id='help");
279            html.append(helpId);
280            html.append("' onMouseOver=\"sMH('");
281            html.append(id);
282            html.append("');\" onMouseOut=\"hMH('");
283            html.append(id);
284            html.append("');\">");
285            html.append(helpText);
286            html.append("</div>\n");
287        }
288        return html.toString();
289    }
290
291    /**
292     * Generates html for the helptext when having one helptext for several buttons.<p>
293     *
294     * @param helpId the id of the help text
295     * @param helpText the help text
296     *
297     * @return html code
298     */
299    public static String defaultHelpHtml(String helpId, String helpText) {
300
301        StringBuffer html = new StringBuffer(1024);
302        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(helpText)) {
303            html.append("<div class='help' id='help");
304            html.append(helpId);
305            html.append("' onMouseOut=\"hMH('");
306            html.append(helpId);
307            html.append("');\">");
308            html.append(helpText);
309            html.append("</div>\n");
310        }
311        return html.toString();
312    }
313
314    /**
315     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#getHelpText()
316     */
317    public CmsMessageContainer getHelpText() {
318
319        if (m_helpText == null) {
320            m_helpText = EMPTY_MESSAGE;
321        }
322        return m_helpText;
323    }
324
325    /**
326     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#getIconPath()
327     */
328    public String getIconPath() {
329
330        return m_iconPath;
331    }
332
333    /**
334     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#getId()
335     */
336    public String getId() {
337
338        return m_id;
339    }
340
341    /**
342     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#getName()
343     */
344    public CmsMessageContainer getName() {
345
346        return m_name;
347    }
348
349    /**
350     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#isEnabled()
351     */
352    public boolean isEnabled() {
353
354        return m_enabled;
355    }
356
357    /**
358     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#isVisible()
359     */
360    public boolean isVisible() {
361
362        return m_visible;
363    }
364
365    /**
366     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#setEnabled(boolean)
367     */
368    public void setEnabled(boolean enabled) {
369
370        m_enabled = enabled;
371    }
372
373    /**
374     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#setHelpText(org.opencms.i18n.CmsMessageContainer)
375     */
376    public void setHelpText(CmsMessageContainer helpText) {
377
378        if (helpText == null) {
379            helpText = EMPTY_MESSAGE;
380        }
381        m_helpText = helpText;
382    }
383
384    /**
385     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#setIconPath(java.lang.String)
386     */
387    public void setIconPath(String iconPath) {
388
389        m_iconPath = iconPath;
390    }
391
392    /**
393     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#setName(org.opencms.i18n.CmsMessageContainer)
394     */
395    public void setName(CmsMessageContainer name) {
396
397        if (name == null) {
398            name = EMPTY_MESSAGE;
399        }
400        m_name = name;
401    }
402
403    /**
404     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#setVisible(boolean)
405     */
406    public void setVisible(boolean visible) {
407
408        m_visible = visible;
409    }
410}