]> gerrit.simantics Code Review - simantics/platform.git/blob
d03199d9c72f549f161d729fc82503fa749adfdd
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.modelBrowser.handlers;
13
14 import java.util.Map;
15
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;
41
42 public class ToggleExternalFlag extends AbstractHandler implements IElementUpdater {
43
44     private static Resource getFlagResource(ISelection sel) {
45         Resource r = AdaptionUtils.adaptToSingle(sel, Resource.class);
46         return r;
47     }
48
49     @Override
50     public Object execute(ExecutionEvent event) throws ExecutionException {
51         ISelection sel = HandlerUtil.getCurrentSelection(event);
52         final Resource resource = getFlagResource(sel);
53         if (resource == null)
54             return null;
55
56         SimanticsUI.getSession().asyncRequest(new WriteRequest() {
57             @Override
58             public void perform(WriteGraph g) throws DatabaseException {
59                 DiagramResource dr = DiagramResource.getInstance(g);
60                 Boolean ext = g.hasStatement(resource, dr.ExternalFlag);
61                 if (ext)
62                     g.deny(resource, dr.ExternalFlag);
63                 else
64                     g.claim(resource, dr.ExternalFlag, resource);
65             }
66         });
67
68         return null;
69     }
70
71     @Override
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");
75
76         // Get a State object for the toggleSubscriptionGroup command
77         State state = command.getState("org.simantics.modeling.ui.toggleExternalFlag.state");
78         if (state == null) {
79             state = new State();
80             state.setValue(Boolean.TRUE);
81             command.addState("org.simantics.modeling.ui.toggleExternalFlag.state", state);
82         }
83
84         // Get the current value for the State object
85         Session s = SimanticsUI.peekSession();
86         if (s != null) {
87             Boolean value = Boolean.TRUE;
88             IWorkbenchSite site = (IWorkbenchSite) parameters.get("org.eclipse.ui.part.IWorkbenchPartSite");
89             ISelectionProvider sp = site.getSelectionProvider();
90             if (sp != null) {
91                 final Resource resource = getFlagResource(sp.getSelection());
92                 if (resource != null) {
93                     try {
94                         value = s.syncRequest(new Read<Boolean>() {
95                             @Override
96                             public Boolean perform(ReadGraph graph) throws DatabaseException {
97                                 DiagramResource dr = DiagramResource.getInstance(graph);
98                                 return Boolean.valueOf(graph.hasStatement(resource, dr.ExternalFlag));
99                             }
100                         });
101                     } catch (DatabaseException e) {
102                         ErrorLogger.defaultLogError(e);
103                     }
104                 }
105             }
106             state.setValue(value);
107         }
108
109         Boolean checked = (Boolean) state.getValue();
110         element.setChecked(checked);
111         element.setIcon(checked ? Activator.TICK_ICON : null);
112     }
113
114 }
115