diff options
author | Mickael ADAM | 2014-10-14 07:15:39 +0000 |
---|---|---|
committer | Mickael ADAM | 2014-10-14 15:50:38 +0000 |
commit | d532bac9fd32e9f2778f01a9cf5c010003f9537b (patch) | |
tree | 458adcb497a426d27fba9f800bd1cf92172c7371 /plugins/infra | |
parent | bb7807ff7375acee6f549dab705a241c5bb2be27 (diff) | |
download | org.eclipse.papyrus-d532bac9fd32e9f2778f01a9cf5c010003f9537b.tar.gz org.eclipse.papyrus-d532bac9fd32e9f2778f01a9cf5c010003f9537b.tar.xz org.eclipse.papyrus-d532bac9fd32e9f2778f01a9cf5c010003f9537b.zip |
445794: [All Diagram] Label Alignment/Justification is lost when the
name is edited instead of the diagram is closed
-Create PapyrusLinkLabelDragPolicy
-Fix the bug
to fix the bug I changed xtend to permit the override of
createEditPolicy for LinkLabelEditPart.
Code will be generated in the next commit
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=445794
Change-Id: Ic66d9d844543f1e66318e190dd8bd91ca6190a7b
Signed-off-by: Mickael ADAM <mickael.adam@ALL4TEC.net>
Diffstat (limited to 'plugins/infra')
4 files changed, 262 insertions, 190 deletions
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpart/PapyrusLabelEditPart.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpart/PapyrusLabelEditPart.java index 287e0c5552e..0fb482deddb 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpart/PapyrusLabelEditPart.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpart/PapyrusLabelEditPart.java @@ -43,7 +43,6 @@ import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationUtils; public abstract class PapyrusLabelEditPart extends LabelEditPart { - /** * Default Margin when not present in CSS */ @@ -157,6 +156,13 @@ public abstract class PapyrusLabelEditPart extends LabelEditPart { } /** + * @return the papyrusLabelLocator + */ + public PapyrusLabelLocator getPapyrusLabelLocator() { + return papyrusLabelLocator; + } + + /** * Sets the external label locator. * * @param offset diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpolicies/PapyrusLinkLabelDragPolicy.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpolicies/PapyrusLinkLabelDragPolicy.java new file mode 100644 index 00000000000..f0dc39eb51e --- /dev/null +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpolicies/PapyrusLinkLabelDragPolicy.java @@ -0,0 +1,97 @@ +/***************************************************************************** + * Copyright (c) 2014 CEA LIST and others. + * + * 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: + * CEA LIST - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.gmfdiag.common.editpolicies; + +import java.util.Collections; +import java.util.List; + +import org.eclipse.draw2d.PositionConstants; +import org.eclipse.draw2d.geometry.Point; +import org.eclipse.draw2d.geometry.PrecisionRectangle; +import org.eclipse.gef.GraphicalEditPart; +import org.eclipse.gef.commands.Command; +import org.eclipse.gef.handles.MoveHandle; +import org.eclipse.gef.requests.ChangeBoundsRequest; +import org.eclipse.gmf.runtime.common.core.command.ICommand; +import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; +import org.eclipse.gmf.runtime.diagram.ui.commands.SetBoundsCommand; +import org.eclipse.gmf.runtime.diagram.ui.editparts.LabelEditPart; +import org.eclipse.gmf.runtime.diagram.ui.editpolicies.NonResizableLabelEditPolicy; +import org.eclipse.gmf.runtime.diagram.ui.l10n.DiagramUIMessages; +import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; +import org.eclipse.gmf.runtime.notation.View; +import org.eclipse.papyrus.infra.gmfdiag.common.editpart.PapyrusLabelEditPart; +import org.eclipse.papyrus.infra.gmfdiag.common.helper.PapyrusLabelHelper; + + +/** + * The Class PapyrusLinkLabelDragPolicy. + * + * @author Mickael ADAM + */ +public class PapyrusLinkLabelDragPolicy extends NonResizableLabelEditPolicy { + + /** + * @see org.eclipse.gef.editpolicies.NonResizableEditPolicy#createSelectionHandles() + * + * @return + */ + @Override + protected List<MoveHandle> createSelectionHandles() { + MoveHandle mh = new MoveHandle((GraphicalEditPart) getHost()); + mh.setBorder(null); + return Collections.singletonList(mh); + } + + /** + * @see org.eclipse.gmf.runtime.diagram.ui.editpolicies.NonResizableLabelEditPolicy#getMoveCommand(org.eclipse.gef.requests.ChangeBoundsRequest) + * + * @param request + * @return + */ + @Override + protected Command getMoveCommand(ChangeBoundsRequest request) { + LabelEditPart editPart = (LabelEditPart) getHost(); + Point refPoint = editPart.getReferencePoint(); + + // translate the feedback figure + PrecisionRectangle rect = new PrecisionRectangle(getInitialFeedbackBounds().getCopy()); + getHostFigure().translateToAbsolute(rect); + rect.translate(request.getMoveDelta()); + rect.resize(request.getSizeDelta()); + getHostFigure().translateToRelative(rect); + + if (editPart instanceof PapyrusLabelEditPart) { + // translate according to the text alignments + switch (((PapyrusLabelEditPart) editPart).getTextAlignment()) { + case PositionConstants.LEFT: + rect.translate(-getHostFigure().getBounds().width / 2, 0); + break; + case PositionConstants.CENTER: + break; + case PositionConstants.RIGHT: + rect.translate(getHostFigure().getBounds().width / 2, 0); + break; + default: + break; + } + } + + Point normalPoint = PapyrusLabelHelper.offsetFromRelativeCoordinate(getHostFigure(), rect, refPoint); + + ICommand moveCommand = new SetBoundsCommand(editPart.getEditingDomain(), DiagramUIMessages.MoveLabelCommand_Label_Location, + new EObjectAdapter((View) editPart.getModel()), normalPoint); + return new ICommandProxy(moveCommand); + } +} diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpolicies/RefreshTextAlignmentEditPolicy.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpolicies/RefreshTextAlignmentEditPolicy.java index 74a3fb970c9..52e2be18b8b 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpolicies/RefreshTextAlignmentEditPolicy.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpolicies/RefreshTextAlignmentEditPolicy.java @@ -16,26 +16,30 @@ package org.eclipse.papyrus.infra.gmfdiag.common.editpolicies; import org.eclipse.core.databinding.observable.ChangeEvent; import org.eclipse.core.databinding.observable.IChangeListener; import org.eclipse.core.databinding.observable.value.IObservableValue; +import org.eclipse.draw2d.IFigure; +import org.eclipse.emf.transaction.util.TransactionUtil; +import org.eclipse.gef.commands.Command; +import org.eclipse.gef.requests.ChangeBoundsRequest; import org.eclipse.gmf.runtime.gef.ui.internal.editpolicies.GraphicalEditPolicyEx; import org.eclipse.gmf.runtime.notation.View; +import org.eclipse.papyrus.commands.wrappers.GEFtoEMFCommandWrapper; import org.eclipse.papyrus.infra.emf.utils.EMFHelper; import org.eclipse.papyrus.infra.gmfdiag.common.databinding.custom.CustomStringStyleObservableValue; +import org.eclipse.papyrus.infra.gmfdiag.common.editpart.PapyrusLabelEditPart; /** * this edit policy has in charge to refresh the edit part when text alignment change. */ public class RefreshTextAlignmentEditPolicy extends GraphicalEditPolicyEx implements IChangeListener { - /** The Constant TEXT_ALIGNMENT. */ - public static final String TEXT_ALIGNMENT = "textAlignment";//$NON-NLS-N$ - /** key for this edit policy. */ public final static String REFRESH_TEXT_ALIGNMENT_EDITPOLICY = "Refresh text alignment edit policy";//$NON-NLS-N$ /** The style observable. */ protected IObservableValue styleObservable; - protected IObservableValue positionObservable; + /** The view. */ + private View view; /** * @@ -44,14 +48,16 @@ public class RefreshTextAlignmentEditPolicy extends GraphicalEditPolicyEx implem @Override public void activate() { // retrieve the view and the element managed by the edit part - View view = (View) getHost().getModel(); + view = (View) getHost().getModel(); if (view == null) { return; } - // add style observable value - styleObservable = new CustomStringStyleObservableValue(view, EMFHelper.resolveEditingDomain(view), TEXT_ALIGNMENT); - styleObservable.addChangeListener(this); + if (getHost() instanceof PapyrusLabelEditPart) { + // add style observable value + styleObservable = new CustomStringStyleObservableValue(view, EMFHelper.resolveEditingDomain(view), PapyrusLabelEditPart.TEXT_ALIGNMENT); + styleObservable.addChangeListener(this); + } getHost().refresh(); } @@ -75,7 +81,33 @@ public class RefreshTextAlignmentEditPolicy extends GraphicalEditPolicyEx implem */ @Override public void handleChange(ChangeEvent event) { + + if (getHost() instanceof PapyrusLabelEditPart) { + // set the position to avoid that position move on text alignment change + PapyrusLabelEditPart editPart = (PapyrusLabelEditPart) getHost(); + + IFigure figure = editPart.getFigure(); + + ChangeBoundsRequest req = new ChangeBoundsRequest(REQ_MOVE); + req.setEditParts(editPart); + req.setLocation(figure.getBounds().getLocation()); + Command command = editPart.getCommand(req); + if (command != null && command.canExecute()) { + TransactionUtil.getEditingDomain(view).getCommandStack().execute(GEFtoEMFCommandWrapper.wrap(command)); + } + + } + + // Save new location only on change + // final Point offset = newOffset; + // ChangeBoundsRequest req = new ChangeBoundsRequest(RequestConstants.REQ_MOVE); + // req.setEditParts(editPart); + // req.setLocation(offset); + // Command command = editPart.getCommand(req); + // if (command != null && command.canExecute() && !cachedIsConstrained) { + // TransactionUtil.getEditingDomain(view).getCommandStack().execute(GEFtoEMFCommandWrapper.wrap(command)); + // } + getHost().refresh(); } - } diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/locator/PapyrusLabelLocator.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/locator/PapyrusLabelLocator.java index f63c9d1de1f..18f22a38e20 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/locator/PapyrusLabelLocator.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/locator/PapyrusLabelLocator.java @@ -23,16 +23,9 @@ import org.eclipse.draw2d.geometry.Dimension; import org.eclipse.draw2d.geometry.Point; import org.eclipse.draw2d.geometry.PointList; import org.eclipse.draw2d.geometry.Rectangle; -import org.eclipse.emf.transaction.TransactionalEditingDomain; -import org.eclipse.emf.transaction.util.TransactionUtil; -import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil; import org.eclipse.gmf.runtime.diagram.ui.internal.util.LabelViewConstants; -import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel; -import org.eclipse.gmf.runtime.draw2d.ui.geometry.LineSeg; import org.eclipse.gmf.runtime.draw2d.ui.geometry.PointListUtilities; -import org.eclipse.gmf.runtime.notation.NotationPackage; import org.eclipse.gmf.runtime.notation.View; -import org.eclipse.papyrus.infra.gmfdiag.common.Activator; import org.eclipse.papyrus.infra.gmfdiag.common.helper.PapyrusLabelHelper; /** @@ -42,58 +35,27 @@ import org.eclipse.papyrus.infra.gmfdiag.common.helper.PapyrusLabelHelper; */ public class PapyrusLabelLocator extends AbstractLocator { - /** the parent figure of this locator. */ - protected IFigure parent; - /** The alignment. */ private int alignment; - /** The offset. */ - private Point offset; - /** The extent. */ private Dimension extent; - /** The cached string. */ - private String cachedString; - - /** The text alignment. */ - private int textAlignment = PositionConstants.MIDDLE; - - /** - * Gets the text alignment. - * - * @return the textAlignment - */ - public int getTextAlignment() { - return textAlignment; - } + /** The margin. */ + private Point margin = new Point(); - /** - * Sets the text alignment. - * - * @param textAlignment - * the textAlignment to set - */ - public void setTextAlignment(int textAlignment) { - this.textAlignment = textAlignment; - } + /** The offset. */ + private Point offset; - /** The cached bounds. */ - private Rectangle cachedBounds = new Rectangle(); + /** the parent figure of this locator. */ + protected IFigure parent; - /** The cached offset. */ - private Point cachedOffset; + /** The text alignment. */ + private int textAlignment = PositionConstants.MIDDLE; /** The view. */ private View view; - /** The cached segment. */ - private LineSeg cachedSegment; - - /** The margin. */ - private Point margin = new Point(); - /** * Constructor to create a an instance of <code>LabelLocator</code> which locates an IFigure offset relative to a calculated reference point. * @@ -127,6 +89,35 @@ public class PapyrusLabelLocator extends AbstractLocator { } /** + * Returns the alignment of ConnectionLocator. + * + * @return The alignment + * + */ + public int getAlignment() { + return alignment; + } + + /** + * Gets the location. + * + * @return the location + */ + @SuppressWarnings("restriction") + private int getLocation() { + switch (getAlignment()) { + case ConnectionLocator.SOURCE: + return LabelViewConstants.TARGET_LOCATION; + case ConnectionLocator.TARGET: + return LabelViewConstants.SOURCE_LOCATION; + case ConnectionLocator.MIDDLE: + return LabelViewConstants.MIDDLE_LOCATION; + default: + return LabelViewConstants.MIDDLE_LOCATION; + } + } + + /** * getter for the offset point. * * @return point @@ -136,13 +127,52 @@ public class PapyrusLabelLocator extends AbstractLocator { } /** - * setter for the offset point. + * Returns the <code>PointList</code> describing the label's parent. * - * @param offset - * the new offset + * @return pointList */ - public void setOffset(Point offset) { - this.offset = offset; + protected PointList getPointList() { + if (parent instanceof Connection) { + return ((Connection) parent).getPoints(); + } else { + PointList ptList = new PointList(); + ptList.addPoint(parent.getBounds().getLocation()); + return ptList; + } + } + + /** + * Returns the reference point for the locator. + * + * @return the reference point + */ + @Override + public Point getReferencePoint() { + if (parent instanceof Connection) { + PointList ptList = ((Connection) parent).getPoints(); + return PointListUtilities.calculatePointRelativeToLine(ptList, 0, getLocation(), true); + } else { + return parent.getBounds().getLocation(); + } + } + + /** + * Gets the size. + * + * @return the size + */ + public Dimension getSize() { + return extent.getCopy(); + } + + + /** + * Gets the text alignment. + * + * @return the textAlignment + */ + public int getTextAlignment() { + return textAlignment; } /** @@ -155,11 +185,9 @@ public class PapyrusLabelLocator extends AbstractLocator { @Override public void relocate(IFigure target) { - // The calculation of the location depends on the size of the shape so - // the size must be set first. + // The calculation of the location depends on the size of the shape so the size must be set first. Dimension size = new Dimension(); - if (extent != null) { PapyrusLabelLocator currentConstraint = (PapyrusLabelLocator) target.getParent().getLayoutManager().getConstraint(target); Dimension currentExtent = currentConstraint.getSize(); @@ -175,114 +203,27 @@ public class PapyrusLabelLocator extends AbstractLocator { target.setSize(new Dimension(target.getPreferredSize().width, target.getPreferredSize().height)); } - // Get the segment - PointList ptLst = PapyrusLabelHelper.getParentPointList(target); - int index = PointListUtilities.findNearestLineSegIndexOfPoint(ptLst, getReferencePoint()); - LineSeg segment = (LineSeg) PointListUtilities.getLineSegments(ptLst).get(index - 1); - Point location = null; - // If it's a rename - if (cachedString != null && !cachedString.equals(((WrappingLabel) target).getText())) { - location = cachedBounds.getLocation(); - int x; - int textWidth = ((IFigure) target.getChildren().get(0)).getBounds().width; - - // Set Location - switch (textAlignment) { - case PositionConstants.LEFT: - x = 0; - break; - case PositionConstants.RIGHT: - x = textWidth - cachedBounds.width + margin.x; - break; - case PositionConstants.CENTER: - x = (textWidth - cachedBounds.width + margin.x) / 2; - break; - default: - x = 0; - break; - } - location.translate(x, 0); - IFigure tmpTarget = target; - tmpTarget.translate(x, 0); - final Point viewLocation = PapyrusLabelHelper.offsetFromRelativeCoordinate(tmpTarget, cachedBounds, getReferencePoint()); - try { - TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(view); - org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.utils.TransactionHelper.run(domain, new Runnable() { - - @Override - public void run() { - // Add modelStylesheet to the resource without command - ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_X(), Integer.valueOf(viewLocation.x)); - ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_Y(), Integer.valueOf(viewLocation.y)); - } - }); - } catch (Exception e) { - Activator.log.debug(e.toString()); - } - } else - // At the creation or It's a d&d move or // If it a move the connection - if (cachedBounds.height == 0 || !offset.equals(cachedOffset) || !segment.equals(cachedSegment)) { - location = PapyrusLabelHelper.relativeCoordinateFromOffset(target, getReferencePoint(), offset); - // cachedString = ((WrappingLabel) target).getText(); - } else { - // nothing change, in refresh case - location = cachedBounds.getLocation(); + // Calculate the position + location = PapyrusLabelHelper.relativeCoordinateFromOffset(target, getReferencePoint(), offset); + + // Translate the position according to the justification + switch (textAlignment) { + case PositionConstants.LEFT: + location.translate(target.getBounds().width / 2, 0); + break; + case PositionConstants.RIGHT: + location.translate(-target.getBounds().width / 2, 0); + break; + case PositionConstants.CENTER: + break; + default: + break; } // Set the location target.setLocation(location); - - // cache actual values - cachedString = ((WrappingLabel) target).getText(); - cachedBounds = target.getBounds(); - cachedOffset = offset; - cachedSegment = segment; - } - - /** - * Returns the reference point for the locator. - * - * @return the reference point - */ - @Override - protected Point getReferencePoint() { - if (parent instanceof Connection) { - PointList ptList = ((Connection) parent).getPoints(); - return PointListUtilities.calculatePointRelativeToLine(ptList, 0, getLocation(), true); - } else { - return parent.getBounds().getLocation(); - } - } - - - /** - * Gets the location. - * - * @return the location - */ - private int getLocation() { - switch (getAlignment()) { - case ConnectionLocator.SOURCE: - return LabelViewConstants.TARGET_LOCATION; - case ConnectionLocator.TARGET: - return LabelViewConstants.SOURCE_LOCATION; - case ConnectionLocator.MIDDLE: - return LabelViewConstants.MIDDLE_LOCATION; - default: - return LabelViewConstants.MIDDLE_LOCATION; - } - } - - /** - * Returns the alignment of ConnectionLocator. - * - * @return The alignment - * - */ - public int getAlignment() { - return alignment; } /** @@ -297,47 +238,43 @@ public class PapyrusLabelLocator extends AbstractLocator { /** - * Gets the size. + * Sets the margin. * - * @return the size + * @param margin + * the margin to set */ - public Dimension getSize() { - return extent.getCopy(); + public void setMargin(Point margin) { + this.margin = margin; } /** - * Returns the <code>PointList</code> describing the label's parent. + * setter for the offset point. * - * @return pointList + * @param offset + * the new offset */ - protected PointList getPointList() { - if (parent instanceof Connection) { - return ((Connection) parent).getPoints(); - } else { - PointList ptList = new PointList(); - ptList.addPoint(parent.getBounds().getLocation()); - return ptList; - } + public void setOffset(Point offset) { + this.offset = offset; } /** - * Sets the view. + * Sets the text alignment. * - * @param view - * the new view + * @param textAlignment + * the textAlignment to set */ - public void setView(View view) { - this.view = view; + public void setTextAlignment(int textAlignment) { + this.textAlignment = textAlignment; } /** - * Sets the margin. + * Sets the view. * - * @param margin - * the margin to set + * @param view + * the new view */ - public void setMargin(Point margin) { - this.margin = margin; + public void setView(View view) { + this.view = view; } |