1 package org
.argeo
.slc
.client
.ui
.views
;
3 import java
.text
.SimpleDateFormat
;
4 import java
.util
.ArrayList
;
5 import java
.util
.HashMap
;
6 import java
.util
.Iterator
;
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
;
55 public class ResultListView
extends ViewPart
{
56 private final static Log log
= LogFactory
.getLog(ResultListView
.class);
58 public static final String ID
= "org.argeo.slc.client.ui.resultListView";
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";
64 private final static String PLATFORM
= SWT
.getPlatform();
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";
71 private final static SimpleDateFormat dateFormatter
= new SimpleDateFormat(
72 "MM/dd/yy', 'HH:mm:ss");
74 private TableViewer viewer
;
75 private TreeTestResultCollectionDao testResultCollectionDao
;
77 private CurrentTableComparator comparator
;
79 private ResultAttributes selectedRa
;
81 // handle locally which result are shown or not
82 private List
<ResultAttributes
> removedResults
= new ArrayList
<ResultAttributes
>();
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());
93 // Initializes sort mecanism
94 // by default we sort by date asc
95 comparator
= new CurrentTableComparator(1,
96 GenericTableComparator
.ASCENDING
);
97 viewer
.setComparator(comparator
);
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
);
108 viewer
.getControl().setMenu(menu
);
109 getSite().registerContextMenu(menuManager
, viewer
);
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;
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);
126 table
.setLinesVisible(true);
127 table
.setHeaderVisible(true);
129 TableColumn column
= new TableColumn(table
, SWT
.LEFT
, 0);
130 column
.setText("Test Case");
131 column
.setWidth(200);
132 column
.addSelectionListener(getSelectionAdapter(column
, 0));
134 column
= new TableColumn(table
, SWT
.LEFT
, 1);
135 column
.setText("Close Date");
136 column
.setWidth(130);
137 column
.addSelectionListener(getSelectionAdapter(column
, 1));
139 column
= new TableColumn(table
, SWT
.LEFT
, 2);
140 column
.setText("UUID");
141 column
.setWidth(300);
142 column
.addSelectionListener(getSelectionAdapter(column
, 2));
147 // Manage list of the result
148 public void setFocus() {
149 viewer
.getControl().setFocus();
152 /** Initialize the view **/
153 public void retrieveResults() {
155 List
<ResultAttributes
> lst
= testResultCollectionDao
156 .listResultAttributes(null);
157 if (log
.isTraceEnabled())
158 log
.trace("Result attributes count: " + lst
.size());
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
168 ResultAttributes tmpRa
= it2
.next();
169 if (tmpRa
.getUuid().equals(curRaId
)) {
171 continue removeResult
;
174 if (lst
.contains(curRa
))
177 viewer
.setInput(lst
);
178 } catch (Exception e
) {
179 throw new ArgeoException("Cannot refresh the result List", e
);
183 public List
<String
> getSelectedResultsId() {
184 List
<String
> resultIds
= new ArrayList
<String
>();
185 IStructuredSelection curSelection
= (IStructuredSelection
) viewer
187 Iterator it
= curSelection
.iterator();
188 while (it
.hasNext()) {
189 ResultAttributes curResult
= (ResultAttributes
) it
.next();
190 resultIds
.add(curResult
.getUuid());
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.
201 public void removeSelected() {
202 IStructuredSelection curSelection
= (IStructuredSelection
) viewer
204 Iterator it
= curSelection
.iterator();
205 while (it
.hasNext()) {
206 ResultAttributes curResult
= (ResultAttributes
) it
.next();
207 if (!removedResults
.contains(curResult
))
208 removedResults
.add(curResult
);
213 // View Specific inner class
216 class SelectionChangedListener
implements ISelectionChangedListener
{
217 public void selectionChanged(SelectionChangedEvent evt
) {
219 IStructuredSelection curSelection
= (IStructuredSelection
) evt
221 Object obj
= curSelection
.getFirstElement();
223 if (obj
instanceof ResultAttributes
) {
224 selectedRa
= (ResultAttributes
) obj
;
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
);
236 menuManager
.remove(ici
);
237 CommandContributionItemParameter contributionItemParameter
= new CommandContributionItemParameter(
238 locator
, null, cmdId
, SWT
.PUSH
);
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
;
253 CommandContributionItem cci
= new CommandContributionItem(
254 contributionItemParameter
);
256 menuManager
.add(cci
);
260 * Defines the commands that will pop up in the context menu. Might be
261 * overriden by user to add or remove certain commands.
263 protected void contextMenuAboutToShow(IMenuManager menuManager
) {
265 IWorkbenchWindow window
= ClientUiPlugin
.getDefault().getWorkbench()
266 .getActiveWorkbenchWindow();
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");
275 refreshCommand(menuManager
, window
, SAVE_AS_XLS_CMD_ID
,
276 "Save selected as Excel files", "icons/excel.png");
278 refreshCommand(menuManager
, window
, REMOVE_CMD_ID
, "Remove selected",
279 "icons/removeAll.png");
284 protected static class ViewContentProvider
implements
285 IStructuredContentProvider
{
287 public void inputChanged(Viewer arg0
, Object arg1
, Object arg2
) {
290 public void dispose() {
293 @SuppressWarnings("unchecked")
294 public Object
[] getElements(Object obj
) {
295 if (obj
instanceof List
) {
296 return ((List
<ResultAttributes
>) obj
).toArray();
298 return new Object
[0];
303 protected class ViewLabelProvider
extends LabelProvider
implements
304 ITableLabelProvider
{
305 public String
getColumnText(Object obj
, int index
) {
306 ResultAttributes ra
= (ResultAttributes
) obj
;
309 return (ra
.getAttributes().get("testCase") == null) ?
null : ra
310 .getAttributes().get("testCase");
312 // otherwise we get null pointer exception when the test is not
314 return (ra
.getCloseDate() == null) ?
null : dateFormatter
315 .format(ra
.getCloseDate());
322 public Image
getColumnImage(Object obj
, int index
) {
329 private class CurrentTableComparator
extends GenericTableComparator
{
331 public CurrentTableComparator(int colIndex
, int direction
) {
332 super(colIndex
, direction
);
336 public int compare(Viewer viewer
, Object r1
, Object r2
) {
339 ResultAttributes ra1
= (ResultAttributes
) r1
;
340 ResultAttributes ra2
= (ResultAttributes
) r2
;
342 switch (propertyIndex
) {
344 if (ra1
.getAttributes().get("testCase") == null)
346 else if (ra2
.getAttributes().get("testCase") == null)
349 rc
= ra1
.getAttributes().get("testCase")
350 .compareTo(ra2
.getAttributes().get("testCase"));
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)
357 else if (ra2
.getCloseDate() == null)
360 rc
= ra1
.getCloseDate().compareTo(ra2
.getCloseDate());
363 rc
= ra1
.getUuid().compareTo(ra2
.getUuid());
367 if (direction
== DESCENDING
) {
374 private SelectionAdapter
getSelectionAdapter(final TableColumn column
,
377 SelectionAdapter selectionAdapter
= new SelectionAdapter() {
379 public void widgetSelected(SelectionEvent e
) {
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
;
388 viewer
.getTable().setSortDirection(dir
);
389 viewer
.getTable().setSortColumn(column
);
393 return selectionAdapter
;
399 * The ResultAttributes expose a part of the information contained in the
400 * TreeTestResult, It has the same UUID as the corresponding treeTestResult.
402 class ViewDoubleClickListener
implements IDoubleClickListener
{
403 public void doubleClick(DoubleClickEvent evt
) {
404 Object obj
= ((IStructuredSelection
) evt
.getSelection())
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);
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");
420 // log.debug("cmd : " + cmd);
421 ArrayList
<Parameterization
> parameters
= new ArrayList
<Parameterization
>();
424 IParameter iparam
= cmd
425 .getParameter("org.argeo.slc.client.commands.resultUuid");
427 Parameterization params
= new Parameterization(iparam
,
429 parameters
.add(params
);
431 // build the parameterized command
432 ParameterizedCommand pc
= new ParameterizedCommand(cmd
,
433 parameters
.toArray(new Parameterization
[parameters
436 // execute the command
437 handlerService
= (IHandlerService
) window
438 .getService(IHandlerService
.class);
439 handlerService
.executeCommand(pc
, null);
441 } catch (Exception e
) {
443 throw new SlcException("Problem while rendering result. "
451 public void setTestResultCollectionDao(
452 TreeTestResultCollectionDao testResultCollectionDao
) {
453 this.testResultCollectionDao
= testResultCollectionDao
;