Bug 415884 - Cannot query size of a multi-line text
*Introduced new method calculateTextSize that reflects new line
characters in the returned height
*Introduced new method calculateTextSize that in case a MultiText is
passed reflects new line characters in the height.
Change-Id: I9ebd8c9747ef5a42db65081faf306ad46899fed7
diff --git a/plugins/org.eclipse.graphiti.ui/.settings/.api_filters b/plugins/org.eclipse.graphiti.ui/.settings/.api_filters
index eff93aa..ef8c49e 100644
--- a/plugins/org.eclipse.graphiti.ui/.settings/.api_filters
+++ b/plugins/org.eclipse.graphiti.ui/.settings/.api_filters
@@ -173,6 +173,18 @@
</filter>
</resource>
<resource path="src/org/eclipse/graphiti/ui/services/IUiLayoutService.java" type="org.eclipse.graphiti.ui.services.IUiLayoutService">
+ <filter id="403804204">
+ <message_arguments>
+ <message_argument value="org.eclipse.graphiti.ui.services.IUiLayoutService"/>
+ <message_argument value="calculateTextSize(AbstractText)"/>
+ </message_arguments>
+ </filter>
+ <filter id="403804204">
+ <message_arguments>
+ <message_argument value="org.eclipse.graphiti.ui.services.IUiLayoutService"/>
+ <message_argument value="calculateTextSize(String, Font, boolean)"/>
+ </message_arguments>
+ </filter>
<filter id="571473929">
<message_arguments>
<message_argument value="ILayoutService"/>
diff --git a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/IGefService.java b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/IGefService.java
index 079171b..61c6cb9 100644
--- a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/IGefService.java
+++ b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/IGefService.java
@@ -1,7 +1,7 @@
/*******************************************************************************
* <copyright>
*
- * Copyright (c) 2005, 2010 SAP AG.
+ * Copyright (c) 2005, 2013 SAP AG.
* 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
@@ -9,6 +9,7 @@
*
* Contributors:
* SAP AG - initial API, implementation and documentation
+ * mwenz - Bug 415884 - Cannot query size of a multi-line text
*
* </copyright>
*
@@ -124,16 +125,35 @@
double getDistance(Point[] points);
/**
- * Calculates and returns the size of the text.
+ * Calculates and returns the size of the text ignoring any new line
+ * characters in the string.
*
* @param text
* @param font
*
* @return the size of the text
+ *
+ * @see #calculateTextSize(String, Font, boolean)
*/
IDimension calculateTextSize(String text, Font font);
/**
+ * Calculates and returns the size of the text.
+ *
+ * @param text
+ * @param font
+ * @param handleMultiline
+ * Defines if line breaks in the string should be used in the
+ * calculation of the size or not. In case <code>true</code>, a
+ * new line character in the string will increase the size of the
+ * returned dimensions by one line, in case <code>false</code> a
+ * new line character will be ignored.
+ *
+ * @return the size of the text
+ */
+ IDimension calculateTextSize(String text, Font font, boolean handleMultiline);
+
+ /**
* @param draw2dPoints
*/
void mirrorArray(Point[] draw2dPoints);
diff --git a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/impl/GefService.java b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/impl/GefService.java
index ff86606..1101a86 100644
--- a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/impl/GefService.java
+++ b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/impl/GefService.java
@@ -1,7 +1,7 @@
/*******************************************************************************
* <copyright>
*
- * Copyright (c) 2005, 2010 SAP AG.
+ * Copyright (c) 2005, 2013 SAP AG.
* 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
@@ -9,6 +9,7 @@
*
* Contributors:
* SAP AG - initial API, implementation and documentation
+ * mwenz - Bug 415884 - Cannot query size of a multi-line text
*
* </copyright>
*
@@ -501,6 +502,10 @@
}
public IDimension calculateTextSize(String text, Font font) {
+ return calculateTextSize(text, font, false);
+ }
+
+ public IDimension calculateTextSize(String text, Font font, boolean handleMultiline) {
IDimension dimension = null;
if (text == null || font == null || font.getName() == null) {
return dimension;
@@ -508,7 +513,13 @@
org.eclipse.swt.graphics.Font swtFont = DataTypeTransformation.toSwtFont(font);
if (swtFont != null) {
- Dimension se = TextUtilities.INSTANCE.getStringExtents(text, swtFont);
+ Dimension se;
+ if (handleMultiline) {
+ se = TextUtilities.INSTANCE.getTextExtents(text, swtFont);
+ } else {
+ se = TextUtilities.INSTANCE.getStringExtents(text, swtFont);
+ }
+
if (se != null) {
dimension = new DimensionImpl(se.width, se.height);
}
diff --git a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/impl/UiLayoutService.java b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/impl/UiLayoutService.java
index 7a50958..6d6c518 100644
--- a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/impl/UiLayoutService.java
+++ b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/internal/services/impl/UiLayoutService.java
@@ -1,7 +1,7 @@
/*******************************************************************************
* <copyright>
*
- * Copyright (c) 2005, 2010 SAP AG.
+ * Copyright (c) 2005, 2013 SAP AG.
* 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
@@ -9,6 +9,7 @@
*
* Contributors:
* SAP AG - initial API, implementation and documentation
+ * mwenz - Bug 415884 - Cannot query size of a multi-line text
*
* </copyright>
*
@@ -18,7 +19,9 @@
import org.eclipse.graphiti.datatypes.IDimension;
import org.eclipse.graphiti.datatypes.ILocation;
import org.eclipse.graphiti.datatypes.IRectangle;
+import org.eclipse.graphiti.mm.algorithms.AbstractText;
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm;
+import org.eclipse.graphiti.mm.algorithms.MultiText;
import org.eclipse.graphiti.mm.algorithms.styles.Font;
import org.eclipse.graphiti.mm.pictograms.Anchor;
import org.eclipse.graphiti.mm.pictograms.Connection;
@@ -99,7 +102,15 @@
}
+ public IDimension calculateTextSize(String text, Font font, boolean handleMultiline) {
+ return GraphitiUiInternal.getGefService().calculateTextSize(text, font, handleMultiline);
+ }
+
public IDimension calculateTextSize(String text, Font font) {
- return GraphitiUiInternal.getGefService().calculateTextSize(text, font);
+ return calculateTextSize(text, font, false);
+ }
+
+ public IDimension calculateTextSize(AbstractText text) {
+ return calculateTextSize(text.getValue(), text.getFont(), (text instanceof MultiText));
}
}
diff --git a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/services/IUiLayoutService.java b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/services/IUiLayoutService.java
index 3fe99c0..acf2a5e 100644
--- a/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/services/IUiLayoutService.java
+++ b/plugins/org.eclipse.graphiti.ui/src/org/eclipse/graphiti/ui/services/IUiLayoutService.java
@@ -1,7 +1,7 @@
/*******************************************************************************
* <copyright>
*
- * Copyright (c) 2005, 2010 SAP AG.
+ * Copyright (c) 2005, 2013 SAP AG.
* 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
@@ -9,6 +9,7 @@
*
* Contributors:
* SAP AG - initial API, implementation and documentation
+ * mwenz - Bug 415884 - Cannot query size of a multi-line text
*
* </copyright>
*
@@ -16,6 +17,9 @@
package org.eclipse.graphiti.ui.services;
import org.eclipse.graphiti.datatypes.IDimension;
+import org.eclipse.graphiti.mm.algorithms.AbstractText;
+import org.eclipse.graphiti.mm.algorithms.MultiText;
+import org.eclipse.graphiti.mm.algorithms.Text;
import org.eclipse.graphiti.mm.algorithms.styles.Font;
import org.eclipse.graphiti.services.ILayoutService;
@@ -29,14 +33,51 @@
public interface IUiLayoutService extends ILayoutService {
/**
- * Calculates the width and height of the given text in the given font.
+ * Calculates the width and height of the given text in the given font
+ * ignoring any new line characters in the string.
*
* @param text
* the string to calculate the rendering size for
* @param font
* the font which should be considered for the string
* @return
+ *
+ * @see #calculateSize(org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm,
+ * boolean)
*/
public IDimension calculateTextSize(String text, Font font);
+ /**
+ * Calculates the width and height of the given text in the given font.
+ *
+ * @param text
+ * the string to calculate the rendering size for
+ * @param font
+ * the font which should be considered for the string
+ * @param handleMultiline
+ * Defines if line breaks in the string should be used in the
+ * calculation of the size or not. In case <code>true</code>, a
+ * new line character in the string will increase the size of the
+ * returned dimensions by one line, in case <code>false</code> a
+ * new line character will be ignored.
+ * @return
+ *
+ * @since 0.11
+ */
+ IDimension calculateTextSize(String text, Font font, boolean handleMultiline);
+
+ /**
+ * Calculates the width and height of the given text in the font of the
+ * gievn text. In case the given text is a {@link MultiText} new line
+ * characters in the string will increase the height of the returned size,
+ * otherwise (text is a {@link Text}) any new line characters will be
+ * ignored.
+ *
+ * @param text
+ * the {@link AbstractText} to calculate the rendering size for
+ * @return
+ *
+ * @since 0.11
+ */
+ IDimension calculateTextSize(AbstractText text);
}
diff --git a/tests/org.eclipse.graphiti.ui.tests/src/org/eclipse/graphiti/ui/tests/AllTests.java b/tests/org.eclipse.graphiti.ui.tests/src/org/eclipse/graphiti/ui/tests/AllTests.java
index 94b498c..d25c299 100644
--- a/tests/org.eclipse.graphiti.ui.tests/src/org/eclipse/graphiti/ui/tests/AllTests.java
+++ b/tests/org.eclipse.graphiti.ui.tests/src/org/eclipse/graphiti/ui/tests/AllTests.java
@@ -1,7 +1,7 @@
/*******************************************************************************
* <copyright>
*
- * Copyright (c) 2005, 2011 SAP AG.
+ * Copyright (c) 2005, 2013 SAP AG.
* 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
@@ -10,6 +10,7 @@
* Contributors:
* SAP AG - initial API, implementation and documentation
* mwenz - Bug 324859 - Need Undo/Redo support for Non-EMF based domain objects
+ * mwenz - Bug 415884 - Cannot query size of a multi-line text
*
* </copyright>
*
@@ -20,10 +21,10 @@
import org.junit.runners.Suite;
@RunWith(Suite.class)
-@Suite.SuiteClasses({ PackageTest.class, CommandStackTest.class, MigrationServiceTest.class, CustomUndoableFeatureTest.class,
- RollbackTest.class })
+@Suite.SuiteClasses({ PackageTest.class, CommandStackTest.class, MigrationServiceTest.class,
+ CustomUndoableFeatureTest.class, RollbackTest.class, LayoutServiceTest.class })
public class AllTests {
-
+
// test comment3
}
diff --git a/tests/org.eclipse.graphiti.ui.tests/src/org/eclipse/graphiti/ui/tests/LayoutServiceTest.java b/tests/org.eclipse.graphiti.ui.tests/src/org/eclipse/graphiti/ui/tests/LayoutServiceTest.java
new file mode 100644
index 0000000..7385ab6
--- /dev/null
+++ b/tests/org.eclipse.graphiti.ui.tests/src/org/eclipse/graphiti/ui/tests/LayoutServiceTest.java
@@ -0,0 +1,105 @@
+/*******************************************************************************
+ * <copyright>
+ *
+ * Copyright (c) 2013, 2013 SAP AG.
+ * 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:
+ * mwenz - Bug 415884 - Cannot query size of a multi-line text
+ *
+ * </copyright>
+ *
+ *******************************************************************************/
+package org.eclipse.graphiti.ui.tests;
+
+import org.eclipse.graphiti.datatypes.IDimension;
+import org.eclipse.graphiti.mm.algorithms.AlgorithmsFactory;
+import org.eclipse.graphiti.mm.algorithms.MultiText;
+import org.eclipse.graphiti.mm.algorithms.Text;
+import org.eclipse.graphiti.mm.algorithms.styles.Font;
+import org.eclipse.graphiti.mm.algorithms.styles.StylesFactory;
+import org.eclipse.graphiti.mm.algorithms.styles.StylesPackage;
+import org.eclipse.graphiti.tests.reuse.GFAbstractTestCase;
+import org.eclipse.graphiti.ui.services.GraphitiUi;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class LayoutServiceTest extends GFAbstractTestCase {
+
+ private static Font font;
+
+ @BeforeClass
+ public static void before() {
+ GFAbstractTestCase.before();
+
+ font = StylesFactory.eINSTANCE.createFont();
+ font.eSet(StylesPackage.eINSTANCE.getFont_Name(), "dummy");
+ }
+
+ @Test
+ public void testPlainCalculation() throws Exception {
+ IDimension textSize1 = GraphitiUi.getUiLayoutService().calculateTextSize("Plain text", font);
+ IDimension textSize2 = GraphitiUi.getUiLayoutService().calculateTextSize("Plain text \n continued", font);
+
+ Assert.assertTrue(isEquivalentHeight(textSize1, textSize2));
+ Assert.assertTrue(textSize1.getWidth() < textSize2.getWidth());
+ }
+
+ @Test
+ public void testNewLineCalculation() throws Exception {
+ IDimension textSize1 = GraphitiUi.getUiLayoutService().calculateTextSize("Plain text", font, true);
+ IDimension textSize2 = GraphitiUi.getUiLayoutService().calculateTextSize("Plain text \n continued", font, true);
+
+ Assert.assertFalse(isEquivalentHeight(textSize1, textSize2));
+ }
+
+ @Test
+ public void testPlainTextCalculation() throws Exception {
+ Text text1 = AlgorithmsFactory.eINSTANCE.createText();
+ text1.setFont(font);
+ text1.setValue("Plain text");
+
+ Text text2 = AlgorithmsFactory.eINSTANCE.createText();
+ text2.setFont(font);
+ text2.setValue("Plain text \n continued");
+
+ IDimension textSize1 = GraphitiUi.getUiLayoutService().calculateTextSize(text1);
+ IDimension textSize2 = GraphitiUi.getUiLayoutService().calculateTextSize(text2);
+
+ Assert.assertTrue(isEquivalentHeight(textSize1, textSize2));
+ Assert.assertTrue(textSize1.getWidth() < textSize2.getWidth());
+ }
+
+ @Test
+ public void testMultiTextCalculation() throws Exception {
+ MultiText text1 = AlgorithmsFactory.eINSTANCE.createMultiText();
+ text1.setFont(font);
+ text1.setValue("Plain text");
+
+ MultiText text2 = AlgorithmsFactory.eINSTANCE.createMultiText();
+ text2.setFont(font);
+ text2.setValue("Plain text \n continued");
+
+ IDimension textSize1 = GraphitiUi.getUiLayoutService().calculateTextSize(text1);
+ IDimension textSize2 = GraphitiUi.getUiLayoutService().calculateTextSize(text2);
+
+ Assert.assertFalse(isEquivalentHeight(textSize1, textSize2));
+ }
+
+ private boolean isEquivalentHeight(IDimension dimension1, IDimension dimension2) {
+ // On Linux the heights of the dimensions differ by one (15 and 16),
+ // while on Windows they are the same
+ // --> accept the difference on Linux as valid
+
+ int height1 = dimension1.getHeight();
+ int height2 = dimension2.getHeight();
+
+ int difference = Math.abs(height1 - height2);
+
+ return difference <= 1;
+ }
+}