From 92b277cb7373f06688e7b1443585e698faaadb29 Mon Sep 17 00:00:00 2001 From: Erwin De Ley Date: Fri, 16 Jun 2017 23:19:58 +0200 Subject: [PATCH] #160 : guideline 6.21 outline/editor selection linking Signed-off-by: Erwin De Ley --- .../workflow/editor/TriqDiagramEditor.java | 12 ++++ .../editor/TriqToolBehaviorProvider.java | 13 ++++ .../outline/DiagramEditorOutlinePage.java | 3 +- .../outline/tree/TriqOutlineTreeViewer.java | 44 ++++++++++++ .../workflow/editor/util/EditorUtils.java | 38 +++++++---- .../views/TriqSelectionSynchronizer.java | 67 +++++++++++++++++++ 6 files changed, 162 insertions(+), 15 deletions(-) create mode 100644 org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/outline/tree/TriqOutlineTreeViewer.java create mode 100644 org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/views/TriqSelectionSynchronizer.java diff --git a/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/TriqDiagramEditor.java b/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/TriqDiagramEditor.java index 5501659f..2ddb83cb 100644 --- a/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/TriqDiagramEditor.java +++ b/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/TriqDiagramEditor.java @@ -20,6 +20,7 @@ import org.eclipse.emf.common.util.URI; import org.eclipse.emf.edit.ui.util.EditUIUtil; import org.eclipse.gef.commands.CommandStack; +import org.eclipse.gef.ui.parts.SelectionSynchronizer; import org.eclipse.graphiti.mm.pictograms.Diagram; import org.eclipse.graphiti.ui.editor.DiagramBehavior; import org.eclipse.graphiti.ui.editor.DiagramEditor; @@ -28,6 +29,7 @@ import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Shell; import org.eclipse.triquetrum.workflow.editor.outline.DiagramEditorOutlinePage; +import org.eclipse.triquetrum.workflow.editor.views.TriqSelectionSynchronizer; import org.eclipse.triquetrum.workflow.model.NamedObj; import org.eclipse.triquetrum.workflow.model.util.PtObjectBuilderVisitor; import org.eclipse.triquetrum.workflow.model.util.PtObjectLinkerVisitor; @@ -41,6 +43,7 @@ public class TriqDiagramEditor extends DiagramEditor { public final static String EDITOR_ID = "org.eclipse.triquetrum.workflow.editor.TriqDiagramEditor"; public final static String DIAGRAM_FILE_EXTENSION = "tdml"; + private SelectionSynchronizer synchronizer; @Override public void init(IEditorSite site, IEditorInput input) throws PartInitException { @@ -178,4 +181,13 @@ public void setFocus() { public CommandStack getCommandStack() { return super.getCommandStack(); } + + @Override + protected SelectionSynchronizer getSelectionSynchronizer() { + if (synchronizer == null) { + synchronizer = new TriqSelectionSynchronizer(); + } + return synchronizer; + } + } diff --git a/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/TriqToolBehaviorProvider.java b/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/TriqToolBehaviorProvider.java index b019fd1a..d5012627 100644 --- a/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/TriqToolBehaviorProvider.java +++ b/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/TriqToolBehaviorProvider.java @@ -20,14 +20,19 @@ import org.eclipse.graphiti.features.context.impl.CustomContext; import org.eclipse.graphiti.features.custom.ICustomFeature; import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm; +import org.eclipse.graphiti.mm.algorithms.styles.LineStyle; import org.eclipse.graphiti.mm.pictograms.Anchor; import org.eclipse.graphiti.mm.pictograms.AnchorContainer; import org.eclipse.graphiti.mm.pictograms.ContainerShape; import org.eclipse.graphiti.mm.pictograms.PictogramElement; +import org.eclipse.graphiti.mm.pictograms.Shape; import org.eclipse.graphiti.services.Graphiti; import org.eclipse.graphiti.tb.ContextButtonEntry; import org.eclipse.graphiti.tb.DefaultToolBehaviorProvider; import org.eclipse.graphiti.tb.IContextButtonPadData; +import org.eclipse.graphiti.tb.IShapeSelectionInfo; +import org.eclipse.graphiti.tb.ShapeSelectionInfoImpl; +import org.eclipse.graphiti.util.IColorConstant; import org.eclipse.triquetrum.workflow.editor.features.ModelElementConfigureFeature; import org.eclipse.triquetrum.workflow.editor.features.PauseFeature; import org.eclipse.triquetrum.workflow.editor.features.ResumeFeature; @@ -169,6 +174,14 @@ public GraphicsAlgorithm getSelectionBorder(PictogramElement pe) { } return super.getSelectionBorder(pe); } + + public IShapeSelectionInfo getSelectionInfoForShape(Shape shape) { + IShapeSelectionInfo si = new ShapeSelectionInfoImpl(); + si.setColor(IColorConstant.BLUE); + si.setLineStyle(LineStyle.DASH); + return si; + } + @Override public boolean isShowFlyoutPalette() { diff --git a/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/outline/DiagramEditorOutlinePage.java b/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/outline/DiagramEditorOutlinePage.java index 1cf35956..10315021 100644 --- a/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/outline/DiagramEditorOutlinePage.java +++ b/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/outline/DiagramEditorOutlinePage.java @@ -40,6 +40,7 @@ import org.eclipse.triquetrum.workflow.editor.ImageConstants; import org.eclipse.triquetrum.workflow.editor.TriqDiagramTypeProvider; import org.eclipse.triquetrum.workflow.editor.outline.tree.OutlinePartFactory; +import org.eclipse.triquetrum.workflow.editor.outline.tree.TriqOutlineTreeViewer; import org.eclipse.ui.IActionBars; import org.eclipse.ui.IPropertyListener; import org.eclipse.ui.actions.ActionFactory; @@ -98,7 +99,7 @@ public class DiagramEditorOutlinePage extends ContentOutlinePage implements IPro * @since 0.9 */ public DiagramEditorOutlinePage(IDiagramContainerUI diagramEditor) { - super(new TreeViewer()); + super(new TriqOutlineTreeViewer(diagramEditor)); _graphicalViewer = diagramEditor.getGraphicalViewer(); _actionRegistry = diagramEditor.getAdapter(ActionRegistry.class); _editDomain = diagramEditor.getEditDomain(); diff --git a/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/outline/tree/TriqOutlineTreeViewer.java b/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/outline/tree/TriqOutlineTreeViewer.java new file mode 100644 index 00000000..271138f7 --- /dev/null +++ b/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/outline/tree/TriqOutlineTreeViewer.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2017 iSencia Belgium NV. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Erwin De Ley - initial API and implementation and/or initial documentation + *******************************************************************************/ +package org.eclipse.triquetrum.workflow.editor.outline.tree; + +import org.eclipse.emf.transaction.util.Adaptable; +import org.eclipse.gef.EditPart; +import org.eclipse.gef.ui.parts.TreeViewer; +import org.eclipse.graphiti.ui.editor.IDiagramContainerUI; +import org.eclipse.triquetrum.workflow.editor.util.EditorUtils; + +public class TriqOutlineTreeViewer extends TreeViewer implements Adaptable { + + private IDiagramContainerUI diagramContainer; + + public TriqOutlineTreeViewer(IDiagramContainerUI diagramContainer) { + this.diagramContainer = diagramContainer; + } + + @Override + public Object getAdapter(Class adapterType) { + if (adapterType == IDiagramContainerUI.class) + return diagramContainer; + else if (diagramContainer != null) + return diagramContainer.getAdapter(adapterType); + return null; + } + + public EditPart convert(EditPart part) { + Object result = EditorUtils.getModelObjectForSelection(part.getModel()); + if (result != null) { + return (EditPart) getEditPartRegistry().get(result); + } else { + return part; + } + } +} diff --git a/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/util/EditorUtils.java b/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/util/EditorUtils.java index 80a1c6ca..655aeec8 100644 --- a/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/util/EditorUtils.java +++ b/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/util/EditorUtils.java @@ -13,7 +13,6 @@ import java.util.LinkedList; import java.util.List; -import org.eclipse.graphiti.features.IFeatureProvider; import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm; import org.eclipse.graphiti.mm.algorithms.PlatformGraphicsAlgorithm; import org.eclipse.graphiti.mm.algorithms.styles.Color; @@ -41,6 +40,7 @@ import org.eclipse.triquetrum.workflow.model.Relation; import org.eclipse.triquetrum.workflow.model.TriqFactory; import org.eclipse.triquetrum.workflow.model.util.PtObjectBuilderAndApplierVisitor; +import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IEditorReference; import org.eclipse.ui.IWorkbenchPage; @@ -100,7 +100,7 @@ public static int[] setExpanded(PictogramElement pe) { Graphiti.getPeService().setPropertyValue(pe, "isCollapsed", "false"); return new int[] { originalWidth, originalHeight }; } else { - return new int[] {pe.getGraphicsAlgorithm().getWidth(), pe.getGraphicsAlgorithm().getHeight()}; + return new int[] { pe.getGraphicsAlgorithm().getWidth(), pe.getGraphicsAlgorithm().getHeight() }; } } @@ -218,10 +218,15 @@ public static TriqDiagramEditor getSelectedDiagramEditor() { TriqDiagramEditor result = null; IWorkbenchPage page = EclipseUtils.getPage(); if (page != null) { - for (IEditorReference editorRef : page.getEditorReferences()) { - if (editorRef.getId().contains("triquetrum")) { - result = ((TriqDiagramEditor) editorRef.getEditor(true)); - break; + IEditorPart activeEditor = page.getActiveEditor(); + if (activeEditor instanceof TriqDiagramEditor) { + result = (TriqDiagramEditor) activeEditor; + } else { + for (IEditorReference editorRef : page.getEditorReferences()) { + if (editorRef.getId().contains("triquetrum")) { + result = ((TriqDiagramEditor) editorRef.getEditor(true)); + break; + } } } } @@ -278,14 +283,19 @@ public static CompositeActor getSelectedModel() { */ public static NamedObj getModelObjectForSelection(Object editPart) { NamedObj result = null; + PictogramElement pe = (editPart instanceof PictogramElement) ? (PictogramElement) editPart : null; if (editPart instanceof GraphitiShapeEditPart) { GraphitiShapeEditPart shapeEditPart = (GraphitiShapeEditPart) editPart; - IFeatureProvider fp = shapeEditPart.getFeatureProvider(); - Object bo = fp.getBusinessObjectForPictogramElement(shapeEditPart.getPictogramElement()); + pe = shapeEditPart.getPictogramElement(); + } + + if (pe != null) { + Object bo = Graphiti.getLinkService().getBusinessObjectForLinkedPictogramElement(pe); if (bo instanceof NamedObj) { result = (NamedObj) bo; } } + return result; } @@ -459,9 +469,8 @@ public static boolean containsExternallyDefinedFigure(PictogramElement pe) { } /** - * Returns a freshly created list of the port anchors for the given actor shape (or composite actor shape) - * that are in the given category. The list can be manipulated/changed without risk of impacting - * the original actorShape definition (at least when the contained anchors properties are not touched!). + * Returns a freshly created list of the port anchors for the given actor shape (or composite actor shape) that are in the given category. The list can be + * manipulated/changed without risk of impacting the original actorShape definition (at least when the contained anchors properties are not touched!). * * @param actorShape * @param portIoType @@ -479,7 +488,7 @@ public static List getContainedPorts(ContainerShape actorShape, PortCate } return portShapes; } - + /** * * @param source @@ -504,14 +513,15 @@ public static Relation createRelation(NamedObj source, NamedObj target, IORelati relation = TriqFactory.eINSTANCE.createRelation(); CompositeActor relationContainer = null; NamedObj potentialRelationContainer = source.getLowestCommonContainer(target); - if(potentialRelationContainer instanceof CompositeActor) { + if (potentialRelationContainer instanceof CompositeActor) { relationContainer = (CompositeActor) potentialRelationContainer; } else { // this should only happen when connecting an output port and an input port of the same atomic actor if (potentialRelationContainer.getContainer() instanceof CompositeActor) { relationContainer = (CompositeActor) potentialRelationContainer.getContainer(); } else { - throw new IllegalActionException(source.getWrappedObject(), target.getWrappedObject(), "Unsupported source and target hierarchy for creating a relation"); + throw new IllegalActionException(source.getWrappedObject(), target.getWrappedObject(), + "Unsupported source and target hierarchy for creating a relation"); } } diff --git a/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/views/TriqSelectionSynchronizer.java b/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/views/TriqSelectionSynchronizer.java new file mode 100644 index 00000000..0fc18039 --- /dev/null +++ b/org.eclipse.triquetrum.workflow.editor/src/main/java/org/eclipse/triquetrum/workflow/editor/views/TriqSelectionSynchronizer.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 2017 iSencia Belgium NV. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Erwin De Ley - initial API and implementation and/or initial documentation + *******************************************************************************/ +package org.eclipse.triquetrum.workflow.editor.views; + +import java.util.List; + +import org.eclipse.gef.EditPart; +import org.eclipse.gef.EditPartViewer; +import org.eclipse.gef.GraphicalViewer; +import org.eclipse.gef.ui.parts.SelectionSynchronizer; +import org.eclipse.graphiti.mm.pictograms.ContainerShape; +import org.eclipse.graphiti.mm.pictograms.PictogramElement; +import org.eclipse.graphiti.services.Graphiti; +import org.eclipse.triquetrum.workflow.editor.outline.tree.OutlineEditPart; +import org.eclipse.triquetrum.workflow.editor.outline.tree.TriqOutlineTreeViewer; +import org.eclipse.triquetrum.workflow.editor.util.EditorUtils; +import org.eclipse.triquetrum.workflow.model.NamedObj; + +/** + * This is used as a selection broker between views combined with converting + * selected elements between the different types of parts/widgets/... as used + * in the involved views. + * + */ +public class TriqSelectionSynchronizer extends SelectionSynchronizer { + + @Override + protected EditPart convert(EditPartViewer viewer, EditPart part) { + if(viewer instanceof TriqOutlineTreeViewer) { + // This is for when the user selects an element in the graphical editor, + // to make sure the outline tree view reflects the same selection. + return ((TriqOutlineTreeViewer)viewer).convert(part); + } else if (viewer instanceof GraphicalViewer && part instanceof OutlineEditPart) { + // This is for when the user selects an element in the outline tree view, + // and we want to select the corresponding shape in the graphical editor. + Object selectedBO = part.getModel(); + NamedObj modelElement = null; + if(selectedBO instanceof NamedObj) { + // We want to select a top-level entity or parameter in the diagram, + // even when the user clicks on an actor port or parameter in the outline tree view. + // So we move up the containment tree a bit, until we reach the right level. + modelElement = (NamedObj) selectedBO; + while (modelElement.getContainer() != modelElement.topLevel()) { + modelElement = modelElement.getContainer(); + } + } + List pes = Graphiti.getLinkService().getPictogramElements(EditorUtils.getSelectedDiagram(), modelElement); + PictogramElement pe = null; + for (PictogramElement pictogramElement : pes) { + if(pictogramElement instanceof ContainerShape) { + pe = pictogramElement; + break; + } + } + return (EditPart) viewer.getEditPartRegistry().get(pe); + } + return super.convert(viewer, part); + } +}