Skip to content

Commit c51d3c3

Browse files
HeikoKlareazoitl
authored andcommitted
Make IFigure#translate...() results backward compatible for Rectangle
The wrapping of Rectangles into their precision representations when calling IFigure#translate...() methods leads to results that are incompatible with pre-existing behavior when only a single scaled layer is involved. The reason is the different rounding applied in a basic Rectangle compared to a PrecisionRectangle. Recent attempts to adapt the PrecisionRectangle behavior failed because they broke backward compatibility. But without streamlining the rounding of Rectangle and PrecisionRectangle, the results of IFigure#translate...() become incompatible. For that reason, this change adapts the unwrapping of PrecisionRectangle to Rectangle in IFigure#translate...() to use the same rounding as applied in Rectangle to ensure backward compatibility of the method's behavior.
1 parent 04a3286 commit c51d3c3

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/PrecisionTests.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import org.eclipse.draw2d.IFigure;
1919
import org.eclipse.draw2d.ScalableLayeredPane;
2020
import org.eclipse.draw2d.geometry.PointList;
21+
import org.eclipse.draw2d.geometry.PrecisionDimension;
2122
import org.eclipse.draw2d.geometry.PrecisionPointList;
22-
import org.eclipse.draw2d.geometry.PrecisionRectangle;
2323
import org.eclipse.draw2d.geometry.Rectangle;
2424

2525
import org.junit.jupiter.api.BeforeEach;
@@ -65,11 +65,13 @@ private static ScalableLayeredPane createLayer(double scale) {
6565

6666
@Test
6767
public void testPreciseTranslateToAbsolute() {
68+
PrecisionDimension scaleDimension = new PrecisionDimension(0, 1);
69+
fig.translateToAbsolute(scaleDimension);
70+
double fullScale = scaleDimension.preciseHeight();
6871
Rectangle r1 = new Rectangle(13, 37, 163, 377);
69-
Rectangle r2 = new PrecisionRectangle(13, 37, 163, 377);
7072
fig.translateToAbsolute(r1);
71-
fig.translateToAbsolute(r2);
72-
assertEquals(493, 1404, 6186, 14307, r1);
73+
Rectangle r2 = new Rectangle(13, 37, 163, 377).scale(fullScale);
74+
assertEquals(493, 1404, 6187, 14309, r1);
7375
assertEquals(r1.x, r2.x);
7476
assertEquals(r1.y, r2.y);
7577
assertEquals(r1.width, r2.width);
@@ -78,11 +80,13 @@ public void testPreciseTranslateToAbsolute() {
7880

7981
@Test
8082
public void testPreciseTranslateToRelative() {
83+
PrecisionDimension scaleDimension = new PrecisionDimension(0, 1);
84+
fig.translateToAbsolute(scaleDimension);
85+
double fullScale = scaleDimension.preciseHeight();
8186
Rectangle r1 = new Rectangle(753, 891, 353, 564);
82-
Rectangle r2 = new PrecisionRectangle(753, 891, 353, 564);
8387
fig.translateToRelative(r1);
84-
fig.translateToRelative(r2);
85-
assertEquals(19, 23, 9, 14, r1);
88+
Rectangle r2 = new Rectangle(753, 891, 353, 564).scale(1 / fullScale);
89+
assertEquals(19, 23, 11, 16, r1);
8690
assertEquals(r1.x, r2.x);
8791
assertEquals(r1.y, r2.y);
8892
assertEquals(r1.width, r2.width);
@@ -108,4 +112,26 @@ public void testPreciseTranslateToRelative_PointList() {
108112
assertArrayEquals(p1.toIntArray(), new int[] { 13, 16, 18, 8 });
109113
assertArrayEquals(p1.toIntArray(), p2.toIntArray());
110114
}
115+
116+
// Ensure that results are compatible with pre-existing behavior for translating
117+
// a rectangle when only a single scaled layer is present
118+
@SuppressWarnings("static-method")
119+
@Test
120+
public void testPreciseTranslateToAbsolute_singleLayerCompatibility() {
121+
Figure figure = new Figure();
122+
ScalableLayeredPane layer = createLayer(1.4);
123+
IFigure root = new Figure();
124+
root.add(layer);
125+
layer.add(figure);
126+
Rectangle customRectangle = new Rectangle(13, 37, 13, 377) {
127+
};
128+
Rectangle plainRectangle = new Rectangle(customRectangle);
129+
130+
figure.translateToAbsolute(customRectangle);
131+
figure.translateToAbsolute(plainRectangle);
132+
assertEquals(customRectangle.x, plainRectangle.x);
133+
assertEquals(customRectangle.y, plainRectangle.y);
134+
assertEquals(customRectangle.width, plainRectangle.width);
135+
assertEquals(customRectangle.height, plainRectangle.height);
136+
}
111137
}

org.eclipse.draw2d/src/org/eclipse/draw2d/Figure.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2128,7 +2128,9 @@ private void fromPreciseShape(Translatable source, Translatable target) {
21282128
} else if (source instanceof PrecisionDimension d1 && target instanceof Dimension d2) {
21292129
d2.setSize(d1.width, d1.height);
21302130
} else if (source instanceof PrecisionRectangle r1 && target instanceof Rectangle r2) {
2131-
r2.setBounds(r1.x, r1.y, r1.width, r1.height);
2131+
r2.setBounds(r1.x, r1.y,
2132+
(int) (Math.ceil(r1.preciseX() + r1.preciseWidth()) - Math.floor(r1.preciseX())),
2133+
(int) (Math.ceil(r1.preciseY() + r1.preciseHeight()) - Math.floor(r1.preciseY())));
21322134
} else if (source instanceof PrecisionPointList p1 && target instanceof PointList p2) {
21332135
System.arraycopy(p1.toIntArray(), 0, p2.toIntArray(), 0, p2.size() * 2);
21342136
}

0 commit comments

Comments
 (0)