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.ui.ErrorLogger;
43 import org.simantics.utils.ui.ISelectionUtils;
46 * @author Tuukka Lehtonen
48 class PreferenceHandler extends AbstractHandler implements IElementUpdater, ActionFactory, ActionFactory2 {
50 protected final String virtualGraphId;
51 private final String tagURI;
52 private final boolean tag;
54 public PreferenceHandler() {
55 this("preferences", null, false);
58 public PreferenceHandler(String tagURI, boolean tag) {
59 this("preferences", tagURI, tag);
62 public PreferenceHandler(String virtualGraphId) {
63 this(virtualGraphId, null, false);
66 public PreferenceHandler(String virtualGraphId, String tagURI, boolean tag) {
67 this.virtualGraphId = virtualGraphId;
72 public ArrayList<Resource> filteredSelection(ReadGraph graph, List<Resource> resources, List<Variable> vars) throws DatabaseException {
73 IssueResource ISSUE = IssueResource.getInstance(graph);
74 ArrayList<Resource> sel = new ArrayList<Resource>();
75 for (Variable v : vars) {
76 //System.out.println("var: " + v.getURI(graph));
77 Resource r = v.getPossibleRepresents(graph);
78 if (r != null && graph.isInstanceOf(r, ISSUE.Issue))
81 for (Resource r : resources)
82 if (graph.isInstanceOf(r, ISSUE.Issue))
88 public Object execute(ExecutionEvent event) throws ExecutionException {
89 IWorkbenchSite site = HandlerUtil.getActiveSite(event);
90 ISelectionProvider sp = site.getSelectionProvider();
91 ISelection selection = sp.getSelection();
92 final List<Variable> vars = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Variable.class);
93 final List<Resource> resources = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
94 execute(vars, resources);
98 public void execute(List<Variable> vars, List<Resource> resources) {
99 Session session = Simantics.peekSession();
102 new TagUtil(virtualGraphId, tagURI, tag) {
104 protected void processSelection(WriteGraph graph, List<Resource> resources) throws DatabaseException {
105 graph.markUndoPoint();
106 ArrayList<Resource> sel = filteredSelection(graph, resources, vars);
109 super.processSelection(graph, sel);
111 }.execute(session, resources);
115 * @return <code>false</code> to perform no further actions after this
118 protected boolean process(WriteGraph graph) throws DatabaseException {
122 @SuppressWarnings("rawtypes")
124 public void updateElement(UIElement element, Map parameters) {
125 Session session = Simantics.peekSession();
129 boolean checked = session.syncRequest(new Read<Boolean>() {
131 public Boolean perform(ReadGraph graph) throws DatabaseException {
132 return isChecked(graph);
135 element.setChecked(checked);
136 } catch (DatabaseException e) {
137 ErrorLogger.defaultLogError(e);
141 protected boolean isChecked(ReadGraph graph) throws DatabaseException {
146 public Runnable create(Object target) {
148 if(!(target instanceof Variable))
151 final Variable issue = (Variable)target;
153 return new Runnable() {
157 execute(Collections.singletonList(issue), Collections.<Resource>emptyList());
165 public Runnable create(Collection<?> targets) {
166 final List<Variable> issues = new ArrayList<Variable>(ISelectionUtils.filterSetSelection(targets, Variable.class));
167 if (issues.isEmpty())
169 return new Runnable() {
172 execute(issues, Collections.<Resource>emptyList());