1 package org
.argeo
.cms
.swt
.useradmin
;
3 import java
.util
.ArrayList
;
6 import org
.argeo
.eclipse
.ui
.ColumnDefinition
;
7 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
8 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
9 import org
.eclipse
.jface
.layout
.TableColumnLayout
;
10 import org
.eclipse
.jface
.viewers
.CheckboxTableViewer
;
11 import org
.eclipse
.jface
.viewers
.ColumnLabelProvider
;
12 import org
.eclipse
.jface
.viewers
.ColumnWeightData
;
13 import org
.eclipse
.jface
.viewers
.IStructuredContentProvider
;
14 import org
.eclipse
.jface
.viewers
.TableViewer
;
15 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
16 import org
.eclipse
.jface
.viewers
.Viewer
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.events
.ModifyEvent
;
19 import org
.eclipse
.swt
.events
.ModifyListener
;
20 import org
.eclipse
.swt
.events
.SelectionAdapter
;
21 import org
.eclipse
.swt
.events
.SelectionEvent
;
22 import org
.eclipse
.swt
.layout
.GridData
;
23 import org
.eclipse
.swt
.layout
.GridLayout
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Link
;
26 import org
.eclipse
.swt
.widgets
.Table
;
27 import org
.eclipse
.swt
.widgets
.TableColumn
;
28 import org
.eclipse
.swt
.widgets
.Text
;
29 import org
.osgi
.service
.useradmin
.User
;
32 * Generic composite that display a filter and a table viewer to display users
33 * (can also be groups)
35 * Warning: this class does not extends <code>TableViewer</code>. Use the
36 * getTableViewer method to access it.
39 public abstract class LdifUsersTable
extends Composite
{
40 private static final long serialVersionUID
= -7385959046279360420L;
43 // private UserAdmin userAdmin;
46 private List
<ColumnDefinition
> columnDefs
= new ArrayList
<ColumnDefinition
>();
47 private boolean hasFilter
;
48 private boolean preventTableLayout
= false;
49 private boolean hasSelectionColumn
;
50 private int tableStyle
;
53 private TableViewer usersViewer
;
54 private Text filterTxt
;
62 public LdifUsersTable(Composite parent
, int style
) {
63 super(parent
, SWT
.NO_FOCUS
);
64 this.tableStyle
= style
;
67 // TODO workaround the bug of the table layout in the Form
68 public LdifUsersTable(Composite parent
, int style
, boolean preventTableLayout
) {
69 super(parent
, SWT
.NO_FOCUS
);
70 this.tableStyle
= style
;
71 this.preventTableLayout
= preventTableLayout
;
74 /** This must be called before the call to populate method */
75 public void setColumnDefinitions(List
<ColumnDefinition
> columnDefinitions
) {
76 this.columnDefs
= columnDefinitions
;
82 * choose to add a field to filter results or not
84 * choose to add a column to select some of the displayed results or
87 public void populate(boolean addFilter
, boolean addSelection
) {
89 Composite parent
= this;
90 hasFilter
= addFilter
;
91 hasSelectionColumn
= addSelection
;
94 GridLayout layout
= EclipseUiUtils
.noSpaceGridLayout();
95 layout
.verticalSpacing
= 5;
96 this.setLayout(layout
);
98 createFilterPart(parent
);
100 Composite tableComp
= new Composite(parent
, SWT
.NO_FOCUS
);
101 tableComp
.setLayoutData(EclipseUiUtils
.fillAll());
102 usersViewer
= createTableViewer(tableComp
);
103 usersViewer
.setContentProvider(new UsersContentProvider());
109 * display static filters on creation
110 * @param addSelection
111 * choose to add a column to select some of the displayed results or
114 public void populateWithStaticFilters(boolean showMore
, boolean addSelection
) {
116 Composite parent
= this;
118 hasSelectionColumn
= addSelection
;
121 GridLayout layout
= EclipseUiUtils
.noSpaceGridLayout();
122 layout
.verticalSpacing
= 5;
123 this.setLayout(layout
);
124 createStaticFilterPart(parent
, showMore
);
126 Composite tableComp
= new Composite(parent
, SWT
.NO_FOCUS
);
127 tableComp
.setLayoutData(EclipseUiUtils
.fillAll());
128 usersViewer
= createTableViewer(tableComp
);
129 usersViewer
.setContentProvider(new UsersContentProvider());
132 /** Enable access to the selected users or groups */
133 public List
<User
> getSelectedUsers() {
134 if (hasSelectionColumn
) {
135 Object
[] elements
= ((CheckboxTableViewer
) usersViewer
).getCheckedElements();
137 List
<User
> result
= new ArrayList
<User
>();
138 for (Object obj
: elements
) {
139 result
.add((User
) obj
);
143 throw new EclipseUiException(
144 "Unvalid request: no selection column " + "has been created for the current table");
147 /** Returns the User table viewer, typically to add doubleclick listener */
148 public TableViewer
getTableViewer() {
153 * Force the refresh of the underlying table using the current filter string if
156 public void refresh() {
157 String filter
= hasFilter ? filterTxt
.getText().trim() : null;
158 if ("".equals(filter
))
160 refreshFilteredList(filter
);
163 /** Effective repository request: caller must implement this method */
164 abstract protected List
<User
> listFilteredElements(String filter
);
166 // protected List<User> listFilteredElements(String filter) {
167 // List<User> users = new ArrayList<User>();
169 // Role[] roles = userAdmin.getRoles(filter);
170 // // Display all users and groups
171 // for (Role role : roles)
172 // users.add((User) role);
173 // } catch (InvalidSyntaxException e) {
174 // throw new EclipseUiException("Unable to get roles with filter: "
180 /* GENERIC COMPOSITE METHODS */
182 public boolean setFocus() {
184 return filterTxt
.setFocus();
186 return usersViewer
.getTable().setFocus();
190 public void dispose() {
194 /* LOCAL CLASSES AND METHODS */
195 // Will be usefull to rather use a virtual table viewer
196 private void refreshFilteredList(String filter
) {
197 List
<User
> users
= listFilteredElements(filter
);
198 usersViewer
.setInput(users
.toArray());
201 private class UsersContentProvider
implements IStructuredContentProvider
{
202 private static final long serialVersionUID
= 1L;
204 public Object
[] getElements(Object inputElement
) {
205 return (Object
[]) inputElement
;
208 public void dispose() {
211 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
216 private void createFilterPart(Composite parent
) {
217 // Text Area for the filter
218 filterTxt
= new Text(parent
, SWT
.BORDER
| SWT
.SEARCH
| SWT
.ICON_SEARCH
| SWT
.ICON_CANCEL
);
219 filterTxt
.setLayoutData(new GridData(GridData
.FILL
, GridData
.FILL
, true, false));
220 filterTxt
.addModifyListener(new ModifyListener() {
221 private static final long serialVersionUID
= 1L;
223 public void modifyText(ModifyEvent event
) {
224 refreshFilteredList(filterTxt
.getText());
229 private void createStaticFilterPart(Composite parent
, boolean showMore
) {
230 Composite filterComp
= new Composite(parent
, SWT
.NO_FOCUS
);
231 filterComp
.setLayout(new GridLayout(2, false));
232 filterComp
.setLayoutData(EclipseUiUtils
.fillWidth());
234 filterTxt
= new Text(filterComp
, SWT
.BORDER
| SWT
.SEARCH
| SWT
.ICON_SEARCH
| SWT
.ICON_CANCEL
);
235 filterTxt
.setLayoutData(new GridData(GridData
.FILL
, GridData
.FILL
, true, false));
236 // filterTxt.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
237 // GridData.HORIZONTAL_ALIGN_FILL));
238 filterTxt
.addModifyListener(new ModifyListener() {
239 private static final long serialVersionUID
= 1L;
241 public void modifyText(ModifyEvent event
) {
242 refreshFilteredList(filterTxt
.getText());
246 // add static filter abilities
247 Link moreLk
= new Link(filterComp
, SWT
.NONE
);
248 Composite staticFilterCmp
= new Composite(filterComp
, SWT
.NO_FOCUS
);
249 staticFilterCmp
.setLayoutData(EclipseUiUtils
.fillWidth(2));
250 populateStaticFilters(staticFilterCmp
);
252 MoreLinkListener listener
= new MoreLinkListener(moreLk
, staticFilterCmp
, showMore
);
253 // initialise the layout
255 moreLk
.addSelectionListener(listener
);
258 /** Overwrite to add static filters */
259 protected void populateStaticFilters(Composite staticFilterCmp
) {
262 // private void addMoreSL(final Link more) {
263 // more.addSelectionListener( }
265 private class MoreLinkListener
extends SelectionAdapter
{
266 private static final long serialVersionUID
= -524987616510893463L;
267 private boolean isShown
;
268 private final Composite staticFilterCmp
;
269 private final Link moreLk
;
271 public MoreLinkListener(Link moreLk
, Composite staticFilterCmp
, boolean isShown
) {
272 this.moreLk
= moreLk
;
273 this.staticFilterCmp
= staticFilterCmp
;
274 this.isShown
= isShown
;
278 public void widgetSelected(SelectionEvent e
) {
283 public void refresh() {
284 GridData gd
= (GridData
) staticFilterCmp
.getLayoutData();
286 moreLk
.setText("<a> Less... </a>");
287 gd
.heightHint
= SWT
.DEFAULT
;
289 moreLk
.setText("<a> More... </a>");
296 private void forceLayout() {
297 LdifUsersTable
.this.getParent().layout(true, true);
300 private TableViewer
createTableViewer(final Composite parent
) {
302 int style
= tableStyle
| SWT
.H_SCROLL
| SWT
.V_SCROLL
;
303 if (hasSelectionColumn
)
304 style
= style
| SWT
.CHECK
;
305 Table table
= new Table(parent
, style
);
306 TableColumnLayout layout
= new TableColumnLayout();
308 // TODO the table layout does not works with the scrolled form
310 if (preventTableLayout
) {
311 parent
.setLayout(EclipseUiUtils
.noSpaceGridLayout());
312 table
.setLayoutData(EclipseUiUtils
.fillAll());
314 parent
.setLayout(layout
);
317 if (hasSelectionColumn
)
318 viewer
= new CheckboxTableViewer(table
);
320 viewer
= new TableViewer(table
);
321 table
.setLinesVisible(true);
322 table
.setHeaderVisible(true);
324 TableViewerColumn column
;
326 if (hasSelectionColumn
) {
328 column
= ViewerUtils
.createTableViewerColumn(viewer
, "", SWT
.NONE
, 25);
329 column
.setLabelProvider(new ColumnLabelProvider() {
330 private static final long serialVersionUID
= 1L;
333 public String
getText(Object element
) {
337 layout
.setColumnData(column
.getColumn(), new ColumnWeightData(25, 25, false));
339 SelectionAdapter selectionAdapter
= new SelectionAdapter() {
340 private static final long serialVersionUID
= 1L;
342 boolean allSelected
= false;
345 public void widgetSelected(SelectionEvent e
) {
346 allSelected
= !allSelected
;
347 ((CheckboxTableViewer
) usersViewer
).setAllChecked(allSelected
);
350 column
.getColumn().addSelectionListener(selectionAdapter
);
353 // NodeViewerComparator comparator = new NodeViewerComparator();
354 // TODO enable the sort by click on the header
356 for (ColumnDefinition colDef
: columnDefs
)
357 createTableColumn(viewer
, layout
, colDef
);
359 // column = ViewerUtils.createTableViewerColumn(viewer,
360 // colDef.getHeaderLabel(), SWT.NONE, colDef.getColumnSize());
361 // column.setLabelProvider(new CLProvider(colDef.getPropertyName()));
362 // column.getColumn().addSelectionListener(
363 // JcrUiUtils.getNodeSelectionAdapter(i,
364 // colDef.getPropertyType(), colDef.getPropertyName(),
365 // comparator, viewer));
369 // IMPORTANT: initialize comparator before setting it
370 // JcrColumnDefinition firstCol = colDefs.get(0);
371 // comparator.setColumn(firstCol.getPropertyType(),
372 // firstCol.getPropertyName());
373 // viewer.setComparator(comparator);
378 /** Default creation of a column for a user table */
379 private TableViewerColumn
createTableColumn(TableViewer tableViewer
, TableColumnLayout layout
,
380 ColumnDefinition columnDef
) {
382 boolean resizable
= true;
383 TableViewerColumn tvc
= new TableViewerColumn(tableViewer
, SWT
.NONE
);
384 TableColumn column
= tvc
.getColumn();
386 column
.setText(columnDef
.getLabel());
387 column
.setWidth(columnDef
.getMinWidth());
388 column
.setResizable(resizable
);
390 ColumnLabelProvider lp
= columnDef
.getLabelProvider();
391 // add a reference to the display to enable font management
392 // if (lp instanceof UserAdminAbstractLP)
393 // ((UserAdminAbstractLP) lp).setDisplay(tableViewer.getTable()
395 tvc
.setLabelProvider(lp
);
397 layout
.setColumnData(column
, new ColumnWeightData(columnDef
.getWeight(), columnDef
.getMinWidth(), resizable
));