]> 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
79618dddd7f3e6de41c84344ac88fd0329688828
[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.HashMap;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.argeo.ArgeoException;
13 import org.argeo.eclipse.ui.GenericTableComparator;
14 import org.argeo.slc.SlcException;
15 import org.argeo.slc.client.ui.ClientUiPlugin;
16 import org.argeo.slc.client.ui.commands.RemoveSelectedResultFromResultList;
17 import org.argeo.slc.client.ui.commands.ResultDetailsDisplayHandler;
18 import org.argeo.slc.core.test.tree.ResultAttributes;
19 import org.argeo.slc.dao.test.tree.TreeTestResultCollectionDao;
20 import org.eclipse.core.commands.Command;
21 import org.eclipse.core.commands.IParameter;
22 import org.eclipse.core.commands.Parameterization;
23 import org.eclipse.core.commands.ParameterizedCommand;
24 import org.eclipse.jface.action.IContributionItem;
25 import org.eclipse.jface.action.IMenuListener;
26 import org.eclipse.jface.action.IMenuManager;
27 import org.eclipse.jface.action.MenuManager;
28 import org.eclipse.jface.viewers.DoubleClickEvent;
29 import org.eclipse.jface.viewers.IDoubleClickListener;
30 import org.eclipse.jface.viewers.ISelectionChangedListener;
31 import org.eclipse.jface.viewers.IStructuredContentProvider;
32 import org.eclipse.jface.viewers.IStructuredSelection;
33 import org.eclipse.jface.viewers.ITableLabelProvider;
34 import org.eclipse.jface.viewers.LabelProvider;
35 import org.eclipse.jface.viewers.SelectionChangedEvent;
36 import org.eclipse.jface.viewers.TableViewer;
37 import org.eclipse.jface.viewers.Viewer;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.events.SelectionAdapter;
40 import org.eclipse.swt.events.SelectionEvent;
41 import org.eclipse.swt.graphics.Image;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Menu;
44 import org.eclipse.swt.widgets.Table;
45 import org.eclipse.swt.widgets.TableColumn;
46 import org.eclipse.ui.IWorkbench;
47 import org.eclipse.ui.IWorkbenchWindow;
48 import org.eclipse.ui.commands.ICommandService;
49 import org.eclipse.ui.handlers.IHandlerService;
50 import org.eclipse.ui.menus.CommandContributionItem;
51 import org.eclipse.ui.menus.CommandContributionItemParameter;
52 import org.eclipse.ui.part.ViewPart;
53 import org.eclipse.ui.services.IServiceLocator;
54
55 public class ResultListView extends ViewPart {
56 private final static Log log = LogFactory.getLog(ResultListView.class);
57
58 public static final String ID = "org.argeo.slc.client.ui.resultListView";
59
60 // TODO : remove dependency from SLC on Sparta
61 private final static String DISPLAY_AS_XLS_CMD_ID = "com.capco.sparta.client.ui.displayResultDetailsWithExcel";
62 private final static String SAVE_AS_XLS_CMD_ID = "com.capco.sparta.client.ui.saveResultAsExcelFile";
63
64 private final static String PLATFORM = SWT.getPlatform();
65
66 private final static String DISPLAY_CMD_ID = ResultDetailsDisplayHandler.ID;
67 private final static String REMOVE_CMD_ID = RemoveSelectedResultFromResultList.ID;
68 private final static String UUID_PARAM_ID = "org.argeo.slc.client.commands.resultUuid";
69 private final static String NAME_PARAM_ID = "org.argeo.slc.client.commands.resultName";
70
71 private final static SimpleDateFormat dateFormatter = new SimpleDateFormat(
72 "MM/dd/yy', 'HH:mm:ss");
73
74 private TableViewer viewer;
75 private TreeTestResultCollectionDao testResultCollectionDao;
76
77 private CurrentTableComparator comparator;
78
79 private ResultAttributes selectedRa;
80
81 // handle locally which result are shown or not
82 private List<ResultAttributes> removedResults = new ArrayList<ResultAttributes>();
83
84 public void createPartControl(Composite parent) {
85 Table table = createTable(parent);
86 viewer = new TableViewer(table);
87 viewer.setLabelProvider(new ViewLabelProvider());
88 viewer.setContentProvider(new ViewContentProvider());
89 viewer.setInput(getViewSite());
90 viewer.addDoubleClickListener(new ViewDoubleClickListener());
91 viewer.addSelectionChangedListener(new SelectionChangedListener());
92
93 // Initializes sort mecanism
94 // by default we sort by date asc
95 comparator = new CurrentTableComparator(1,
96 GenericTableComparator.ASCENDING);
97 viewer.setComparator(comparator);
98
99 // create the context menu
100 MenuManager menuManager = new MenuManager();
101 Menu menu = menuManager.createContextMenu(viewer.getControl());
102 menuManager.addMenuListener(new IMenuListener() {
103 public void menuAboutToShow(IMenuManager manager) {
104 contextMenuAboutToShow(manager);
105 }
106 });
107
108 viewer.getControl().setMenu(menu);
109 getSite().registerContextMenu(menuManager, viewer);
110 }
111
112 protected Table createTable(Composite parent) {
113 int style = SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL
114 | SWT.FULL_SELECTION;
115 // does not function with RAP, commented for the moment being
116 // | SWT.HIDE_SELECTION;
117
118 Table table = new Table(parent, style);
119 // table.addMouseListener(new RightClickListener());
120 // GridData gridData = new GridData(GridData.FILL_BOTH);
121 // gridData.grabExcessVerticalSpace = true;
122 // gridData.grabExcessHorizontalSpace = true;
123 // gridData.horizontalSpan = 3;
124 // table.setLayoutData(gridData);
125
126 table.setLinesVisible(true);
127 table.setHeaderVisible(true);
128
129 TableColumn column = new TableColumn(table, SWT.LEFT, 0);
130 column.setText("Test Case");
131 column.setWidth(200);
132 column.addSelectionListener(getSelectionAdapter(column, 0));
133
134 column = new TableColumn(table, SWT.LEFT, 1);
135 column.setText("Close Date");
136 column.setWidth(130);
137 column.addSelectionListener(getSelectionAdapter(column, 1));
138
139 column = new TableColumn(table, SWT.LEFT, 2);
140 column.setText("UUID");
141 column.setWidth(300);
142 column.addSelectionListener(getSelectionAdapter(column, 2));
143
144 return table;
145 }
146
147 // Manage list of the result
148 public void setFocus() {
149 viewer.getControl().setFocus();
150 }
151
152 /** Initialize the view **/
153 public void retrieveResults() {
154 try {
155 List<ResultAttributes> lst = testResultCollectionDao
156 .listResultAttributes(null);
157 if (log.isTraceEnabled())
158 log.trace("Result attributes count: " + lst.size());
159
160 // handle removed result from the list locally
161 // Might be optimised.
162 Iterator<ResultAttributes> it = removedResults.iterator();
163 removeResult: while (it.hasNext()) {
164 ResultAttributes curRa = it.next();
165 String curRaId = curRa.getUuid();
166 for (Iterator<ResultAttributes> it2 = lst.iterator(); it2
167 .hasNext();) {
168 ResultAttributes tmpRa = it2.next();
169 if (tmpRa.getUuid().equals(curRaId)) {
170 lst.remove(tmpRa);
171 continue removeResult;
172 }
173 }
174 if (lst.contains(curRa))
175 lst.remove(curRa);
176 }
177 viewer.setInput(lst);
178 } catch (Exception e) {
179 throw new ArgeoException("Cannot refresh the result List", e);
180 }
181 }
182
183 public List<String> getSelectedResultsId() {
184 List<String> resultIds = new ArrayList<String>();
185 IStructuredSelection curSelection = (IStructuredSelection) viewer
186 .getSelection();
187 Iterator it = curSelection.iterator();
188 while (it.hasNext()) {
189 ResultAttributes curResult = (ResultAttributes) it.next();
190 resultIds.add(curResult.getUuid());
191 }
192 return resultIds;
193 }
194
195 /**
196 * Remove selected items from the result list. This process is handled
197 * locally in the UI side : trying to handle that on the server side throws
198 * HibernateLazyInit exception, especially when trying to access
199 * TreeTestResultCollection items.
200 */
201 public void removeSelected() {
202 IStructuredSelection curSelection = (IStructuredSelection) viewer
203 .getSelection();
204 Iterator it = curSelection.iterator();
205 while (it.hasNext()) {
206 ResultAttributes curResult = (ResultAttributes) it.next();
207 if (!removedResults.contains(curResult))
208 removedResults.add(curResult);
209 }
210 retrieveResults();
211 }
212
213 // View Specific inner class
214
215 // Handle Events
216 class SelectionChangedListener implements ISelectionChangedListener {
217 public void selectionChanged(SelectionChangedEvent evt) {
218
219 IStructuredSelection curSelection = (IStructuredSelection) evt
220 .getSelection();
221 Object obj = curSelection.getFirstElement();
222
223 if (obj instanceof ResultAttributes) {
224 selectedRa = (ResultAttributes) obj;
225 }
226 }
227 }
228
229 // Relatively useless in the current case.
230 // Yet we keep it as sample code to show how we can implement a context menu
231 // which commands are dynamically linked to the selected item of a control
232 protected void refreshCommand(IMenuManager menuManager,
233 IServiceLocator locator, String cmdId, String label, String iconPath) {
234 IContributionItem ici = menuManager.find(cmdId);
235 if (ici != null)
236 menuManager.remove(ici);
237 CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter(
238 locator, null, cmdId, SWT.PUSH);
239
240 // Set Params
241 contributionItemParameter.label = label;
242 contributionItemParameter.icon = ClientUiPlugin
243 .getImageDescriptor(iconPath);
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 }