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.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     * Help method to resolve the help text to use.<p>
063     *
064     * @param locale the used locale
065     *
066     * @return the help text
067     */
068    protected String resolveHelpText(Locale locale) {
069
070        String helpText = getHelpText().key(locale);
071        if ((getColumnForTexts() != null) && (getItem().get(getColumnForTexts()) != null)) {
072            helpText = new MessageFormat(helpText, locale).format(new Object[] {getItem().get(getColumnForTexts())});
073        }
074        return helpText;
075    }
076
077    /**
078     * Help method to resolve the on clic text to use.<p>
079     *
080     * @param locale the used locale
081     *
082     * @return the on clic text
083     */
084    protected String resolveOnClic(Locale locale) {
085
086        String confirmationMessage = getConfirmationMessage().key(locale);
087        if ((getColumnForTexts() != null) && (getItem().get(getColumnForTexts()) != null)) {
088            confirmationMessage = new MessageFormat(confirmationMessage, locale).format(
089                new Object[] {getItem().get(getColumnForTexts())});
090        }
091        StringBuffer onClic = new StringBuffer(128);
092        onClic.append("listAction('");
093        onClic.append(getListId());
094        onClic.append("', '");
095        onClic.append(getId());
096        onClic.append("', '");
097        if ((getColumnForTexts() == null)
098            || (getItem().get(getColumnForTexts()) == null)
099            || confirmationMessage.equals(new MessageFormat(confirmationMessage, locale).format(new Object[] {""}))) {
100            onClic.append("conf" + getId());
101        } else {
102            onClic.append(CmsStringUtil.escapeJavaScript(confirmationMessage));
103        }
104        onClic.append("', '");
105        onClic.append(CmsStringUtil.escapeJavaScript(getItem().getId()));
106        onClic.append("');");
107        return onClic.toString();
108    }
109
110    /**
111     * Help method to resolve the name to use.<p>
112     *
113     * @param locale the used locale
114     *
115     * @return the name
116     */
117    protected String resolveName(Locale locale) {
118
119        return getName().key(locale);
120    }
121
122    /**
123     * Help method to resolve the style of the button.<p>
124     *
125     * @return the style of the button
126     */
127    protected CmsHtmlIconButtonStyleEnum resolveButtonStyle() {
128
129        return CmsHtmlIconButtonStyleEnum.SMALL_ICON_ONLY;
130    }
131
132    /**
133     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#buttonHtml(CmsWorkplace)
134     */
135    public String buttonHtml(CmsWorkplace wp) {
136
137        if (!isVisible()) {
138            return "";
139        }
140        return A_CmsHtmlIconButton.defaultButtonHtml(
141            resolveButtonStyle(),
142            getId() + getItem().getId(),
143            getId(),
144            resolveName(wp.getLocale()),
145            resolveHelpText(wp.getLocale()),
146            isEnabled(),
147            getIconPath(),
148            null,
149            resolveOnClic(wp.getLocale()),
150            getColumnForTexts() == null,
151            null);
152    }
153
154    /**
155     * @see org.opencms.workplace.list.I_CmsListDirectAction#confirmationTextHtml()
156     */
157    public String confirmationTextHtml() {
158
159        StringBuffer html = new StringBuffer(512);
160        String cm = getConfirmationMessage().key(getWp().getLocale());
161        String confMessage = new MessageFormat(cm, getWp().getLocale()).format(new Object[] {""});
162        if ((getColumnForTexts() == null)
163            || confMessage.equals(
164                new MessageFormat(cm, getWp().getLocale()).format(new Object[] {getItem().get(getColumnForTexts())}))) {
165            html.append(A_CmsListAction.defaultConfirmationHtml(getId(), confMessage));
166        }
167        return html.toString();
168    }
169
170    /**
171     * @see org.opencms.workplace.list.I_CmsListDirectAction#getColumnForTexts()
172     */
173    public String getColumnForTexts() {
174
175        return m_columnForTexts;
176    }
177
178    /**
179     * @see org.opencms.workplace.list.I_CmsListDirectAction#getItem()
180     */
181    public CmsListItem getItem() {
182
183        return m_listItem;
184    }
185
186    /**
187     * @see org.opencms.workplace.list.I_CmsListDirectAction#helpTextHtml()
188     */
189    public String helpTextHtml() {
190
191        StringBuffer html = new StringBuffer(512);
192        String ht = getHelpText().key(getWp().getLocale());
193        String helptext = new MessageFormat(ht, getWp().getLocale()).format(new Object[] {""});
194        if ((getColumnForTexts() == null)
195            || helptext.equals(
196                new MessageFormat(ht, getWp().getLocale()).format(new Object[] {getItem().get(getColumnForTexts())}))) {
197            html.append(A_CmsHtmlIconButton.defaultHelpHtml(getId(), helptext));
198        }
199        return html.toString();
200    }
201
202    /**
203     * @see org.opencms.workplace.list.I_CmsListDirectAction#setColumnForTexts(java.lang.String)
204     */
205    public void setColumnForTexts(String columnId) {
206
207        m_columnForTexts = columnId;
208    }
209
210    /**
211     * @see org.opencms.workplace.list.I_CmsListDirectAction#setItem(org.opencms.workplace.list.CmsListItem)
212     */
213    public void setItem(CmsListItem item) {
214
215        m_listItem = item;
216    }
217
218}