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 setItemIconPropertyId(PROP_ICON); 132 setColumnWidth(null, 40); 133 setRowHeaderMode(RowHeaderMode.ICON_ONLY); 134 setSelectable(true); 135 setMultiSelect(true); 136 setColumnHeader(PROP_NAME, dialog.getItemName()); 137 setColumnHeader(PROP_ADD, ""); 138 139 setColumnWidth(PROP_ADD, 40); 140 if (m_dialog.getFurtherColumnId() != null) { 141 setVisibleColumns(PROP_ADD, PROP_NAME, m_dialog.getFurtherColumnId()); 142 } else { 143 setVisibleColumns(PROP_ADD, PROP_NAME); 144 } 145 146 m_menu = new CmsContextMenu(); 147 m_menu.setAsTableContextMenu(this); 148 149 addItemClickListener(new ItemClickListener() { 150 151 private static final long serialVersionUID = 4807195510202231174L; 152 153 public void itemClick(ItemClickEvent event) { 154 155 if (!event.isCtrlKey() && !event.isShiftKey()) { 156 157 changeValueIfNotMultiSelect(event.getItemId()); 158 159 if (event.getButton().equals(MouseButton.RIGHT) || (event.getPropertyId() == null)) { 160 m_menu.setEntries(getMenuEntries(), m_dialog.getStringSetValue((Set<Object>)getValue())); 161 m_menu.openForTable( 162 event, 163 event.getItemId(), 164 event.getPropertyId(), 165 CmsAvailableRoleOrPrincipalTable.this); 166 } 167 168 } 169 } 170 171 }); 172 setItemDescriptionGenerator(new ItemDescriptionGenerator() { 173 174 private static final long serialVersionUID = 7367011213487089661L; 175 176 public String generateDescription(Component source, Object itemId, Object propertyId) { 177 178 return m_dialog.getDescriptionForItemId(itemId); 179 } 180 }); 181 addGeneratedColumn(PROP_ADD, new ColumnGenerator() { 182 183 private static final long serialVersionUID = -778841579899729529L; 184 185 public Object generateCell(Table source, final Object itemId, Object columnId) { 186 187 Button button = new Button(FontOpenCms.CIRCLE_PLUS); 188 button.addStyleName(ValoTheme.BUTTON_BORDERLESS); 189 button.addStyleName(ValoTheme.BUTTON_ICON_ONLY); 190 button.addClickListener(new Button.ClickListener() { 191 192 private static final long serialVersionUID = -44051469061574153L; 193 194 public void buttonClick(ClickEvent event) { 195 196 m_dialog.addItem(m_dialog.getStringSetValue(Collections.singleton(itemId))); 197 m_dialog.init(); 198 199 } 200 }); 201 //button.set 202 return button; 203 } 204 205 }); 206 } 207 208 /** 209 * Filters the table according to given search string.<p> 210 * 211 * @param search string to be looked for. 212 */ 213 public void filterTable(String search) { 214 215 m_container.removeAllContainerFilters(); 216 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(search)) { 217 if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_dialog.getFurtherColumnId())) { 218 m_container.addContainerFilter(new Or(new SimpleStringFilter(PROP_NAME, search, true, false))); 219 } else { 220 m_container.addContainerFilter( 221 new Or( 222 new SimpleStringFilter(PROP_NAME, search, true, false), 223 new SimpleStringFilter(m_dialog.getFurtherColumnId(), search, true, false))); 224 } 225 226 } 227 if ((getValue() != null) & !((Set<String>)getValue()).isEmpty()) { 228 setCurrentPageFirstItemId(((Set<String>)getValue()).iterator().next()); 229 } 230 } 231 232 /** 233 * Checks value of table and sets it new if needed:<p> 234 * if multiselect: new itemId is in current Value? -> no change of value<p> 235 * no multiselect and multiselect, but new item not selected before: set value to new item<p> 236 * 237 * @param itemId if of clicked item 238 */ 239 void changeValueIfNotMultiSelect(Object itemId) { 240 241 @SuppressWarnings("unchecked") 242 Set<String> value = (Set<String>)getValue(); 243 if (value == null) { 244 select(itemId); 245 } else if (!value.contains(itemId)) { 246 setValue(null); 247 select(itemId); 248 } 249 } 250 251 /** 252 * Returns the available menu entries.<p> 253 * 254 * @return the menu entries 255 */ 256 List<I_CmsSimpleContextMenuEntry<Set<String>>> getMenuEntries() { 257 258 if (m_menuEntries == null) { 259 m_menuEntries = new ArrayList<I_CmsSimpleContextMenuEntry<Set<String>>>(); 260 m_menuEntries.add(new EntryAdd()); 261 } 262 return m_menuEntries; 263 } 264}