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.db.CmsUserSettings;
031import org.opencms.i18n.CmsMessageContainer;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.util.CmsStringUtil;
034import org.opencms.util.CmsUUID;
035import org.opencms.workplace.CmsWorkplace;
036import org.opencms.workplace.CmsWorkplaceSettings;
037
038import java.io.IOException;
039
040import javax.servlet.ServletException;
041import javax.servlet.http.HttpServletRequest;
042import javax.servlet.jsp.JspException;
043
044/**
045 * A base list dialog to select a resource.<p>
046 *
047 * This dialog can be used as part of a wizard based dialog by forwarding to it and after the selection switching back
048 * to the wizard. Necessary request parameters have to be passed through.<p>
049 *
050 * @since 6.5.6
051 */
052public abstract class A_CmsSelectResourceList extends A_CmsListExplorerDialog {
053
054    /** Constant for the "Finish" button in the build button methods. */
055    public static final int BUTTON_FINISH = 30;
056
057    /** Constant for the "Next" button in the build button methods. */
058    public static final int BUTTON_NEXT = 20;
059
060    /** List column id constant. */
061    public static final String LIST_COLUMN_SELECT = "cs";
062
063    /** List independent action id constant. */
064    public static final String LIST_RACTION_SEL = "rs";
065
066    /**
067     * Creates a new select resource list ordered and searchable by name.<p>
068     *
069     * @param jsp an initialized JSP action element
070     * @param listId the id of the displayed list
071     * @param listName the name of the list
072     */
073    protected A_CmsSelectResourceList(CmsJspActionElement jsp, String listId, CmsMessageContainer listName) {
074
075        super(jsp, listId, listName);
076        // hide print button
077        getList().getMetadata().getIndependentAction(CmsListPrintIAction.LIST_ACTION_ID).setVisible(false);
078        // suppress the box around the list
079        getList().setBoxed(false);
080    }
081
082    /**
083     * Default constructor.<p>
084     *
085     * @param jsp an initialized JSP action element
086     * @param listId the id of the displayed list
087     * @param listName the name of the list
088     * @param sortedColId the a priory sorted column
089     * @param sortOrder the order of the sorted column
090     * @param searchableColId the column to search into
091     */
092    protected A_CmsSelectResourceList(
093        CmsJspActionElement jsp,
094        String listId,
095        CmsMessageContainer listName,
096        String sortedColId,
097        CmsListOrderEnum sortOrder,
098        String searchableColId) {
099
100        super(jsp, listId, listName, sortedColId, sortOrder, searchableColId);
101        // hide print button
102        getList().getMetadata().getIndependentAction(CmsListPrintIAction.LIST_ACTION_ID).setVisible(false);
103        // suppress the box around the list
104        getList().setBoxed(false);
105    }
106
107    /**
108     * Override this to set additional parameters before forwarding or to change the forward target.<p>
109     *
110     * Usually you have to set the "action" parameter to another value before forwarding.<p>
111     *
112     * @see org.opencms.workplace.list.A_CmsListDialog#actionDialog()
113     */
114    @Override
115    public void actionDialog() throws JspException, ServletException, IOException {
116
117        if (getAction() == ACTION_CONTINUE) {
118            sendForward(nextUrl(), paramsAsParameterMap());
119            return;
120        }
121        super.actionDialog();
122    }
123
124    /**
125     * Builds a default button row with a continue and cancel button.<p>
126     *
127     * Override this to have special buttons for your dialog.<p>
128     *
129     * @return the button row
130     */
131    public String dialogButtons() {
132
133        return dialogButtons(new int[] {BUTTON_CONTINUE, BUTTON_CANCEL}, new String[] {"", ""});
134    }
135
136    /**
137     * @see org.opencms.workplace.list.A_CmsListDialog#displayDialog()
138     */
139    @Override
140    public void displayDialog() throws JspException, IOException, ServletException {
141
142        getList().setShowTitle(false);
143        super.displayDialog();
144    }
145
146    /**
147     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
148     */
149    @Override
150    public void executeListMultiActions() {
151
152        // no multi actions present
153    }
154
155    /**
156     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
157     */
158    @Override
159    public void executeListSingleActions() {
160
161        // no single actions present
162    }
163
164    /**
165     * Returns the title of the list to display.<p>
166     *
167     * @return the title of the list to display
168     */
169    public abstract String getListTitle();
170
171    /**
172     * Returns the resource name of the selected resource.<p>
173     *
174     * @return the resource name of the selected resource or null if no resource was selected
175     */
176    public String getSelectedResourceName() {
177
178        String resParam = getJsp().getRequest().getParameter(getListId() + LIST_RACTION_SEL);
179        if (CmsStringUtil.isNotEmpty(resParam)) {
180            CmsListItem item = getList().getItem(resParam);
181            return (String)item.get(LIST_COLUMN_NAME);
182        }
183        return null;
184    }
185
186    /**
187     * Returns the url to forward the parameters after selection.<p>
188     *
189     * @return the url to forward the parameters after selection
190     */
191    public abstract String nextUrl();
192
193    /**
194     * @see org.opencms.workplace.list.A_CmsListDialog#customHtmlEnd()
195     */
196    @Override
197    protected String customHtmlEnd() {
198
199        StringBuffer result = new StringBuffer(256);
200
201        result.append(dialogWhiteBoxEnd());
202        result.append(dialogBlockEnd());
203        result.append(dialogSpacer());
204        result.append(dialogButtons());
205        result.append(super.customHtmlEnd());
206        return result.toString();
207    }
208
209    /**
210     * @see org.opencms.workplace.list.A_CmsListDialog#customHtmlStart()
211     */
212    @Override
213    protected String customHtmlStart() {
214
215        StringBuffer result = new StringBuffer(256);
216        result.append("<script  src='");
217        result.append(CmsWorkplace.getSkinUri());
218        result.append("admin/javascript/general.js'></script>\n");
219        result.append("<script  src='");
220        result.append(CmsWorkplace.getSkinUri());
221        result.append("editors/xmlcontent/help.js'></script>\n");
222        result.append(dialogBlockStart(getListTitle()));
223        result.append(dialogWhiteBoxStart());
224        return result.toString();
225    }
226
227    /**
228     * @see org.opencms.workplace.CmsDialog#dialogButtonsHtml(java.lang.StringBuffer, int, java.lang.String)
229     */
230    @Override
231    protected void dialogButtonsHtml(StringBuffer result, int button, String attribute) {
232
233        attribute = appendDelimiter(attribute);
234
235        switch (button) {
236            case BUTTON_FINISH:
237                result.append("<input name='" + DIALOG_CONTINUE + "' type=\"button\" value=\"");
238                result.append(key(org.opencms.workplace.explorer.Messages.GUI_BUTTON_ENDWIZARD_0) + "\"");
239                if (attribute.toLowerCase().indexOf("onclick") == -1) {
240                    result.append(" onclick=\"submitAction('" + DIALOG_CONTINUE + "', form, '");
241                    result.append(getListId());
242                    result.append("-form');\"");
243                }
244                result.append("\" class=\"dialogbutton\"");
245                result.append(attribute);
246                result.append(">\n");
247                break;
248            case BUTTON_NEXT:
249                result.append("<input name='" + DIALOG_CONTINUE + "' type=\"button\" value=\"");
250                result.append(key(org.opencms.workplace.explorer.Messages.GUI_BUTTON_NEXTSCREEN_0) + "\"");
251                if (attribute.toLowerCase().indexOf("onclick") == -1) {
252                    result.append(" onclick=\"submitAction('" + DIALOG_CONTINUE + "', form, '");
253                    result.append(getListId());
254                    result.append("-form');\"");
255                }
256                result.append("\" class=\"dialogbutton\"");
257                result.append(attribute);
258                result.append(">\n");
259                break;
260            case BUTTON_CANCEL:
261                result.append("<input name='" + DIALOG_CANCEL + "' type=\"button\" value=\"");
262                result.append(key(org.opencms.workplace.Messages.GUI_DIALOG_BUTTON_CANCEL_0) + "\"");
263                if (attribute.toLowerCase().indexOf("onclick") == -1) {
264                    result.append(" onclick=\"submitAction('" + DIALOG_CANCEL + "', form, '");
265                    result.append(getListId());
266                    result.append("-form');\"");
267                }
268                result.append(" class=\"dialogbutton\"");
269                result.append(attribute);
270                result.append(">\n");
271                break;
272            default:
273                super.dialogButtonsHtml(result, button, attribute);
274        }
275    }
276
277    /**
278     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
279     */
280    @Override
281    protected void fillDetails(String detailId) {
282
283        // no details
284    }
285
286    /**
287     * @see org.opencms.workplace.list.A_CmsListExplorerDialog#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
288     */
289    @Override
290    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
291
292        super.initWorkplaceRequestValues(settings, request);
293        if (DIALOG_CONTINUE.equals(getParamAction())) {
294            setAction(ACTION_CONTINUE);
295        }
296    }
297
298    /**
299     * The following columns are visible by default: type icon, resource name, title and last modification date.<p>
300     *
301     * Override this to set different column visibilities.<p>
302     *
303     * @see org.opencms.workplace.list.A_CmsListExplorerDialog#isColumnVisible(int)
304     */
305    @Override
306    protected boolean isColumnVisible(int colFlag) {
307
308        if (colFlag == LIST_COLUMN_TYPEICON.hashCode()) {
309            return true;
310        }
311        if (colFlag == LIST_COLUMN_NAME.hashCode()) {
312            return true;
313        }
314        if (colFlag == CmsUserSettings.FILELIST_TITLE) {
315            return true;
316        }
317        if (colFlag == CmsUserSettings.FILELIST_DATE_LASTMODIFIED) {
318            return true;
319        }
320        return false;
321    }
322
323    /**
324     * @see org.opencms.workplace.list.A_CmsListExplorerDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
325     */
326    @Override
327    protected void setColumns(CmsListMetadata metadata) {
328
329        // create column for radio button
330        CmsListColumnDefinition radioSelCol = new CmsListColumnDefinition(LIST_COLUMN_SELECT);
331        radioSelCol.setName(Messages.get().container(Messages.GUI_EXPLORER_LIST_COLS_SELECT_0));
332        radioSelCol.setWidth("20");
333        radioSelCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
334        radioSelCol.setSorteable(false);
335
336        // add item selection action to column
337        CmsListItemSelectionAction selAction = new CmsListItemSelectionAction(LIST_RACTION_SEL, null);
338        selAction.setName(Messages.get().container(Messages.GUI_EXPLORER_LIST_COLS_SELECT_HELP_0));
339        selAction.setEnabled(true);
340        selAction.setSelectedItemId(CmsUUID.getConstantUUID("nones").toString());
341        radioSelCol.addDirectAction(selAction);
342
343        // add the column at first position
344        metadata.addColumn(radioSelCol);
345
346        // add the other columns
347        super.setColumns(metadata);
348    }
349
350    /**
351     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
352     */
353    @Override
354    protected void setMultiActions(CmsListMetadata metadata) {
355
356        // no multi action present
357    }
358}