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.list;
029
030import org.opencms.main.OpenCms;
031import org.opencms.util.CmsStringUtil;
032import org.opencms.workplace.CmsWorkplace;
033import org.opencms.workplace.explorer.CmsResourceUtil;
034
035import java.io.File;
036
037/**
038 * Displays a 16x16 icon from the explorer view in a list action.<p>
039 *
040 * @since 6.0.0
041 */
042public class CmsListExplorerDirectAction extends CmsListDirectAction {
043
044    /** The current resource util object. */
045    private CmsResourceUtil m_resourceUtil;
046
047    /**
048     * Default Constructor.<p>
049     *
050     * @param id the unique id
051     */
052    public CmsListExplorerDirectAction(String id) {
053
054        super(id);
055    }
056
057    /**
058     * @see org.opencms.workplace.list.CmsListDirectAction#buttonHtml(org.opencms.workplace.CmsWorkplace)
059     */
060    @Override
061    public String buttonHtml(CmsWorkplace wp) {
062
063        if (!isVisible()) {
064            return "";
065        }
066        return defButtonHtml(
067            getId() + getItem().getId(),
068            getId(),
069            resolveName(wp.getLocale()),
070            resolveHelpText(wp.getLocale()),
071            isEnabled(),
072            getIconPath(),
073            null,
074            resolveOnClic(wp.getLocale()),
075            getColumnForTexts() == null);
076    }
077
078    /**
079     * @see org.opencms.workplace.list.I_CmsListDirectAction#setItem(org.opencms.workplace.list.CmsListItem)
080     */
081    @Override
082    public void setItem(CmsListItem item) {
083
084        m_resourceUtil = ((A_CmsListExplorerDialog)getWp()).getResourceUtil(item);
085        super.setItem(item);
086    }
087
088    /**
089     * Generates a default html code where several buttons can have the same help text.<p>
090     *
091     * the only diff to <code>{@link org.opencms.workplace.tools.A_CmsHtmlIconButton#defaultButtonHtml(org.opencms.workplace.tools.CmsHtmlIconButtonStyleEnum, String, String, String, boolean, String, String, String)}</code>
092     * is that the icons are 16x16.<p>
093     *
094     * @param id the id
095     * @param helpId the id of the helptext div tag
096     * @param name the name, if empty only the icon is displayed
097     * @param helpText the help text, if empty no mouse events are generated
098     * @param enabled if enabled or not, if not set be sure to take an according helptext
099     * @param iconPath the path to the icon, if empty only the name is displayed
100     * @param onClick the js code to execute, if empty no link is generated
101     * @param confirmationMessage the confirmation message
102     * @param singleHelp if set, no helptext is written, you have to use the defaultHelpHtml() method later
103     *
104     * @return html code
105     *
106     * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#defaultButtonHtml(org.opencms.workplace.tools.CmsHtmlIconButtonStyleEnum, String, String, String, boolean, String, String, String)
107     */
108    protected String defButtonHtml(
109        String id,
110        String helpId,
111        String name,
112        String helpText,
113        boolean enabled,
114        String iconPath,
115        String confirmationMessage,
116        String onClick,
117        boolean singleHelp) {
118
119        StringBuffer html = new StringBuffer(1024);
120        html.append("\t<span class=\"link");
121        if (enabled) {
122            html.append("\"");
123        } else {
124            html.append(" linkdisabled\"");
125        }
126        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(helpText)) {
127            if (!singleHelp) {
128                html.append(" onMouseOver=\"sMH('");
129                html.append(id);
130                html.append("');\" onMouseOut=\"hMH('");
131                html.append(id);
132                html.append("');\"");
133            } else {
134                html.append(" onMouseOver=\"sMHS('");
135                html.append(id);
136                html.append("', '");
137                html.append(helpId);
138                html.append("');\" onMouseOut=\"hMH('");
139                html.append(id);
140                html.append("', '");
141                html.append(helpId);
142                html.append("');\"");
143            }
144        }
145        if (enabled && CmsStringUtil.isNotEmptyOrWhitespaceOnly(onClick)) {
146            html.append(" onClick=\"");
147            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(confirmationMessage)) {
148                html.append("if (confirm('" + CmsStringUtil.escapeJavaScript(confirmationMessage) + "')) {");
149            }
150            html.append(onClick);
151            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(confirmationMessage)) {
152                html.append(" }");
153            }
154            html.append("\"");
155        }
156        html.append(" title='");
157        html.append(name);
158        html.append("'");
159        html.append(" style='display: block; width: 20px; height: 20px;'>");
160        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(iconPath)) {
161            html.append("<img src='");
162            html.append(CmsWorkplace.getSkinUri());
163            if (!enabled) {
164                StringBuffer icon = new StringBuffer(128);
165                icon.append(iconPath.substring(0, iconPath.lastIndexOf('.')));
166                icon.append("_disabled");
167                icon.append(iconPath.substring(iconPath.lastIndexOf('.')));
168                String resourcesRoot = OpenCms.getSystemInfo().getWebApplicationRfsPath() + "resources/";
169                File test = new File(resourcesRoot + icon.toString());
170                if (test.exists()) {
171                    html.append(icon);
172                } else {
173                    html.append(iconPath);
174                }
175            } else {
176                html.append(iconPath);
177            }
178            html.append("'");
179            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(name)) {
180                html.append(" alt='");
181                html.append(name);
182                html.append("'");
183                html.append(" title='");
184                html.append(name);
185                html.append("'");
186            }
187            html.append(" style='width: 16px; height: 16px;' >");
188        }
189        html.append("</span>\n");
190        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(helpText) && !singleHelp) {
191            html.append("<div class='help' id='help");
192            html.append(helpId);
193            html.append("' onMouseOver=\"sMH('");
194            html.append(id);
195            html.append("');\" onMouseOut=\"hMH('");
196            html.append(id);
197            html.append("');\">");
198            html.append(helpText);
199            html.append("</div>\n");
200        }
201        return html.toString();
202    }
203
204    /**
205     * Returns the current result Util.<p>
206     *
207     * @return the current result Util
208     */
209    protected CmsResourceUtil getResourceUtil() {
210
211        return m_resourceUtil;
212    }
213}