From: jsimomaa Date: Wed, 5 Dec 2018 09:41:33 +0000 (+0200) Subject: Track changes control & split mapping to multiple diagrams control X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=985b0e7fb25091941011286a135731ad44809633;p=simantics%2Fdistrict.git Track changes control & split mapping to multiple diagrams control gitlab #21 Change-Id: If516d580664ca4fa976f08cf1d4f3be1e973be2b --- diff --git a/org.simantics.district.network.ui/fragment.e4xmi b/org.simantics.district.network.ui/fragment.e4xmi index f5169618..a77581cd 100644 --- a/org.simantics.district.network.ui/fragment.e4xmi +++ b/org.simantics.district.network.ui/fragment.e4xmi @@ -7,6 +7,8 @@ + + @@ -24,5 +26,7 @@ + + diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ToggleSplitToMultipleDiagrams.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ToggleSplitToMultipleDiagrams.java new file mode 100644 index 00000000..9d8c855d --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ToggleSplitToMultipleDiagrams.java @@ -0,0 +1,108 @@ +package org.simantics.district.network.ui.contributions; + +import java.util.List; + +import javax.inject.Named; + +import org.eclipse.e4.core.di.annotations.CanExecute; +import org.eclipse.e4.core.di.annotations.Execute; +import org.eclipse.e4.ui.di.AboutToShow; +import org.eclipse.e4.ui.model.application.MApplication; +import org.eclipse.e4.ui.model.application.ui.basic.MPart; +import org.eclipse.e4.ui.model.application.ui.menu.ItemType; +import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem; +import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement; +import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory; +import org.eclipse.e4.ui.services.IServiceConstants; +import org.eclipse.ui.IEditorPart; +import org.simantics.Simantics; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.UnaryRead; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Write; +import org.simantics.district.network.ontology.DistrictNetworkResource; +import org.simantics.district.network.ui.DistrictDiagramEditor; +import org.simantics.district.network.ui.internal.Activator; +import org.simantics.ui.workbench.IResourceEditorInput; +import org.simantics.ui.workbench.e4.E4WorkbenchUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ToggleSplitToMultipleDiagrams { + + private static final Logger LOGGER = LoggerFactory.getLogger(ToggleSplitToMultipleDiagrams.class); + private static final String COMMAND_ID = "org.simantics.district.network.ui.command.splittomultiplediagrams"; + + @AboutToShow + public void aboutToShow(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart, MApplication app, List items) { + Resource diagram = getDiagramFromEditorInput(mActiveEditorPart); + if (diagram != null) { + MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem(); + menuItem.setType(ItemType.CHECK); + menuItem.setLabel("Split to Multiple Diagrams"); + menuItem.setContributorURI(Activator.PLUGIN_ID); + menuItem.setCommand(app.getCommand(COMMAND_ID)); + menuItem.setSelected(isSplitToMultipleEnabled(diagram)); + items.add(menuItem); + } + } + + @CanExecute + public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart) { + IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart); + if (activeEditorPart == null) + return false; + if (!(activeEditorPart instanceof DistrictDiagramEditor)) + return false; + return true; + } + + @Execute + public void toggleSplitToMultiple(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart) { + Resource diagram = getDiagramFromEditorInput(mActiveEditorPart); + if (diagram != null) { + Simantics.getSession() + .asyncRequest((Write) graph -> graph.claimLiteral(diagram, + DistrictNetworkResource.getInstance(graph).Diagram_splitToMultipleEnabled, + !splitToMultipleEnabled(graph, diagram), Bindings.BOOLEAN)); + } + } + + private static boolean isSplitToMultipleEnabled(Resource diagram) { + try { + return Simantics.getSession().syncRequest(new SplitToMultipleEnabledRequest(diagram)); + } catch (DatabaseException e) { + LOGGER.error("Could not check if split to multiple is enabled for diagram {}", diagram, e); + return false; + } + } + + private static Boolean splitToMultipleEnabled(ReadGraph graph, Resource diagram) throws DatabaseException { + return Boolean.TRUE.equals(graph.getPossibleRelatedValue(diagram, + DistrictNetworkResource.getInstance(graph).Diagram_splitToMultipleEnabled)); + } + + private static Resource getDiagramFromEditorInput(MPart mActiveEditorPart) { + IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart); + if (activeEditorPart == null) + return null; + if (!(activeEditorPart instanceof DistrictDiagramEditor)) + return null; + IResourceEditorInput input = (IResourceEditorInput) activeEditorPart.getEditorInput(); + return input.getResource(); + } + + private static class SplitToMultipleEnabledRequest extends UnaryRead { + + public SplitToMultipleEnabledRequest(Resource diagram) { + super(diagram); + } + + @Override + public Boolean perform(ReadGraph graph) throws DatabaseException { + return splitToMultipleEnabled(graph, parameter); + } + } +} diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ToggleTrackChanges.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ToggleTrackChanges.java new file mode 100644 index 00000000..b6fdb4d0 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ToggleTrackChanges.java @@ -0,0 +1,104 @@ +package org.simantics.district.network.ui.contributions; + +import java.util.List; + +import javax.inject.Named; + +import org.eclipse.e4.core.di.annotations.CanExecute; +import org.eclipse.e4.core.di.annotations.Execute; +import org.eclipse.e4.ui.di.AboutToShow; +import org.eclipse.e4.ui.model.application.MApplication; +import org.eclipse.e4.ui.model.application.ui.basic.MPart; +import org.eclipse.e4.ui.model.application.ui.menu.ItemType; +import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem; +import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement; +import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory; +import org.eclipse.e4.ui.services.IServiceConstants; +import org.eclipse.ui.IEditorPart; +import org.simantics.Simantics; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.UnaryRead; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Write; +import org.simantics.district.network.DistrictNetworkUtil; +import org.simantics.district.network.ontology.DistrictNetworkResource; +import org.simantics.district.network.ui.DistrictDiagramEditor; +import org.simantics.district.network.ui.internal.Activator; +import org.simantics.ui.workbench.IResourceEditorInput; +import org.simantics.ui.workbench.e4.E4WorkbenchUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ToggleTrackChanges { + + private static final Logger LOGGER = LoggerFactory.getLogger(ToggleTrackChanges.class); + private static final String COMMAND_ID = "org.simantics.district.network.ui.command.toggletrackchanges"; + + @AboutToShow + public void aboutToShow(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart, MApplication app, List items) { + Resource diagram = getDiagramFromEditorInput(mActiveEditorPart); + if (diagram != null) { + MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem(); + menuItem.setType(ItemType.CHECK); + menuItem.setLabel("Track Changes"); + menuItem.setContributorURI(Activator.PLUGIN_ID); + menuItem.setCommand(app.getCommand(COMMAND_ID)); + menuItem.setSelected(isTrackChangesEnabled(diagram)); + items.add(menuItem); + } + } + + @CanExecute + public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart) { + IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart); + if (activeEditorPart == null) + return false; + if (!(activeEditorPart instanceof DistrictDiagramEditor)) + return false; + return true; + } + + @Execute + public void toggleTrackChanges(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditorPart) { + Resource diagram = getDiagramFromEditorInput(mActiveEditorPart); + if (diagram != null) { + Simantics.getSession() + .asyncRequest((Write) graph -> graph.claimLiteral(diagram, + DistrictNetworkResource.getInstance(graph).Diagram_trackChangesEnabled, + !DistrictNetworkUtil.trackChangesEnabled(graph, diagram), Bindings.BOOLEAN)); + } + } + + private static boolean isTrackChangesEnabled(Resource diagram) { + try { + return Simantics.getSession().syncRequest(new TrackChangesEnabledRequest(diagram)); + } catch (DatabaseException e) { + LOGGER.error("Could not check if track changes is enabled for diagram {}", diagram, e); + return false; + } + } + + private static Resource getDiagramFromEditorInput(MPart mActiveEditorPart) { + IEditorPart activeEditorPart = E4WorkbenchUtils.getActiveIEditorPart(mActiveEditorPart); + if (activeEditorPart == null) + return null; + if (!(activeEditorPart instanceof DistrictDiagramEditor)) + return null; + IResourceEditorInput input = (IResourceEditorInput) activeEditorPart.getEditorInput(); + return input.getResource(); + } + + private static class TrackChangesEnabledRequest extends UnaryRead { + + public TrackChangesEnabledRequest(Resource diagram) { + super(diagram); + } + + @Override + public Boolean perform(ReadGraph graph) throws DatabaseException { + return DistrictNetworkUtil.trackChangesEnabled(graph, parameter); + } + } +}