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
,
70 boolean preventTableLayout
) {
71 super(parent
, SWT
.NO_FOCUS
);
72 this.tableStyle
= style
;
73 this.preventTableLayout
= preventTableLayout
;
76 /** This must be called before the call to populate method */
77 public void setColumnDefinitions(List
<ColumnDefinition
> columnDefinitions
) {
78 this.columnDefs
= columnDefinitions
;
84 * choose to add a field to filter results or not
86 * choose to add a column to select some of the displayed results
89 public void populate(boolean addFilter
, boolean addSelection
) {
91 Composite parent
= this;
92 hasFilter
= addFilter
;
93 hasSelectionColumn
= addSelection
;
96 GridLayout layout
= EclipseUiUtils
.noSpaceGridLayout();
97 layout
.verticalSpacing
= 5;
98 this.setLayout(layout
);
100 createFilterPart(parent
);
102 Composite tableComp
= new Composite(parent
, SWT
.NO_FOCUS
);
103 tableComp
.setLayoutData(EclipseUiUtils
.fillAll());
104 usersViewer
= createTableViewer(tableComp
);
105 usersViewer
.setContentProvider(new UsersContentProvider());
111 * display static filters on creation
112 * @param addSelection
113 * choose to add a column to select some of the displayed results
116 public void populateWithStaticFilters(boolean showMore
, boolean addSelection
) {
118 Composite parent
= this;
120 hasSelectionColumn
= addSelection
;
123 GridLayout layout
= EclipseUiUtils
.noSpaceGridLayout();
124 layout
.verticalSpacing
= 5;
125 this.setLayout(layout
);
126 createStaticFilterPart(parent
, showMore
);
128 Composite tableComp
= new Composite(parent
, SWT
.NO_FOCUS
);
129 tableComp
.setLayoutData(EclipseUiUtils
.fillAll());
130 usersViewer
= createTableViewer(tableComp
);
131 usersViewer
.setContentProvider(new UsersContentProvider());
134 /** Enable access to the selected users or groups */
135 public List
<User
> getSelectedUsers() {
136 if (hasSelectionColumn
) {
137 Object
[] elements
= ((CheckboxTableViewer
) usersViewer
)
138 .getCheckedElements();
140 List
<User
> result
= new ArrayList
<User
>();
141 for (Object obj
: elements
) {
142 result
.add((User
) obj
);
146 throw new EclipseUiException("Unvalid request: no selection column "
147 + "has been created for the current table");
150 /** Returns the User table viewer, typically to add doubleclick listener */
151 public TableViewer
getTableViewer() {
156 * Force the refresh of the underlying table using the current filter string
159 public void refresh() {
160 String filter
= hasFilter ? filterTxt
.getText().trim() : null;
161 if ("".equals(filter
))
163 refreshFilteredList(filter
);
166 /** Effective repository request: caller must implement this method */
167 abstract protected List
<User
> listFilteredElements(String filter
);
169 // protected List<User> listFilteredElements(String filter) {
170 // List<User> users = new ArrayList<User>();
172 // Role[] roles = userAdmin.getRoles(filter);
173 // // Display all users and groups
174 // for (Role role : roles)
175 // users.add((User) role);
176 // } catch (InvalidSyntaxException e) {
177 // throw new EclipseUiException("Unable to get roles with filter: "
183 /* GENERIC COMPOSITE METHODS */
185 public boolean setFocus() {
187 return filterTxt
.setFocus();
189 return usersViewer
.getTable().setFocus();
193 public void dispose() {
197 /* LOCAL CLASSES AND METHODS */
198 // Will be usefull to rather use a virtual table viewer
199 private void refreshFilteredList(String filter
) {
200 List
<User
> users
= listFilteredElements(filter
);
201 usersViewer
.setInput(users
.toArray());
204 private class UsersContentProvider
implements IStructuredContentProvider
{
205 private static final long serialVersionUID
= 1L;
207 public Object
[] getElements(Object inputElement
) {
208 return (Object
[]) inputElement
;
211 public void dispose() {
214 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
219 private void createFilterPart(Composite parent
) {
220 // Text Area for the filter
221 filterTxt
= new Text(parent
, SWT
.BORDER
| SWT
.SEARCH
| SWT
.ICON_SEARCH
223 filterTxt
.setLayoutData(new GridData(GridData
.GRAB_HORIZONTAL
224 | GridData
.HORIZONTAL_ALIGN_FILL
));
225 filterTxt
.addModifyListener(new ModifyListener() {
226 private static final long serialVersionUID
= 1L;
228 public void modifyText(ModifyEvent event
) {
229 refreshFilteredList(filterTxt
.getText());
234 private void createStaticFilterPart(Composite parent
, boolean showMore
) {
235 Composite filterComp
= new Composite(parent
, SWT
.NO_FOCUS
);
236 filterComp
.setLayout(new GridLayout(2, false));
237 filterComp
.setLayoutData(EclipseUiUtils
.fillWidth());
239 filterTxt
= new Text(filterComp
, SWT
.BORDER
| SWT
.SEARCH
240 | SWT
.ICON_SEARCH
| SWT
.ICON_CANCEL
);
241 filterTxt
.setLayoutData(new GridData(GridData
.GRAB_HORIZONTAL
242 | GridData
.HORIZONTAL_ALIGN_FILL
));
243 filterTxt
.addModifyListener(new ModifyListener() {
244 private static final long serialVersionUID
= 1L;
246 public void modifyText(ModifyEvent event
) {
247 refreshFilteredList(filterTxt
.getText());
251 // add static filter abilities
252 Link moreLk
= new Link(filterComp
, SWT
.NONE
);
253 Composite staticFilterCmp
= new Composite(filterComp
, SWT
.NO_FOCUS
);
254 staticFilterCmp
.setLayoutData(EclipseUiUtils
.fillWidth(2));
255 populateStaticFilters(staticFilterCmp
);
257 MoreLinkListener listener
= new MoreLinkListener(moreLk
,
258 staticFilterCmp
, showMore
);
259 // initialise the layout
261 moreLk
.addSelectionListener(listener
);
264 /** Overwrite to add static filters */
265 protected void populateStaticFilters(Composite staticFilterCmp
) {
268 // private void addMoreSL(final Link more) {
269 // more.addSelectionListener( }
271 private class MoreLinkListener
extends SelectionAdapter
{
272 private static final long serialVersionUID
= -524987616510893463L;
273 private boolean isShown
;
274 private final Composite staticFilterCmp
;
275 private final Link moreLk
;
277 public MoreLinkListener(Link moreLk
, Composite staticFilterCmp
,
279 this.moreLk
= moreLk
;
280 this.staticFilterCmp
= staticFilterCmp
;
281 this.isShown
= isShown
;
285 public void widgetSelected(SelectionEvent e
) {
290 public void refresh() {
291 GridData gd
= (GridData
) staticFilterCmp
.getLayoutData();
293 moreLk
.setText("<a> Less... </a>");
294 gd
.heightHint
= SWT
.DEFAULT
;
296 moreLk
.setText("<a> More... </a>");
303 private void forceLayout() {
304 LdifUsersTable
.this.getParent().layout(true, true);
307 private TableViewer
createTableViewer(final Composite parent
) {
309 int style
= tableStyle
| SWT
.H_SCROLL
| SWT
.V_SCROLL
;
310 if (hasSelectionColumn
)
311 style
= style
| SWT
.CHECK
;
312 Table table
= new Table(parent
, style
);
313 TableColumnLayout layout
= new TableColumnLayout();
315 // TODO the table layout does not works with the scrolled form
317 if (preventTableLayout
) {
318 parent
.setLayout(EclipseUiUtils
.noSpaceGridLayout());
319 table
.setLayoutData(EclipseUiUtils
.fillAll());
321 parent
.setLayout(layout
);
324 if (hasSelectionColumn
)
325 viewer
= new CheckboxTableViewer(table
);
327 viewer
= new TableViewer(table
);
328 table
.setLinesVisible(true);
329 table
.setHeaderVisible(true);
331 TableViewerColumn column
;
333 if (hasSelectionColumn
) {
335 column
= ViewerUtils
.createTableViewerColumn(viewer
, "", SWT
.NONE
,
337 column
.setLabelProvider(new ColumnLabelProvider() {
338 private static final long serialVersionUID
= 1L;
341 public String
getText(Object element
) {
345 layout
.setColumnData(column
.getColumn(), new ColumnWeightData(25,
348 SelectionAdapter selectionAdapter
= new SelectionAdapter() {
349 private static final long serialVersionUID
= 1L;
351 boolean allSelected
= false;
354 public void widgetSelected(SelectionEvent e
) {
355 allSelected
= !allSelected
;
356 ((CheckboxTableViewer
) usersViewer
)
357 .setAllChecked(allSelected
);
360 column
.getColumn().addSelectionListener(selectionAdapter
);
363 // NodeViewerComparator comparator = new NodeViewerComparator();
364 // TODO enable the sort by click on the header
366 for (ColumnDefinition colDef
: columnDefs
)
367 createTableColumn(viewer
, layout
, colDef
);
369 // column = ViewerUtils.createTableViewerColumn(viewer,
370 // colDef.getHeaderLabel(), SWT.NONE, colDef.getColumnSize());
371 // column.setLabelProvider(new CLProvider(colDef.getPropertyName()));
372 // column.getColumn().addSelectionListener(
373 // JcrUiUtils.getNodeSelectionAdapter(i,
374 // colDef.getPropertyType(), colDef.getPropertyName(),
375 // comparator, viewer));
379 // IMPORTANT: initialize comparator before setting it
380 // JcrColumnDefinition firstCol = colDefs.get(0);
381 // comparator.setColumn(firstCol.getPropertyType(),
382 // firstCol.getPropertyName());
383 // viewer.setComparator(comparator);
388 /** Default creation of a column for a user table */
389 private TableViewerColumn
createTableColumn(TableViewer tableViewer
,
390 TableColumnLayout layout
, ColumnDefinition columnDef
) {
392 boolean resizable
= true;
393 TableViewerColumn tvc
= new TableViewerColumn(tableViewer
, SWT
.NONE
);
394 TableColumn column
= tvc
.getColumn();
396 column
.setText(columnDef
.getLabel());
397 column
.setWidth(columnDef
.getMinWidth());
398 column
.setResizable(resizable
);
400 ColumnLabelProvider lp
= columnDef
.getLabelProvider();
401 // add a reference to the display to enable font management
402 // if (lp instanceof UserAdminAbstractLP)
403 // ((UserAdminAbstractLP) lp).setDisplay(tableViewer.getTable()
405 tvc
.setLabelProvider(lp
);
407 layout
.setColumnData(column
, new ColumnWeightData(
408 columnDef
.getWeight(), columnDef
.getMinWidth(), resizable
));