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        setItemIconPropertyId(PROP_ICON);
171        setColumnWidth(null, 40);
172        setRowHeaderMode(RowHeaderMode.ICON_ONLY);
173
174        setSelectable(true);
175        setMultiSelect(true);
176
177        setColumnWidth(PROP_REMOVE, 40);
178        setColumnHeader(PROP_REMOVE, "");
179
180        if (m_dialog.getFurtherColumnId() != null) {
181            setVisibleColumns(PROP_REMOVE, PROP_NAME, m_dialog.getFurtherColumnId());
182        } else {
183            setVisibleColumns(PROP_REMOVE, PROP_NAME);
184        }
185        setColumnHeader(PROP_NAME, m_dialog.getItemName());
186
187        addItemClickListener(new ItemClickListener() {
188
189            private static final long serialVersionUID = 4807195510202231174L;
190
191            @SuppressWarnings("unchecked")
192            public void itemClick(ItemClickEvent event) {
193
194                if (!event.isCtrlKey()
195                    && !event.isShiftKey()
196                    && ((Boolean)getItem(event.getItemId()).getItemProperty(PROP_STATUS).getValue()).booleanValue()) {
197
198                    changeValueIfNotMultiSelect(event.getItemId());
199
200                    if (event.getButton().equals(MouseButton.RIGHT) || (event.getPropertyId() == null)) {
201                        m_menu.setEntries(getMenuEntries(), m_dialog.getStringSetValue((Set<Object>)getValue()));
202                        m_menu.openForTable(
203                            event,
204                            event.getItemId(),
205                            event.getPropertyId(),
206                            CmsCurrentRoleOrPrincipalTable.this);
207                    }
208                }
209            }
210        });
211
212        setCellStyleGenerator(new CellStyleGenerator() {
213
214            private static final long serialVersionUID = 4685652851810828147L;
215
216            public String getStyle(Table source, Object itemId, Object propertyId) {
217
218                if (!((Boolean)source.getItem(itemId).getItemProperty(PROP_STATUS).getValue()).booleanValue()) {
219                    return " " + OpenCmsTheme.TABLE_CELL_DISABLED;
220                }
221                return null;
222            }
223
224        });
225
226        setItemDescriptionGenerator(new ItemDescriptionGenerator() {
227
228            private static final long serialVersionUID = 7367011213487089661L;
229
230            public String generateDescription(Component source, Object itemId, Object propertyId) {
231
232                return m_dialog.getDescriptionForItemId(itemId);
233            }
234        });
235
236        addGeneratedColumn(PROP_REMOVE, new ColumnGenerator() {
237
238            private static final long serialVersionUID = -7212693904376423407L;
239
240            public Object generateCell(Table source, final Object itemId, Object columnId) {
241
242                if (((Boolean)source.getItem(itemId).getItemProperty(PROP_STATUS).getValue()).booleanValue()) {
243                    Button button = new Button(FontOpenCms.CIRCLE_MINUS);
244                    button.addStyleName(ValoTheme.BUTTON_BORDERLESS);
245                    button.addStyleName(ValoTheme.BUTTON_ICON_ONLY);
246                    button.addClickListener(new Button.ClickListener() {
247
248                        private static final long serialVersionUID = 3789328000442885119L;
249
250                        public void buttonClick(ClickEvent event) {
251
252                            m_dialog.removeItem(m_dialog.getStringSetValue(Collections.singleton(itemId)));
253                            m_dialog.init();
254
255                        }
256                    });
257                    //button.set
258                    return button;
259                }
260                return null;
261            }
262        });
263    }
264
265    /**
266     * Checks value of table and sets it new if needed:<p>
267     * if multiselect: new itemId is in current Value? -> no change of value<p>
268     * no multiselect and multiselect, but new item not selected before: set value to new item<p>
269     *
270     * @param itemId if of clicked item
271     */
272    void changeValueIfNotMultiSelect(Object itemId) {
273
274        @SuppressWarnings("unchecked")
275        Set<String> value = (Set<String>)getValue();
276        if (value == null) {
277            select(itemId);
278        } else if (!value.contains(itemId)) {
279            setValue(null);
280            select(itemId);
281        }
282    }
283
284    /**
285     * Returns the available menu entries.<p>
286     *
287     * @return the menu entries
288     */
289    List<I_CmsSimpleContextMenuEntry<Set<String>>> getMenuEntries() {
290
291        if (m_menuEntries == null) {
292            m_menuEntries = new ArrayList<I_CmsSimpleContextMenuEntry<Set<String>>>();
293            m_menuEntries.add(new EntryRemove());
294        }
295        return m_menuEntries;
296    }
297}