1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.event.view.handler;
\r
14 import java.util.List;
\r
15 import java.util.Map;
\r
17 import org.eclipse.core.commands.AbstractHandler;
\r
18 import org.eclipse.core.commands.ExecutionEvent;
\r
19 import org.eclipse.core.commands.ExecutionException;
\r
20 import org.eclipse.jface.viewers.ISelection;
\r
21 import org.eclipse.ui.commands.IElementUpdater;
\r
22 import org.eclipse.ui.handlers.HandlerUtil;
\r
23 import org.eclipse.ui.menus.UIElement;
\r
24 import org.simantics.Simantics;
\r
25 import org.simantics.db.ReadGraph;
\r
26 import org.simantics.db.Resource;
\r
27 import org.simantics.db.Session;
\r
28 import org.simantics.db.WriteGraph;
\r
29 import org.simantics.db.common.utils.TagUtil;
\r
30 import org.simantics.db.exception.DatabaseException;
\r
31 import org.simantics.db.layer0.SelectionHints;
\r
32 import org.simantics.db.request.Read;
\r
33 import org.simantics.utils.ui.ErrorLogger;
\r
34 import org.simantics.utils.ui.ISelectionUtils;
\r
37 * @author Tuukka Lehtonen
\r
39 class PreferenceHandler extends AbstractHandler implements IElementUpdater {
\r
41 protected final String virtualGraphId;
\r
42 private final String tagURI;
\r
43 private final boolean tag;
\r
45 public PreferenceHandler() {
\r
46 this("preferences", null, false);
\r
49 public PreferenceHandler(String tagURI, boolean tag) {
\r
50 this("preferences", tagURI, tag);
\r
53 public PreferenceHandler(String virtualGraphId) {
\r
54 this(virtualGraphId, null, false);
\r
57 public PreferenceHandler(String virtualGraphId, String tagURI, boolean tag) {
\r
58 this.virtualGraphId = virtualGraphId;
\r
59 this.tagURI = tagURI;
\r
64 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
65 Session session = Simantics.peekSession();
\r
66 if (session == null)
\r
68 ISelection selection = HandlerUtil.getCurrentSelection(event);
\r
69 final List<Resource> resources = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
\r
70 new TagUtil(virtualGraphId, tagURI, tag) {
\r
72 protected void processSelection(WriteGraph graph, List<Resource> resources) throws DatabaseException {
\r
73 if (!process(graph))
\r
75 super.processSelection(graph, resources);
\r
77 }.execute(session, resources);
\r
82 * @return <code>false</code> to perform no further actions after this
\r
85 protected boolean process(WriteGraph graph) throws DatabaseException {
\r
89 @SuppressWarnings("rawtypes")
\r
91 public void updateElement(UIElement element, Map parameters) {
\r
92 Session session = Simantics.peekSession();
\r
93 if (session == null)
\r
96 boolean checked = session.syncRequest(new Read<Boolean>() {
\r
98 public Boolean perform(ReadGraph graph) throws DatabaseException {
\r
99 return isChecked(graph);
\r
102 element.setChecked(checked);
\r
103 } catch (DatabaseException e) {
\r
104 ErrorLogger.defaultLogError(e);
\r
108 protected boolean isChecked(ReadGraph graph) throws DatabaseException {
\r