]> git.argeo.org Git - gpl/argeo-slc.git/blob - eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/views/ResultListView.java
Improve process management
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.client.ui / src / main / java / org / argeo / slc / client / ui / views / ResultListView.java
1 package org.argeo.slc.client.ui.views;
2
3 import java.text.SimpleDateFormat;
4 import java.util.ArrayList;
5 import java.util.Iterator;
6 import java.util.List;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.argeo.ArgeoException;
11 import org.argeo.eclipse.ui.GenericTableComparator;
12 import org.argeo.slc.SlcException;
13 import org.argeo.slc.client.ui.ClientUiPlugin;
14 import org.argeo.slc.client.ui.commands.RemoveSelectedResultFromResultList;
15 import org.argeo.slc.client.ui.commands.ResultDetailsDisplayHandler;
16 import org.argeo.slc.core.test.tree.ResultAttributes;
17 import org.argeo.slc.dao.test.tree.TreeTestResultCollectionDao;
18 import org.eclipse.core.commands.Command;
19 import org.eclipse.core.commands.IParameter;
20 import org.eclipse.core.commands.Parameterization;
21 import org.eclipse.core.commands.ParameterizedCommand;
22 import org.eclipse.jface.action.IContributionItem;
23 import org.eclipse.jface.action.IMenuListener;
24 import org.eclipse.jface.action.IMenuManager;
25 import org.eclipse.jface.action.MenuManager;
26 import org.eclipse.jface.viewers.DoubleClickEvent;
27 import org.eclipse.jface.viewers.IDoubleClickListener;
28 import org.eclipse.jface.viewers.ISelectionChangedListener;
29 import org.eclipse.jface.viewers.IStructuredContentProvider;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.viewers.ITableLabelProvider;
32 import org.eclipse.jface.viewers.LabelProvider;
33 import org.eclipse.jface.viewers.SelectionChangedEvent;
34 import org.eclipse.jface.viewers.TableViewer;
35 import org.eclipse.jface.viewers.Viewer;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.events.SelectionAdapter;
38 import org.eclipse.swt.events.SelectionEvent;
39 import org.eclipse.swt.graphics.Image;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Menu;
42 import org.eclipse.swt.widgets.Table;
43 import org.eclipse.swt.widgets.TableColumn;
44 import org.eclipse.ui.IWorkbench;
45 import org.eclipse.ui.IWorkbenchWindow;
46 import org.eclipse.ui.commands.ICommandService;
47 import org.eclipse.ui.handlers.IHandlerService;
48 import org.eclipse.ui.menus.CommandContributionItem;
49 import org.eclipse.ui.menus.CommandContributionItemParameter;
50 import org.eclipse.ui.part.ViewPart;
51 import org.eclipse.ui.services.IServiceLocator;
52
53 public class ResultListView extends ViewPart {
54 private final static Log log = LogFactory.getLog(ResultListView.class);
55
56 public static final String ID = "org.argeo.slc.client.ui.resultListView";
57
58 // TODO : remove dependency from SLC on Sparta
59 private final static String DISPLAY_AS_XLS_CMD_ID = "com.capco.sparta.client.ui.displayResultDetailsWithExcel";
60 private final static String SAVE_AS_XLS_CMD_ID = "com.capco.sparta.client.ui.saveResultAsExcelFile";
61
62 private final static String PLATFORM = SWT.getPlatform();
63
64 private final static String DISPLAY_CMD_ID = ResultDetailsDisplayHandler.ID;
65 private final static String REMOVE_CMD_ID = RemoveSelectedResultFromResultList.ID;
66 // private final static String UUID_PARAM_ID = "org.argeo.slc.client.commands.resultUuid";
67 // private final static String NAME_PARAM_ID = "org.argeo.slc.client.commands.resultName";
68
69 private final static SimpleDateFormat dateFormatter = new SimpleDateFormat(
70 "MM/dd/yy', 'HH:mm:ss");
71
72 private TableViewer viewer;
73 private TreeTestResultCollectionDao testResultCollectionDao;
74
75 private CurrentTableComparator comparator;
76
77 private ResultAttributes selectedRa;
78
79 // handle locally which result are shown or not
80 private List<ResultAttributes> removedResults = new ArrayList<ResultAttributes>();
81
82 public void createPartControl(Composite parent) {
83 Table table = createTable(parent);
84 viewer = new TableViewer(table);
85 viewer.setLabelProvider(new ViewLabelProvider());
86 viewer.setContentProvider(new ViewContentProvider());
87 viewer.setInput(getViewSite());
88 viewer.addDoubleClickListener(new ViewDoubleClickListener());
89 viewer.addSelectionChangedListener(new SelectionChangedListener());
90
91 // Initializes sort mecanism
92 // by default we sort by date asc
93 comparator = new CurrentTableComparator(1,
94 GenericTableComparator.ASCENDING);
95 viewer.setComparator(comparator);
96
97 // create the context menu
98 MenuManager menuManager = new MenuManager();
99 Menu menu = menuManager.createContextMenu(viewer.getControl());
100 menuManager.addMenuListener(new IMenuListener() {
101 public void menuAboutToShow(IMenuManager manager) {
102 contextMenuAboutToShow(manager);
103 }
104 });
105
106 viewer.getControl().setMenu(menu);
107 getSite().registerContextMenu(menuManager, viewer);
108 }
109
110 protected Table createTable(Composite parent) {
111 int style = SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL
112 | SWT.FULL_SELECTION;
113 // does not function with RAP, commented for the moment being
114 // | SWT.HIDE_SELECTION;
115
116 Table table = new Table(parent, style);
117 // table.addMouseListener(new RightClickListener());
118 // GridData gridData = new GridData(GridData.FILL_BOTH);
119 // gridData.grabExcessVerticalSpace = true;
120 // gridData.grabExcessHorizontalSpace = true;
121 // gridData.horizontalSpan = 3;
122 // table.setLayoutData(gridData);
123
124 table.setLinesVisible(true);
125 table.setHeaderVisible(true);
126
127 TableColumn column = new TableColumn(table, SWT.LEFT, 0);
128 column.setText("Test Case");
129 column.setWidth(200);
130 column.addSelectionListener(getSelectionAdapter(column, 0));
131
132 column = new TableColumn(table, SWT.LEFT, 1);
133 column.setText("Close Date");
134 column.setWidth(130);
135 column.addSelectionListener(getSelectionAdapter(column, 1));
136
137 column = new TableColumn(table, SWT.LEFT, 2);
138 column.setText("UUID");
139 column.setWidth(300);
140 column.addSelectionListener(getSelectionAdapter(column, 2));
141
142 return table;
143 }
144
145 // Manage list of the result
146 public void setFocus() {
147 viewer.getControl().setFocus();
148 }
149
150 /** Initialize the view **/
151 public void retrieveResults() {
152 try {
153 List<ResultAttributes> lst = testResultCollectionDao
154 .listResultAttributes(null);
155 if (log.isTraceEnabled())
156 log.trace("Result attributes count: " + lst.size());
157
158 // handle removed result from the list locally
159 // Might be optimised.
160 Iterator<ResultAttributes> it = removedResults.iterator();
161 removeResult: while (it.hasNext()) {
162 ResultAttributes curRa = it.next();
163 String curRaId = curRa.getUuid();
164 for (Iterator<ResultAttributes> it2 = lst.iterator(); it2
165 .hasNext();) {
166 ResultAttributes tmpRa = it2.next();
167 if (tmpRa.getUuid().equals(curRaId)) {
168 lst.remove(tmpRa);
169 continue removeResult;
170 }
171 }
172 if (lst.contains(curRa))
173 lst.remove(curRa);
174 }
175 viewer.setInput(lst);
176 } catch (Exception e) {
177 throw new ArgeoException("Cannot refresh the result List", e);
178 }
179 }
180
181 public List<String> getSelectedResultsId() {
182 List<String> resultIds = new ArrayList<String>();
183 IStructuredSelection curSelection = (IStructuredSelection) viewer
184 .getSelection();
185 Iterator it = curSelection.iterator();
186 while (it.hasNext()) {
187 ResultAttributes curResult = (ResultAttributes) it.next();
188 resultIds.add(curResult.getUuid());
189 }
190 return resultIds;
191 }
192
193 /**
194 * Remove selected items from the result list. This process is handled
195 * locally in the UI side : trying to handle that on the server side throws
196 * HibernateLazyInit exception, especially when trying to access
197 * TreeTestResultCollection items.
198 */
199 public void removeSelected() {
200 IStructuredSelection curSelection = (IStructuredSelection) viewer
201 .getSelection();
202 Iterator it = curSelection.iterator();
203 while (it.hasNext()) {
204 ResultAttributes curResult = (ResultAttributes) it.next();
205 if (!removedResults.contains(curResult))
206 removedResults.add(curResult);
207 }
208 retrieveResults();
209 }
210
211 // View Specific inner class
212
213 // Handle Events
214 class SelectionChangedListener implements ISelectionChangedListener {
215 public void selectionChanged(SelectionChangedEvent evt) {
216
217 IStructuredSelection curSelection = (IStructuredSelection) evt
218 .getSelection();
219 Object obj = curSelection.getFirstElement();
220
221 if (obj instanceof ResultAttributes) {
222 selectedRa = (ResultAttributes) obj;
223 }
224 }
225 }
226
227 // Relatively useless in the current case.
228 // Yet we keep it as sample code to show how we can implement a context menu
229 // which commands are dynamically linked to the selected item of a control
230 protected void refreshCommand(IMenuManager menuManager,
231 IServiceLocator locator, String cmdId, String label, String iconPath) {
232 IContributionItem ici = menuManager.find(cmdId);
233 if (ici != null)
234 menuManager.remove(ici);
235 CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter(
236 locator, null, cmdId, SWT.PUSH);
237
238 // Set Params
239 contributionItemParameter.label = label;
240 contributionItemParameter.icon = ClientUiPlugin
241 .getImageDescriptor(iconPath);
242
243
244 // if (!REMOVE_CMD_ID.equals(cmdId)) {
245 // Map<String, String> params = new HashMap<String, String>();
246 // params.put(UUID_PARAM_ID, selectedRa.getUuid());
247 // params.put(NAME_PARAM_ID,
248 // (selectedRa.getAttributes().get("testCase") == null) ? null
249 // : selectedRa.getAttributes().get("testCase"));
250 // contributionItemParameter.parameters = params;
251 // }
252
253 CommandContributionItem cci = new CommandContributionItem(
254 contributionItemParameter);
255 cci.setId(cmdId);
256 menuManager.add(cci);
257 }
258
259 /**
260 * Defines the commands that will pop up in the context menu. Might be
261 * overriden by user to add or remove certain commands.
262 **/
263 protected void contextMenuAboutToShow(IMenuManager menuManager) {
264
265 IWorkbenchWindow window = ClientUiPlugin.getDefault().getWorkbench()
266 .getActiveWorkbenchWindow();
267
268 refreshCommand(menuManager, window, DISPLAY_CMD_ID,
269 "Display selected as tree", "icons/result_details.gif");
270 // We only show this command on windows OS
271 if (PLATFORM.equals("win32") || PLATFORM.equals("wpf")) {
272 refreshCommand(menuManager, window, DISPLAY_AS_XLS_CMD_ID,
273 "Display selected with Excel", "icons/excel.png");
274 }
275 refreshCommand(menuManager, window, SAVE_AS_XLS_CMD_ID,
276 "Save selected as Excel files", "icons/excel.png");
277
278 refreshCommand(menuManager, window, REMOVE_CMD_ID, "Remove selected",
279 "icons/removeAll.png");
280
281 }
282
283 // Providers
284 protected static class ViewContentProvider implements
285 IStructuredContentProvider {
286
287 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
288 }
289
290 public void dispose() {
291 }
292
293 @SuppressWarnings("unchecked")
294 public Object[] getElements(Object obj) {
295 if (obj instanceof List) {
296 return ((List<ResultAttributes>) obj).toArray();
297 } else {
298 return new Object[0];
299 }
300 }
301 }
302
303 protected class ViewLabelProvider extends LabelProvider implements
304 ITableLabelProvider {
305 public String getColumnText(Object obj, int index) {
306 ResultAttributes ra = (ResultAttributes) obj;
307 switch (index) {
308 case 0:
309 return (ra.getAttributes().get("testCase") == null) ? null : ra
310 .getAttributes().get("testCase");
311 case 1:
312 // otherwise we get null pointer exception when the test is not
313 // closed yet.
314 return (ra.getCloseDate() == null) ? null : dateFormatter
315 .format(ra.getCloseDate());
316 case 2:
317 return ra.getUuid();
318 }
319 return getText(obj);
320 }
321
322 public Image getColumnImage(Object obj, int index) {
323 return null;
324 }
325
326 }
327
328 // Sort Mecanism
329 private class CurrentTableComparator extends GenericTableComparator {
330
331 public CurrentTableComparator(int colIndex, int direction) {
332 super(colIndex, direction);
333 }
334
335 @Override
336 public int compare(Viewer viewer, Object r1, Object r2) {
337
338 int rc = 0;
339 ResultAttributes ra1 = (ResultAttributes) r1;
340 ResultAttributes ra2 = (ResultAttributes) r2;
341
342 switch (propertyIndex) {
343 case 0:
344 if (ra1.getAttributes().get("testCase") == null)
345 rc = -1;
346 else if (ra2.getAttributes().get("testCase") == null)
347 rc = 1;
348 else
349 rc = ra1.getAttributes().get("testCase")
350 .compareTo(ra2.getAttributes().get("testCase"));
351 break;
352 case 1:
353 // result with close date == null are put at the end : either
354 // they are nor finished or in error
355 if (ra1.getCloseDate() == null)
356 rc = 1;
357 else if (ra2.getCloseDate() == null)
358 rc = -1;
359 else
360 rc = ra1.getCloseDate().compareTo(ra2.getCloseDate());
361 break;
362 case 2:
363 rc = ra1.getUuid().compareTo(ra2.getUuid());
364 break;
365 }
366
367 if (direction == DESCENDING) {
368 rc = -rc;
369 }
370 return rc;
371 }
372 }
373
374 private SelectionAdapter getSelectionAdapter(final TableColumn column,
375 final int index) {
376
377 SelectionAdapter selectionAdapter = new SelectionAdapter() {
378 @Override
379 public void widgetSelected(SelectionEvent e) {
380
381 comparator.setColumn(index);
382 int dir = viewer.getTable().getSortDirection();
383 if (viewer.getTable().getSortColumn() == column) {
384 dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
385 } else {
386 dir = SWT.DOWN;
387 }
388 viewer.getTable().setSortDirection(dir);
389 viewer.getTable().setSortColumn(column);
390 viewer.refresh();
391 }
392 };
393 return selectionAdapter;
394 }
395
396 // Handle Events
397
398 /**
399 * The ResultAttributes expose a part of the information contained in the
400 * TreeTestResult, It has the same UUID as the corresponding treeTestResult.
401 */
402 class ViewDoubleClickListener implements IDoubleClickListener {
403 public void doubleClick(DoubleClickEvent evt) {
404 Object obj = ((IStructuredSelection) evt.getSelection())
405 .getFirstElement();
406
407 if (obj instanceof ResultAttributes) {
408 ResultAttributes ra = (ResultAttributes) obj;
409 IWorkbench iw = ClientUiPlugin.getDefault().getWorkbench();
410 IHandlerService handlerService = (IHandlerService) iw
411 .getService(IHandlerService.class);
412 try {
413 // get the command from plugin.xml
414 IWorkbenchWindow window = iw.getActiveWorkbenchWindow();
415 ICommandService cmdService = (ICommandService) window
416 .getService(ICommandService.class);
417 Command cmd = cmdService
418 .getCommand("org.argeo.slc.client.ui.displayResultDetails");
419
420 // log.debug("cmd : " + cmd);
421 ArrayList<Parameterization> parameters = new ArrayList<Parameterization>();
422
423 // get the parameter
424 IParameter iparam = cmd
425 .getParameter("org.argeo.slc.client.commands.resultUuid");
426
427 Parameterization params = new Parameterization(iparam,
428 ra.getUuid());
429 parameters.add(params);
430
431 // build the parameterized command
432 ParameterizedCommand pc = new ParameterizedCommand(cmd,
433 parameters.toArray(new Parameterization[parameters
434 .size()]));
435
436 // execute the command
437 handlerService = (IHandlerService) window
438 .getService(IHandlerService.class);
439 handlerService.executeCommand(pc, null);
440
441 } catch (Exception e) {
442 e.printStackTrace();
443 throw new SlcException("Problem while rendering result. "
444 + e.getMessage());
445 }
446 }
447 }
448 }
449
450 // IoC
451 public void setTestResultCollectionDao(
452 TreeTestResultCollectionDao testResultCollectionDao) {
453 this.testResultCollectionDao = testResultCollectionDao;
454 }
455 }