]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser/handlers/ToggleExternalFlag.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser / handlers / ToggleExternalFlag.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.ui.modelBrowser.handlers;\r
13 \r
14 import java.util.Map;\r
15 \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
41 \r
42 public class ToggleExternalFlag extends AbstractHandler implements IElementUpdater {\r
43 \r
44     private static Resource getFlagResource(ISelection sel) {\r
45         Resource r = AdaptionUtils.adaptToSingle(sel, Resource.class);\r
46         return r;\r
47     }\r
48 \r
49     @Override\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
54             return null;\r
55 \r
56         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
57             @Override\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
61                 if (ext)\r
62                     g.deny(resource, dr.ExternalFlag);\r
63                 else\r
64                     g.claim(resource, dr.ExternalFlag, resource);\r
65             }\r
66         });\r
67 \r
68         return null;\r
69     }\r
70 \r
71     @Override\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
75 \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
82         }\r
83 \r
84         // Get the current value for the State object\r
85         Session s = SimanticsUI.peekSession();\r
86         if (s != null) {\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
90             if (sp != null) {\r
91                 final Resource resource = getFlagResource(sp.getSelection());\r
92                 if (resource != null) {\r
93                     try {\r
94                         value = s.syncRequest(new Read<Boolean>() {\r
95                             @Override\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
99                             }\r
100                         });\r
101                     } catch (DatabaseException e) {\r
102                         ErrorLogger.defaultLogError(e);\r
103                     }\r
104                 }\r
105             }\r
106             state.setValue(value);\r
107         }\r
108 \r
109         Boolean checked = (Boolean) state.getValue();\r
110         element.setChecked(checked);\r
111         element.setIcon(checked ? Activator.TICK_ICON : null);\r
112     }\r
113 \r
114 }\r
115 \r