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