1 package org
.argeo
.eclipse
.ui
.parts
;
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
.argeo
.eclipse
.ui
.utils
.ViewerUtils
;
10 import org
.eclipse
.jface
.layout
.TableColumnLayout
;
11 import org
.eclipse
.jface
.viewers
.CheckboxTableViewer
;
12 import org
.eclipse
.jface
.viewers
.ColumnLabelProvider
;
13 import org
.eclipse
.jface
.viewers
.ColumnWeightData
;
14 import org
.eclipse
.jface
.viewers
.IStructuredContentProvider
;
15 import org
.eclipse
.jface
.viewers
.TableViewer
;
16 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
17 import org
.eclipse
.jface
.viewers
.Viewer
;
18 import org
.eclipse
.swt
.SWT
;
19 import org
.eclipse
.swt
.events
.ModifyEvent
;
20 import org
.eclipse
.swt
.events
.ModifyListener
;
21 import org
.eclipse
.swt
.events
.SelectionAdapter
;
22 import org
.eclipse
.swt
.events
.SelectionEvent
;
23 import org
.eclipse
.swt
.layout
.GridData
;
24 import org
.eclipse
.swt
.layout
.GridLayout
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Link
;
27 import org
.eclipse
.swt
.widgets
.Table
;
28 import org
.eclipse
.swt
.widgets
.TableColumn
;
29 import org
.eclipse
.swt
.widgets
.Text
;
30 import org
.osgi
.service
.useradmin
.User
;
33 * Generic composite that display a filter and a table viewer to display users
34 * (can also be groups)
36 * Warning: this class does not extends <code>TableViewer</code>. Use the
37 * getTableViewer method to access it.
40 public abstract class LdifUsersTable
extends Composite
{
41 private static final long serialVersionUID
= -7385959046279360420L;
44 // private UserAdmin userAdmin;
47 private List
<ColumnDefinition
> columnDefs
= new ArrayList
<ColumnDefinition
>();
48 private boolean hasFilter
;
49 private boolean preventTableLayout
= false;
50 private boolean hasSelectionColumn
;
51 private int tableStyle
;
54 private TableViewer usersViewer
;
55 private Text filterTxt
;
63 public LdifUsersTable(Composite parent
, int style
) {
64 super(parent
, SWT
.NO_FOCUS
);
65 this.tableStyle
= style
;
68 // TODO workaround the bug of the table layout in the Form
69 public LdifUsersTable(Composite parent
, int style
, boolean preventTableLayout
) {
70 super(parent
, SWT
.NO_FOCUS
);
71 this.tableStyle
= style
;
72 this.preventTableLayout
= preventTableLayout
;
75 /** This must be called before the call to populate method */
76 public void setColumnDefinitions(List
<ColumnDefinition
> columnDefinitions
) {
77 this.columnDefs
= columnDefinitions
;
83 * choose to add a field to filter results or not
85 * choose to add a column to select some of the displayed results or
88 public void populate(boolean addFilter
, boolean addSelection
) {
90 Composite parent
= this;
91 hasFilter
= addFilter
;
92 hasSelectionColumn
= addSelection
;
95 GridLayout layout
= EclipseUiUtils
.noSpaceGridLayout();
96 layout
.verticalSpacing
= 5;
97 this.setLayout(layout
);
99 createFilterPart(parent
);
101 Composite tableComp
= new Composite(parent
, SWT
.NO_FOCUS
);
102 tableComp
.setLayoutData(EclipseUiUtils
.fillAll());
103 usersViewer
= createTableViewer(tableComp
);
104 usersViewer
.setContentProvider(new UsersContentProvider());
110 * display static filters on creation
111 * @param addSelection
112 * choose to add a column to select some of the displayed results or
115 public void populateWithStaticFilters(boolean showMore
, boolean addSelection
) {
117 Composite parent
= this;
119 hasSelectionColumn
= addSelection
;
122 GridLayout layout
= EclipseUiUtils
.noSpaceGridLayout();
123 layout
.verticalSpacing
= 5;
124 this.setLayout(layout
);
125 createStaticFilterPart(parent
, showMore
);
127 Composite tableComp
= new Composite(parent
, SWT
.NO_FOCUS
);
128 tableComp
.setLayoutData(EclipseUiUtils
.fillAll());
129 usersViewer
= createTableViewer(tableComp
);
130 usersViewer
.setContentProvider(new UsersContentProvider());
133 /** Enable access to the selected users or groups */
134 public List
<User
> getSelectedUsers() {
135 if (hasSelectionColumn
) {
136 Object
[] elements
= ((CheckboxTableViewer
) usersViewer
).getCheckedElements();
138 List
<User
> result
= new ArrayList
<User
>();
139 for (Object obj
: elements
) {
140 result
.add((User
) obj
);
144 throw new EclipseUiException(
145 "Unvalid request: no selection column " + "has been created for the current table");
148 /** Returns the User table viewer, typically to add doubleclick listener */
149 public TableViewer
getTableViewer() {
154 * Force the refresh of the underlying table using the current filter string if
157 public void refresh() {
158 String filter
= hasFilter ? filterTxt
.getText().trim() : null;
159 if ("".equals(filter
))
161 refreshFilteredList(filter
);
164 /** Effective repository request: caller must implement this method */
165 abstract protected List
<User
> listFilteredElements(String filter
);
167 // protected List<User> listFilteredElements(String filter) {
168 // List<User> users = new ArrayList<User>();
170 // Role[] roles = userAdmin.getRoles(filter);
171 // // Display all users and groups
172 // for (Role role : roles)
173 // users.add((User) role);
174 // } catch (InvalidSyntaxException e) {
175 // throw new EclipseUiException("Unable to get roles with filter: "
181 /* GENERIC COMPOSITE METHODS */
183 public boolean setFocus() {
185 return filterTxt
.setFocus();
187 return usersViewer
.getTable().setFocus();
191 public void dispose() {
195 /* LOCAL CLASSES AND METHODS */
196 // Will be usefull to rather use a virtual table viewer
197 private void refreshFilteredList(String filter
) {
198 List
<User
> users
= listFilteredElements(filter
);
199 usersViewer
.setInput(users
.toArray());
202 private class UsersContentProvider
implements IStructuredContentProvider
{
203 private static final long serialVersionUID
= 1L;
205 public Object
[] getElements(Object inputElement
) {
206 return (Object
[]) inputElement
;
209 public void dispose() {
212 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
217 private void createFilterPart(Composite parent
) {
218 // Text Area for the filter
219 filterTxt
= new Text(parent
, SWT
.BORDER
| SWT
.SEARCH
| SWT
.ICON_SEARCH
| SWT
.ICON_CANCEL
);
220 filterTxt
.setLayoutData(new GridData(GridData
.FILL
, GridData
.FILL
, true, false));
221 filterTxt
.addModifyListener(new ModifyListener() {
222 private static final long serialVersionUID
= 1L;
224 public void modifyText(ModifyEvent event
) {
225 refreshFilteredList(filterTxt
.getText());
230 private void createStaticFilterPart(Composite parent
, boolean showMore
) {
231 Composite filterComp
= new Composite(parent
, SWT
.NO_FOCUS
);
232 filterComp
.setLayout(new GridLayout(2, false));
233 filterComp
.setLayoutData(EclipseUiUtils
.fillWidth());
235 filterTxt
= new Text(filterComp
, SWT
.BORDER
| SWT
.SEARCH
| SWT
.ICON_SEARCH
| SWT
.ICON_CANCEL
);
236 filterTxt
.setLayoutData(new GridData(GridData
.FILL
, GridData
.FILL
, true, false));
237 // filterTxt.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
238 // GridData.HORIZONTAL_ALIGN_FILL));
239 filterTxt
.addModifyListener(new ModifyListener() {
240 private static final long serialVersionUID
= 1L;
242 public void modifyText(ModifyEvent event
) {
243 refreshFilteredList(filterTxt
.getText());
247 // add static filter abilities
248 Link moreLk
= new Link(filterComp
, SWT
.NONE
);
249 Composite staticFilterCmp
= new Composite(filterComp
, SWT
.NO_FOCUS
);
250 staticFilterCmp
.setLayoutData(EclipseUiUtils
.fillWidth(2));
251 populateStaticFilters(staticFilterCmp
);
253 MoreLinkListener listener
= new MoreLinkListener(moreLk
, staticFilterCmp
, showMore
);
254 // initialise the layout
256 moreLk
.addSelectionListener(listener
);
259 /** Overwrite to add static filters */
260 protected void populateStaticFilters(Composite staticFilterCmp
) {
263 // private void addMoreSL(final Link more) {
264 // more.addSelectionListener( }
266 private class MoreLinkListener
extends SelectionAdapter
{
267 private static final long serialVersionUID
= -524987616510893463L;
268 private boolean isShown
;
269 private final Composite staticFilterCmp
;
270 private final Link moreLk
;
272 public MoreLinkListener(Link moreLk
, Composite staticFilterCmp
, boolean isShown
) {
273 this.moreLk
= moreLk
;
274 this.staticFilterCmp
= staticFilterCmp
;
275 this.isShown
= isShown
;
279 public void widgetSelected(SelectionEvent e
) {
284 public void refresh() {
285 GridData gd
= (GridData
) staticFilterCmp
.getLayoutData();
287 moreLk
.setText("<a> Less... </a>");
288 gd
.heightHint
= SWT
.DEFAULT
;
290 moreLk
.setText("<a> More... </a>");
297 private void forceLayout() {
298 LdifUsersTable
.this.getParent().layout(true, true);
301 private TableViewer
createTableViewer(final Composite parent
) {
303 int style
= tableStyle
| SWT
.H_SCROLL
| SWT
.V_SCROLL
;
304 if (hasSelectionColumn
)
305 style
= style
| SWT
.CHECK
;
306 Table table
= new Table(parent
, style
);
307 TableColumnLayout layout
= new TableColumnLayout();
309 // TODO the table layout does not works with the scrolled form
311 if (preventTableLayout
) {
312 parent
.setLayout(EclipseUiUtils
.noSpaceGridLayout());
313 table
.setLayoutData(EclipseUiUtils
.fillAll());
315 parent
.setLayout(layout
);
318 if (hasSelectionColumn
)
319 viewer
= new CheckboxTableViewer(table
);
321 viewer
= new TableViewer(table
);
322 table
.setLinesVisible(true);
323 table
.setHeaderVisible(true);
325 TableViewerColumn column
;
327 if (hasSelectionColumn
) {
329 column
= ViewerUtils
.createTableViewerColumn(viewer
, "", SWT
.NONE
, 25);
330 column
.setLabelProvider(new ColumnLabelProvider() {
331 private static final long serialVersionUID
= 1L;
334 public String
getText(Object element
) {
338 layout
.setColumnData(column
.getColumn(), new ColumnWeightData(25, 25, false));
340 SelectionAdapter selectionAdapter
= new SelectionAdapter() {
341 private static final long serialVersionUID
= 1L;
343 boolean allSelected
= false;
346 public void widgetSelected(SelectionEvent e
) {
347 allSelected
= !allSelected
;
348 ((CheckboxTableViewer
) usersViewer
).setAllChecked(allSelected
);
351 column
.getColumn().addSelectionListener(selectionAdapter
);
354 // NodeViewerComparator comparator = new NodeViewerComparator();
355 // TODO enable the sort by click on the header
357 for (ColumnDefinition colDef
: columnDefs
)
358 createTableColumn(viewer
, layout
, colDef
);
360 // column = ViewerUtils.createTableViewerColumn(viewer,
361 // colDef.getHeaderLabel(), SWT.NONE, colDef.getColumnSize());
362 // column.setLabelProvider(new CLProvider(colDef.getPropertyName()));
363 // column.getColumn().addSelectionListener(
364 // JcrUiUtils.getNodeSelectionAdapter(i,
365 // colDef.getPropertyType(), colDef.getPropertyName(),
366 // comparator, viewer));
370 // IMPORTANT: initialize comparator before setting it
371 // JcrColumnDefinition firstCol = colDefs.get(0);
372 // comparator.setColumn(firstCol.getPropertyType(),
373 // firstCol.getPropertyName());
374 // viewer.setComparator(comparator);
379 /** Default creation of a column for a user table */
380 private TableViewerColumn
createTableColumn(TableViewer tableViewer
, TableColumnLayout layout
,
381 ColumnDefinition columnDef
) {
383 boolean resizable
= true;
384 TableViewerColumn tvc
= new TableViewerColumn(tableViewer
, SWT
.NONE
);
385 TableColumn column
= tvc
.getColumn();
387 column
.setText(columnDef
.getLabel());
388 column
.setWidth(columnDef
.getMinWidth());
389 column
.setResizable(resizable
);
391 ColumnLabelProvider lp
= columnDef
.getLabelProvider();
392 // add a reference to the display to enable font management
393 // if (lp instanceof UserAdminAbstractLP)
394 // ((UserAdminAbstractLP) lp).setDisplay(tableViewer.getTable()
396 tvc
.setLabelProvider(lp
);
398 layout
.setColumnData(column
, new ColumnWeightData(columnDef
.getWeight(), columnDef
.getMinWidth(), resizable
));