1 package org.simantics.district.network.ui.contributions;
5 import javax.inject.Named;
7 import org.eclipse.e4.core.di.annotations.CanExecute;
8 import org.eclipse.e4.core.di.annotations.Execute;
9 import org.eclipse.e4.ui.di.AboutToShow;
10 import org.eclipse.e4.ui.model.application.MApplication;
11 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
12 import org.eclipse.e4.ui.model.application.ui.menu.ItemType;
13 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
14 import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
15 import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
16 import org.eclipse.e4.ui.services.IServiceConstants;
17 import org.eclipse.ui.IEditorPart;
18 import org.simantics.Simantics;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.request.UnaryRead;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.request.Write;
25 import org.simantics.district.network.DistrictNetworkUtil;
26 import org.simantics.district.network.ontology.DistrictNetworkResource;
27 import org.simantics.district.network.ui.DistrictDiagramEditor;
28 import org.simantics.district.network.ui.internal.Activator;
29 import org.simantics.ui.workbench.IResourceEditorInput;
30 import org.simantics.ui.workbench.e4.E4WorkbenchUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class ToggleTrackChanges {
36 private static final Logger LOGGER = LoggerFactory.getLogger(ToggleTrackChanges.class);
37 private static final String COMMAND_ID = "org.simantics.district.network.ui.command.toggletrackchanges";
40 public void aboutToShow(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart, MApplication app, List<MMenuElement> items) {
41 Resource diagram = getDiagramFromEditorInput(mActiveEditorPart);
42 if (diagram != null) {
43 MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
44 menuItem.setType(ItemType.CHECK);
45 menuItem.setLabel("Track Changes");
46 menuItem.setContributorURI(Activator.PLUGIN_ID);
47 menuItem.setCommand(app.getCommand(COMMAND_ID));
48 menuItem.setSelected(isTrackChangesEnabled(diagram));
54 public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart) {
55 IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart);
56 if (activeEditorPart == null)
58 if (!(activeEditorPart instanceof DistrictDiagramEditor))
64 public void toggleTrackChanges(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart) {
65 Resource diagram = getDiagramFromEditorInput(mActiveEditorPart);
66 if (diagram != null) {
67 Simantics.getSession()
68 .asyncRequest((Write) graph -> graph.claimLiteral(diagram,
69 DistrictNetworkResource.getInstance(graph).Diagram_trackChangesEnabled,
70 !DistrictNetworkUtil.trackChangesEnabled(graph, diagram), Bindings.BOOLEAN));
74 private static boolean isTrackChangesEnabled(Resource diagram) {
76 return Simantics.getSession().syncRequest(new TrackChangesEnabledRequest(diagram));
77 } catch (DatabaseException e) {
78 LOGGER.error("Could not check if track changes is enabled for diagram {}", diagram, e);
83 private static Resource getDiagramFromEditorInput(MPart mActiveEditorPart) {
84 IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart);
85 if (activeEditorPart == null)
87 if (!(activeEditorPart instanceof DistrictDiagramEditor))
89 IResourceEditorInput input = (IResourceEditorInput) activeEditorPart.getEditorInput();
90 return input.getResource();
93 private static class TrackChangesEnabledRequest extends UnaryRead<Resource, Boolean> {
95 public TrackChangesEnabledRequest(Resource diagram) {
100 public Boolean perform(ReadGraph graph) throws DatabaseException {
101 return DistrictNetworkUtil.trackChangesEnabled(graph, parameter);