]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/contribution/DiagramDynamicMenuContribution.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / contribution / DiagramDynamicMenuContribution.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.contribution;
13
14 import org.eclipse.core.runtime.Platform;
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.jface.action.ActionContributionItem;
17 import org.eclipse.jface.action.IContributionItem;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.swt.widgets.Menu;
20 import org.eclipse.ui.IWorkbenchPart;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Session;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.g2d.canvas.ICanvasContext;
25 import org.simantics.g2d.diagram.participant.Selection;
26 import org.simantics.ui.contribution.DynamicMenuContribution;
27 import org.simantics.utils.threads.ThreadUtils;
28 import org.simantics.utils.ui.BundleUtils;
29 import org.simantics.utils.ui.workbench.WorkbenchUtils;
30
31
32 /**
33  * @author Antti Villberg
34  */
35 abstract public class DiagramDynamicMenuContribution<T> extends DynamicMenuContribution {
36
37         private static final IContributionItem[] NONE = {};
38
39         private ICanvasContext canvas;
40
41         @Override
42         public void fill(Menu menu, int index) {
43                 // Need to grab active part here, we're still in the SWT thread.
44                 IWorkbenchPart activePart = WorkbenchUtils.getActiveWorkbenchPart();
45                 if (activePart == null)
46                         return;
47                 ICanvasContext ctx = (ICanvasContext) activePart.getAdapter(ICanvasContext.class);
48                 if (ctx == null)
49                         return;
50
51                 this.canvas = ctx;
52                 try {
53                         super.fill(menu, index);
54                 } finally {
55                         this.canvas = null;
56                 }
57         }
58
59         @Override
60         protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection) throws DatabaseException {
61
62                 T input = computeInput(graph, selection);
63
64                 if(input == null) return NONE;
65
66                 return new IContributionItem[] {
67                                 new ActionContributionItem(new Helper(graph.getSession(), canvas, input))
68                 };
69
70         }
71
72         public class Helper extends Action {
73
74                 private final T input;
75                 protected final Session        session;
76                 protected final ICanvasContext context;
77
78                 public Helper(Session session, ICanvasContext context, T input) {
79                         super(getName(), getImage());
80                         this.session = session;
81                         this.context = context;
82                         this.input = input;
83                 }
84
85                 @Override
86                 public void run() {
87                         perform(session, context, input);
88                         ThreadUtils.asyncExec(context.getThreadAccess(), new Runnable() {
89                                 @Override
90                                 public void run() {
91                                         if (context.isDisposed())
92                                                 return;
93                                         Selection selection = context.getAtMostOneItemOfClass(Selection.class);
94                                         if (selection != null) {
95                                                 // This prevents workbench selection from being left over.
96                                                 // Also prevents scene graph crap from being left on the screen.
97                                                 selection.clear(0);
98                                         }
99                                 }
100                         });
101                 }
102
103         }
104
105         protected ImageDescriptor silk(String name) {
106                 return BundleUtils.getImageDescriptorFromBundle(Platform.getBundle("com.famfamfam.silk"), "/icons/" + name);
107         }
108
109         abstract protected T computeInput(ReadGraph graph, Object[] selection) throws DatabaseException;
110         abstract protected void perform(Session session, ICanvasContext context, T input);
111         abstract protected String getName();
112         abstract protected ImageDescriptor getImage();
113
114 }