001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.util.CmsStringUtil;
031import org.opencms.workplace.CmsWorkplace;
032import org.opencms.workplace.tools.A_CmsHtmlIconButton;
033import org.opencms.workplace.tools.CmsHtmlIconButtonStyleEnum;
034
035import java.text.MessageFormat;
036import java.util.Locale;
037
038/**
039 * Default implementation of a direct action for a html list column.<p>
040 *
041 * @since 6.0.0
042 */
043public class CmsListDirectAction extends A_CmsListAction implements I_CmsListDirectAction {
044
045    /** Id of the Column to use when setting param in help and conf texts. */
046    private String m_columnForTexts;
047
048    /** List item. */
049    private CmsListItem m_listItem;
050
051    /**
052     * Default Constructor.<p>
053     *
054     * @param id unique id
055     */
056    public CmsListDirectAction(String id) {
057
058        super(id);
059    }
060
061    /**
062     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#buttonHtml(CmsWorkplace)
063     */
064    public String buttonHtml(CmsWorkplace wp) {
065
066        if (!isVisible()) {
067            return "";
068        }
069        return A_CmsHtmlIconButton.defaultButtonHtml(
070            resolveButtonStyle(),
071            getId() + getItem().getId(),
072            getId(),
073            resolveName(wp.getLocale()),
074            resolveHelpText(wp.getLocale()),
075            isEnabled(),
076            getIconPath(),
077            null,
078            resolveOnClic(wp.getLocale()),
079            getColumnForTexts() == null,
080            null);
081    }
082
083    /**
084     * @see org.opencms.workplace.list.I_CmsListDirectAction#confirmationTextHtml()
085     */
086    public String confirmationTextHtml() {
087
088        StringBuffer html = new StringBuffer(512);
089        String cm = getConfirmationMessage().key(getWp().getLocale());
090        String confMessage = new MessageFormat(cm, getWp().getLocale()).format(new Object[] {""});
091        if ((getColumnForTexts() == null)
092            || confMessage.equals(
093                new MessageFormat(cm, getWp().getLocale()).format(new Object[] {getItem().get(getColumnForTexts())}))) {
094            html.append(A_CmsListAction.defaultConfirmationHtml(getId(), confMessage));
095        }
096        return html.toString();
097    }
098
099    /**
100     * @see org.opencms.workplace.list.I_CmsListDirectAction#getColumnForTexts()
101     */
102    public String getColumnForTexts() {
103
104        return m_columnForTexts;
105    }
106
107    /**
108     * @see org.opencms.workplace.list.I_CmsListDirectAction#getItem()
109     */
110    public CmsListItem getItem() {
111
112        return m_listItem;
113    }
114
115    /**
116     * @see org.opencms.workplace.list.I_CmsListDirectAction#helpTextHtml()
117     */
118    public String helpTextHtml() {
119
120        StringBuffer html = new StringBuffer(512);
121        String ht = getHelpText().key(getWp().getLocale());
122        String helptext = new MessageFormat(ht, getWp().getLocale()).format(new Object[] {""});
123        if ((getColumnForTexts() == null)
124            || helptext.equals(
125                new MessageFormat(ht, getWp().getLocale()).format(new Object[] {getItem().get(getColumnForTexts())}))) {
126            html.append(A_CmsHtmlIconButton.defaultHelpHtml(getId(), helptext));
127        }
128        return html.toString();
129    }
130
131    /**
132     * @see org.opencms.workplace.list.I_CmsListDirectAction#setColumnForTexts(java.lang.String)
133     */
134    public void setColumnForTexts(String columnId) {
135
136        m_columnForTexts = columnId;
137    }
138
139    /**
140     * @see org.opencms.workplace.list.I_CmsListDirectAction#setItem(org.opencms.workplace.list.CmsListItem)
141     */
142    public void setItem(CmsListItem item) {
143
144        m_listItem = item;
145    }
146
147    /**
148     * Help method to resolve the style of the button.<p>
149     *
150     * @return the style of the button
151     */
152    protected CmsHtmlIconButtonStyleEnum resolveButtonStyle() {
153
154        return CmsHtmlIconButtonStyleEnum.SMALL_ICON_ONLY;
155    }
156
157    /**
158     * Help method to resolve the help text to use.<p>
159     *
160     * @param locale the used locale
161     *
162     * @return the help text
163     */
164    protected String resolveHelpText(Locale locale) {
165
166        String helpText = getHelpText().key(locale);
167        if ((getColumnForTexts() != null) && (getItem().get(getColumnForTexts()) != null)) {
168            helpText = new MessageFormat(helpText, locale).format(new Object[] {getItem().get(getColumnForTexts())});
169        }
170        return helpText;
171    }
172
173    /**
174     * Help method to resolve the name to use.<p>
175     *
176     * @param locale the used locale
177     *
178     * @return the name
179     */
180    protected String resolveName(Locale locale) {
181
182        return getName().key(locale);
183    }
184
185    /**
186     * Help method to resolve the on clic text to use.<p>
187     *
188     * @param locale the used locale
189     *
190     * @return the on clic text
191     */
192    protected String resolveOnClic(Locale locale) {
193
194        String confirmationMessage = getConfirmationMessage().key(locale);
195        if ((getColumnForTexts() != null) && (getItem().get(getColumnForTexts()) != null)) {
196            confirmationMessage = new MessageFormat(confirmationMessage, locale).format(
197                new Object[] {getItem().get(getColumnForTexts())});
198        }
199        StringBuffer onClic = new StringBuffer(128);
200        onClic.append("listAction('");
201        onClic.append(getListId());
202        onClic.append("', '");
203        onClic.append(getId());
204        onClic.append("', '");
205        if ((getColumnForTexts() == null)
206            || (getItem().get(getColumnForTexts()) == null)
207            || confirmationMessage.equals(new MessageFormat(confirmationMessage, locale).format(new Object[] {""}))) {
208            onClic.append("conf" + getId());
209        } else {
210            onClic.append(CmsStringUtil.escapeJavaScript(confirmationMessage));
211        }
212        onClic.append("', '");
213        onClic.append(CmsStringUtil.escapeJavaScript(getItem().getId()));
214        onClic.append("');");
215        return onClic.toString();
216    }
217
218}