]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ToggleSplitToMultipleDiagrams.java
Updating of component properties when type code changes
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / contributions / ToggleSplitToMultipleDiagrams.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.ontology.DistrictNetworkResource;
26 import org.simantics.district.network.ui.DistrictDiagramEditor;
27 import org.simantics.district.network.ui.internal.Activator;
28 import org.simantics.ui.workbench.IResourceEditorInput;
29 import org.simantics.ui.workbench.e4.E4WorkbenchUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class ToggleSplitToMultipleDiagrams {
34
35     private static final Logger LOGGER = LoggerFactory.getLogger(ToggleSplitToMultipleDiagrams.class);
36     private static final String COMMAND_ID = "org.simantics.district.network.ui.command.splittomultiplediagrams";
37
38     @AboutToShow
39     public void aboutToShow(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart, MApplication app, List<MMenuElement> items) {
40         Resource diagram = getDiagramFromEditorInput(mActiveEditorPart);
41         if (diagram != null) {
42             MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
43             menuItem.setType(ItemType.CHECK);
44             menuItem.setLabel("Split to Multiple Diagrams");
45             menuItem.setContributorURI(Activator.PLUGIN_ID);
46             menuItem.setCommand(app.getCommand(COMMAND_ID));
47             menuItem.setSelected(isSplitToMultipleEnabled(diagram));
48             items.add(menuItem);
49         }
50     }
51     
52     @CanExecute
53     public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart) {
54         IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart);
55         if (activeEditorPart == null)
56             return false;
57         if (!(activeEditorPart instanceof DistrictDiagramEditor))
58             return false;
59         return true;
60     }
61
62     @Execute
63     public void toggleSplitToMultiple(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart) {
64         Resource diagram = getDiagramFromEditorInput(mActiveEditorPart);
65         if (diagram != null) {
66             Simantics.getSession()
67                     .asyncRequest((Write) graph -> graph.claimLiteral(diagram,
68                             DistrictNetworkResource.getInstance(graph).Diagram_splitToMultipleEnabled,
69                             !splitToMultipleEnabled(graph, diagram), Bindings.BOOLEAN));
70         }
71     }
72
73     private static boolean isSplitToMultipleEnabled(Resource diagram) {
74         try {
75             return Simantics.getSession().syncRequest(new SplitToMultipleEnabledRequest(diagram));
76         } catch (DatabaseException e) { 
77             LOGGER.error("Could not check if split to multiple is enabled for diagram {}", diagram, e);
78             return false;
79         }
80     }
81
82     private static Boolean splitToMultipleEnabled(ReadGraph graph, Resource diagram) throws DatabaseException {
83         return Boolean.TRUE.equals(graph.getPossibleRelatedValue(diagram,
84                 DistrictNetworkResource.getInstance(graph).Diagram_splitToMultipleEnabled));
85     }
86
87     private static Resource getDiagramFromEditorInput(MPart mActiveEditorPart) {
88         IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart);
89         if (activeEditorPart == null)
90             return null;
91         if (!(activeEditorPart instanceof DistrictDiagramEditor))
92             return null;
93         IResourceEditorInput input = (IResourceEditorInput) activeEditorPart.getEditorInput();
94         return input.getResource();
95     }
96
97     private static class SplitToMultipleEnabledRequest extends UnaryRead<Resource, Boolean> {
98
99         public SplitToMultipleEnabledRequest(Resource diagram) {
100             super(diagram);
101         }
102
103         @Override
104         public Boolean perform(ReadGraph graph) throws DatabaseException {
105             return splitToMultipleEnabled(graph, parameter);
106         }
107     }
108 }