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.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.request.WriteRequest;
35 import org.simantics.db.exception.DatabaseException;
36 import org.simantics.db.request.Read;
37 import org.simantics.diagram.stubs.DiagramResource;
38 import org.simantics.modeling.ui.Activator;
39 import org.simantics.ui.SimanticsUI;
40 import org.simantics.utils.ui.AdaptionUtils;
41 import org.simantics.utils.ui.ErrorLogger;
43 public class ToggleExternalFlag extends AbstractHandler implements IElementUpdater {
45 private static Resource getFlagResource(ISelection sel) {
46 Resource r = AdaptionUtils.adaptToSingle(sel, Resource.class);
51 public Object execute(ExecutionEvent event) throws ExecutionException {
52 ISelection sel = HandlerUtil.getCurrentSelection(event);
53 final Resource resource = getFlagResource(sel);
57 Simantics.getSession().asyncRequest(new WriteRequest() {
59 public void perform(WriteGraph g) throws DatabaseException {
60 DiagramResource dr = DiagramResource.getInstance(g);
61 Boolean ext = g.hasStatement(resource, dr.ExternalFlag);
63 g.deny(resource, dr.ExternalFlag);
65 g.claim(resource, dr.ExternalFlag, resource);
73 public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {
74 ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
75 Command command = commandService.getCommand("org.simantics.modeling.ui.toggleExternalFlag");
77 // Get a State object for the toggleSubscriptionGroup command
78 State state = command.getState("org.simantics.modeling.ui.toggleExternalFlag.state");
81 state.setValue(Boolean.TRUE);
82 command.addState("org.simantics.modeling.ui.toggleExternalFlag.state", state);
85 // Get the current value for the State object
86 Session s = Simantics.peekSession();
88 Boolean value = Boolean.TRUE;
89 IWorkbenchSite site = (IWorkbenchSite) parameters.get("org.eclipse.ui.part.IWorkbenchPartSite");
90 ISelectionProvider sp = site.getSelectionProvider();
92 final Resource resource = getFlagResource(sp.getSelection());
93 if (resource != null) {
95 value = s.syncRequest(new Read<Boolean>() {
97 public Boolean perform(ReadGraph graph) throws DatabaseException {
98 DiagramResource dr = DiagramResource.getInstance(graph);
99 return Boolean.valueOf(graph.hasStatement(resource, dr.ExternalFlag));
102 } catch (DatabaseException e) {
103 ErrorLogger.defaultLogError(e);
107 state.setValue(value);
110 Boolean checked = (Boolean) state.getValue();
111 element.setChecked(checked);
112 element.setIcon(checked ? Activator.TICK_ICON : null);