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.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;
33 public class ToggleSplitToMultipleDiagrams {
35 private static final Logger LOGGER = LoggerFactory.getLogger(ToggleSplitToMultipleDiagrams.class);
36 private static final String COMMAND_ID = "org.simantics.district.network.ui.command.splittomultiplediagrams";
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));
53 public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart) {
54 IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart);
55 if (activeEditorPart == null)
57 if (!(activeEditorPart instanceof DistrictDiagramEditor))
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));
73 private static boolean isSplitToMultipleEnabled(Resource diagram) {
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);
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));
87 private static Resource getDiagramFromEditorInput(MPart mActiveEditorPart) {
88 IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart);
89 if (activeEditorPart == null)
91 if (!(activeEditorPart instanceof DistrictDiagramEditor))
93 IResourceEditorInput input = (IResourceEditorInput) activeEditorPart.getEditorInput();
94 return input.getResource();
97 private static class SplitToMultipleEnabledRequest extends UnaryRead<Resource, Boolean> {
99 public SplitToMultipleEnabledRequest(Resource diagram) {
104 public Boolean perform(ReadGraph graph) throws DatabaseException {
105 return splitToMultipleEnabled(graph, parameter);