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, 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.ade.galleries.client.ui;
029
030import org.opencms.ade.galleries.client.CmsTypesTabHandler;
031import org.opencms.ade.galleries.client.Messages;
032import org.opencms.ade.galleries.client.ui.css.I_CmsLayoutBundle;
033import org.opencms.ade.galleries.shared.CmsGallerySearchBean;
034import org.opencms.ade.galleries.shared.CmsResourceTypeBean;
035import org.opencms.ade.galleries.shared.CmsResourceTypeBean.TypeVisibility;
036import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.GalleryTabId;
037import org.opencms.gwt.client.CmsCoreProvider;
038import org.opencms.gwt.client.dnd.CmsDNDHandler;
039import org.opencms.gwt.client.ui.CmsListItem;
040import org.opencms.gwt.client.ui.CmsListItemWidget;
041import org.opencms.gwt.client.ui.input.CmsCheckBox;
042import org.opencms.gwt.client.util.CmsStyleVariable;
043import org.opencms.util.CmsStringUtil;
044
045import java.util.ArrayList;
046import java.util.Collection;
047import java.util.HashMap;
048import java.util.LinkedHashMap;
049import java.util.List;
050import java.util.Map;
051
052import com.google.common.collect.Lists;
053import com.google.gwt.event.logical.shared.ValueChangeEvent;
054import com.google.gwt.event.logical.shared.ValueChangeHandler;
055import com.google.gwt.user.client.ui.FlowPanel;
056import com.google.gwt.user.client.ui.Widget;
057
058/**
059 * Provides the widget for the types tab.<p>
060 *
061 * It displays the available types in the given sort order.
062 *
063 * @since 8.0.
064 */
065public class CmsTypesTab extends A_CmsListTab {
066
067    /**
068     * Handles the change of the item selection.<p>
069     */
070    private class SelectionHandler extends A_SelectionHandler {
071
072        /** The resource type (name/id?) as id for the selected type. */
073        private String m_resourceType;
074
075        /**
076         * Constructor.<p>
077         *
078         * @param resourceType as id(name) for the selected type
079         * @param checkBox the reference to the checkbox
080         */
081        public SelectionHandler(String resourceType, CmsCheckBox checkBox) {
082
083            super(checkBox);
084            m_resourceType = resourceType;
085            m_selectionHandlers.add(this);
086        }
087
088        /**
089         * @see org.opencms.ade.galleries.client.ui.A_CmsListTab.A_SelectionHandler#onSelectionChange()
090         */
091        @Override
092        protected void onSelectionChange() {
093
094            if (getCheckBox().isChecked()) {
095                getTabHandler().selectType(m_resourceType);
096            } else {
097                getTabHandler().deselectType(m_resourceType);
098            }
099        }
100
101        /**
102         * @see org.opencms.ade.galleries.client.ui.A_CmsListTab.A_SelectionHandler#selectBeforeGoingToResultTab()
103         */
104        @Override
105        protected void selectBeforeGoingToResultTab() {
106
107            for (SelectionHandler otherHandler : m_selectionHandlers) {
108                if ((otherHandler != this)
109                    && (otherHandler.getCheckBox() != null)
110                    && otherHandler.getCheckBox().isChecked()) {
111                    otherHandler.getCheckBox().setChecked(false);
112                    otherHandler.onSelectionChange();
113                }
114            }
115            getCheckBox().setChecked(true);
116            onSelectionChange();
117        }
118    }
119
120    /** The list of selection handlers. */
121    List<SelectionHandler> m_selectionHandlers = Lists.newArrayList();
122
123    /** Style variable to control whether the full type list should be shown. */
124    CmsStyleVariable m_typeListMode;
125
126    /** Custom widget to display in the top bar of the types tab. */
127    private Widget m_additionalTypeTabControl;
128
129    /** The reference to the drag handler for the list elements. */
130    private CmsDNDHandler m_dndHandler;
131
132    /** The reference to the handler of this tab. */
133    private CmsTypesTabHandler m_tabHandler;
134
135    /** The switch to enable/disable the full type list. */
136    private Widget m_typeModeToggle;
137
138    /** Map of type beans to type name. */
139    private Map<String, CmsResourceTypeBean> m_types;
140
141    /**
142     * Constructor.<p>
143     *
144     * @param tabHandler the tab handler
145     * @param dndHandler the drag and drop handler
146     * @param additionalControl an additional custom widget that can be displayed in the top bar
147     */
148    public CmsTypesTab(CmsTypesTabHandler tabHandler, CmsDNDHandler dndHandler, Widget additionalControl) {
149
150        super(GalleryTabId.cms_tab_types);
151        m_tabHandler = tabHandler;
152        m_dndHandler = dndHandler;
153        m_additionalTypeTabControl = additionalControl;
154        init();
155    }
156
157    /**
158     * Fill the content of the types tab panel.<p>
159     *
160     * @param typeInfos the type info beans
161     * @param selectedTypes the list of types to select
162     */
163    public void fillContent(List<CmsResourceTypeBean> typeInfos, List<String> selectedTypes) {
164
165        if (m_types == null) {
166            m_types = new HashMap<String, CmsResourceTypeBean>();
167        }
168        clearList();
169        m_types.clear();
170        for (CmsResourceTypeBean typeBean : typeInfos) {
171            m_types.put(typeBean.getType(), typeBean);
172            CmsListItemWidget listItemWidget;
173            listItemWidget = new CmsListItemWidget(typeBean);
174            listItemWidget.setUnselectable();
175            CmsCheckBox checkBox = new CmsCheckBox();
176            CmsListItem listItem = new CmsListItem(checkBox, listItemWidget);
177            if (typeBean.isDeactivated()) {
178                if (CmsCoreProvider.get().isHideDisabledGalleryTypes()) {
179                    continue;
180                } else {
181                    checkBox.disable("");
182                    listItem.addStyleName(
183                        org.opencms.gwt.client.ui.css.I_CmsLayoutBundle.INSTANCE.listItemWidgetCss().expired());
184                }
185            } else {
186                SelectionHandler selectionHandler = new SelectionHandler(typeBean.getType(), checkBox);
187                checkBox.addClickHandler(selectionHandler);
188                listItemWidget.addClickHandler(selectionHandler);
189                if ((selectedTypes != null) && selectedTypes.contains(typeBean.getType())) {
190                    checkBox.setChecked(true);
191                }
192                listItemWidget.addButton(createSelectButton(selectionHandler));
193
194                if (typeBean.isCreatableType() && (m_dndHandler != null)) {
195                    listItem.initMoveHandle(m_dndHandler, true);
196                    listItem.getMoveHandle().setTitle(Messages.get().key(Messages.GUI_TAB_TYPES_CREATE_NEW_0));
197                }
198            }
199            listItem.setId(typeBean.getType());
200
201            if (typeBean.getVisibility() == TypeVisibility.showOptional) {
202                listItem.addStyleName(I_CmsLayoutBundle.INSTANCE.galleryDialogCss().shouldOnlyShowInFullTypeList());
203            }
204            addWidgetToList(listItem);
205        }
206        updateTypeModeToggle();
207    }
208
209    /**
210     * @see org.opencms.ade.galleries.client.ui.A_CmsTab#getParamPanels(org.opencms.ade.galleries.shared.CmsGallerySearchBean)
211     */
212    @Override
213    public List<CmsSearchParamPanel> getParamPanels(CmsGallerySearchBean searchObj) {
214
215        List<CmsSearchParamPanel> result = new ArrayList<CmsSearchParamPanel>();
216        for (String type : searchObj.getTypes()) {
217            CmsResourceTypeBean typeBean = m_types.get(type);
218            String title = type;
219            if (typeBean != null) {
220                title = typeBean.getTitle();
221                if (CmsStringUtil.isEmptyOrWhitespaceOnly(title)) {
222                    title = typeBean.getType();
223                }
224            }
225            CmsSearchParamPanel panel = new CmsSearchParamPanel(
226                Messages.get().key(Messages.GUI_PARAMS_LABEL_TYPES_0),
227                this);
228            panel.setContent(title, type);
229            result.add(panel);
230        }
231        return result;
232    }
233
234    /**
235     * Deselect the types  in the types list.<p>
236     *
237     * @param types the categories to deselect
238     */
239    public void uncheckTypes(Collection<String> types) {
240
241        for (String type : types) {
242            CmsListItem item = (CmsListItem)m_scrollList.getItem(type);
243            if (item != null) {
244                item.getCheckBox().setChecked(false);
245            }
246        }
247    }
248
249    /**
250     * Updates the types list.<p>
251     *
252     * @param types the new types list
253     * @param selectedTypes the list of types to select
254     */
255    public void updateContent(List<CmsResourceTypeBean> types, List<String> selectedTypes) {
256
257        clearList();
258        fillContent(types, selectedTypes);
259    }
260
261    /**
262     * @see org.opencms.ade.galleries.client.ui.A_CmsListTab#getSortList()
263     */
264    @Override
265    protected LinkedHashMap<String, String> getSortList() {
266
267        return null;
268
269    }
270
271    /**
272     * @see org.opencms.ade.galleries.client.ui.A_CmsListTab#getTabHandler()
273     */
274    @Override
275    protected CmsTypesTabHandler getTabHandler() {
276
277        return m_tabHandler;
278    }
279
280    /**
281     * @see org.opencms.ade.galleries.client.ui.A_CmsListTab#hasQuickSearch()
282     */
283    @Override
284    protected boolean hasQuickSearch() {
285
286        // quick filter not available for this tab
287        return true;
288    }
289
290    /**
291     * @see org.opencms.ade.galleries.client.ui.A_CmsListTab#init()
292     */
293    @Override
294    protected void init() {
295
296        super.init();
297
298        m_typeListMode = new CmsStyleVariable(this);
299        m_typeListMode.setValue(I_CmsLayoutBundle.INSTANCE.galleryDialogCss().typesImportant());
300        final CmsCheckBox typeToggle = new CmsCheckBox(Messages.get().key(Messages.GUI_SHOW_SYSTEM_TYPES_0));
301        FlowPanel toggleContainer = new FlowPanel();
302        toggleContainer.add(typeToggle);
303        toggleContainer.addStyleName(I_CmsLayoutBundle.INSTANCE.galleryDialogCss().typeModeSwitch());
304        m_options.add(toggleContainer);
305        if (m_additionalTypeTabControl != null) {
306            m_options.add(m_additionalTypeTabControl);
307        }
308        typeToggle.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
309
310            public void onValueChange(ValueChangeEvent<Boolean> event) {
311
312                boolean value = event.getValue().booleanValue();
313                setShowAllTypes(value);
314                m_list.onResizeDescendant();
315            }
316        });
317        m_typeModeToggle = toggleContainer;
318        updateTypeModeToggle();
319    }
320
321    /**
322     * Enables/disables full type list mode.<p>
323     *
324     * @param showAllTypes true if all types should be shown
325     */
326    protected void setShowAllTypes(boolean showAllTypes) {
327
328        m_typeListMode.setValue(showAllTypes ? null : I_CmsLayoutBundle.INSTANCE.galleryDialogCss().typesImportant());
329        m_tabHandler.updateSize();
330    }
331
332    /**
333     * Updates the type mode switch.<p>
334     */
335    protected void updateTypeModeToggle() {
336
337        m_typeModeToggle.setVisible(false);
338        if (m_types != null) {
339            for (CmsResourceTypeBean type : m_types.values()) {
340                if (type.getVisibility() == TypeVisibility.showOptional) {
341                    m_typeModeToggle.setVisible(true);
342                    return;
343
344                }
345            }
346        }
347    }
348}