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.ui.components.categoryselect;
029
030import org.opencms.relations.CmsCategory;
031import org.opencms.ui.A_CmsUI;
032import org.opencms.ui.CmsVaadinUtils;
033import org.opencms.ui.FontOpenCms;
034import org.opencms.ui.apps.Messages;
035import org.opencms.ui.components.CmsBasicDialog;
036import org.opencms.ui.components.fileselect.I_CmsSelectionHandler;
037import org.opencms.util.CmsStringUtil;
038
039import java.util.ArrayList;
040import java.util.Collection;
041import java.util.List;
042
043import com.vaadin.v7.data.Container.Filter;
044import com.vaadin.v7.data.Item;
045import com.vaadin.v7.data.util.HierarchicalContainer;
046import com.vaadin.v7.event.FieldEvents.TextChangeEvent;
047import com.vaadin.v7.event.FieldEvents.TextChangeListener;
048import com.vaadin.ui.Button;
049import com.vaadin.ui.Button.ClickEvent;
050import com.vaadin.ui.Button.ClickListener;
051import com.vaadin.v7.ui.TextField;
052import com.vaadin.ui.UI;
053import com.vaadin.ui.themes.ValoTheme;
054
055/**
056 * The category select dialog.<p>
057 */
058public class CmsCategorySelectDialog extends CmsBasicDialog {
059
060    /** The serial version id. */
061    private static final long serialVersionUID = 247205018045790127L;
062
063    /** The category filter field. */
064    private TextField m_filter;
065
066    /** the OK button. */
067    private Button m_okButton;
068
069    /** The selection handlers. */
070    private List<I_CmsSelectionHandler<Collection<CmsCategory>>> m_selectionHandlers;
071
072    /** The category tree. */
073    private CmsCategoryTree m_tree;
074
075    /**
076     * Constructor.<p>
077     *
078     * @param contextPath the context path to read the categories from
079     */
080    public CmsCategorySelectDialog(String contextPath) {
081        m_selectionHandlers = new ArrayList<I_CmsSelectionHandler<Collection<CmsCategory>>>();
082        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
083
084        m_filter.setIcon(FontOpenCms.FILTER);
085        m_filter.setInputPrompt(
086            Messages.get().getBundle(UI.getCurrent().getLocale()).key(Messages.GUI_EXPLORER_FILTER_0));
087        m_filter.addStyleName(ValoTheme.TEXTFIELD_INLINE_ICON);
088        m_filter.setWidth("200px");
089        m_filter.addTextChangeListener(new TextChangeListener() {
090
091            private static final long serialVersionUID = 1L;
092
093            public void textChange(TextChangeEvent event) {
094
095                filterTree(event.getText());
096
097            }
098        });
099        m_tree.loadCategories(A_CmsUI.getCmsObject(), contextPath);
100        m_okButton.addClickListener(new ClickListener() {
101
102            private static final long serialVersionUID = 1L;
103
104            public void buttonClick(ClickEvent event) {
105
106                onOk();
107            }
108        });
109
110    }
111
112    /**
113     * Adds a selection handler.<p>
114     *
115     * @param selectionHandler the selection handler to add
116     */
117    public void addSelectionHandler(I_CmsSelectionHandler<Collection<CmsCategory>> selectionHandler) {
118
119        m_selectionHandlers.add(selectionHandler);
120    }
121
122    /**
123     * Removes a selection handler.<p>
124     *
125     * @param selectionHandler the selection handler to remove
126     */
127    public void removeSelectionHandler(I_CmsSelectionHandler<Collection<CmsCategory>> selectionHandler) {
128
129        m_selectionHandlers.remove(selectionHandler);
130    }
131
132    /**
133     * Sets the selected categories.<p>
134     *
135     * @param categories the categories to select
136     */
137    public void setSelectedCategories(Collection<CmsCategory> categories) {
138
139        m_tree.setSelectedCategories(categories);
140    }
141
142    /**
143     * Adds a filter to the category tree container.<p>
144     *
145     * @param filter the filter to add
146     */
147    void filterTree(String filter) {
148
149        HierarchicalContainer container = (HierarchicalContainer)m_tree.getContainerDataSource();
150        container.removeAllContainerFilters();
151        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(filter)) {
152            final String lowerCaseFilter = filter.toLowerCase();
153            container.addContainerFilter(new Filter() {
154
155                private static final long serialVersionUID = 1L;
156
157                public boolean appliesToProperty(Object propertyId) {
158
159                    return true;
160                }
161
162                public boolean passesFilter(Object itemId, Item item) throws UnsupportedOperationException {
163
164                    CmsCategory cat = (CmsCategory)itemId;
165
166                    return cat.getPath().toLowerCase().contains(lowerCaseFilter)
167                        || ((cat.getTitle() != null) && cat.getTitle().toLowerCase().contains(lowerCaseFilter));
168                }
169            });
170        }
171    }
172
173    /**
174     * On OK click.<p>
175     */
176    void onOk() {
177
178        if (!m_selectionHandlers.isEmpty()) {
179            Collection<CmsCategory> categories = m_tree.getSelectedCategories();
180            for (I_CmsSelectionHandler<Collection<CmsCategory>> handler : m_selectionHandlers) {
181                handler.onSelection(categories);
182            }
183        }
184    }
185}