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.event.view.handler;
14 import java.util.List;
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.ui.commands.IElementUpdater;
22 import org.eclipse.ui.handlers.HandlerUtil;
23 import org.eclipse.ui.menus.UIElement;
24 import org.simantics.Simantics;
25 import org.simantics.db.ReadGraph;
26 import org.simantics.db.Resource;
27 import org.simantics.db.Session;
28 import org.simantics.db.WriteGraph;
29 import org.simantics.db.common.utils.TagUtil;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.db.layer0.SelectionHints;
32 import org.simantics.db.request.Read;
33 import org.simantics.utils.ui.ErrorLogger;
34 import org.simantics.utils.ui.ISelectionUtils;
37 * @author Tuukka Lehtonen
39 class PreferenceHandler extends AbstractHandler implements IElementUpdater {
41 protected final String virtualGraphId;
42 private final String tagURI;
43 private final boolean tag;
45 public PreferenceHandler() {
46 this("preferences", null, false);
49 public PreferenceHandler(String tagURI, boolean tag) {
50 this("preferences", tagURI, tag);
53 public PreferenceHandler(String virtualGraphId) {
54 this(virtualGraphId, null, false);
57 public PreferenceHandler(String virtualGraphId, String tagURI, boolean tag) {
58 this.virtualGraphId = virtualGraphId;
64 public Object execute(ExecutionEvent event) throws ExecutionException {
65 Session session = Simantics.peekSession();
68 ISelection selection = HandlerUtil.getCurrentSelection(event);
69 final List<Resource> resources = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
70 new TagUtil(virtualGraphId, tagURI, tag) {
72 protected void processSelection(WriteGraph graph, List<Resource> resources) throws DatabaseException {
75 super.processSelection(graph, resources);
77 }.execute(session, resources);
82 * @return <code>false</code> to perform no further actions after this
85 protected boolean process(WriteGraph graph) throws DatabaseException {
89 @SuppressWarnings("rawtypes")
91 public void updateElement(UIElement element, Map parameters) {
92 Session session = Simantics.peekSession();
96 boolean checked = session.syncRequest(new Read<Boolean>() {
98 public Boolean perform(ReadGraph graph) throws DatabaseException {
99 return isChecked(graph);
102 element.setChecked(checked);
103 } catch (DatabaseException e) {
104 ErrorLogger.defaultLogError(e);
108 protected boolean isChecked(ReadGraph graph) throws DatabaseException {