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