]> git.argeo.org Git - lgpl/argeo-commons.git/blob - LdifUsersTable.java
91e01d07a63b5f2cb4cb2e1f977a5184040b4c74
[lgpl/argeo-commons.git] / LdifUsersTable.java
1 package org.argeo.eclipse.ui.parts;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
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;
31
32 /**
33 * Generic composite that display a filter and a table viewer to display users
34 * (can also be groups)
35 *
36 * Warning: this class does not extends <code>TableViewer</code>. Use the
37 * getTableViewer method to access it.
38 *
39 */
40 public abstract class LdifUsersTable extends Composite {
41 private static final long serialVersionUID = -7385959046279360420L;
42
43 // Context
44 // private UserAdmin userAdmin;
45
46 // Configuration
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;
52
53 // Local UI Objects
54 private TableViewer usersViewer;
55 private Text filterTxt;
56
57 /* EXPOSED METHODS */
58
59 /**
60 * @param parent
61 * @param style
62 */
63 public LdifUsersTable(Composite parent, int style) {
64 super(parent, SWT.NO_FOCUS);
65 this.tableStyle = style;
66 }
67
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;
74 }
75
76 /** This must be called before the call to populate method */
77 public void setColumnDefinitions(List<ColumnDefinition> columnDefinitions) {
78 this.columnDefs = columnDefinitions;
79 }
80
81 /**
82 *
83 * @param addFilter
84 * choose to add a field to filter results or not
85 * @param addSelection
86 * choose to add a column to select some of the displayed results
87 * or not
88 */
89 public void populate(boolean addFilter, boolean addSelection) {
90 // initialization
91 Composite parent = this;
92 hasFilter = addFilter;
93 hasSelectionColumn = addSelection;
94
95 // Main Layout
96 GridLayout layout = EclipseUiUtils.noSpaceGridLayout();
97 layout.verticalSpacing = 5;
98 this.setLayout(layout);
99 if (hasFilter)
100 createFilterPart(parent);
101
102 Composite tableComp = new Composite(parent, SWT.NO_FOCUS);
103 tableComp.setLayoutData(EclipseUiUtils.fillAll());
104 usersViewer = createTableViewer(tableComp);
105 usersViewer.setContentProvider(new UsersContentProvider());
106 }
107
108 /**
109 *
110 * @param showMore
111 * display static filters on creation
112 * @param addSelection
113 * choose to add a column to select some of the displayed results
114 * or not
115 */
116 public void populateWithStaticFilters(boolean showMore, boolean addSelection) {
117 // initialization
118 Composite parent = this;
119 hasFilter = true;
120 hasSelectionColumn = addSelection;
121
122 // Main Layout
123 GridLayout layout = EclipseUiUtils.noSpaceGridLayout();
124 layout.verticalSpacing = 5;
125 this.setLayout(layout);
126 createStaticFilterPart(parent, showMore);
127
128 Composite tableComp = new Composite(parent, SWT.NO_FOCUS);
129 tableComp.setLayoutData(EclipseUiUtils.fillAll());
130 usersViewer = createTableViewer(tableComp);
131 usersViewer.setContentProvider(new UsersContentProvider());
132 }
133
134 /** Enable access to the selected users or groups */
135 public List<User> getSelectedUsers() {
136 if (hasSelectionColumn) {
137 Object[] elements = ((CheckboxTableViewer) usersViewer)
138 .getCheckedElements();
139
140 List<User> result = new ArrayList<User>();
141 for (Object obj : elements) {
142 result.add((User) obj);
143 }
144 return result;
145 } else
146 throw new EclipseUiException("Unvalid request: no selection column "
147 + "has been created for the current table");
148 }
149
150 /** Returns the User table viewer, typically to add doubleclick listener */
151 public TableViewer getTableViewer() {
152 return usersViewer;
153 }
154
155 /**
156 * Force the refresh of the underlying table using the current filter string
157 * if relevant
158 */
159 public void refresh() {
160 String filter = hasFilter ? filterTxt.getText().trim() : null;
161 if ("".equals(filter))
162 filter = null;
163 refreshFilteredList(filter);
164 }
165
166 /** Effective repository request: caller must implement this method */
167 abstract protected List<User> listFilteredElements(String filter);
168
169 // protected List<User> listFilteredElements(String filter) {
170 // List<User> users = new ArrayList<User>();
171 // try {
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: "
178 // + filter, e);
179 // }
180 // return users;
181 // }
182
183 /* GENERIC COMPOSITE METHODS */
184 @Override
185 public boolean setFocus() {
186 if (hasFilter)
187 return filterTxt.setFocus();
188 else
189 return usersViewer.getTable().setFocus();
190 }
191
192 @Override
193 public void dispose() {
194 super.dispose();
195 }
196
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());
202 }
203
204 private class UsersContentProvider implements IStructuredContentProvider {
205 private static final long serialVersionUID = 1L;
206
207 public Object[] getElements(Object inputElement) {
208 return (Object[]) inputElement;
209 }
210
211 public void dispose() {
212 }
213
214 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
215 }
216 }
217
218 /* MANAGE FILTER */
219 private void createFilterPart(Composite parent) {
220 // Text Area for the filter
221 filterTxt = new Text(parent, SWT.BORDER | SWT.SEARCH | SWT.ICON_SEARCH
222 | SWT.ICON_CANCEL);
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;
227
228 public void modifyText(ModifyEvent event) {
229 refreshFilteredList(filterTxt.getText());
230 }
231 });
232 }
233
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());
238 // generic search
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;
245
246 public void modifyText(ModifyEvent event) {
247 refreshFilteredList(filterTxt.getText());
248 }
249 });
250
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);
256
257 MoreLinkListener listener = new MoreLinkListener(moreLk,
258 staticFilterCmp, showMore);
259 // initialise the layout
260 listener.refresh();
261 moreLk.addSelectionListener(listener);
262 }
263
264 /** Overwrite to add static filters */
265 protected void populateStaticFilters(Composite staticFilterCmp) {
266 }
267
268 // private void addMoreSL(final Link more) {
269 // more.addSelectionListener( }
270
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;
276
277 public MoreLinkListener(Link moreLk, Composite staticFilterCmp,
278 boolean isShown) {
279 this.moreLk = moreLk;
280 this.staticFilterCmp = staticFilterCmp;
281 this.isShown = isShown;
282 }
283
284 @Override
285 public void widgetSelected(SelectionEvent e) {
286 isShown = !isShown;
287 refresh();
288 }
289
290 public void refresh() {
291 GridData gd = (GridData) staticFilterCmp.getLayoutData();
292 if (isShown) {
293 moreLk.setText("<a> Less... </a>");
294 gd.heightHint = SWT.DEFAULT;
295 } else {
296 moreLk.setText("<a> More... </a>");
297 gd.heightHint = 0;
298 }
299 forceLayout();
300 }
301 }
302
303 private void forceLayout() {
304 LdifUsersTable.this.getParent().layout(true, true);
305 }
306
307 private TableViewer createTableViewer(final Composite parent) {
308
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();
314
315 // TODO the table layout does not works with the scrolled form
316
317 if (preventTableLayout) {
318 parent.setLayout(EclipseUiUtils.noSpaceGridLayout());
319 table.setLayoutData(EclipseUiUtils.fillAll());
320 } else
321 parent.setLayout(layout);
322
323 TableViewer viewer;
324 if (hasSelectionColumn)
325 viewer = new CheckboxTableViewer(table);
326 else
327 viewer = new TableViewer(table);
328 table.setLinesVisible(true);
329 table.setHeaderVisible(true);
330
331 TableViewerColumn column;
332 // int offset = 0;
333 if (hasSelectionColumn) {
334 // offset = 1;
335 column = ViewerUtils.createTableViewerColumn(viewer, "", SWT.NONE,
336 25);
337 column.setLabelProvider(new ColumnLabelProvider() {
338 private static final long serialVersionUID = 1L;
339
340 @Override
341 public String getText(Object element) {
342 return null;
343 }
344 });
345 layout.setColumnData(column.getColumn(), new ColumnWeightData(25,
346 25, false));
347
348 SelectionAdapter selectionAdapter = new SelectionAdapter() {
349 private static final long serialVersionUID = 1L;
350
351 boolean allSelected = false;
352
353 @Override
354 public void widgetSelected(SelectionEvent e) {
355 allSelected = !allSelected;
356 ((CheckboxTableViewer) usersViewer)
357 .setAllChecked(allSelected);
358 }
359 };
360 column.getColumn().addSelectionListener(selectionAdapter);
361 }
362
363 // NodeViewerComparator comparator = new NodeViewerComparator();
364 // TODO enable the sort by click on the header
365 // int i = offset;
366 for (ColumnDefinition colDef : columnDefs)
367 createTableColumn(viewer, layout, colDef);
368
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));
376 // i++;
377 // }
378
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);
384
385 return viewer;
386 }
387
388 /** Default creation of a column for a user table */
389 private TableViewerColumn createTableColumn(TableViewer tableViewer,
390 TableColumnLayout layout, ColumnDefinition columnDef) {
391
392 boolean resizable = true;
393 TableViewerColumn tvc = new TableViewerColumn(tableViewer, SWT.NONE);
394 TableColumn column = tvc.getColumn();
395
396 column.setText(columnDef.getLabel());
397 column.setWidth(columnDef.getMinWidth());
398 column.setResizable(resizable);
399
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()
404 // .getDisplay());
405 tvc.setLabelProvider(lp);
406
407 layout.setColumnData(column, new ColumnWeightData(
408 columnDef.getWeight(), columnDef.getMinWidth(), resizable));
409
410 return tvc;
411 }
412 }