]> git.argeo.org Git - gpl/argeo-slc.git/blob - eclipse/plugins/runtime/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/views/ResultListView.java
Add a Rap interface to handle SLC.
[gpl/argeo-slc.git] / eclipse / plugins / runtime / 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.List;
7 import java.util.Map;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.argeo.slc.SlcException;
12 import org.argeo.slc.client.ui.ClientUiPlugin;
13 import org.argeo.slc.core.test.tree.ResultAttributes;
14 import org.argeo.slc.dao.test.tree.TreeTestResultCollectionDao;
15 import org.eclipse.core.commands.Command;
16 import org.eclipse.core.commands.IParameter;
17 import org.eclipse.core.commands.Parameterization;
18 import org.eclipse.core.commands.ParameterizedCommand;
19 import org.eclipse.jface.action.IContributionItem;
20 import org.eclipse.jface.action.IMenuListener;
21 import org.eclipse.jface.action.IMenuManager;
22 import org.eclipse.jface.action.MenuManager;
23 import org.eclipse.jface.viewers.DoubleClickEvent;
24 import org.eclipse.jface.viewers.IDoubleClickListener;
25 import org.eclipse.jface.viewers.ISelectionChangedListener;
26 import org.eclipse.jface.viewers.IStructuredContentProvider;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.jface.viewers.ITableLabelProvider;
29 import org.eclipse.jface.viewers.LabelProvider;
30 import org.eclipse.jface.viewers.SelectionChangedEvent;
31 import org.eclipse.jface.viewers.TableViewer;
32 import org.eclipse.jface.viewers.Viewer;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.graphics.Image;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Menu;
38 import org.eclipse.swt.widgets.Table;
39 import org.eclipse.swt.widgets.TableColumn;
40 import org.eclipse.ui.IWorkbench;
41 import org.eclipse.ui.IWorkbenchWindow;
42 import org.eclipse.ui.commands.ICommandService;
43 import org.eclipse.ui.handlers.IHandlerService;
44 import org.eclipse.ui.menus.CommandContributionItem;
45 import org.eclipse.ui.menus.CommandContributionItemParameter;
46 import org.eclipse.ui.part.ViewPart;
47 import org.eclipse.ui.services.IServiceLocator;
48
49 public class ResultListView extends ViewPart {
50 private final static Log log = LogFactory.getLog(ResultListView.class);
51
52 public static final String ID = "org.argeo.slc.client.ui.resultListView";
53
54 private final static String DISPLAY_CMD_ID = "org.argeo.slc.client.ui.displayResultDetails";
55 private final static String DISPLAY_AS_XLS_CMD_ID = "com.capco.sparta.client.ui.displayResultDetailsWithExcel";
56 private final static String SAVE_AS_XLS_CMD_ID = "com.capco.sparta.client.ui.saveResultAsExcelFile";
57 private final static String UUID_PARAM_ID = "org.argeo.slc.client.commands.resultUuid";
58 private final static String NAME_PARAM_ID = "org.argeo.slc.client.commands.resultName";
59 private final static String PLATFORM = SWT.getPlatform();
60
61 private final static SimpleDateFormat dateFormatter = new SimpleDateFormat(
62 "MM/dd/yyyy 'at' HH:mm:ss");
63
64 private TableViewer viewer;
65 private TreeTestResultCollectionDao testResultCollectionDao;
66
67 private ResultAttributes selectedRa;
68
69 public void createPartControl(Composite parent) {
70 Table table = createTable(parent);
71 viewer = new TableViewer(table);
72 viewer.setLabelProvider(new ViewLabelProvider());
73 viewer.setContentProvider(new ViewContentProvider());
74 viewer.setInput(getViewSite());
75 viewer.addDoubleClickListener(new ViewDoubleClickListener());
76 viewer.addSelectionChangedListener(new SelectionChangedListener());
77
78 // create the context menu
79
80 MenuManager menuManager = new MenuManager();
81 Menu menu = menuManager.createContextMenu(viewer.getControl());
82 menuManager.addMenuListener(new IMenuListener() {
83 public void menuAboutToShow(IMenuManager manager) {
84 contextMenuAboutToShow(manager);
85 }
86 });
87
88 viewer.getControl().setMenu(menu);
89 getSite().registerContextMenu(menuManager, viewer);
90 }
91
92 protected Table createTable(Composite parent) {
93 int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL
94 | SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
95
96 Table table = new Table(parent, style);
97 // table.addMouseListener(new RightClickListener());
98 //GridData gridData = new GridData(GridData.FILL_BOTH);
99 //gridData.grabExcessVerticalSpace = true;
100 //gridData.grabExcessHorizontalSpace = true;
101 //gridData.horizontalSpan = 3;
102 //table.setLayoutData(gridData);
103
104 table.setLinesVisible(true);
105 table.setHeaderVisible(true);
106
107 TableColumn column = new TableColumn(table, SWT.LEFT, 0);
108 column.setText("Test Case");
109 column.setWidth(200);
110
111 column = new TableColumn(table, SWT.LEFT, 1);
112 column.setText("Close Date");
113 column.setWidth(120);
114
115 column = new TableColumn(table, SWT.LEFT, 2);
116 column.setText("UUID");
117 column.setWidth(300);
118
119 return table;
120 }
121
122 // TODO : Improve this methods.
123 // For now it is a workaround because we cannot dynamically update context
124 // menu to pass the UUID as command parameter
125 // public String[] getSelectedResult() {
126 // Object obj = ((IStructuredSelection) viewer.getSelection())
127 // .getFirstElement();
128 //
129 // String[] attributes = new String[2];
130 //
131 // if (obj == null || !(obj instanceof ResultAttributes))
132 // return null;
133 // else {
134 // ResultAttributes ra = (ResultAttributes) obj;
135 // attributes[0] = ra.getUuid();
136 // attributes[1] = (ra.getAttributes().get("testCase") == null) ? null
137 // : ra.getAttributes().get("testCase");
138 // return attributes;
139 // }
140 // }
141
142 // View Specific inner class
143
144 // Handle Events
145 class SelectionChangedListener implements ISelectionChangedListener {
146 public void selectionChanged(SelectionChangedEvent evt) {
147
148 IStructuredSelection curSelection = (IStructuredSelection) evt
149 .getSelection();
150 Object obj = curSelection.getFirstElement();
151
152 if (obj instanceof ResultAttributes) {
153 selectedRa = (ResultAttributes) obj;
154 }
155 }
156 }
157
158 private void refreshCommand(IMenuManager menuManager,
159 IServiceLocator locator, String cmdId, String label, String iconPath) {
160 IContributionItem ici = menuManager.find(cmdId);
161 if (ici != null)
162 menuManager.remove(ici);
163 CommandContributionItemParameter contributionItemParameter = new CommandContributionItemParameter(
164 locator, null, cmdId, SWT.PUSH);
165
166 // Set Params
167 contributionItemParameter.label = label;
168 contributionItemParameter.icon = ClientUiPlugin
169 .getImageDescriptor(iconPath);
170
171 Map<String, String> params = new HashMap<String, String>();
172 params.put(UUID_PARAM_ID, selectedRa.getUuid());
173 params.put(NAME_PARAM_ID,
174 (selectedRa.getAttributes().get("testCase") == null) ? null
175 : selectedRa.getAttributes().get("testCase"));
176 contributionItemParameter.parameters = params;
177
178 CommandContributionItem cci = new CommandContributionItem(
179 contributionItemParameter);
180 cci.setId(cmdId);
181 menuManager.add(cci);
182 }
183
184 private void contextMenuAboutToShow(IMenuManager menuManager) {
185
186 IWorkbenchWindow window = ClientUiPlugin.getDefault().getWorkbench()
187 .getActiveWorkbenchWindow();
188
189 refreshCommand(menuManager, window, DISPLAY_CMD_ID,
190 "Display as a tree", "icons/result_details.gif");
191 // We only show this command on windows OS
192 if (PLATFORM.equals("win32") || PLATFORM.equals("wpf")) {
193 refreshCommand(menuManager, window, DISPLAY_AS_XLS_CMD_ID,
194 "Display with Excel", "icons/excel.png");
195 }
196 refreshCommand(menuManager, window, SAVE_AS_XLS_CMD_ID,
197 "Save as Excel file", "icons/excel.png");
198 }
199
200 // Providers
201
202 protected static class ViewContentProvider implements
203 IStructuredContentProvider {
204
205 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
206 }
207
208 public void dispose() {
209 }
210
211 @SuppressWarnings("unchecked")
212 public Object[] getElements(Object obj) {
213 if (obj instanceof List) {
214 return ((List<ResultAttributes>) obj).toArray();
215 } else {
216 return new Object[0];
217 }
218 }
219 }
220
221 protected class ViewLabelProvider extends LabelProvider implements
222 ITableLabelProvider {
223 public String getColumnText(Object obj, int index) {
224 ResultAttributes ra = (ResultAttributes) obj;
225 switch (index) {
226 case 0:
227 return (ra.getAttributes().get("testCase") == null) ? null : ra
228 .getAttributes().get("testCase");
229 case 1:
230 // otherwise we get null pointer exception when the test is not
231 // closed yet.
232 return (ra.getCloseDate() == null) ? null : dateFormatter
233 .format(ra.getCloseDate());
234 case 2:
235 return ra.getUuid();
236 }
237 return getText(obj);
238 }
239
240 public Image getColumnImage(Object obj, int index) {
241 return null;
242 }
243
244 }
245
246 public void setFocus() {
247 viewer.getControl().setFocus();
248 }
249
250 public void retrieveResults() {
251 try {
252 List<ResultAttributes> lst = testResultCollectionDao
253 .listResultAttributes(null);
254 if (log.isTraceEnabled())
255 log.trace("Result attributes count: " + lst.size());
256 viewer.setInput(lst);
257 // viewer.refresh();
258 } catch (Exception e) {
259 // TODO Auto-generated catch block
260 e.printStackTrace();
261 }
262 }
263
264 // Handle Events
265
266 /**
267 * The ResultAttributes expose a part of the information contained in the
268 * TreeTestResult, It has the same UUID as the corresponding treeTestResult.
269 */
270 class ViewDoubleClickListener implements IDoubleClickListener {
271 public void doubleClick(DoubleClickEvent evt) {
272 Object obj = ((IStructuredSelection) evt.getSelection())
273 .getFirstElement();
274
275 if (obj instanceof ResultAttributes) {
276 ResultAttributes ra = (ResultAttributes) obj;
277 IWorkbench iw = ClientUiPlugin.getDefault().getWorkbench();
278 IHandlerService handlerService = (IHandlerService) iw
279 .getService(IHandlerService.class);
280 try {
281 // get the command from plugin.xml
282 IWorkbenchWindow window = iw.getActiveWorkbenchWindow();
283 ICommandService cmdService = (ICommandService) window
284 .getService(ICommandService.class);
285 Command cmd = cmdService
286 .getCommand("org.argeo.slc.client.ui.displayResultDetails");
287
288 // log.debug("cmd : " + cmd);
289 ArrayList<Parameterization> parameters = new ArrayList<Parameterization>();
290
291 // get the parameter
292 IParameter iparam = cmd
293 .getParameter("org.argeo.slc.client.commands.resultUuid");
294
295 Parameterization params = new Parameterization(iparam,
296 ra.getUuid());
297 parameters.add(params);
298
299 // build the parameterized command
300 ParameterizedCommand pc = new ParameterizedCommand(cmd,
301 parameters.toArray(new Parameterization[parameters
302 .size()]));
303
304 // execute the command
305 handlerService = (IHandlerService) window
306 .getService(IHandlerService.class);
307 handlerService.executeCommand(pc, null);
308
309 } catch (Exception e) {
310 e.printStackTrace();
311 throw new SlcException("Problem while rendering result. "
312 + e.getMessage());
313 }
314 }
315 }
316 }
317
318 // IoC
319 public void setTestResultCollectionDao(
320 TreeTestResultCollectionDao testResultCollectionDao) {
321 this.testResultCollectionDao = testResultCollectionDao;
322 }
323 }