]> 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
ca2b4a5b6b9dcfe31052d3882d4e24279ceedc03
[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.util.ArrayList;
4 import java.util.List;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.argeo.slc.SlcException;
9 import org.argeo.slc.client.ui.ClientUiPlugin;
10 import org.argeo.slc.core.test.tree.ResultAttributes;
11 import org.argeo.slc.dao.test.tree.TreeTestResultCollectionDao;
12 import org.eclipse.core.commands.Command;
13 import org.eclipse.core.commands.IParameter;
14 import org.eclipse.core.commands.Parameterization;
15 import org.eclipse.core.commands.ParameterizedCommand;
16 import org.eclipse.jface.action.MenuManager;
17 import org.eclipse.jface.viewers.DoubleClickEvent;
18 import org.eclipse.jface.viewers.IDoubleClickListener;
19 import org.eclipse.jface.viewers.IStructuredContentProvider;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.ITableLabelProvider;
22 import org.eclipse.jface.viewers.LabelProvider;
23 import org.eclipse.jface.viewers.TableViewer;
24 import org.eclipse.jface.viewers.Viewer;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Menu;
30 import org.eclipse.swt.widgets.MenuItem;
31 import org.eclipse.swt.widgets.Table;
32 import org.eclipse.swt.widgets.TableColumn;
33 import org.eclipse.ui.IWorkbench;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.commands.ICommandService;
36 import org.eclipse.ui.handlers.IHandlerService;
37 import org.eclipse.ui.part.ViewPart;
38
39 public class ResultListView extends ViewPart {
40 private final static Log log = LogFactory.getLog(ResultListView.class);
41
42 public static final String ID = "org.argeo.slc.client.ui.resultListView";
43
44 private TableViewer viewer;
45
46 private TreeTestResultCollectionDao testResultCollectionDao;
47
48 public void createPartControl(Composite parent) {
49 Table table = createTable(parent);
50 viewer = new TableViewer(table);
51 viewer.setLabelProvider(new ViewLabelProvider());
52 viewer.setContentProvider(new ViewContentProvider());
53 viewer.setInput(getViewSite());
54 viewer.addDoubleClickListener(new ViewDoubleClickListener());
55
56 // Context Menu for the end user to choose what kind of display he wants
57 // Problem to dynamically add parameters linked with the current
58 // selected object
59 MenuManager menuManager = new MenuManager();
60 Menu menu = menuManager.createContextMenu(viewer.getControl());
61
62 // unable excel commands if not on windows
63 MenuItem[] items = menu.getItems();
64 String platform = SWT.getPlatform();
65 if (!(platform.equals("win32") || platform.equals("wpf"))) {
66 items[1].setEnabled(false);
67 }
68
69 viewer.getControl().setMenu(menu);
70 getSite().registerContextMenu(menuManager, viewer);
71 }
72
73 protected Table createTable(Composite parent) {
74 int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL
75 | SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
76
77 Table table = new Table(parent, style);
78 // table.addMouseListener(new RightClickListener());
79 GridData gridData = new GridData(GridData.FILL_BOTH);
80 gridData.grabExcessVerticalSpace = true;
81 gridData.grabExcessHorizontalSpace = true;
82 gridData.horizontalSpan = 3;
83 table.setLayoutData(gridData);
84
85 table.setLinesVisible(true);
86 table.setHeaderVisible(true);
87
88 TableColumn column = new TableColumn(table, SWT.LEFT, 0);
89 column.setText("Date");
90 column.setWidth(200);
91
92 column = new TableColumn(table, SWT.LEFT, 1);
93 column.setText("UUID");
94 column.setWidth(300);
95
96 return table;
97 }
98
99 // TODO : Improve this methods.
100 // For now it is a workaround because we cannot dynamically update context
101 // menu to pass the UUID as command parameter
102 public String[] getSelectedResult() {
103 Object obj = ((IStructuredSelection) viewer.getSelection())
104 .getFirstElement();
105
106 String[] attributes = new String[2];
107
108 if (obj == null || !(obj instanceof ResultAttributes))
109 return null;
110 else {
111 ResultAttributes ra = (ResultAttributes) obj;
112 attributes[0] = ra.getUuid();
113 attributes[1] = (ra.getAttributes().get("testCase") == null) ? null
114 : ra.getAttributes().get("testCase");
115 return attributes;
116 }
117 }
118
119 // View Specific inner class
120 protected static class ViewContentProvider implements
121 IStructuredContentProvider {
122
123 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
124 }
125
126 public void dispose() {
127 }
128
129 @SuppressWarnings("unchecked")
130 public Object[] getElements(Object obj) {
131 if (obj instanceof List) {
132 return ((List<ResultAttributes>) obj).toArray();
133 } else {
134 return new Object[0];
135 }
136 }
137 }
138
139 protected class ViewLabelProvider extends LabelProvider implements
140 ITableLabelProvider {
141 public String getColumnText(Object obj, int index) {
142 ResultAttributes ra = (ResultAttributes) obj;
143 switch (index) {
144 case 0:
145 return getText(ra.getCloseDate());
146 case 1:
147 return ra.getUuid();
148 }
149 return getText(obj);
150 }
151
152 public Image getColumnImage(Object obj, int index) {
153 return null;
154 }
155
156 }
157
158 public void setFocus() {
159 viewer.getControl().setFocus();
160 }
161
162 public void retrieveResults() {
163 try {
164 List<ResultAttributes> lst = testResultCollectionDao
165 .listResultAttributes(null);
166 if (log.isTraceEnabled())
167 log.trace("Result attributes count: " + lst.size());
168 viewer.setInput(lst);
169 // viewer.refresh();
170 } catch (Exception e) {
171 // TODO Auto-generated catch block
172 e.printStackTrace();
173 }
174 }
175
176 // Handle Events
177
178 /**
179 * The ResultAttributes expose a part of the information contained in the
180 * TreeTestResult, It has the same UUID as the corresponding treeTestResult.
181 */
182 class ViewDoubleClickListener implements IDoubleClickListener {
183 public void doubleClick(DoubleClickEvent evt) {
184 Object obj = ((IStructuredSelection) evt.getSelection())
185 .getFirstElement();
186
187 if (obj instanceof ResultAttributes) {
188 ResultAttributes ra = (ResultAttributes) obj;
189
190 IWorkbench iw = ClientUiPlugin.getDefault().getWorkbench();
191 IHandlerService handlerService = (IHandlerService) iw
192 .getService(IHandlerService.class);
193 try {
194 // get the command from plugin.xml
195 IWorkbenchWindow window = iw.getActiveWorkbenchWindow();
196 ICommandService cmdService = (ICommandService) window
197 .getService(ICommandService.class);
198 Command cmd = cmdService
199 .getCommand("org.argeo.slc.client.ui.displayResultDetails");
200
201 // log.debug("cmd : " + cmd);
202 ArrayList<Parameterization> parameters = new ArrayList<Parameterization>();
203
204 // get the parameter
205 IParameter iparam = cmd
206 .getParameter("org.argeo.slc.client.commands.resultUuid");
207
208 Parameterization params = new Parameterization(iparam,
209 ra.getUuid());
210 parameters.add(params);
211
212 // build the parameterized command
213 ParameterizedCommand pc = new ParameterizedCommand(cmd,
214 parameters.toArray(new Parameterization[parameters
215 .size()]));
216
217 // execute the command
218 handlerService = (IHandlerService) window
219 .getService(IHandlerService.class);
220 handlerService.executeCommand(pc, null);
221
222 } catch (Exception e) {
223 e.printStackTrace();
224 throw new SlcException("Problem while rendering result. "
225 + e.getMessage());
226 }
227 }
228 }
229 }
230
231 // IoC
232 public void setTestResultCollectionDao(
233 TreeTestResultCollectionDao testResultCollectionDao) {
234 this.testResultCollectionDao = testResultCollectionDao;
235 }
236 }