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