]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/handler/PreferenceHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.issues.ui / src / org / simantics / issues / ui / handler / PreferenceHandler.java
diff --git a/bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/handler/PreferenceHandler.java b/bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/handler/PreferenceHandler.java
new file mode 100644 (file)
index 0000000..ee10a96
--- /dev/null
@@ -0,0 +1,178 @@
+/*******************************************************************************\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.issues.ui.handler;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\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.jface.viewers.ISelectionProvider;\r
+import org.eclipse.ui.IWorkbenchSite;\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.layer0.adapter.ActionFactory;\r
+import org.simantics.db.layer0.adapter.ActionFactory2;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.issues.ontology.IssueResource;\r
+import org.simantics.utils.datastructures.SelectionUtils;\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, ActionFactory, ActionFactory2 {\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
+    public ArrayList<Resource> filteredSelection(ReadGraph graph, List<Resource> resources, List<Variable> vars) throws DatabaseException {\r
+        IssueResource ISSUE = IssueResource.getInstance(graph);\r
+        ArrayList<Resource> sel = new ArrayList<Resource>();\r
+        for (Variable v : vars) {\r
+            //System.out.println("var: " + v.getURI(graph));\r
+            Resource r = v.getPossibleRepresents(graph);\r
+            if (r != null && graph.isInstanceOf(r, ISSUE.Issue))\r
+                sel.add(r);\r
+        }\r
+        for (Resource r : resources)\r
+            if (graph.isInstanceOf(r, ISSUE.Issue))\r
+                sel.add(r);\r
+               return sel;\r
+    }\r
+\r
+    @Override\r
+    public Object execute(ExecutionEvent event) throws ExecutionException {\r
+        IWorkbenchSite site = HandlerUtil.getActiveSite(event);\r
+        ISelectionProvider sp = site.getSelectionProvider();\r
+        ISelection selection = sp.getSelection();\r
+        final List<Variable> vars = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Variable.class);\r
+        final List<Resource> resources = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);\r
+        execute(vars, resources);\r
+        return null;\r
+    }\r
+\r
+    public void execute(final List<Variable> vars, List<Resource> resources) {\r
+        Session session = Simantics.peekSession();\r
+        if (session == null)\r
+            return;\r
+        new TagUtil(virtualGraphId, tagURI, tag) {\r
+            @Override\r
+            protected void processSelection(WriteGraph graph, List<Resource> resources) throws DatabaseException {\r
+                graph.markUndoPoint();\r
+                ArrayList<Resource> sel = filteredSelection(graph, resources, vars);\r
+                if (!process(graph))\r
+                    return;\r
+                super.processSelection(graph, sel);\r
+            }\r
+        }.execute(session, resources);\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
+       @Override\r
+       public Runnable create(Object target) {\r
+\r
+               if(!(target instanceof Variable))\r
+                       return null;\r
+\r
+               final Variable issue = (Variable)target;\r
+\r
+               return new Runnable() {\r
+\r
+                       @Override\r
+                       public void run() {\r
+                               execute(Collections.singletonList(issue), Collections.<Resource>emptyList());\r
+                       }\r
+\r
+               };\r
+\r
+       }\r
+\r
+       @Override\r
+       public Runnable create(Collection<?> targets) {\r
+               final List<Variable> issues = new ArrayList<Variable>(ISelectionUtils.filterSetSelection(targets, Variable.class));\r
+               if (issues.isEmpty())\r
+                       return null;\r
+               return new Runnable() {\r
+                       @Override\r
+                       public void run() {\r
+                               execute(issues, Collections.<Resource>emptyList());\r
+                       }\r
+               };\r
+       }\r
+\r
+}\r