Method: createPartControl(Composite)

1: /*
2: * #%~
3: * Combinatorial Testing
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.plugins.combinatorialtesting.views;
23:
24: import java.util.ArrayList;
25: import java.util.List;
26:
27: import org.eclipse.jface.action.Action;
28: import org.eclipse.jface.dialogs.MessageDialog;
29: import org.eclipse.jface.viewers.ColumnWeightData;
30: import org.eclipse.jface.viewers.DoubleClickEvent;
31: import org.eclipse.jface.viewers.IDoubleClickListener;
32: import org.eclipse.jface.viewers.ISelection;
33: import org.eclipse.jface.viewers.IStructuredContentProvider;
34: import org.eclipse.jface.viewers.IStructuredSelection;
35: import org.eclipse.jface.viewers.ITableLabelProvider;
36: import org.eclipse.jface.viewers.LabelProvider;
37: import org.eclipse.jface.viewers.TableLayout;
38: import org.eclipse.jface.viewers.TableViewer;
39: import org.eclipse.jface.viewers.Viewer;
40: import org.eclipse.jface.viewers.ViewerSorter;
41: import org.eclipse.swt.SWT;
42: import org.eclipse.swt.graphics.Image;
43: import org.eclipse.swt.widgets.Composite;
44: import org.eclipse.swt.widgets.Display;
45: import org.eclipse.swt.widgets.TableColumn;
46: import org.eclipse.ui.ISelectionListener;
47: import org.eclipse.ui.ISharedImages;
48: import org.eclipse.ui.IWorkbenchPart;
49: import org.eclipse.ui.PlatformUI;
50: import org.eclipse.ui.part.ViewPart;
51: import org.eclipse.ui.views.IViewDescriptor;
52: import org.overture.ct.utils.TraceTestResult;
53: import org.overture.ide.plugins.combinatorialtesting.ITracesConstants;
54: import org.overture.ide.plugins.combinatorialtesting.OvertureTracesPlugin;
55: import org.overture.ide.plugins.combinatorialtesting.views.treeView.NotYetReadyTreeNode;
56: import org.overture.ide.plugins.combinatorialtesting.views.treeView.TraceTestGroup;
57: import org.overture.ide.plugins.combinatorialtesting.views.treeView.TraceTestTreeNode;
58: import org.overture.interpreter.traces.Verdict;
59:
60: /**
61: * This sample class demonstrates how to plug-in a new workbench view. The view shows data obtained from the model. The
62: * sample creates a dummy model on the fly, but a real implementation would connect to the model available either in
63: * this or another plug-in (e.g. the workspace). The view is connected to the model using a content provider.
64: * <p>
65: * The view uses a label provider to define how model objects should be presented in the view. Each view can present the
66: * same model objects using different labels and icons, if needed. Alternatively, a single label provider can be shared
67: * between views in order to ensure that objects of the same type are presented in the same way everywhere.
68: * <p>
69: */
70:
71: public class TraceTest extends ViewPart implements ISelectionListener
72: {
73:         private TableViewer viewer;
74:         private Action action1;
75:         private Action action2;
76:         private Action doubleClickAction;
77:         final Display display = Display.getCurrent();
78:
79:         private class Data
80:         {
81:                 String traceDef;
82:                 String description;
83:                 Verdict status;
84:
85:                 private Data(String traceDef, String Description, Verdict status)
86:                 {
87:                         this.description = Description;
88:                         this.traceDef = traceDef;
89:                         this.status = status;
90:                 }
91:
92:                 public String GetDescription()
93:                 {
94:                         return this.description;
95:                 }
96:
97:         }
98:
99:         /*
100:          * The content provider class is responsible for providing objects to the view. It can wrap existing objects in
101:          * adapters or simply return objects as-is. These objects may be sensitive to the current input of the view, or
102:          * ignore it and always show the same content (like Task List, for example).
103:          */
104:
105:         class ViewContentProvider implements IStructuredContentProvider
106:         {
107:                 public void inputChanged(Viewer v, Object oldInput, Object newInput)
108:                 {
109:                 }
110:
111:                 public void dispose()
112:                 {
113:                 }
114:
115:                 public Object[] getElements(Object inputElement)
116:                 {
117:                         if (inputElement instanceof List)
118:                         {
119:                                 @SuppressWarnings("rawtypes")
120:                                 List list = (List) inputElement;
121:                                 return list.toArray();
122:                         }
123:                         return new Object[0];
124:                 }
125:
126:         }
127:
128:         class ViewLabelProvider extends LabelProvider implements
129:                         ITableLabelProvider
130:         {
131:                 public String getColumnText(Object element, int columnIndex)
132:                 {
133:                         Data data = (Data) element;
134:                         String columnText;
135:                         switch (columnIndex)
136:                         {
137:                                 case 0:
138:                                         columnText = data.traceDef;
139:                                         break;
140:                                 case 1:
141:                                         columnText = data.description; // StringConverter.asString(data.number);
142:                                         break;
143:                                 case 2:
144:                                         columnText = ""; // StringConverter.asString(data.number);
145:                                         break;
146:                                 default:
147:                                         columnText = "not set";
148:                         }
149:                         return columnText;
150:
151:                 }
152:
153:                 public Image getColumnImage(Object obj, int index)
154:                 {
155:                         if (index == 2)
156:                         {
157:                                 return getImage(obj);
158:                         }
159:                         return null;
160:                 }
161:
162:                 @Override
163:                 public Image getImage(Object obj)
164:                 {
165:                         Data data = (Data) obj;
166:
167:                         String imgPath = OvertureTracesPlugin.IMG_TRACE_TEST_CASE_UNKNOWN;
168:
169:                         if (data.status == Verdict.PASSED)
170:                         {
171:                                 imgPath = OvertureTracesPlugin.IMG_TRACE_TEST_CASE_SUCCES;
172:                         } else if (data.status == null)
173:                         {
174:                                 imgPath = OvertureTracesPlugin.IMG_TRACE_TEST_CASE_UNKNOWN;
175:                         } else if (data.status == Verdict.INCONCLUSIVE)
176:                         {
177:                                 imgPath = OvertureTracesPlugin.IMG_TRACE_TEST_CASE_UNDETERMINED;
178:                         } else if (data.status == Verdict.FAILED)
179:                         {
180:                                 imgPath = OvertureTracesPlugin.IMG_TRACE_TEST_CASE_FAIL;
181:                         } else if (data.status == Verdict.SKIPPED)
182:                         {
183:                                 imgPath = OvertureTracesPlugin.IMG_TRACE_TEST_CASE_SKIPPED;
184:                         }
185:
186:                         return OvertureTracesPlugin.getImageDescriptor(imgPath).createImage();
187:
188:                         // return
189:                         // PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
190:
191:                 }
192:         }
193:
194:         class NameSorter extends ViewerSorter
195:         {
196:         }
197:
198:         /**
199:          * The constructor.
200:          */
201:         public TraceTest()
202:         {
203:         }
204:
205:         /**
206:          * This is a callback that will allow us to create the viewer and initialize it.
207:          */
208:         @Override
209:         public void createPartControl(Composite parent)
210:         {
211:                 viewer = new TableViewer(parent, SWT.FULL_SELECTION | SWT.H_SCROLL
212:                                 | SWT.V_SCROLL);
213:                 // test setup columns...
214:                 TableLayout layout = new TableLayout();
215:                 layout.addColumnData(new ColumnWeightData(50, 100, true));
216:                 layout.addColumnData(new ColumnWeightData(50, 100, true));
217:                 // layout.addColumnData(new ColumnWeightData(50, 100, true));
218:                 viewer.getTable().setLayout(layout);
219:                 viewer.getTable().setLinesVisible(true);
220:                 viewer.getTable().setHeaderVisible(true);
221:                 viewer.getTable().setSortDirection(SWT.NONE);
222:                 viewer.setSorter(null);
223:
224:                 TableColumn column = new TableColumn(viewer.getTable(), SWT.LEFT);
225:                 column.setText("Trace Test case");
226:                 column.setToolTipText("Trace Name");
227:
228:                 TableColumn column2 = new TableColumn(viewer.getTable(), SWT.LEFT);
229:                 column2.setText("Result");
230:                 column2.setToolTipText("Show Description");
231:
232:                 viewer.setContentProvider(new ViewContentProvider());
233:                 viewer.setLabelProvider(new ViewLabelProvider());
234:                 // viewer.setSorter(new NameSorter());
235:
236:                 // input
237:
238:
239:                 makeActions();
240:                 hookDoubleClickAction();
241:
242:         }
243:
244:         private void makeActions()
245:         {
246:                 action1 = new Action()
247:                 {
248:                         @Override
249:                         public void run()
250:                         {
251:                                 showMessage("Action 1 executed");
252:                                 for (IViewDescriptor v : getViewSite().getWorkbenchWindow().getWorkbench().getViewRegistry().getViews())
253:                                 {
254:                                         if (v.getId().equals(ITracesConstants.TRACES_VIEW_ID))
255:                                         {
256:                                                 this.addListenerObject(v);
257:                                         }
258:                                 }
259:                         }
260:                 };
261:                 action1.setText("Action 1");
262:                 action1.setToolTipText("Action 1 tooltip");
263:                 action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
264:
265:                 action2 = new Action()
266:                 {
267:                         @Override
268:                         public void run()
269:                         {
270:                                 showMessage("Action 2 executed");
271:                         }
272:                 };
273:                 action2.setText("Action 2");
274:                 action2.setToolTipText("Action 2 tooltip");
275:                 action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
276:                 doubleClickAction = new Action()
277:                 {
278:                         @Override
279:                         public void run()
280:                         {
281:                                 ISelection selection = viewer.getSelection();
282:                                 Object obj = ((IStructuredSelection) selection).getFirstElement();
283:                                 if (obj instanceof Data)
284:                                 {
285:                                         showMessage(((Data) obj).GetDescription().toString());
286:                                 }
287:                         }
288:                 };
289:         }
290:
291:         private void hookDoubleClickAction()
292:         {
293:                 viewer.addDoubleClickListener(new IDoubleClickListener()
294:                 {
295:                         public void doubleClick(DoubleClickEvent event)
296:                         {
297:                                 doubleClickAction.run();
298:                         }
299:                 });
300:         }
301:
302:         private void showMessage(String message)
303:         {
304:                 MessageDialog.openInformation(viewer.getControl().getShell(), "Trace Test", message);
305:         }
306:
307:         /**
308:          * Passing the focus request to the viewer's control.
309:          */
310:         @Override
311:         public void setFocus()
312:         {
313:                 viewer.getControl().setFocus();
314:         }
315:
316:         public void selectionChanged(IWorkbenchPart part, ISelection selection)
317:         {
318:
319:                 if (selection instanceof IStructuredSelection
320:                                 && part instanceof TracesTreeView)
321:                 {
322:                         Object first = ((IStructuredSelection) selection).getFirstElement();
323:                         // System.out.println(first);
324:                         if (first instanceof TraceTestTreeNode
325:                                         && part instanceof TracesTreeView
326:                                         && !(first instanceof NotYetReadyTreeNode)
327:                                         && !(first instanceof TraceTestGroup))
328:                         {
329:                                 TraceTestTreeNode traceTestCaseNode = (TraceTestTreeNode) first;
330:
331:                                 TraceTestResult res = traceTestCaseNode.getResult();
332:
333:                                 List<Data> list = new ArrayList<Data>();
334:
335:                                 for (int i = 0; res != null && i < res.getArguments().size(); i++)
336:                                 {
337:
338:                                         if (res.getResults().size() > i)
339:                                         {
340:                                                 list.add(new Data(res.getArguments().get(i), res.getResults().get(i), res.getStatus()));
341:                                         } else if (res.getResults().size() <= i)
342:                                         {
343:                                                 list.add(new Data(res.getArguments().get(i), "N / A", res.getStatus()));
344:                                         }
345:                                 }
346:
347:                                 viewer.setInput(list);
348:
349:                         } else
350:                         {
351:                                 viewer.setInput(null);
352:
353:                         }
354:                         refreshList();
355:                 }
356:
357:         }
358:
359:         private void refreshList()
360:         {
361:                 display.asyncExec(new Runnable()
362:                 {
363:
364:                         public void run()
365:                         {
366:                                 viewer.refresh();
367:                         }
368:
369:                 });
370:         }
371: }