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