1 package org
.argeo
.slc
.client
.ui
.views
;
3 import java
.text
.SimpleDateFormat
;
4 import java
.util
.ArrayList
;
5 import java
.util
.Iterator
;
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
;
53 public class ResultListView
extends ViewPart
{
54 private final static Log log
= LogFactory
.getLog(ResultListView
.class);
56 public static final String ID
= "org.argeo.slc.client.ui.resultListView";
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";
62 private final static String PLATFORM
= SWT
.getPlatform();
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";
69 private final static SimpleDateFormat dateFormatter
= new SimpleDateFormat(
70 "MM/dd/yy', 'HH:mm:ss");
72 private TableViewer viewer
;
73 private TreeTestResultCollectionDao testResultCollectionDao
;
75 private CurrentTableComparator comparator
;
77 private ResultAttributes selectedRa
;
79 // handle locally which result are shown or not
80 private List
<ResultAttributes
> removedResults
= new ArrayList
<ResultAttributes
>();
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());
91 // Initializes sort mecanism
92 // by default we sort by date asc
93 comparator
= new CurrentTableComparator(1,
94 GenericTableComparator
.ASCENDING
);
95 viewer
.setComparator(comparator
);
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
);
106 viewer
.getControl().setMenu(menu
);
107 getSite().registerContextMenu(menuManager
, viewer
);
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;
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);
124 table
.setLinesVisible(true);
125 table
.setHeaderVisible(true);
127 TableColumn column
= new TableColumn(table
, SWT
.LEFT
, 0);
128 column
.setText("Test Case");
129 column
.setWidth(200);
130 column
.addSelectionListener(getSelectionAdapter(column
, 0));
132 column
= new TableColumn(table
, SWT
.LEFT
, 1);
133 column
.setText("Close Date");
134 column
.setWidth(130);
135 column
.addSelectionListener(getSelectionAdapter(column
, 1));
137 column
= new TableColumn(table
, SWT
.LEFT
, 2);
138 column
.setText("UUID");
139 column
.setWidth(300);
140 column
.addSelectionListener(getSelectionAdapter(column
, 2));
145 // Manage list of the result
146 public void setFocus() {
147 viewer
.getControl().setFocus();
150 /** Initialize the view **/
151 public void retrieveResults() {
153 List
<ResultAttributes
> lst
= testResultCollectionDao
154 .listResultAttributes(null);
155 if (log
.isTraceEnabled())
156 log
.trace("Result attributes count: " + lst
.size());
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
166 ResultAttributes tmpRa
= it2
.next();
167 if (tmpRa
.getUuid().equals(curRaId
)) {
169 continue removeResult
;
172 if (lst
.contains(curRa
))
175 viewer
.setInput(lst
);
176 } catch (Exception e
) {
177 throw new ArgeoException("Cannot refresh the result List", e
);
181 public List
<String
> getSelectedResultsId() {
182 List
<String
> resultIds
= new ArrayList
<String
>();
183 IStructuredSelection curSelection
= (IStructuredSelection
) viewer
185 Iterator it
= curSelection
.iterator();
186 while (it
.hasNext()) {
187 ResultAttributes curResult
= (ResultAttributes
) it
.next();
188 resultIds
.add(curResult
.getUuid());
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.
199 public void removeSelected() {
200 IStructuredSelection curSelection
= (IStructuredSelection
) viewer
202 Iterator it
= curSelection
.iterator();
203 while (it
.hasNext()) {
204 ResultAttributes curResult
= (ResultAttributes
) it
.next();
205 if (!removedResults
.contains(curResult
))
206 removedResults
.add(curResult
);
211 // View Specific inner class
214 class SelectionChangedListener
implements ISelectionChangedListener
{
215 public void selectionChanged(SelectionChangedEvent evt
) {
217 IStructuredSelection curSelection
= (IStructuredSelection
) evt
219 Object obj
= curSelection
.getFirstElement();
221 if (obj
instanceof ResultAttributes
) {
222 selectedRa
= (ResultAttributes
) obj
;
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
);
234 menuManager
.remove(ici
);
235 CommandContributionItemParameter contributionItemParameter
= new CommandContributionItemParameter(
236 locator
, null, cmdId
, SWT
.PUSH
);
239 contributionItemParameter
.label
= label
;
240 contributionItemParameter
.icon
= ClientUiPlugin
241 .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
;