From 429ecaa9bda75b89c884cd6f19855e9181f17f34 Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Tue, 4 Jun 2019 19:51:22 +0300 Subject: [PATCH] Add UI controls for route view to activate/deactivate route mode gitlab #48 APROS-15258 Change-Id: I019b76cbdfd095e2be667e6865ae5fc48d75a738 --- .../META-INF/MANIFEST.MF | 3 +- .../fragment.e4xmi | 4 ++ .../district/route/ui/RouteView.java | 33 +++++++++++ .../ui/handlers/ActivateCreateRoute.java | 53 ++++++++++++++++++ .../ui/handlers/DeactivateCreateRoute.java | 56 +++++++++++++++++++ 5 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 org.simantics.district.route.ui/src/org/simantics/district/route/ui/handlers/ActivateCreateRoute.java create mode 100644 org.simantics.district.route.ui/src/org/simantics/district/route/ui/handlers/DeactivateCreateRoute.java diff --git a/org.simantics.district.route.ui/META-INF/MANIFEST.MF b/org.simantics.district.route.ui/META-INF/MANIFEST.MF index 3b531e70..70045519 100644 --- a/org.simantics.district.route.ui/META-INF/MANIFEST.MF +++ b/org.simantics.district.route.ui/META-INF/MANIFEST.MF @@ -15,7 +15,8 @@ Require-Bundle: org.eclipse.e4.core.di, org.simantics.ui, org.simantics.district.route, org.simantics.district.network.ui;bundle-version="1.0.0", - org.slf4j.api + org.slf4j.api, + org.simantics.modeling.ui;bundle-version="1.1.1" Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Automatic-Module-Name: fi.vtt.apros.district.route.ui Import-Package: javax.annotation;version="1.0.0";resolution:=optional, diff --git a/org.simantics.district.route.ui/fragment.e4xmi b/org.simantics.district.route.ui/fragment.e4xmi index 16c774b3..291140d8 100644 --- a/org.simantics.district.route.ui/fragment.e4xmi +++ b/org.simantics.district.route.ui/fragment.e4xmi @@ -6,12 +6,16 @@ + + + + diff --git a/org.simantics.district.route.ui/src/org/simantics/district/route/ui/RouteView.java b/org.simantics.district.route.ui/src/org/simantics/district/route/ui/RouteView.java index 4499782d..78b6be9e 100644 --- a/org.simantics.district.route.ui/src/org/simantics/district/route/ui/RouteView.java +++ b/org.simantics.district.route.ui/src/org/simantics/district/route/ui/RouteView.java @@ -10,9 +10,11 @@ import org.eclipse.e4.core.services.events.IEventBroker; import org.eclipse.e4.ui.di.Focus; 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.MHandledToolItem; import org.eclipse.e4.ui.model.application.ui.menu.MMenuContribution; import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory; import org.eclipse.e4.ui.model.application.ui.menu.MPopupMenu; +import org.eclipse.e4.ui.model.application.ui.menu.MToolBar; import org.eclipse.e4.ui.services.EMenuService; import org.eclipse.e4.ui.workbench.modeling.ESelectionService; import org.eclipse.swt.widgets.Composite; @@ -52,6 +54,37 @@ public class RouteView { } } part.getMenus().add(popupMenu); + + /** + * See + * http://www.vogella.com/tutorials/EclipsePlugin/article.html#adding-eclipse-4-x-parts-to-eclipse-3-x-applications-via-the-code-org-eclipse-ui-views-code-extension-point + * + * Cannot contribute toolbar items via fragment at this point + */ + MToolBar toolBar = MMenuFactory.INSTANCE.createToolBar(); + toolBar.setToBeRendered(true); + toolBar.getChildren().add(createActivateRouteToolItem(app)); + toolBar.getChildren().add(createDeactivateRouteToolItem(app)); + part.setToolbar(toolBar); + } + + + private static MHandledToolItem createActivateRouteToolItem(MApplication app) { + MHandledToolItem createHandledToolItem = MMenuFactory.INSTANCE.createHandledToolItem(); + createHandledToolItem.setCommand(app.getCommand("org.simantics.district.route.ui.command.activatecreateroute")); // Command is contributed via fragment //$NON-NLS-1$ + createHandledToolItem.setLabel("Activate create route"); + createHandledToolItem.setIconURI("platform:/plugin/com.famfamfam.silk/icons/table_add.png"); //$NON-NLS-1$ + createHandledToolItem.setEnabled(true); + return createHandledToolItem; + } + + private static MHandledToolItem createDeactivateRouteToolItem(MApplication app) { + MHandledToolItem createHandledToolItem = MMenuFactory.INSTANCE.createHandledToolItem(); + createHandledToolItem.setCommand(app.getCommand("org.simantics.district.route.ui.command.deactivatecreateroute")); // Command is contributed via fragment //$NON-NLS-1$ + createHandledToolItem.setLabel("Deactivate create route"); + createHandledToolItem.setIconURI("platform:/plugin/com.famfamfam.silk/icons/table_edit.png"); //$NON-NLS-1$ + createHandledToolItem.setEnabled(true); + return createHandledToolItem; } @PostConstruct diff --git a/org.simantics.district.route.ui/src/org/simantics/district/route/ui/handlers/ActivateCreateRoute.java b/org.simantics.district.route.ui/src/org/simantics/district/route/ui/handlers/ActivateCreateRoute.java new file mode 100644 index 00000000..0dc21353 --- /dev/null +++ b/org.simantics.district.route.ui/src/org/simantics/district/route/ui/handlers/ActivateCreateRoute.java @@ -0,0 +1,53 @@ +package org.simantics.district.route.ui.handlers; + +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.model.application.ui.basic.MPart; +import org.eclipse.e4.ui.services.IServiceConstants; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IEditorPart; +import org.simantics.district.network.ui.DistrictDiagramEditor; +import org.simantics.district.network.ui.participants.RoutingMode; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.modeling.ui.diagramEditor.DiagramViewer; +import org.simantics.utils.ui.workbench.WorkbenchUtils; + +/** + * @author Jani Simomaa + */ +public class ActivateCreateRoute { + + @CanExecute + public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) { + IEditorPart activeEditor = WorkbenchUtils.getActiveEditor(); + if (activeEditor != null && activeEditor instanceof DistrictDiagramEditor) { + DistrictDiagramEditor editor = (DistrictDiagramEditor) activeEditor; + DiagramViewer viewer = editor.getAdapter(DiagramViewer.class); + ICanvasContext context = viewer.getAdapter(ICanvasContext.class); + RoutingMode routingMode = context.getAtMostOneItemOfClass(RoutingMode.class); + return routingMode == null; + } + return false; + } + + @Execute + public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell activeShell, + @Named(IServiceConstants.ACTIVE_PART) MPart part, + @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) { + + IEditorPart activeEditor = WorkbenchUtils.getActiveEditor(); + if (activeEditor != null && activeEditor instanceof DistrictDiagramEditor) { + DistrictDiagramEditor editor = (DistrictDiagramEditor) activeEditor; + DiagramViewer viewer = editor.getAdapter(DiagramViewer.class); + ICanvasContext context = viewer.getAdapter(ICanvasContext.class); + RoutingMode routingMode = context.getAtMostOneItemOfClass(RoutingMode.class); + if (routingMode == null || routingMode.isRemoved()) { + routingMode = new RoutingMode(0); + context.add(routingMode); + } + } + } +} \ No newline at end of file diff --git a/org.simantics.district.route.ui/src/org/simantics/district/route/ui/handlers/DeactivateCreateRoute.java b/org.simantics.district.route.ui/src/org/simantics/district/route/ui/handlers/DeactivateCreateRoute.java new file mode 100644 index 00000000..a60478d8 --- /dev/null +++ b/org.simantics.district.route.ui/src/org/simantics/district/route/ui/handlers/DeactivateCreateRoute.java @@ -0,0 +1,56 @@ +package org.simantics.district.route.ui.handlers; + +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.model.application.ui.basic.MPart; +import org.eclipse.e4.ui.services.IServiceConstants; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IEditorPart; +import org.simantics.district.network.ui.DistrictDiagramEditor; +import org.simantics.district.network.ui.participants.RoutingMode; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.modeling.ui.diagramEditor.DiagramViewer; +import org.simantics.utils.ui.workbench.WorkbenchUtils; + +/** + * @author Jani Simomaa + */ +public class DeactivateCreateRoute { + + @CanExecute + public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) { + IEditorPart activeEditor = WorkbenchUtils.getActiveEditor(); + if (activeEditor != null && activeEditor instanceof DistrictDiagramEditor) { + DistrictDiagramEditor editor = (DistrictDiagramEditor) activeEditor; + DiagramViewer viewer = editor.getAdapter(DiagramViewer.class); + ICanvasContext context = viewer.getAdapter(ICanvasContext.class); + RoutingMode routingMode = context.getAtMostOneItemOfClass(RoutingMode.class); + return routingMode != null; + } + return false; + } + + @Execute + public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell activeShell, + @Named(IServiceConstants.ACTIVE_PART) MPart part, + @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) { + + IEditorPart activeEditor = WorkbenchUtils.getActiveEditor(); + if (activeEditor != null && activeEditor instanceof DistrictDiagramEditor) { + DistrictDiagramEditor editor = (DistrictDiagramEditor) activeEditor; + DiagramViewer viewer = editor.getAdapter(DiagramViewer.class); + ICanvasContext context = viewer.getAdapter(ICanvasContext.class); + +// context.getSceneGraph +// +// RoutingMode routingMode = context.getAtMostOneItemOfClass(RoutingMode.class); +// if (routingMode == null || routingMode.isRemoved()) { +// routingMode = new RoutingMode(0); +// context.add(routingMode); +// } + } + } +} \ No newline at end of file -- 2.47.1