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