]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ToggleTrackChanges.java
Fixed most warnings from district codebase after JavaSE-11 switch
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / contributions / ToggleTrackChanges.java
1 package org.simantics.district.network.ui.contributions;
2
3 import java.util.List;
4
5 import javax.inject.Named;
6
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;
33
34 public class ToggleTrackChanges {
35
36     private static final Logger LOGGER = LoggerFactory.getLogger(ToggleTrackChanges.class);
37     private static final String COMMAND_ID = "org.simantics.district.network.ui.command.toggletrackchanges";
38
39     @AboutToShow
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));
49             items.add(menuItem);
50         }
51     }
52     
53     @CanExecute
54     public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart) {
55         IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart);
56         if (activeEditorPart == null)
57             return false;
58         if (!(activeEditorPart instanceof DistrictDiagramEditor))
59             return false;
60         return true;
61     }
62
63     @Execute
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));
71         }
72     }
73
74     private static boolean isTrackChangesEnabled(Resource diagram) {
75         try {
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);
79             return false;
80         }
81     }
82
83     private static Resource getDiagramFromEditorInput(MPart mActiveEditorPart) {
84         IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart);
85         if (activeEditorPart == null)
86             return null;
87         if (!(activeEditorPart instanceof DistrictDiagramEditor))
88             return null;
89         IResourceEditorInput input = (IResourceEditorInput) activeEditorPart.getEditorInput();
90         return input.getResource();
91     }
92
93     private static class TrackChangesEnabledRequest extends UnaryRead<Resource, Boolean> {
94
95         public TrackChangesEnabledRequest(Resource diagram) {
96             super(diagram);
97         }
98
99         @Override
100         public Boolean perform(ReadGraph graph) throws DatabaseException {
101             return DistrictNetworkUtil.trackChangesEnabled(graph, parameter);
102         }
103     }
104 }