]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.event/src/org/simantics/event/view/handler/PreferenceHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.event / src / org / simantics / event / view / handler / PreferenceHandler.java
diff --git a/bundles/org.simantics.event/src/org/simantics/event/view/handler/PreferenceHandler.java b/bundles/org.simantics.event/src/org/simantics/event/view/handler/PreferenceHandler.java
new file mode 100644 (file)
index 0000000..8f9da61
--- /dev/null
@@ -0,0 +1,112 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 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.event.view.handler;\r
+\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.jface.viewers.ISelection;\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.Simantics;\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.utils.TagUtil;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.SelectionHints;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+import org.simantics.utils.ui.ISelectionUtils;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+class PreferenceHandler extends AbstractHandler implements IElementUpdater {\r
+\r
+    protected final String virtualGraphId;\r
+    private final String   tagURI;\r
+    private final boolean  tag;\r
+\r
+    public PreferenceHandler() {\r
+        this("preferences", null, false);\r
+    }\r
+\r
+    public PreferenceHandler(String tagURI, boolean tag) {\r
+        this("preferences", tagURI, tag);\r
+    }\r
+\r
+    public PreferenceHandler(String virtualGraphId) {\r
+        this(virtualGraphId, null, false);\r
+    }\r
+\r
+    public PreferenceHandler(String virtualGraphId, String tagURI, boolean tag) {\r
+        this.virtualGraphId = virtualGraphId;\r
+        this.tagURI = tagURI;\r
+        this.tag = tag;\r
+    }\r
+\r
+    @Override\r
+    public Object execute(ExecutionEvent event) throws ExecutionException {\r
+        Session session = Simantics.peekSession();\r
+        if (session == null)\r
+            return null;\r
+        ISelection selection = HandlerUtil.getCurrentSelection(event);\r
+        final List<Resource> resources = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);\r
+        new TagUtil(virtualGraphId, tagURI, tag) {\r
+            @Override\r
+            protected void processSelection(WriteGraph graph, List<Resource> resources) throws DatabaseException {\r
+                if (!process(graph))\r
+                    return;\r
+                super.processSelection(graph, resources);\r
+            }\r
+        }.execute(session, resources);\r
+        return null;\r
+    }\r
+\r
+    /**\r
+     * @return <code>false</code> to perform no further actions after this\r
+     *         method completes.\r
+     */\r
+    protected boolean process(WriteGraph graph) throws DatabaseException {\r
+        return true;\r
+    }\r
+\r
+    @SuppressWarnings("rawtypes")\r
+    @Override\r
+    public void updateElement(UIElement element, Map parameters) {\r
+        Session session = Simantics.peekSession();\r
+        if (session == null)\r
+            return;\r
+        try {\r
+            boolean checked = session.syncRequest(new Read<Boolean>() {\r
+                @Override\r
+                public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+                    return isChecked(graph);\r
+                }\r
+            });\r
+            element.setChecked(checked);\r
+        } catch (DatabaseException e) {\r
+            ErrorLogger.defaultLogError(e);\r
+        }\r
+    }\r
+\r
+    protected boolean isChecked(ReadGraph graph) throws DatabaseException {\r
+        return false;\r
+    }\r
+\r
+}\r