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.apps.user;
029
030import org.opencms.ui.FontOpenCms;
031import org.opencms.ui.contextmenu.CmsContextMenu;
032import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode;
033import org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry;
034import org.opencms.util.CmsStringUtil;
035
036import java.util.ArrayList;
037import java.util.Collections;
038import java.util.List;
039import java.util.Locale;
040import java.util.Set;
041
042import com.vaadin.shared.MouseEventDetails.MouseButton;
043import com.vaadin.ui.Button;
044import com.vaadin.ui.Button.ClickEvent;
045import com.vaadin.ui.Component;
046import com.vaadin.ui.themes.ValoTheme;
047import com.vaadin.v7.data.util.IndexedContainer;
048import com.vaadin.v7.data.util.filter.Or;
049import com.vaadin.v7.data.util.filter.SimpleStringFilter;
050import com.vaadin.v7.event.ItemClickEvent;
051import com.vaadin.v7.event.ItemClickEvent.ItemClickListener;
052import com.vaadin.v7.ui.Table;
053
054/**
055 * Table showing available items from A_CmsEditUserGroupRoleDialog.<p>
056 */
057public class CmsAvailableRoleOrPrincipalTable extends Table {
058
059    /**
060     * Remove function.<p>
061     */
062    class EntryAdd implements I_CmsSimpleContextMenuEntry<Set<String>> {
063
064        /**
065         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
066         */
067        public void executeAction(Set<String> context) {
068
069            m_dialog.addItem(context);
070            m_dialog.init();
071
072        }
073
074        /**
075         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
076         */
077        public String getTitle(Locale locale) {
078
079            return m_dialog.getAddActionCaption();
080        }
081
082        /**
083         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
084         */
085        public CmsMenuItemVisibilityMode getVisibility(Set<String> context) {
086
087            return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
088        }
089
090    }
091
092    /**vaadin serial id. */
093    private static final long serialVersionUID = -2348361817972942946L;
094
095    /**Icon property. */
096    private static final String PROP_ICON = "icon";
097
098    /**Name property. */
099    private static final String PROP_NAME = "name";
100
101    /**Add column. */
102    private static final String PROP_ADD = "add";
103
104    /** The context menu. */
105    CmsContextMenu m_menu;
106
107    /**Indexed Container. */
108    IndexedContainer m_container;
109
110    /**Dialog which holds the table. */
111    A_CmsEditUserGroupRoleDialog m_dialog;
112
113    /** The available menu entries. */
114    private List<I_CmsSimpleContextMenuEntry<Set<String>>> m_menuEntries;
115
116    /**
117     * public constructor.<p>
118     *
119     * @param dialog which displays the table
120     */
121    @SuppressWarnings("deprecation")
122    public CmsAvailableRoleOrPrincipalTable(A_CmsEditUserGroupRoleDialog dialog) {
123
124        m_dialog = dialog;
125        setSizeFull();
126
127        setHeight("100%");
128        m_container = dialog.getAvailableItemsIndexedContainer(PROP_NAME, PROP_ICON);
129        m_container.addContainerProperty(PROP_ADD, com.vaadin.ui.Button.class, null);
130        setContainerDataSource(m_container);
131        sort(new Object[] {PROP_NAME}, new boolean[] {true});
132        setItemIconPropertyId(PROP_ICON);
133        setColumnWidth(null, 40);
134        setRowHeaderMode(RowHeaderMode.ICON_ONLY);
135        setSelectable(true);
136        setMultiSelect(true);
137        setColumnHeader(PROP_NAME, dialog.getItemName());
138        setColumnHeader(PROP_ADD, "");
139
140        setColumnWidth(PROP_ADD, 40);
141        if (m_dialog.getFurtherColumnId() != null) {
142            setVisibleColumns(PROP_ADD, PROP_NAME, m_dialog.getFurtherColumnId());
143        } else {
144            setVisibleColumns(PROP_ADD, PROP_NAME);
145        }
146
147        m_menu = new CmsContextMenu();
148        m_menu.setAsTableContextMenu(this);
149
150        addItemClickListener(new ItemClickListener() {
151
152            private static final long serialVersionUID = 4807195510202231174L;
153
154            public void itemClick(ItemClickEvent event) {
155
156                if (!event.isCtrlKey() && !event.isShiftKey()) {
157
158                    changeValueIfNotMultiSelect(event.getItemId());
159
160                    if (event.getButton().equals(MouseButton.RIGHT) || (event.getPropertyId() == null)) {
161                        m_menu.setEntries(getMenuEntries(), m_dialog.getStringSetValue((Set<Object>)getValue()));
162                        m_menu.openForTable(
163                            event,
164                            event.getItemId(),
165                            event.getPropertyId(),
166                            CmsAvailableRoleOrPrincipalTable.this);
167                    }
168
169                }
170            }
171
172        });
173        setItemDescriptionGenerator(new ItemDescriptionGenerator() {
174
175            private static final long serialVersionUID = 7367011213487089661L;
176
177            public String generateDescription(Component source, Object itemId, Object propertyId) {
178
179                return m_dialog.getDescriptionForItemId(itemId);
180            }
181        });
182        addGeneratedColumn(PROP_ADD, new ColumnGenerator() {
183
184            private static final long serialVersionUID = -778841579899729529L;
185
186            public Object generateCell(Table source, final Object itemId, Object columnId) {
187
188                Button button = new Button(FontOpenCms.CIRCLE_PLUS);
189                button.addStyleName(ValoTheme.BUTTON_BORDERLESS);
190                button.addStyleName(ValoTheme.BUTTON_ICON_ONLY);
191                button.addClickListener(new Button.ClickListener() {
192
193                    private static final long serialVersionUID = -44051469061574153L;
194
195                    public void buttonClick(ClickEvent event) {
196
197                        m_dialog.addItem(m_dialog.getStringSetValue(Collections.singleton(itemId)));
198                        m_dialog.init();
199
200                    }
201                });
202                //button.set
203                return button;
204            }
205
206        });
207    }
208
209    /**
210     * Filters the table according to given search string.<p>
211     *
212     * @param search string to be looked for.
213     */
214    public void filterTable(String search) {
215
216        m_container.removeAllContainerFilters();
217        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(search)) {
218            if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_dialog.getFurtherColumnId())) {
219                m_container.addContainerFilter(new Or(new SimpleStringFilter(PROP_NAME, search, true, false)));
220            } else {
221                m_container.addContainerFilter(
222                    new Or(
223                        new SimpleStringFilter(PROP_NAME, search, true, false),
224                        new SimpleStringFilter(m_dialog.getFurtherColumnId(), search, true, false)));
225            }
226
227        }
228        if ((getValue() != null) & !((Set<String>)getValue()).isEmpty()) {
229            setCurrentPageFirstItemId(((Set<String>)getValue()).iterator().next());
230        }
231    }
232
233    /**
234     * Checks value of table and sets it new if needed:<p>
235     * if multiselect: new itemId is in current Value? -> no change of value<p>
236     * no multiselect and multiselect, but new item not selected before: set value to new item<p>
237     *
238     * @param itemId if of clicked item
239     */
240    void changeValueIfNotMultiSelect(Object itemId) {
241
242        @SuppressWarnings("unchecked")
243        Set<String> value = (Set<String>)getValue();
244        if (value == null) {
245            select(itemId);
246        } else if (!value.contains(itemId)) {
247            setValue(null);
248            select(itemId);
249        }
250    }
251
252    /**
253     * Returns the available menu entries.<p>
254     *
255     * @return the menu entries
256     */
257    List<I_CmsSimpleContextMenuEntry<Set<String>>> getMenuEntries() {
258
259        if (m_menuEntries == null) {
260            m_menuEntries = new ArrayList<I_CmsSimpleContextMenuEntry<Set<String>>>();
261            m_menuEntries.add(new EntryAdd());
262        }
263        return m_menuEntries;
264    }
265}