1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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.modeling.ui.modelBrowser.handlers;
16 import org.eclipse.core.commands.AbstractHandler;
17 import org.eclipse.core.commands.Command;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.commands.State;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.ISelectionProvider;
23 import org.eclipse.ui.IWorkbenchSite;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.commands.ICommandService;
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.db.ReadGraph;
30 import org.simantics.db.Resource;
31 import org.simantics.db.Session;
32 import org.simantics.db.WriteGraph;
33 import org.simantics.db.common.request.WriteRequest;
34 import org.simantics.db.exception.DatabaseException;
35 import org.simantics.db.request.Read;
36 import org.simantics.diagram.stubs.DiagramResource;
37 import org.simantics.modeling.ui.Activator;
38 import org.simantics.ui.SimanticsUI;
39 import org.simantics.utils.ui.AdaptionUtils;
40 import org.simantics.utils.ui.ErrorLogger;
42 public class ToggleExternalFlag extends AbstractHandler implements IElementUpdater {
44 private static Resource getFlagResource(ISelection sel) {
45 Resource r = AdaptionUtils.adaptToSingle(sel, Resource.class);
50 public Object execute(ExecutionEvent event) throws ExecutionException {
51 ISelection sel = HandlerUtil.getCurrentSelection(event);
52 final Resource resource = getFlagResource(sel);
56 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
58 public void perform(WriteGraph g) throws DatabaseException {
59 DiagramResource dr = DiagramResource.getInstance(g);
60 Boolean ext = g.hasStatement(resource, dr.ExternalFlag);
62 g.deny(resource, dr.ExternalFlag);
64 g.claim(resource, dr.ExternalFlag, resource);
72 public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {
73 ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
74 Command command = commandService.getCommand("org.simantics.modeling.ui.toggleExternalFlag");
76 // Get a State object for the toggleSubscriptionGroup command
77 State state = command.getState("org.simantics.modeling.ui.toggleExternalFlag.state");
80 state.setValue(Boolean.TRUE);
81 command.addState("org.simantics.modeling.ui.toggleExternalFlag.state", state);
84 // Get the current value for the State object
85 Session s = SimanticsUI.peekSession();
87 Boolean value = Boolean.TRUE;
88 IWorkbenchSite site = (IWorkbenchSite) parameters.get("org.eclipse.ui.part.IWorkbenchPartSite");
89 ISelectionProvider sp = site.getSelectionProvider();
91 final Resource resource = getFlagResource(sp.getSelection());
92 if (resource != null) {
94 value = s.syncRequest(new Read<Boolean>() {
96 public Boolean perform(ReadGraph graph) throws DatabaseException {
97 DiagramResource dr = DiagramResource.getInstance(graph);
98 return Boolean.valueOf(graph.hasStatement(resource, dr.ExternalFlag));
101 } catch (DatabaseException e) {
102 ErrorLogger.defaultLogError(e);
106 state.setValue(value);
109 Boolean checked = (Boolean) state.getValue();
110 element.setChecked(checked);
111 element.setIcon(checked ? Activator.TICK_ICON : null);