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.file.CmsGroup;
031import org.opencms.file.CmsObject;
032import org.opencms.main.CmsLog;
033import org.opencms.security.CmsPrincipal;
034import org.opencms.ui.CmsVaadinUtils;
035import org.opencms.ui.FontOpenCms;
036import org.opencms.ui.apps.Messages;
037import org.opencms.ui.components.OpenCmsTheme;
038import org.opencms.ui.contextmenu.CmsContextMenu;
039import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode;
040import org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry;
041
042import java.util.ArrayList;
043import java.util.Collections;
044import java.util.List;
045import java.util.Locale;
046import java.util.Set;
047
048import org.apache.commons.logging.Log;
049
050import com.vaadin.shared.MouseEventDetails.MouseButton;
051import com.vaadin.ui.Button;
052import com.vaadin.ui.Button.ClickEvent;
053import com.vaadin.ui.Component;
054import com.vaadin.ui.themes.ValoTheme;
055import com.vaadin.v7.data.Item;
056import com.vaadin.v7.data.util.IndexedContainer;
057import com.vaadin.v7.event.ItemClickEvent;
058import com.vaadin.v7.event.ItemClickEvent.ItemClickListener;
059import com.vaadin.v7.ui.Table;
060
061/**
062 * Class for the table to view and edit groups of a given user.<p>
063 */
064public class CmsCurrentRoleOrPrincipalTable extends Table {
065
066    /**
067     * Remove function.<p>
068     */
069    class EntryRemove implements I_CmsSimpleContextMenuEntry<Set<String>> {
070
071        /**
072         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
073         */
074        public void executeAction(Set<String> context) {
075
076            m_dialog.removeItem(context);
077            m_dialog.init();
078        }
079
080        /**
081         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
082         */
083        public String getTitle(Locale locale) {
084
085            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_EDIT_REMOVE_0);
086        }
087
088        /**
089         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
090         */
091        @SuppressWarnings("synthetic-access")
092        public CmsMenuItemVisibilityMode getVisibility(Set<String> context) {
093
094            List<Item> itemsToCheck = new ArrayList<>();
095            for (Object groupObj : m_container.getItemIds()) {
096                if (groupObj instanceof CmsGroup) {
097                    CmsGroup group = (CmsGroup)groupObj;
098                    if (context.contains(group.getName())) {
099                        itemsToCheck.add(m_container.getItem(group));
100                    }
101                }
102            }
103            for (Item item : itemsToCheck) {
104                if (!item.getItemProperty(PROP_STATUS).getValue().equals(Boolean.TRUE)) {
105                    return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE;
106                }
107            }
108            return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
109        }
110
111    }
112
113    /**vaadin serial id. */
114    private static final long serialVersionUID = 2443401032626693747L;
115
116    /**Name table column. */
117    private static final String PROP_NAME = "name";
118
119    /**Icon table column.*/
120    private static final String PROP_ICON = "icon";
121
122    /**Status column. */
123    private static final String PROP_STATUS = "status";
124
125    /**Add column. */
126    private static final String PROP_REMOVE = "remove";
127
128    /** The log object for this class. */
129    protected static final Log LOG = CmsLog.getLog(CmsCurrentRoleOrPrincipalTable.class);
130
131    /**CmsObject.*/
132    CmsObject m_cms;
133
134    /**Dialog. */
135    A_CmsEditUserGroupRoleDialog m_dialog;
136
137    /**CmsUser to be edited.*/
138    CmsPrincipal m_principal;
139
140    /** The context menu. */
141    CmsContextMenu m_menu;
142
143    /**Container. */
144    private IndexedContainer m_container;
145
146    /** The available menu entries. */
147    private List<I_CmsSimpleContextMenuEntry<Set<String>>> m_menuEntries;
148
149    /**
150     * public constructor.<p>
151     *
152     * @param dialog dialog
153     * @param cms CmsObject
154     * @param principal CmsPrincipal
155     */
156    public CmsCurrentRoleOrPrincipalTable(A_CmsEditUserGroupRoleDialog dialog, CmsObject cms, CmsPrincipal principal) {
157
158        m_cms = cms;
159        m_dialog = dialog;
160        m_principal = principal;
161        m_menu = new CmsContextMenu();
162        m_menu.setAsTableContextMenu(this);
163
164        setSizeFull();
165        setHeight("100%");
166        m_container = m_dialog.getItemsOfUserIndexedContainer(PROP_NAME, PROP_ICON, PROP_STATUS);
167
168        m_container.addContainerProperty(PROP_REMOVE, com.vaadin.ui.Button.class, null);
169        setContainerDataSource(m_container);
170        sort(new Object[] {PROP_NAME}, new boolean[] {true});
171        setItemIconPropertyId(PROP_ICON);
172        setColumnWidth(null, 40);
173        setRowHeaderMode(RowHeaderMode.ICON_ONLY);
174
175        setSelectable(true);
176        setMultiSelect(true);
177
178        setColumnWidth(PROP_REMOVE, 40);
179        setColumnHeader(PROP_REMOVE, "");
180
181        if (m_dialog.getFurtherColumnId() != null) {
182            setVisibleColumns(PROP_REMOVE, PROP_NAME, m_dialog.getFurtherColumnId());
183        } else {
184            setVisibleColumns(PROP_REMOVE, PROP_NAME);
185        }
186        setColumnHeader(PROP_NAME, m_dialog.getItemName());
187
188        addItemClickListener(new ItemClickListener() {
189
190            private static final long serialVersionUID = 4807195510202231174L;
191
192            @SuppressWarnings("unchecked")
193            public void itemClick(ItemClickEvent event) {
194
195                if (!event.isCtrlKey()
196                    && !event.isShiftKey()
197                    && ((Boolean)getItem(event.getItemId()).getItemProperty(PROP_STATUS).getValue()).booleanValue()) {
198
199                    changeValueIfNotMultiSelect(event.getItemId());
200
201                    if (event.getButton().equals(MouseButton.RIGHT) || (event.getPropertyId() == null)) {
202                        m_menu.setEntries(getMenuEntries(), m_dialog.getStringSetValue((Set<Object>)getValue()));
203                        m_menu.openForTable(
204                            event,
205                            event.getItemId(),
206                            event.getPropertyId(),
207                            CmsCurrentRoleOrPrincipalTable.this);
208                    }
209                }
210            }
211        });
212
213        setCellStyleGenerator(new CellStyleGenerator() {
214
215            private static final long serialVersionUID = 4685652851810828147L;
216
217            public String getStyle(Table source, Object itemId, Object propertyId) {
218
219                if (!((Boolean)source.getItem(itemId).getItemProperty(PROP_STATUS).getValue()).booleanValue()) {
220                    return " " + OpenCmsTheme.TABLE_CELL_DISABLED;
221                }
222                return null;
223            }
224
225        });
226
227        setItemDescriptionGenerator(new ItemDescriptionGenerator() {
228
229            private static final long serialVersionUID = 7367011213487089661L;
230
231            public String generateDescription(Component source, Object itemId, Object propertyId) {
232
233                return m_dialog.getDescriptionForItemId(itemId);
234            }
235        });
236
237        addGeneratedColumn(PROP_REMOVE, new ColumnGenerator() {
238
239            private static final long serialVersionUID = -7212693904376423407L;
240
241            public Object generateCell(Table source, final Object itemId, Object columnId) {
242
243                if (((Boolean)source.getItem(itemId).getItemProperty(PROP_STATUS).getValue()).booleanValue()) {
244                    Button button = new Button(FontOpenCms.CIRCLE_MINUS);
245                    button.addStyleName(ValoTheme.BUTTON_BORDERLESS);
246                    button.addStyleName(ValoTheme.BUTTON_ICON_ONLY);
247                    button.addClickListener(new Button.ClickListener() {
248
249                        private static final long serialVersionUID = 3789328000442885119L;
250
251                        public void buttonClick(ClickEvent event) {
252
253                            m_dialog.removeItem(m_dialog.getStringSetValue(Collections.singleton(itemId)));
254                            m_dialog.init();
255
256                        }
257                    });
258                    //button.set
259                    return button;
260                }
261                return null;
262            }
263        });
264    }
265
266    /**
267     * Checks value of table and sets it new if needed:<p>
268     * if multiselect: new itemId is in current Value? -> no change of value<p>
269     * no multiselect and multiselect, but new item not selected before: set value to new item<p>
270     *
271     * @param itemId if of clicked item
272     */
273    void changeValueIfNotMultiSelect(Object itemId) {
274
275        @SuppressWarnings("unchecked")
276        Set<String> value = (Set<String>)getValue();
277        if (value == null) {
278            select(itemId);
279        } else if (!value.contains(itemId)) {
280            setValue(null);
281            select(itemId);
282        }
283    }
284
285    /**
286     * Returns the available menu entries.<p>
287     *
288     * @return the menu entries
289     */
290    List<I_CmsSimpleContextMenuEntry<Set<String>>> getMenuEntries() {
291
292        if (m_menuEntries == null) {
293            m_menuEntries = new ArrayList<I_CmsSimpleContextMenuEntry<Set<String>>>();
294            m_menuEntries.add(new EntryRemove());
295        }
296        return m_menuEntries;
297    }
298}