]> gerrit.simantics Code Review - simantics/platform.git/blob
d6570a68f93d67fcde8158a3dd8efe99b99eb5ed
[simantics/platform.git] /
1 package org.simantics.modeling.ui.diagram;
2
3 import java.util.Map;
4
5 import org.eclipse.core.runtime.CoreException;
6 import org.eclipse.core.runtime.IConfigurationElement;
7 import org.eclipse.core.runtime.IExecutableExtension;
8 import org.eclipse.jface.action.Action;
9 import org.eclipse.jface.action.ActionContributionItem;
10 import org.eclipse.jface.action.IContributionItem;
11 import org.eclipse.jface.resource.ImageDescriptor;
12 import org.eclipse.ui.IEditorPart;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.g2d.diagram.DiagramHints;
16 import org.simantics.g2d.diagram.IDiagram;
17 import org.simantics.g2d.layers.ILayersEditor;
18 import org.simantics.modeling.ui.Activator;
19 import org.simantics.modeling.ui.diagramEditor.DiagramEditor;
20 import org.simantics.ui.contribution.DynamicMenuContribution;
21 import org.simantics.utils.threads.AWTThread;
22 import org.simantics.utils.threads.ThreadUtils;
23 import org.simantics.utils.ui.workbench.WorkbenchUtils;
24
25 /**
26  * @author Tuukka Lehtonen
27  */
28 public class SetFocusabilityContribution extends DynamicMenuContribution implements IExecutableExtension {
29
30     String          name    = "";
31     ImageDescriptor image   = null;
32     boolean         allow = true;
33
34     @Override
35     public void setInitializationData(IConfigurationElement config, String propertyName, Object data)
36             throws CoreException {
37         if (data instanceof Map<?,?>) {
38             @SuppressWarnings("unchecked")
39             Map<String, String> args = (Map<String, String>) data;
40             name = args.get("name");
41             String imageId = args.get("image");
42             image = Activator.getDefault().getImageRegistry().getDescriptor(imageId);
43             allow = Boolean.parseBoolean(args.get("allow"));
44         }
45     }
46
47     @Override
48     protected Object[] getSelectedObjects() {
49         IEditorPart editorPart = WorkbenchUtils.getActiveEditor();
50         if (editorPart == null)
51             return NO_OBJECTS;
52         if(editorPart instanceof DiagramEditor) {
53             DiagramEditor editor = (DiagramEditor) editorPart;
54             IDiagram diagram = (IDiagram) editor.getAdapter(IDiagram.class);
55             if (diagram == null)
56                 return NO_OBJECTS;
57             ILayersEditor le = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR);
58             if (le == null)
59                 return NO_OBJECTS;
60             return new Object[] { le, le.getIgnoreFocusSettings() };
61         }
62         return NO_OBJECTS;
63     }
64
65     @Override
66     protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] elements) throws DatabaseException {
67         if (elements == NO_OBJECTS)
68             return NONE;
69
70         final ILayersEditor le = (ILayersEditor) elements[0];
71         final Boolean value = (Boolean) elements[1];
72
73         if (allow == value)
74             return NONE;
75
76         return new IContributionItem[] {
77                 new ActionContributionItem(new Action(name, image) {
78                     @Override
79                     public void run() {
80                         ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
81                             @Override
82                             public void run() {
83                                 le.setIgnoreFocusSettings(!value);
84                             }
85                         });
86                     }
87                 })
88         };
89     }
90
91 }