Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCéline Janssens2014-09-03 12:02:20 +0000
committerCéline Janssens2014-09-22 15:16:07 +0000
commit802249ab83497e7e4f8b472a89c92b3486db874b (patch)
treeba487be56628a1c1d06816f62438585c098dcd36 /plugins/infra
parente2666ce6ba7779d2c5087bc91c7a1dd236cdc563 (diff)
downloadorg.eclipse.papyrus-802249ab83497e7e4f8b472a89c92b3486db874b.tar.gz
org.eclipse.papyrus-802249ab83497e7e4f8b472a89c92b3486db874b.tar.xz
org.eclipse.papyrus-802249ab83497e7e4f8b472a89c92b3486db874b.zip
440230: [All Diagrams] Add Margins to the Labels
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=440230 - Margin for Node Named Element - PapyrusWrappingLabel deprecated has been deleted and references changed to oep.infra.gmfdiag.common.figure.node.PapyrusWrappingLabel - Add refreshMargin() method to PapyrusLabelEditPart - New format and CleanUp refactor for class and composite diagramme - Add reference to PapyrusLabelEditPart fort PortNameEditPart (via xtend) - Add ref to PapyrusWrappingLabel into UMLEdgeFigure children - modify GmfGen for Composite Diag : PortNameEditPart is now a PapyrusWrappingLabel - rebase with Master Luna Change-Id: I3b8fcd39bf9b1c00886b6357c7d054ebc3fd8aba Signed-off-by: Céline Janssens <Celine.Janssens@all4tec.net>
Diffstat (limited to 'plugins/infra')
-rw-r--r--plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.xtext/xtend-gen/org/eclipse/papyrus/infra/gmfdiag/generator/CSSGenerator.java29
-rw-r--r--plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/META-INF/MANIFEST.MF1
-rw-r--r--plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/AbstractAlignLabelCommand.java8
-rw-r--r--plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/AlignLabelCommand.java23
-rw-r--r--plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/editpart/PapyrusLabelEditPart.java142
-rw-r--r--plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/figure/IPapyrusWrappingLabel.java34
-rw-r--r--plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/figure/node/PapyrusWrappingLabel.java24
-rw-r--r--plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/model/NotationUtils.java87
-rw-r--r--plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/utils/FigureUtils.java148
9 files changed, 408 insertions, 88 deletions
diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.xtext/xtend-gen/org/eclipse/papyrus/infra/gmfdiag/generator/CSSGenerator.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.xtext/xtend-gen/org/eclipse/papyrus/infra/gmfdiag/generator/CSSGenerator.java
index a076db98ff3..4fe96783a9d 100644
--- a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.xtext/xtend-gen/org/eclipse/papyrus/infra/gmfdiag/generator/CSSGenerator.java
+++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.xtext/xtend-gen/org/eclipse/papyrus/infra/gmfdiag/generator/CSSGenerator.java
@@ -1,15 +1,14 @@
-/**
- * generated by Xtext
- */
-package org.eclipse.papyrus.infra.gmfdiag.generator;
-
-import org.eclipse.emf.ecore.resource.Resource;
-import org.eclipse.xtext.generator.IFileSystemAccess;
-import org.eclipse.xtext.generator.IGenerator;
-
-@SuppressWarnings("all")
-public class CSSGenerator implements IGenerator {
- @Override
-public void doGenerate(final Resource resource, final IFileSystemAccess fsa) {
- }
-}
+/**
+ * generated by Xtext
+ */
+package org.eclipse.papyrus.infra.gmfdiag.generator;
+
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.xtext.generator.IFileSystemAccess;
+import org.eclipse.xtext.generator.IGenerator;
+
+@SuppressWarnings("all")
+public class CSSGenerator implements IGenerator {
+ public void doGenerate(final Resource resource, final IFileSystemAccess fsa) {
+ }
+}
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/META-INF/MANIFEST.MF b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/META-INF/MANIFEST.MF
index 1cf6fc227d5..1effd1c4ca7 100644
--- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/META-INF/MANIFEST.MF
+++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/META-INF/MANIFEST.MF
@@ -7,6 +7,7 @@ Export-Package: org.eclipse.papyrus.infra.gmfdiag.common,
org.eclipse.papyrus.infra.gmfdiag.common.decoration,
org.eclipse.papyrus.infra.gmfdiag.common.editpart,
org.eclipse.papyrus.infra.gmfdiag.common.editpolicies,
+ org.eclipse.papyrus.infra.gmfdiag.common.figure,
org.eclipse.papyrus.infra.gmfdiag.common.figure.edge,
org.eclipse.papyrus.infra.gmfdiag.common.figure.node,
org.eclipse.papyrus.infra.gmfdiag.common.handler,
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/AbstractAlignLabelCommand.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/AbstractAlignLabelCommand.java
deleted file mode 100644
index 577ea2cd271..00000000000
--- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/AbstractAlignLabelCommand.java
+++ /dev/null
@@ -1,8 +0,0 @@
-package org.eclipse.papyrus.infra.gmfdiag.common.commands;
-
-import org.eclipse.gef.commands.Command;
-
-public class AbstractAlignLabelCommand extends Command {
-
-
-}
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/AlignLabelCommand.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/AlignLabelCommand.java
deleted file mode 100644
index e5dfcbc67ed..00000000000
--- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/AlignLabelCommand.java
+++ /dev/null
@@ -1,23 +0,0 @@
-package org.eclipse.papyrus.infra.gmfdiag.common.commands;
-
-import org.eclipse.draw2d.geometry.Rectangle;
-import org.eclipse.gef.commands.Command;
-import org.eclipse.papyrus.infra.gmfdiag.common.editpart.PapyrusLabelEditPart;
-
-public class AlignLabelCommand extends AbstractAlignLabelCommand {
-
- private Rectangle constraint;
-
- private PapyrusLabelEditPart label;
-
- @Override
- public void execute() {
- // TODO Auto-generated method stub
- //super.execute();
-
- System.out.println("____J'execute la commande");
- }
-
-
-
-}
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 c80c3124e77..d3cf12ce35e 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
@@ -1,41 +1,101 @@
-/*****************************************************************************
- * Copyright (c) 2014 CEA LIST.
- *
- * 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:
- * Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
- * Cline Janssens (ALL4TEC) celine.janssens@all4tec.net
- * Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Bug 443235
- *****************************************************************************/
-package org.eclipse.papyrus.infra.gmfdiag.common.editpart;
-
-import org.eclipse.gmf.runtime.diagram.ui.editparts.LabelEditPart;
-import org.eclipse.gmf.runtime.notation.View;
-import org.eclipse.papyrus.infra.gmfdiag.common.editpolicies.BorderDisplayEditPolicy;
-import org.eclipse.papyrus.infra.gmfdiag.common.editpolicies.LabelAlignmentEditPolicy;
-import org.eclipse.papyrus.infra.gmfdiag.common.editpolicies.LabelPrimarySelectionEditPolicy;
-
-
-
-public abstract class PapyrusLabelEditPart extends LabelEditPart {
-
- public PapyrusLabelEditPart(View view) {
- super(view);
- }
-
- @Override
- protected void createDefaultEditPolicies() {
- super.createDefaultEditPolicies();
-
- installEditPolicy(BorderDisplayEditPolicy.BORDER_DISPLAY_EDITPOLICY, new BorderDisplayEditPolicy());
- installEditPolicy(LabelAlignmentEditPolicy.LABEL_ALIGNMENT_KEY, new LabelAlignmentEditPolicy());
- installEditPolicy(LabelPrimarySelectionEditPolicy.LABEL_PRIMARY_SELECTION_KEY, new LabelPrimarySelectionEditPolicy());
-
- }
-}
-
-
+/*****************************************************************************
+ * Copyright (c) 2014 CEA LIST.
+ *
+ * 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:
+ * Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
+ * Céline Janssens (ALL4TEC) celine.janssens@all4tec.net
+ * Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Bug 443235
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.gmfdiag.common.editpart;
+
+import org.eclipse.draw2d.IFigure;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
+import org.eclipse.gmf.runtime.diagram.ui.editparts.LabelEditPart;
+import org.eclipse.gmf.runtime.notation.View;
+import org.eclipse.papyrus.infra.gmfdiag.common.editpolicies.BorderDisplayEditPolicy;
+import org.eclipse.papyrus.infra.gmfdiag.common.editpolicies.LabelAlignmentEditPolicy;
+import org.eclipse.papyrus.infra.gmfdiag.common.editpolicies.LabelPrimarySelectionEditPolicy;
+import org.eclipse.papyrus.infra.gmfdiag.common.figure.IPapyrusWrappingLabel;
+import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationUtils;
+
+
+public abstract class PapyrusLabelEditPart extends LabelEditPart {
+
+
+ /**
+ * Default Margin when not present in CSS
+ */
+ static final int DEFAULT_MARGIN = 1;
+
+ /**
+ * CSS Integer property to define the horizontal Label Margin
+ */
+ static final String X_MARGIN_PROPERTY = "xMarginLabel";
+
+ /**
+ * CSS Integer property to define the vertical Label Margin
+ */
+ static final String Y_MARGIN_PROPERTY = "yMarginLabel";
+
+
+ public PapyrusLabelEditPart(View view) {
+ super(view);
+ }
+
+ @Override
+ protected void createDefaultEditPolicies() {
+ super.createDefaultEditPolicies();
+
+ installEditPolicy(BorderDisplayEditPolicy.BORDER_DISPLAY_EDITPOLICY, new BorderDisplayEditPolicy());
+ installEditPolicy(LabelAlignmentEditPolicy.LABEL_ALIGNMENT_KEY, new LabelAlignmentEditPolicy());
+ installEditPolicy(LabelPrimarySelectionEditPolicy.LABEL_PRIMARY_SELECTION_KEY, new LabelPrimarySelectionEditPolicy());
+ }
+
+
+
+ /**
+ * @see org.eclipse.gmf.runtime.diagram.ui.editparts.LabelEditPart#refreshVisuals()
+ *
+ */
+ @Override
+ protected void refreshVisuals() {
+ super.refreshVisuals();
+ refreshLabelMargin();
+ }
+
+ /**
+ *
+ */
+ public void refreshLabelMargin() {
+ IFigure figure = null;
+
+ int horizontalMargin = DEFAULT_MARGIN;
+ int verticalMargin= DEFAULT_MARGIN;
+
+ Object model = this.getModel();
+
+
+
+ if (model instanceof View) {
+ horizontalMargin = NotationUtils.getIntValue((View)model, X_MARGIN_PROPERTY, DEFAULT_MARGIN);
+ verticalMargin = NotationUtils.getIntValue((View)model, Y_MARGIN_PROPERTY, DEFAULT_MARGIN);
+ }
+
+ if (this instanceof GraphicalEditPart){
+ figure = ((GraphicalEditPart) this).getFigure();
+
+ if (figure instanceof IPapyrusWrappingLabel){
+ ((IPapyrusWrappingLabel)figure).setMarginLabel(horizontalMargin, verticalMargin);
+ }
+
+ }
+
+
+ }
+
+} \ No newline at end of file
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/figure/IPapyrusWrappingLabel.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/figure/IPapyrusWrappingLabel.java
new file mode 100644
index 00000000000..1c1c8def10d
--- /dev/null
+++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/figure/IPapyrusWrappingLabel.java
@@ -0,0 +1,34 @@
+/*****************************************************************************
+ * 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:
+ * Céline Janssens (ALL4TEC) celine.janssens@all4tec.net - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.infra.gmfdiag.common.figure;
+
+import org.eclipse.draw2d.IFigure;
+
+/**
+ * @author Céline JANSSENS
+ * Bug 440230
+ * This interface is dedicated to PapyrusWrappingLabel abstract methods.
+ *
+ */
+public interface IPapyrusWrappingLabel extends IFigure {
+
+ /**
+ * Set the margin of the Label in the horizontal direction and vertical Direction
+ * @param xMargin Horizontal Margin
+ * @param yMargin Vertical Margin
+ *
+ */
+ void setMarginLabel(int xMargin , int yMargin);
+
+}
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/figure/node/PapyrusWrappingLabel.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/figure/node/PapyrusWrappingLabel.java
index a053c350f2e..52b4b352ba3 100644
--- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/figure/node/PapyrusWrappingLabel.java
+++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/figure/node/PapyrusWrappingLabel.java
@@ -10,18 +10,22 @@
* Contributors:
* Atos Origin - Initial API and implementation
* Arthur Daussy - Bug 354622 - [ActivityDiagram] Object Flows selection prevent selecting other close elements.
+ * Céline Janssens (ALL4TEC) celine.janssens@all4tec.net - Bug 440230 - Margin Label
*
*****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.figure.node;
+import org.eclipse.draw2d.MarginBorder;
+import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
+import org.eclipse.papyrus.infra.gmfdiag.common.figure.IPapyrusWrappingLabel;
/**
* This correct the bug where invisible label can be selected
*
* @author arthur daussy
*/
-public class PapyrusWrappingLabel extends WrappingLabel {
+public class PapyrusWrappingLabel extends WrappingLabel implements IPapyrusWrappingLabel{
/**
* Bug 354622 - [ActivityDiagram] Object Flows selection prevent selecting other close elements.
@@ -44,4 +48,22 @@ public class PapyrusWrappingLabel extends WrappingLabel {
return false;
}
+ /**
+ * @see org.eclipse.papyrus.infra.gmfdiag.common.figure.IPapyrusWrappingLabel#setMarginLabel(int, int)
+ *
+ * @param xMargin Horizontal margin
+ * @param yMargin Vertical margin
+ */
+ @Override
+ public void setMarginLabel(int xMargin, int yMargin) {
+
+ MarginBorder mb = new MarginBorder(xMargin, yMargin, xMargin, yMargin);
+ this.setBorder(mb);
+
+
+
+
+ }
+
+
}
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/model/NotationUtils.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/model/NotationUtils.java
index cf2ac4652dd..699f3575b31 100644
--- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/model/NotationUtils.java
+++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/model/NotationUtils.java
@@ -16,12 +16,19 @@ package org.eclipse.papyrus.infra.gmfdiag.common.model;
import java.util.LinkedList;
import java.util.List;
+import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.gmf.runtime.notation.BooleanValueStyle;
import org.eclipse.gmf.runtime.notation.Diagram;
+import org.eclipse.gmf.runtime.notation.IntValueStyle;
+import org.eclipse.gmf.runtime.notation.NamedStyle;
+import org.eclipse.gmf.runtime.notation.NotationPackage;
+import org.eclipse.gmf.runtime.notation.StringValueStyle;
+import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.core.resource.IModel;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.services.ServiceException;
@@ -247,5 +254,85 @@ public class NotationUtils {
throw new RuntimeException("Resource Set is not a ModelSet or is null"); //$NON-NLS-1$
}
}
+
+
+ /**
+ * @param view
+ * @param property
+ * @param defaultValue
+ * @return Integer corresponding to the property
+ */
+ public static int getIntValue(View view, String property, int defaultInt) {
+ int value = defaultInt;
+ EClass intValueStyle = NotationPackage.eINSTANCE.getIntValueStyle();
+ NamedStyle style ;
+
+ if (intValueStyle != null){
+
+ style = view.getNamedStyle(intValueStyle, property);
+
+ if (style != null){
+ if (style instanceof IntValueStyle){
+ value = ((IntValueStyle) style).getIntValue();
+ }
+ }
+ }
+
+ return value;
+ }
+
+
+
+ /**
+ * @param view
+ * @param property
+ * @param defaultValue
+ * @return Boolean corresponding to the property
+ */
+ public static boolean getBooleanValue(View view, String property, boolean defaultValue) {
+ boolean value = defaultValue;
+ EClass booleanValueStyle = NotationPackage.eINSTANCE.getBooleanValueStyle();
+ NamedStyle style ;
+
+ if (booleanValueStyle != null){
+
+ style = view.getNamedStyle(booleanValueStyle, property);
+
+ if (style != null){
+ if (style instanceof BooleanValueStyle){
+ value = ((BooleanValueStyle) style).isBooleanValue();
+ }
+ }
+ }
+
+ return value;
+ }
+
+
+ /**
+ * @param view
+ * @param property
+ * @param defaultValue
+ * @return String corresponding to the property
+ */
+ public static String getStringValue(View view, String property, String defaultValue) {
+ String value = defaultValue;
+ EClass stringValueStyle = NotationPackage.eINSTANCE.getStringValueStyle();
+ NamedStyle style ;
+
+ if (stringValueStyle != null){
+
+ style = view.getNamedStyle(stringValueStyle, property);
+
+ if (style != null){
+ if (style instanceof StringValueStyle){
+ value = ((StringValueStyle) style).getStringValue();
+ }
+ }
+ }
+
+ return value;
+ }
+
}
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/utils/FigureUtils.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/utils/FigureUtils.java
new file mode 100644
index 00000000000..2ca645313d4
--- /dev/null
+++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/utils/FigureUtils.java
@@ -0,0 +1,148 @@
+/*****************************************************************************
+ * Copyright (c) 2012 CEA LIST.
+ *
+ *
+ * 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
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.gmfdiag.common.utils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.draw2d.FigureUtilities;
+import org.eclipse.draw2d.IFigure;
+import org.eclipse.draw2d.LayeredPane;
+import org.eclipse.draw2d.ScalableFigure;
+import org.eclipse.draw2d.geometry.Point;
+import org.eclipse.draw2d.geometry.Rectangle;
+import org.eclipse.gmf.runtime.diagram.ui.figures.ResizableCompartmentFigure;
+
+public final class FigureUtils {
+
+ private FigureUtils() {
+ // utility class
+ }
+
+ /**
+ * Returns a child figure of the given type recursively contained in the given parent figure.
+ *
+ * @param parent
+ * the parent figure
+ * @param childFigureClass
+ * the type of the child figure that is looked for
+ * @return the child figure if found, or <code>null</code> if the parent figure contains no child figure of the
+ * given type
+ */
+ @SuppressWarnings("unchecked")
+ public static <T extends IFigure> T findChildFigureInstance(final IFigure parent, final Class<T> childFigureClass) {
+ final List<IFigure> children = parent.getChildren();
+ for (final IFigure child : children) {
+ if (childFigureClass.isAssignableFrom(child.getClass())) {
+ return (T) child;
+ }
+ // look recursively
+ final IFigure subresult = findChildFigureInstance(child, childFigureClass);
+ if (subresult != null) {
+ return (T) subresult;
+ }
+ }
+ // not found
+ return null;
+ }
+
+ public static <T extends IFigure> List<T> findChildFigureInstances(final IFigure parent, final Class<T> childFigureClass) {
+ final List<T> result = new ArrayList<T>();
+ internalFindChildFigureInstances(parent, result, childFigureClass);
+ return result;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <T extends IFigure> void internalFindChildFigureInstances(final IFigure parent, final List<T> result, final Class<T> childFigureClass) {
+ final List<IFigure> children = parent.getChildren();
+ for (final IFigure child : children) {
+ if (childFigureClass.isAssignableFrom(child.getClass())) {
+ result.add((T) child);
+ } else {
+ internalFindChildFigureInstances(child, result, childFigureClass);
+ }
+
+ }
+ }
+
+ /**
+ * Returns the first parent figure of the given type recursively containing the given figure.
+ *
+ * @param figure
+ * the figure
+ * @param parentFigureClass
+ * the type of the parent figure that is looked for
+ * @return the parent figure if found, or <code>null</code> if the figure is not contained by any figure of the
+ * given type
+ */
+ @SuppressWarnings("unchecked")
+ public static <T extends IFigure> T findParentFigureInstance(final IFigure figure, final Class<T> parentFigureClass) {
+ IFigure parent = figure.getParent();
+ while (parent != null) {
+ if (parentFigureClass.isAssignableFrom(parent.getClass())) {
+ return (T) parent;
+ }
+ parent = parent.getParent();
+ }
+ return null;
+ }
+
+ /**
+ * Constrain the given bounds (whose presumed purpose is to become the new bounds for the given Figure), so that
+ * they don't stick out of the bounds of the {@link ResizableCompartmentFigure} in which the given figure is
+ * contained. Returns the original bounds if the given Figure is not contained in a {@link ResizableCompartmentFigure}.
+ *
+ * @param bounds
+ * The bounds to constrain (will not be modified)
+ * @param figure
+ * the figure in the containing {@link ResizableCompartmentFigure} of which the bound must be constrained
+ * @return the bounds, constrained so that they don't stick out of the {@link ResizableCompartmentFigure} in which
+ * the Figure is contained
+ *
+ */
+ public static Rectangle constrainBoundsWithinContainingCompartment(final Rectangle bounds, final IFigure figure) {
+ final Rectangle result = new Rectangle(bounds);
+ final ResizableCompartmentFigure parentCompartment = FigureUtils.findParentFigureInstance(figure, ResizableCompartmentFigure.class);
+ if (parentCompartment == null) {
+ return result;
+ }
+ final Rectangle parentBounds = parentCompartment.getBounds();
+ result.x = Math.max(result.x, 0);
+ result.y = Math.max(result.y, 0);
+ result.x = Math.min(result.x, parentBounds.width - result.width);
+ result.y = Math.min(result.y, parentBounds.height - result.height);
+ return result;
+ }
+
+ /**
+ * @return The origin of the coordinate system applied to the given figure (i.e.: the amount scrolled in the scroll
+ * pane)
+ */
+ public static Point getLayeredPaneOrigin(final IFigure figure) {
+ final LayeredPane layeredPane = FigureUtils.findChildFigureInstance(FigureUtilities.getRoot(figure), LayeredPane.class);
+ final Point origin = new Point(0, 0);
+ layeredPane.translateToRelative(origin);
+ return origin;
+ }
+
+ /**
+ * @return The scale applied to the given figure (i.e.: the zoom set by the user)
+ */
+ public static double getScale(final IFigure figure) {
+ final ScalableFigure scalableFigure = FigureUtils.findParentFigureInstance(figure, ScalableFigure.class);
+ return scalableFigure.getScale();
+ }
+
+ public static Point scaleByZoom(final Point delta, final IFigure figure) {
+ return new Point(delta).scale(1.0 / getScale(figure));
+ }
+
+}

Back to the top