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.i18n.CmsMessageContainer;
031import org.opencms.util.CmsStringUtil;
032import org.opencms.workplace.CmsWorkplace;
033import org.opencms.workplace.tools.CmsIdentifiableObjectContainer;
034
035import java.util.ArrayList;
036import java.util.Iterator;
037import java.util.List;
038
039/**
040 * Independent action to switch the resource state filter.<p>
041 *
042 * Do not forget to add parameter evaluation code overwritting the A_CmsListDialog#getList() method,
043 * like in <tt>CmsProjectFilesDialog</tt>.<p>
044 *
045 * @since 6.0.0
046 */
047public class CmsListDropdownAction extends CmsListIndependentAction {
048
049    /** parameter name suffix. */
050    public static final String SUFFIX_PARAM = "-sel";
051
052    /** The item ids, as a list of String objects. */
053    private List<String> m_ids = new ArrayList<String>();
054
055    /** The items, a map of [ids, display names] as [String, CmsMessageContainer] objects. */
056    private CmsIdentifiableObjectContainer<CmsMessageContainer> m_items = new CmsIdentifiableObjectContainer<CmsMessageContainer>(
057        true,
058        false);
059
060    /** The selected item id. */
061    private String m_selection;
062
063    /**
064     * Default Constructor.<p>
065     *
066     * @param id unique id
067     */
068    public CmsListDropdownAction(String id) {
069
070        super(id);
071    }
072
073    /**
074     * Adds an item to be displayed in the drop-down list.<p>
075     *
076     * @param id the id of the item
077     * @param name the display name
078     */
079    public void addItem(String id, CmsMessageContainer name) {
080
081        m_ids.add(id);
082        m_items.addIdentifiableObject(id, name);
083    }
084
085    /**
086     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#buttonHtml(org.opencms.workplace.CmsWorkplace)
087     */
088    @Override
089    public String buttonHtml(CmsWorkplace wp) {
090
091        StringBuffer html = new StringBuffer(512);
092        html.append("\t<span class='link'");
093        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getHelpText().key(wp.getLocale()))) {
094            html.append(" onMouseOver=\"sMH('");
095            html.append(getId());
096            html.append("');\" onMouseOut=\"hMH('");
097            html.append(getId());
098            html.append("');\"");
099        }
100        html.append("><p>");
101        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getIconPath())) {
102            html.append("<img src='");
103            html.append(CmsWorkplace.getSkinUri());
104            if (!isEnabled()) {
105                StringBuffer icon = new StringBuffer(128);
106                icon.append(getIconPath().substring(0, getIconPath().lastIndexOf('.')));
107                icon.append("_disabled");
108                icon.append(getIconPath().substring(getIconPath().lastIndexOf('.')));
109                if (wp.getCms().existsResource(CmsWorkplace.VFS_PATH_RESOURCES + icon.toString())) {
110                    html.append(icon);
111                } else {
112                    html.append(getIconPath());
113                }
114            } else {
115                html.append(getIconPath());
116            }
117            html.append("'");
118            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getName().key(wp.getLocale()))) {
119                html.append(" alt='");
120                html.append(getName().key(wp.getLocale()));
121                html.append("'");
122                html.append(" title='");
123                html.append(getName().key(wp.getLocale()));
124                html.append("'");
125            }
126            html.append(">");
127        }
128        html.append(getName().key(wp.getLocale()));
129        html.append("<select name='").append(getId()).append(SUFFIX_PARAM).append("' onchange=\"");
130        html.append(resolveOnClic(wp)).append("\">\n");
131        Iterator<String> it = m_ids.iterator();
132        while (it.hasNext()) {
133            String itemId = it.next();
134            html.append("\t\t\t\t<option value='");
135            html.append(itemId);
136            html.append("'");
137            html.append(itemId.equals(getSelection()) ? " selected" : "");
138            html.append(">");
139            html.append(m_items.getObject(itemId).key(wp.getLocale()));
140            html.append("</option>\n");
141        }
142        html.append("</select>\n");
143        html.append("</p></span>");
144        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getHelpText().key(wp.getLocale()))) {
145            html.append("<div class='help' id='help");
146            html.append(getId());
147            html.append("' onMouseOver=\"sMH('");
148            html.append(getId());
149            html.append("');\" onMouseOut=\"hMH('");
150            html.append(getId());
151            html.append("');\">");
152            html.append(getHelpText().key(wp.getLocale()));
153            html.append("</div>\n");
154        }
155        return html.toString();
156    }
157
158    /**
159     * Returns the selected item.<p>
160     *
161     * @return the selected item
162     */
163    public String getSelection() {
164
165        return m_selection;
166    }
167
168    /**
169     * Sets the selected item.<p>
170     *
171     * @param selection the selected item to set
172     */
173    public void setSelection(String selection) {
174
175        m_selection = selection;
176    }
177}