1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.issues.ui.handler;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
20 import org.eclipse.core.commands.AbstractHandler;
21 import org.eclipse.core.commands.ExecutionEvent;
22 import org.eclipse.core.commands.ExecutionException;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.ISelectionProvider;
25 import org.eclipse.ui.IWorkbenchSite;
26 import org.eclipse.ui.commands.IElementUpdater;
27 import org.eclipse.ui.handlers.HandlerUtil;
28 import org.eclipse.ui.menus.UIElement;
29 import org.simantics.Simantics;
30 import org.simantics.db.ReadGraph;
31 import org.simantics.db.Resource;
32 import org.simantics.db.Session;
33 import org.simantics.db.WriteGraph;
34 import org.simantics.db.common.utils.TagUtil;
35 import org.simantics.db.exception.DatabaseException;
36 import org.simantics.db.layer0.SelectionHints;
37 import org.simantics.db.layer0.adapter.ActionFactory;
38 import org.simantics.db.layer0.adapter.ActionFactory2;
39 import org.simantics.db.layer0.variable.Variable;
40 import org.simantics.db.request.Read;
41 import org.simantics.issues.ontology.IssueResource;
42 import org.simantics.utils.datastructures.SelectionUtils;
43 import org.simantics.utils.ui.ErrorLogger;
44 import org.simantics.utils.ui.ISelectionUtils;
47 * @author Tuukka Lehtonen
49 class PreferenceHandler extends AbstractHandler implements IElementUpdater, ActionFactory, ActionFactory2 {
51 protected final String virtualGraphId;
52 private final String tagURI;
53 private final boolean tag;
55 public PreferenceHandler() {
56 this("preferences", null, false);
59 public PreferenceHandler(String tagURI, boolean tag) {
60 this("preferences", tagURI, tag);
63 public PreferenceHandler(String virtualGraphId) {
64 this(virtualGraphId, null, false);
67 public PreferenceHandler(String virtualGraphId, String tagURI, boolean tag) {
68 this.virtualGraphId = virtualGraphId;
73 public ArrayList<Resource> filteredSelection(ReadGraph graph, List<Resource> resources, List<Variable> vars) throws DatabaseException {
74 IssueResource ISSUE = IssueResource.getInstance(graph);
75 ArrayList<Resource> sel = new ArrayList<Resource>();
76 for (Variable v : vars) {
77 //System.out.println("var: " + v.getURI(graph));
78 Resource r = v.getPossibleRepresents(graph);
79 if (r != null && graph.isInstanceOf(r, ISSUE.Issue))
82 for (Resource r : resources)
83 if (graph.isInstanceOf(r, ISSUE.Issue))
89 public Object execute(ExecutionEvent event) throws ExecutionException {
90 IWorkbenchSite site = HandlerUtil.getActiveSite(event);
91 ISelectionProvider sp = site.getSelectionProvider();
92 ISelection selection = sp.getSelection();
93 final List<Variable> vars = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Variable.class);
94 final List<Resource> resources = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
95 execute(vars, resources);
99 public void execute(final List<Variable> vars, List<Resource> resources) {
100 Session session = Simantics.peekSession();
103 new TagUtil(virtualGraphId, tagURI, tag) {
105 protected void processSelection(WriteGraph graph, List<Resource> resources) throws DatabaseException {
106 graph.markUndoPoint();
107 ArrayList<Resource> sel = filteredSelection(graph, resources, vars);
110 super.processSelection(graph, sel);
112 }.execute(session, resources);
116 * @return <code>false</code> to perform no further actions after this
119 protected boolean process(WriteGraph graph) throws DatabaseException {
123 @SuppressWarnings("rawtypes")
125 public void updateElement(UIElement element, Map parameters) {
126 Session session = Simantics.peekSession();
130 boolean checked = session.syncRequest(new Read<Boolean>() {
132 public Boolean perform(ReadGraph graph) throws DatabaseException {
133 return isChecked(graph);
136 element.setChecked(checked);
137 } catch (DatabaseException e) {
138 ErrorLogger.defaultLogError(e);
142 protected boolean isChecked(ReadGraph graph) throws DatabaseException {
147 public Runnable create(Object target) {
149 if(!(target instanceof Variable))
152 final Variable issue = (Variable)target;
154 return new Runnable() {
158 execute(Collections.singletonList(issue), Collections.<Resource>emptyList());
166 public Runnable create(Collection<?> targets) {
167 final List<Variable> issues = new ArrayList<Variable>(ISelectionUtils.filterSetSelection(targets, Variable.class));
168 if (issues.isEmpty())
170 return new Runnable() {
173 execute(issues, Collections.<Resource>emptyList());