-/*******************************************************************************\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
+/*******************************************************************************
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.issues.ui.handler;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.ISelectionProvider;
+import org.eclipse.ui.IWorkbenchSite;
+import org.eclipse.ui.commands.IElementUpdater;
+import org.eclipse.ui.handlers.HandlerUtil;
+import org.eclipse.ui.menus.UIElement;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.utils.TagUtil;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.SelectionHints;
+import org.simantics.db.layer0.adapter.ActionFactory;
+import org.simantics.db.layer0.adapter.ActionFactory2;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.request.Read;
+import org.simantics.issues.ontology.IssueResource;
+import org.simantics.utils.ui.ErrorLogger;
+import org.simantics.utils.ui.ISelectionUtils;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+class PreferenceHandler extends AbstractHandler implements IElementUpdater, ActionFactory, ActionFactory2 {
+
+ protected final String virtualGraphId;
+ private final String tagURI;
+ private final boolean tag;
+
+ public PreferenceHandler() {
+ this("preferences", null, false); //$NON-NLS-1$
+ }
+
+ public PreferenceHandler(String tagURI, boolean tag) {
+ this("preferences", tagURI, tag); //$NON-NLS-1$
+ }
+
+ public PreferenceHandler(String virtualGraphId) {
+ this(virtualGraphId, null, false);
+ }
+
+ public PreferenceHandler(String virtualGraphId, String tagURI, boolean tag) {
+ this.virtualGraphId = virtualGraphId;
+ this.tagURI = tagURI;
+ this.tag = tag;
+ }
+
+ public ArrayList<Resource> filteredSelection(ReadGraph graph, List<Resource> resources, List<Variable> vars) throws DatabaseException {
+ IssueResource ISSUE = IssueResource.getInstance(graph);
+ ArrayList<Resource> sel = new ArrayList<Resource>();
+ for (Variable v : vars) {
+ //System.out.println("var: " + v.getURI(graph));
+ Resource r = v.getPossibleRepresents(graph);
+ if (r != null && graph.isInstanceOf(r, ISSUE.Issue))
+ sel.add(r);
+ }
+ for (Resource r : resources)
+ if (graph.isInstanceOf(r, ISSUE.Issue))
+ sel.add(r);
+ return sel;
+ }
+
+ @Override
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ IWorkbenchSite site = HandlerUtil.getActiveSite(event);
+ ISelectionProvider sp = site.getSelectionProvider();
+ ISelection selection = sp.getSelection();
+ final List<Variable> vars = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Variable.class);
+ final List<Resource> resources = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
+ execute(vars, resources);
+ return null;
+ }
+
+ public void execute(List<Variable> vars, List<Resource> resources) {
+ Session session = Simantics.peekSession();
+ if (session == null)
+ return;
+ new TagUtil(virtualGraphId, tagURI, tag) {
+ @Override
+ protected void processSelection(WriteGraph graph, List<Resource> resources) throws DatabaseException {
+ graph.markUndoPoint();
+ ArrayList<Resource> sel = filteredSelection(graph, resources, vars);
+ if (!process(graph))
+ return;
+ super.processSelection(graph, sel);
+ }
+ }.execute(session, resources);
+ }
+
+ /**
+ * @return <code>false</code> to perform no further actions after this
+ * method completes.
+ */
+ protected boolean process(WriteGraph graph) throws DatabaseException {
+ return true;
+ }
+
+ @SuppressWarnings("rawtypes")
+ @Override
+ public void updateElement(UIElement element, Map parameters) {
+ Session session = Simantics.peekSession();
+ if (session == null)
+ return;
+ try {
+ boolean checked = session.syncRequest(new Read<Boolean>() {
+ @Override
+ public Boolean perform(ReadGraph graph) throws DatabaseException {
+ return isChecked(graph);
+ }
+ });
+ element.setChecked(checked);
+ } catch (DatabaseException e) {
+ ErrorLogger.defaultLogError(e);
+ }
+ }
+
+ protected boolean isChecked(ReadGraph graph) throws DatabaseException {
+ return false;
+ }
+
+ @Override
+ public Runnable create(Object target) {
+
+ if(!(target instanceof Variable))
+ return null;
+
+ final Variable issue = (Variable)target;
+
+ return new Runnable() {
+
+ @Override
+ public void run() {
+ execute(Collections.singletonList(issue), Collections.<Resource>emptyList());
+ }
+
+ };
+
+ }
+
+ @Override
+ public Runnable create(Collection<?> targets) {
+ final List<Variable> issues = new ArrayList<Variable>(ISelectionUtils.filterSetSelection(targets, Variable.class));
+ if (issues.isEmpty())
+ return null;
+ return new Runnable() {
+ @Override
+ public void run() {
+ execute(issues, Collections.<Resource>emptyList());
+ }
+ };
+ }
+
+}