Method: {...}

1: /*
2: * #%~
3: * org.overture.ide.plugins.poviewer
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.poviewer.view;
23:
24: import java.util.List;
25:
26: import org.eclipse.core.resources.IFile;
27: import org.eclipse.core.runtime.IProgressMonitor;
28: import org.eclipse.core.runtime.IStatus;
29: import org.eclipse.core.runtime.Status;
30: import org.eclipse.jface.action.Action;
31: import org.eclipse.jface.viewers.ColumnWeightData;
32: import org.eclipse.jface.viewers.DoubleClickEvent;
33: import org.eclipse.jface.viewers.IDoubleClickListener;
34: import org.eclipse.jface.viewers.ISelection;
35: import org.eclipse.jface.viewers.ISelectionChangedListener;
36: import org.eclipse.jface.viewers.IStructuredContentProvider;
37: import org.eclipse.jface.viewers.IStructuredSelection;
38: import org.eclipse.jface.viewers.ITableLabelProvider;
39: import org.eclipse.jface.viewers.LabelProvider;
40: import org.eclipse.jface.viewers.SelectionChangedEvent;
41: import org.eclipse.jface.viewers.TableLayout;
42: import org.eclipse.jface.viewers.TableViewer;
43: import org.eclipse.jface.viewers.Viewer;
44: import org.eclipse.swt.SWT;
45: import org.eclipse.swt.graphics.Image;
46: import org.eclipse.swt.widgets.Composite;
47: import org.eclipse.swt.widgets.Display;
48: import org.eclipse.swt.widgets.TableColumn;
49: import org.eclipse.ui.ISelectionListener;
50: import org.eclipse.ui.IViewPart;
51: import org.eclipse.ui.IWorkbenchPage;
52: import org.eclipse.ui.IWorkbenchPart;
53: import org.eclipse.ui.PartInitException;
54: import org.eclipse.ui.PlatformUI;
55: import org.eclipse.ui.part.ViewPart;
56: import org.eclipse.ui.progress.UIJob;
57: import org.overture.ide.core.ElementChangedEvent;
58: import org.overture.ide.core.ElementChangedEvent.DeltaType;
59: import org.overture.ide.core.IElementChangedListener;
60: import org.overture.ide.core.IVdmElement;
61: import org.overture.ide.core.IVdmElementDelta;
62: import org.overture.ide.core.IVdmModel;
63: import org.overture.ide.core.VdmCore;
64: import org.overture.ide.core.resources.IVdmProject;
65: import org.overture.ide.plugins.poviewer.IPoviewerConstants;
66: import org.overture.ide.plugins.poviewer.PoGeneratorUtil;
67: import org.overture.ide.ui.utility.EditorUtility;
68: import org.overture.pog.obligation.ProofObligation;
69: import org.overture.pog.pub.IProofObligation;
70:
71: public class PoOverviewTableView extends ViewPart implements ISelectionListener {
72:
73:         protected TableViewer viewer;
74:         protected Action doubleClickAction;
75:         protected Display display = Display.getCurrent();
76:         protected IVdmProject project;
77:
78:         protected class ViewContentProvider implements IStructuredContentProvider {
79:                 public void inputChanged(Viewer v, Object oldInput, Object newInput) {
80:                 }
81:
82:                 public void dispose() {
83:                 }
84:
85:                 public Object[] getElements(Object inputElement) {
86:                         if (inputElement instanceof List) {
87:                                 @SuppressWarnings("rawtypes")
88:                                 List list = (List) inputElement;
89:                                 return list.toArray();
90:                         }
91:                         return new Object[0];
92:                 }
93:
94:         }
95:
96:         class ViewLabelProvider extends LabelProvider implements
97:                         ITableLabelProvider {
98:
99:                 public void resetCounter() {
100:                         count = 0;
101:                 }
102:
103:                 private Integer count = 0;
104:
105:                 public String getColumnText(Object element, int columnIndex) {
106:                         ProofObligation data = (ProofObligation) element;
107:                         String columnText;
108:                         switch (columnIndex) {
109:                         case 0:
110:                                 count++;
111:                                 columnText = new Integer(data.number).toString();// count.toString();
112:                                 break;
113:                         case 1:
114:                                 if (!data.getLocation().getModule().equals("DEFAULT"))
115:                                         columnText = data.getLocation().getModule() + "`"
116:                                                         + data.name;
117:                                 else
118:                                         columnText = data.name;
119:                                 break;
120:                         case 2:
121:                                 columnText = data.kind.toString();
122:                                 break;
123:                         default:
124:                                 columnText = "not set";
125:                         }
126:                         return columnText;
127:
128:                 }
129:
130:                 @Override
131:                 public Image getColumnImage(Object element, int columnIndex)
132:                 {
133:                         return null;
134:                 }
135:
136:         }
137:
138:
139:         private IElementChangedListener vdmlistner = new IElementChangedListener() {
140:
141:                 @Override
142:                 public void elementChanged(ElementChangedEvent event) {
143:
144:                         if (event.getType() == DeltaType.POST_RECONCILE) {
145:
146:                                 if (event.getDelta().getKind() == IVdmElementDelta.F_TYPE_CHECKED) {
147:
148:                                         final IVdmElement source = event.getDelta().getElement();
149:
150:                                         final UIJob showJob = new UIJob("Generating Proof Obligations") {
151:
152:                                                 @Override
153:                                                 public IStatus runInUIThread(IProgressMonitor monitor) {
154:
155:                                                         if (source instanceof IVdmModel) {
156:                                                                 IVdmModel castSource = (IVdmModel) source;
157:                                                                 IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
158:                                                                 if (!page.getPerspective().getId().equals(IPoviewerConstants.ProofObligationPerspectiveId))
159:                                                                 {
160:                                                                         return new Status(IStatus.OK,
161:                                                                                         "org.overture.ide.plugins.poviewer", "Ok");
162:                                                                 }
163:                                                                 PoGeneratorUtil util = new PoGeneratorUtil(
164:                                                                                 display.getActiveShell(), page
165:                                                                                                 .getActivePart().getSite());
166:
167:                                                                 if (!util.isPoggedModel(castSource)){
168:                                                                         return new Status(IStatus.OK,
169:                                                                                         "org.overture.ide.plugins.poviewer", "Ok");
170:                                                                 }
171:                                                                 util.generate(castSource);
172:
173:                                                                 System.out.println("built something");
174:                                                         }
175:
176:                                                         if (viewer != null && viewer.getControl() != null
177:                                                                         && viewer.getControl().getDisplay() != null)
178:                                                                 viewer.getControl().getDisplay()
179:                                                                                 .asyncExec(new Runnable() {
180:                                                                                         /*
181:                                                                                          * (non-Javadoc)
182:                                                                                          *
183:                                                                                          * @see java.lang.Runnable#run()
184:                                                                                          */
185:                                                                                         public void run() {
186:                                                                                                 if (!viewer.getControl()
187:                                                                                                                 .isDisposed()) {
188:                                                                                                         viewer.refresh();
189:                                                                                                 }
190:                                                                                         }
191:                                                                                 });
192:
193:                                                         return new Status(IStatus.OK,
194:                                                                         "org.overture.ide.plugins.poviewer", "Ok");
195:                                                 }
196:
197:                                         };
198:                                         showJob.schedule();
199:
200:                                 }
201:
202:                         }
203:
204:                 }
205:
206:         };
207:
208:         /**
209:          * The constructor.
210:          */
211:         public PoOverviewTableView() {
212:                 VdmCore.addElementChangedListener(vdmlistner);
213:         }
214:
215:         @Override
216:         public void dispose() {
217:                 super.dispose();
218:         VdmCore.removeElementChangedListener(vdmlistner);
219:         }
220:
221:         /**
222:          * This is a callback that will allow us to create the viewer and initialize
223:          * it.
224:          */
225:         @Override
226:         public void createPartControl(Composite parent) {
227:                 viewer = new TableViewer(parent, SWT.FULL_SELECTION | SWT.H_SCROLL
228:                                 | SWT.V_SCROLL);
229:                 // test setup columns...
230:                 TableLayout layout = new TableLayout();
231:                 layout.addColumnData(new ColumnWeightData(20, true));
232:                 layout.addColumnData(new ColumnWeightData(30, true));
233:                 layout.addColumnData(new ColumnWeightData(50, false));
234:                 viewer.getTable().setLayout(layout);
235:                 viewer.getTable().setLinesVisible(true);
236:                 viewer.getTable().setHeaderVisible(true);
237:                 viewer.getTable().setSortDirection(SWT.NONE);
238:                 viewer.setSorter(null);
239:
240:                 TableColumn column01 = new TableColumn(viewer.getTable(), SWT.LEFT);
241:                 column01.setText("No.");
242:                 column01.setToolTipText("No.");
243:
244:                 TableColumn column = new TableColumn(viewer.getTable(), SWT.LEFT);
245:                 column.setText("PO Name");
246:                 column.setToolTipText("PO Name");
247:
248:                 TableColumn column2 = new TableColumn(viewer.getTable(), SWT.LEFT);
249:                 column2.setText("Type");
250:                 column2.setToolTipText("Show Type");
251:                 
252:                 viewer.setContentProvider(new ViewContentProvider());
253:                 viewer.setLabelProvider(new ViewLabelProvider());
254:
255:                 makeActions();
256:                 hookDoubleClickAction();
257:
258:                 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
259:
260:                         public void selectionChanged(SelectionChangedEvent event) {
261:
262:                                 Object first = ((IStructuredSelection) event.getSelection())
263:                                                 .getFirstElement();
264:                                 if (first instanceof ProofObligation) {
265:                                         try {
266:                                                 IViewPart v = getSite().getPage().showView(
267:                                                                 IPoviewerConstants.PoTableViewId);
268:
269:                                                 if (v instanceof PoTableView)
270:                                                         ((PoTableView) v).setDataList(project,
271:                                                                         (ProofObligation) first);
272:                                         } catch (PartInitException e) {
273:
274:                                                 e.printStackTrace();
275:                                         }
276:                                 }
277:
278:                         }
279:                 });
280:         }
281:
282:         protected void makeActions() {
283:                 doubleClickAction = new Action() {
284:                         @Override
285:                         public void run() {
286:                                 ISelection selection = viewer.getSelection();
287:                                 Object obj = ((IStructuredSelection) selection)
288:                                                 .getFirstElement();
289:                                 if (obj instanceof ProofObligation) {
290:                                         gotoDefinition((ProofObligation) obj);
291:                                         // showMessage(((ProofObligation) obj).toString());
292:                                 }
293:                         }
294:
295:                         private void gotoDefinition(ProofObligation po) {
296:                                 IFile file = project.findIFile(po.getLocation().getFile());
297:                                 if (IVdmProject.externalFileContentType.isAssociatedWith(file
298:                                                 .getName())) {
299:                                         EditorUtility.gotoLocation(
300:                                                         IPoviewerConstants.ExternalEditorId, file,
301:                                                         po.getLocation(), po.name);
302:                                 } else {
303:                                         EditorUtility.gotoLocation(file, po.getLocation(), po.name);
304:                                 }
305:
306:                         }
307:                 };
308:
309:         }
310:
311:         protected void hookDoubleClickAction() {
312:                 viewer.addDoubleClickListener(new IDoubleClickListener() {
313:                         public void doubleClick(DoubleClickEvent event) {
314:                                 doubleClickAction.run();
315:                         }
316:                 });
317:         }
318:
319:         // private void showMessage(String message)
320:         // {
321:         // MessageDialog.openInformation(
322:         // viewer.getControl().getShell(),
323:         // "PO Test",
324:         // message);
325:         // }
326:
327:         /**
328:          * Passing the focus request to the viewer's control.
329:          */
330:         @Override
331:         public void setFocus() {
332:                 viewer.getControl().setFocus();
333:         }
334:
335:         public void selectionChanged(IWorkbenchPart part, ISelection selection) {
336:
337:                 if (selection instanceof IStructuredSelection
338:                                 && part instanceof PoOverviewTableView) {
339:                         Object first = ((IStructuredSelection) selection).getFirstElement();
340:                         if (first instanceof ProofObligation) {
341:                                 try {
342:                                         IViewPart v = part
343:                                                         .getSite()
344:                                                         .getPage()
345:                                                         .showView(
346:                                                                         "org.overture.ide.plugins.poviewer.views.PoTableView");
347:
348:                                         if (v instanceof PoTableView)
349:                                                 ((PoTableView) v).setDataList(project,
350:                                                                 (ProofObligation) first);
351:                                 } catch (PartInitException e) {
352:
353:                                         e.printStackTrace();
354:                                 }
355:                         }
356:                 }
357:
358:         }
359:
360:         public void refreshList() {
361:                 display.asyncExec(new Runnable() {
362:
363:                         public void run() {
364:                                 viewer.refresh();
365:                         }
366:
367:                 });
368:         }
369:
370:         public void setDataList(final IVdmProject project,
371:                         final List<IProofObligation> data) {
372:                 this.project = project;
373:                 display.asyncExec(new Runnable() {
374:
375:                         public void run() {
376:                                 if (viewer.getLabelProvider() instanceof ViewLabelProvider)
377:                                         ((ViewLabelProvider) viewer.getLabelProvider())
378:                                                         .resetCounter(); // this
379:                                                                                                 // is
380:                                                                                                 // needed
381:                                                                                                 // to
382:                                                                                                 // reset
383:                                                                                                 // the
384:                                 // numbering
385:
386:                                 viewer.setInput(data);
387:
388:                                 for (TableColumn col : viewer.getTable().getColumns()) {
389:                                         col.pack();
390:                                 }
391:                         }
392:
393:                 });
394:         }
395: }