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); } } }