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.jsp.CmsJspActionElement;
032import org.opencms.main.CmsRuntimeException;
033import org.opencms.main.OpenCms;
034import org.opencms.workplace.CmsWorkplace;
035import org.opencms.workplace.explorer.CmsExplorerTypeSettings;
036import org.opencms.workplace.explorer.Messages;
037
038import java.io.IOException;
039import java.util.ArrayList;
040import java.util.Collection;
041import java.util.Comparator;
042import java.util.Iterator;
043import java.util.List;
044import java.util.Locale;
045
046import javax.servlet.ServletException;
047import javax.servlet.http.HttpServletRequest;
048import javax.servlet.http.HttpServletResponse;
049import javax.servlet.jsp.JspException;
050import javax.servlet.jsp.PageContext;
051
052/**
053 * Super class for all dialogs needed to display a list of resource types.<p>
054 *
055 * @since 6.7.2
056 */
057public abstract class A_CmsListResourceTypeDialog extends A_CmsListDialog {
058
059    /** List independent action id constant. */
060    public static final String LIST_ACTION_SEL = "rs";
061
062    /** List column id constant. */
063    public static final String LIST_COLUMN_ICON = "nrci";
064
065    /** List column id constant. */
066    public static final String LIST_COLUMN_NAME = "nrcn";
067
068    /** List column id constant. */
069    public static final String LIST_COLUMN_SELECT = "nrcs";
070
071    /** List detail description info. */
072    public static final String LIST_DETAIL_DESCRIPTION = "dd";
073
074    /** List id constant. */
075    public static final String LIST_ID = "nrt";
076
077    /** Request parameter name for the index page resource type. */
078    public static final String PARAM_SELECTED_TYPE = "selectedtype";
079
080    /** Item comparator to ensure that special types go first. */
081    private static final I_CmsListItemComparator LIST_ITEM_COMPARATOR = new I_CmsListItemComparator() {
082
083        /**
084         * @see org.opencms.workplace.list.I_CmsListItemComparator#getComparator(java.lang.String, java.util.Locale)
085         */
086        @Override
087        public Comparator<CmsListItem> getComparator(final String columnId, final Locale locale) {
088
089            return new Comparator<CmsListItem>() {
090
091                /**
092                 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
093                 */
094                @Override
095                public int compare(CmsListItem li1, CmsListItem li2) {
096
097                    if (li1 == li2) {
098                        return 0;
099                    }
100
101                    if (li1 == null) {
102                        return -1;
103                    } else if (li2 == null) {
104                        return 1;
105                    }
106
107                    CmsExplorerTypeSettings set1 = OpenCms.getWorkplaceManager().getExplorerTypeSetting(li1.getId());
108                    CmsExplorerTypeSettings set2 = OpenCms.getWorkplaceManager().getExplorerTypeSetting(li2.getId());
109                    if (set1 == null) {
110                        return -1;
111                    } else if (set2 == null) {
112                        return 1;
113                    }
114                    return set1.compareTo(set2);
115                }
116            };
117        }
118    };
119
120    /** Parameter which contains the selected resource type. */
121    private String m_paramSelectedType;
122
123    /**
124     * Public constructor with JSP action element.<p>
125     *
126     * @param jsp an initialized JSP action element
127     */
128    public A_CmsListResourceTypeDialog(CmsJspActionElement jsp) {
129
130        this(
131            jsp,
132            LIST_ID,
133            Messages.get().container(Messages.GUI_NEWRESOURCE_LIST_SELECT_NAME_0),
134            null,
135            CmsListOrderEnum.ORDER_ASCENDING,
136            null);
137    }
138
139    /**
140     * Public constructor.<p>
141     *
142     * @param jsp an initialized JSP action element
143     * @param listId the id of the displayed list
144     * @param listName the name of the list
145     * @param sortedColId the a priory sorted column
146     * @param sortOrder the order of the sorted column
147     * @param searchableColId the column to search into
148     */
149    public A_CmsListResourceTypeDialog(
150        CmsJspActionElement jsp,
151        String listId,
152        CmsMessageContainer listName,
153        String sortedColId,
154        CmsListOrderEnum sortOrder,
155        String searchableColId) {
156
157        super(jsp, listId, listName, sortedColId, sortOrder, searchableColId);
158
159        // set the style to show common workplace dialog layout
160        setParamStyle("");
161
162        // prevent paging, usually there are only few model files
163        getList().setMaxItemsPerPage(Integer.MAX_VALUE);
164
165        // hide print button
166        getList().getMetadata().getIndependentAction(CmsListPrintIAction.LIST_ACTION_ID).setVisible(false);
167
168        // suppress the box around the list
169        getList().setBoxed(false);
170
171        // hide title of the list
172        getList().setShowTitle(false);
173    }
174
175    /**
176     * Public constructor with JSP variables.<p>
177     *
178     * @param context the JSP page context
179     * @param req the JSP request
180     * @param res the JSP response
181     */
182    public A_CmsListResourceTypeDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
183
184        this(new CmsJspActionElement(context, req, res));
185    }
186
187    /**
188     * @see org.opencms.workplace.list.A_CmsListDialog#actionDialog()
189     */
190    @Override
191    public void actionDialog() throws JspException, ServletException, IOException {
192
193        // set selected type
194        I_CmsListDirectAction action = getList().getMetadata().getColumnDefinition(LIST_COLUMN_SELECT).getDirectAction(
195            LIST_ACTION_SEL);
196        if (action != null) {
197            String selected = getParamSelectedType();
198            if (selected != null) {
199                ((CmsListItemSelectionAction)action).setSelectedItemId(selected);
200            }
201        }
202
203        super.actionDialog();
204    }
205
206    /**
207     * Builds a default button row with a continue and cancel button.<p>
208     *
209     * Override this to have special buttons for your dialog.<p>
210     *
211     * @return the button row
212     */
213    public String dialogButtons() {
214
215        return dialogButtons(
216            new int[] {BUTTON_CONTINUE, BUTTON_CANCEL},
217            new String[] {
218                " onclick=\"submitAction('"
219                    + DIALOG_CONTINUE
220                    + "', form, '"
221                    + getListId()
222                    + "-form');\" id=\"nextButton\"",
223                " onclick=\"submitAction('" + DIALOG_CANCEL + "', form, '" + getListId() + "-form');\""});
224    }
225
226    /**
227     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
228     */
229    @Override
230    public void executeListMultiActions() throws CmsRuntimeException {
231
232        // noop
233    }
234
235    /**
236     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
237     */
238    @Override
239    public void executeListSingleActions() throws CmsRuntimeException {
240
241        // noop
242    }
243
244    /**
245     * Returns the paramSelectedType.<p>
246     *
247     * @return the paramSelectedType
248     */
249    public String getParamSelectedType() {
250
251        return m_paramSelectedType;
252    }
253
254    /**
255     * @see org.opencms.workplace.CmsWorkplace#paramsAsHidden()
256     */
257    @Override
258    public String paramsAsHidden() {
259
260        return paramsAsHidden(new ArrayList<String>());
261    }
262
263    /**
264     * @see org.opencms.workplace.CmsWorkplace#paramsAsHidden(java.util.Collection)
265     */
266    @Override
267    public String paramsAsHidden(Collection<String> excludes) {
268
269        excludes.add(PARAM_SELECTED_TYPE);
270        excludes.add(PARAM_SORT_COL);
271        excludes.add(LIST_INDEPENDENT_ACTION);
272        return super.paramsAsHidden(excludes);
273    }
274
275    /**
276     * Sets the paramSelectedType.<p>
277     *
278     * @param paramSelectedType the paramSelectedType to set
279     */
280    public void setParamSelectedType(String paramSelectedType) {
281
282        m_paramSelectedType = paramSelectedType;
283    }
284
285    /**
286     * Returns the html code to add directly before the list inside the form element.<p>
287     *
288     * @return the html code to add directly before the list inside the form element
289     */
290    protected String customHtmlBeforeList() {
291
292        return "";
293    }
294
295    /**
296     * @see org.opencms.workplace.list.A_CmsListDialog#customHtmlEnd()
297     */
298    @Override
299    protected String customHtmlEnd() {
300
301        StringBuffer result = new StringBuffer(256);
302
303        result.append(dialogSpacer());
304        result.append(dialogButtons());
305        result.append("</form>\n");
306
307        return result.toString();
308    }
309
310    /**
311     * @see org.opencms.workplace.list.A_CmsListDialog#customHtmlStart()
312     */
313    @Override
314    protected String customHtmlStart() {
315
316        StringBuffer result = new StringBuffer(256);
317
318        result.append("<script  src='");
319        result.append(CmsWorkplace.getSkinUri());
320        result.append("admin/javascript/general.js'></script>\n");
321        result.append("<script  src='");
322        result.append(CmsWorkplace.getSkinUri());
323        result.append("editors/xmlcontent/help.js'></script>\n");
324
325        //        result.append("<style type='text/css'>\n");
326        //        result.append(".evenrowbg {\n");
327        //        result.append("\tbackground-color: InfoBackground;\n");
328        //        result.append("\tcolor: InfoText;\n");
329        //        result.append("}\n");
330        //        result.append("</style>");
331
332        result.append("<form name='");
333        result.append(getList().getId());
334        result.append("-form' action='");
335        result.append(getDialogRealUri());
336        result.append("' method='post' class='nomargin'");
337        if (getList().getMetadata().isSearchable()) {
338            result.append(" onsubmit=\"listSearchAction('");
339            result.append(getList().getId());
340            result.append("', '");
341            result.append(getList().getMetadata().getSearchAction().getId());
342            result.append("', '");
343            result.append(getList().getMetadata().getSearchAction().getConfirmationMessage().key(getLocale()));
344            result.append("');\"");
345        }
346        result.append(">\n");
347
348        result.append(paramsAsHidden());
349        result.append("\n");
350
351        result.append("<input type=\"hidden\" name=\"" + PARAM_FRAMENAME + "\" value=\"\">\n");
352        result.append("<input type=\"hidden\" name=\"" + PARAM_LIST_ACTION + "\" value=\"\">\n");
353        result.append("<input type=\"hidden\" name=\"" + PARAM_SORT_COL + "\" value=\"\">\n");
354
355        return result.toString();
356    }
357
358    /**
359     * @see org.opencms.workplace.list.A_CmsListDialog#defaultActionHtmlContent()
360     */
361    @Override
362    protected String defaultActionHtmlContent() {
363
364        StringBuffer result = new StringBuffer(2048);
365
366        result.append("<!-- start before list -->\n");
367        result.append(customHtmlBeforeList());
368        result.append("<!-- end before list -->\n");
369
370        result.append(dialogBlockStart(key(getList().getName().getKey())));
371        result.append(dialogWhiteBoxStart());
372
373        // start scrollbox
374        // result.append("<div style=\"overflow: auto; height: 150px; \">");
375
376        getList().setWp(this);
377        result.append(getList().listHtml());
378
379        // end scrollbox
380        // result.append("</div>");
381
382        result.append(dialogWhiteBoxEnd());
383        result.append(dialogBlockEnd());
384
385        return result.toString();
386    }
387
388    /**
389     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
390     */
391    @Override
392    protected void fillDetails(String detailId) {
393
394        // get listed resource types
395        List<CmsListItem> types = getList().getAllContent();
396        Iterator<CmsListItem> iter = types.iterator();
397        while (iter.hasNext()) {
398            CmsListItem item = iter.next();
399            String resType = item.getId();
400            StringBuffer description = new StringBuffer();
401            if (detailId.equals(LIST_DETAIL_DESCRIPTION)) {
402                // set description detail
403                try {
404                    try {
405                        int resTypeId = Integer.parseInt(resType);
406                        resType = OpenCms.getResourceManager().getResourceType(resTypeId).getTypeName();
407                    } catch (NumberFormatException e) {
408                        // ignore, resource type was already the type name
409                    }
410                    // get settings for resource type
411                    CmsExplorerTypeSettings set = OpenCms.getWorkplaceManager().getExplorerTypeSetting(resType);
412                    if (set != null) {
413                        // add the description text from resource bundle
414                        description.append(key(set.getInfo()));
415                    }
416
417                } catch (Exception e) {
418                    // ignore it, because the dummy file throws an exception in any case
419                }
420            } else {
421                continue;
422            }
423            item.set(detailId, description);
424        }
425    }
426
427    /**
428     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
429     */
430    @Override
431    protected void setColumns(CmsListMetadata metadata) {
432
433        // add column for radio button
434        CmsListColumnDefinition radioSelCol = new CmsListColumnDefinition(LIST_COLUMN_SELECT);
435        radioSelCol.setName(Messages.get().container(Messages.GUI_NEWRESOURCE_LIST_COLS_SELECT_0));
436        radioSelCol.setWidth("20");
437        radioSelCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
438        radioSelCol.setSorteable(false);
439
440        // add item selection action to column
441        CmsListItemSelectionCustomAction selAction = new CmsListItemSelectionCustomAction(
442            LIST_ACTION_SEL,
443            PARAM_SELECTED_TYPE);
444        selAction.setName(Messages.get().container(Messages.GUI_NEWRESOURCE_LIST_SELECT_NAME_0));
445        selAction.setEnabled(true);
446        radioSelCol.addDirectAction(selAction);
447        metadata.addColumn(radioSelCol);
448
449        // add column icon
450        CmsListColumnDefinition iconCol = new CmsListColumnDefinition(LIST_COLUMN_ICON);
451        iconCol.setName(Messages.get().container(Messages.GUI_NEWRESOURCE_LIST_COLS_ICON_0));
452        iconCol.setWidth("20");
453        iconCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
454        iconCol.setSorteable(true);
455        iconCol.setListItemComparator(LIST_ITEM_COMPARATOR);
456        metadata.addColumn(iconCol);
457
458        // add column name
459        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
460        nameCol.setName(Messages.get().container(Messages.GUI_NEWRESOURCE_LIST_COLS_NAME_0));
461        metadata.addColumn(nameCol);
462    }
463
464    /**
465     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
466     */
467    @Override
468    protected void setIndependentActions(CmsListMetadata metadata) {
469
470        // create list item detail: description
471        CmsListItemDetails resourceTypeDescription = new CmsListItemDetails(LIST_DETAIL_DESCRIPTION);
472        resourceTypeDescription.setAtColumn(LIST_COLUMN_NAME);
473        resourceTypeDescription.setVisible(false);
474        resourceTypeDescription.setShowActionName(
475            Messages.get().container(Messages.GUI_NEWRESOURCE_DETAIL_SHOW_DESCRIPTION_NAME_0));
476        resourceTypeDescription.setShowActionHelpText(
477            Messages.get().container(Messages.GUI_NEWRESOURCE_DETAIL_SHOW_DESCRIPTION_HELP_0));
478        resourceTypeDescription.setHideActionName(
479            Messages.get().container(Messages.GUI_NEWRESOURCE_DETAIL_HIDE_DESCRIPTION_NAME_0));
480        resourceTypeDescription.setHideActionHelpText(
481            Messages.get().container(Messages.GUI_NEWRESOURCE_DETAIL_HIDE_DESCRIPTION_HELP_0));
482
483        metadata.addItemDetails(resourceTypeDescription);
484    }
485
486    /**
487     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
488     */
489    @Override
490    protected void setMultiActions(CmsListMetadata metadata) {
491
492        // noop
493    }
494
495}