]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/ToggleExternalFlag.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / handlers / ToggleExternalFlag.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/ToggleExternalFlag.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/ToggleExternalFlag.java
new file mode 100644 (file)
index 0000000..5ef611b
--- /dev/null
@@ -0,0 +1,115 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.modelBrowser.handlers;\r
+\r
+import java.util.Map;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.Command;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.commands.State;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.jface.viewers.ISelectionProvider;\r
+import org.eclipse.ui.IWorkbenchSite;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.eclipse.ui.commands.ICommandService;\r
+import org.eclipse.ui.commands.IElementUpdater;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.eclipse.ui.menus.UIElement;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.modeling.ui.Activator;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.ui.AdaptionUtils;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+\r
+public class ToggleExternalFlag extends AbstractHandler implements IElementUpdater {\r
+\r
+    private static Resource getFlagResource(ISelection sel) {\r
+        Resource r = AdaptionUtils.adaptToSingle(sel, Resource.class);\r
+        return r;\r
+    }\r
+\r
+    @Override\r
+    public Object execute(ExecutionEvent event) throws ExecutionException {\r
+        ISelection sel = HandlerUtil.getCurrentSelection(event);\r
+        final Resource resource = getFlagResource(sel);\r
+        if (resource == null)\r
+            return null;\r
+\r
+        SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+            @Override\r
+            public void perform(WriteGraph g) throws DatabaseException {\r
+                DiagramResource dr = DiagramResource.getInstance(g);\r
+                Boolean ext = g.hasStatement(resource, dr.ExternalFlag);\r
+                if (ext)\r
+                    g.deny(resource, dr.ExternalFlag);\r
+                else\r
+                    g.claim(resource, dr.ExternalFlag, resource);\r
+            }\r
+        });\r
+\r
+        return null;\r
+    }\r
+\r
+    @Override\r
+    public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {\r
+        ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);\r
+        Command command = commandService.getCommand("org.simantics.modeling.ui.toggleExternalFlag");\r
+\r
+        // Get a State object for the toggleSubscriptionGroup command\r
+        State state = command.getState("org.simantics.modeling.ui.toggleExternalFlag.state");\r
+        if (state == null) {\r
+            state = new State();\r
+            state.setValue(Boolean.TRUE);\r
+            command.addState("org.simantics.modeling.ui.toggleExternalFlag.state", state);\r
+        }\r
+\r
+        // Get the current value for the State object\r
+        Session s = SimanticsUI.peekSession();\r
+        if (s != null) {\r
+            Boolean value = Boolean.TRUE;\r
+            IWorkbenchSite site = (IWorkbenchSite) parameters.get("org.eclipse.ui.part.IWorkbenchPartSite");\r
+            ISelectionProvider sp = site.getSelectionProvider();\r
+            if (sp != null) {\r
+                final Resource resource = getFlagResource(sp.getSelection());\r
+                if (resource != null) {\r
+                    try {\r
+                        value = s.syncRequest(new Read<Boolean>() {\r
+                            @Override\r
+                            public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+                                DiagramResource dr = DiagramResource.getInstance(graph);\r
+                                return Boolean.valueOf(graph.hasStatement(resource, dr.ExternalFlag));\r
+                            }\r
+                        });\r
+                    } catch (DatabaseException e) {\r
+                        ErrorLogger.defaultLogError(e);\r
+                    }\r
+                }\r
+            }\r
+            state.setValue(value);\r
+        }\r
+\r
+        Boolean checked = (Boolean) state.getValue();\r
+        element.setChecked(checked);\r
+        element.setIcon(checked ? Activator.TICK_ICON : null);\r
+    }\r
+\r
+}\r
+\r