Skip to content

Commit 5b9f3a0

Browse files
amartya4256fedejeanne
authored andcommitted
Internalize MonitorAware* Classes to Parent #62
This commit moves the classes MonitorAwareRectangle and MonitorAwarePoint to Rectangle and Point as a static class and renames them to WithMonitor to allow them to be used as Rectangle.WithMonitor and Point.WithMonitor. contributes to #62 and #128
1 parent 5389894 commit 5b9f3a0

File tree

6 files changed

+95
-144
lines changed

6 files changed

+95
-144
lines changed

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void translateRectangleInGapPartiallyInRightBackAndForthInSingleZoomShouldBeTheS
137137
void translateRectangleInGapPartiallyInRightBackAndForthInMultiZoomShouldBeInside() {
138138
MultiZoomCoordinateSystemMapper mapper = getMultiZoomCoordinateSystemMapper();
139139
setupMonitors(mapper);
140-
Rectangle rectInPts = new MonitorAwareRectangle(1950, 400, 150, 100, monitors[1]);
140+
Rectangle rectInPts = new Rectangle.WithMonitor(1950, 400, 150, 100, monitors[1]);
141141
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
142142
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
143143
}
@@ -223,15 +223,15 @@ private Point createExpectedPoint(CoordinateSystemMapper mapper, int x, int y, M
223223
if (mapper instanceof SingleZoomCoordinateSystemMapper) {
224224
return new Point(x, y);
225225
} else {
226-
return new MonitorAwarePoint(x, y, monitor);
226+
return new Point.WithMonitor(x, y, monitor);
227227
}
228228
}
229229

230230
private Rectangle createExpectedRectangle(CoordinateSystemMapper mapper, int x, int y, int width, int height, Monitor monitor) {
231231
if (mapper instanceof SingleZoomCoordinateSystemMapper) {
232232
return new Rectangle(x, y, width, height);
233233
} else {
234-
return new MonitorAwareRectangle(x, y, width, height, monitor);
234+
return new Rectangle.WithMonitor(x, y, width, height, monitor);
235235
}
236236
}
237237

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwarePoint.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwareRectangle.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import java.io.*;
1818

19+
import org.eclipse.swt.widgets.*;
20+
1921
/**
2022
* Instances of this class represent places on the (x, y)
2123
* coordinate plane.
@@ -41,7 +43,7 @@
4143
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
4244
*/
4345

44-
public sealed class Point implements Serializable permits MonitorAwarePoint {
46+
public sealed class Point implements Serializable permits Point.WithMonitor {
4547

4648
/**
4749
* the x coordinate of the point
@@ -116,5 +118,41 @@ public String toString () {
116118
return "Point {" + x + ", " + y + "}"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
117119
}
118120

121+
/**
122+
* Instances of this class represent {@link org.eclipse.swt.graphics.Point}
123+
* objects along with the context of the monitor in relation to which they are
124+
* placed on the display. The monitor awareness makes it easy to scale and
125+
* translate the points between pixels and points.
126+
*
127+
* @since 3.131
128+
* @noreference This class is not intended to be referenced by clients
129+
*/
130+
public static final class WithMonitor extends Point {
131+
132+
private static final long serialVersionUID = 6077427420686999194L;
133+
134+
private final Monitor monitor;
135+
136+
/**
137+
* Constructs a new Point.WithMonitor
138+
*
139+
* @param x the x coordinate of the point
140+
* @param y the y coordinate of the point
141+
* @param monitor the monitor with whose context the point is created
142+
*/
143+
public WithMonitor(int x, int y, Monitor monitor) {
144+
super(x, y);
145+
this.monitor = monitor;
146+
}
147+
148+
/**
149+
* {@return the monitor with whose context the instance is created}
150+
*/
151+
public Monitor getMonitor() {
152+
return monitor;
153+
}
154+
155+
}
156+
119157
}
120158

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.io.*;
1818

1919
import org.eclipse.swt.*;
20+
import org.eclipse.swt.widgets.*;
2021

2122
/**
2223
* Instances of this class represent rectangular areas in an
@@ -45,7 +46,7 @@
4546
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
4647
*/
4748

48-
public sealed class Rectangle implements Serializable, Cloneable permits MonitorAwareRectangle {
49+
public sealed class Rectangle implements Serializable, Cloneable permits Rectangle.WithMonitor {
4950

5051
/**
5152
* the x coordinate of the rectangle
@@ -373,8 +374,8 @@ public Rectangle union (Rectangle rect) {
373374
* @since 3.131
374375
*/
375376
public static Rectangle of(Point topLeft, int width, int height) {
376-
if (topLeft instanceof MonitorAwarePoint monitorAwareTopLeft) {
377-
return new MonitorAwareRectangle(topLeft.x, topLeft.y, width, height, monitorAwareTopLeft.getMonitor());
377+
if (topLeft instanceof Point.WithMonitor monitorAwareTopLeft) {
378+
return new Rectangle.WithMonitor(topLeft.x, topLeft.y, width, height, monitorAwareTopLeft.getMonitor());
378379
}
379380
return new Rectangle(topLeft.x, topLeft.y, width, height);
380381
}
@@ -396,4 +397,47 @@ public static Rectangle of(Point topLeft, int width, int height) {
396397
public Rectangle clone() {
397398
return new Rectangle(x, y, width, height);
398399
}
400+
401+
/**
402+
* Instances of this class represent {@link org.eclipse.swt.graphics.Rectangle}
403+
* objects along with the context of the monitor in relation to which they are
404+
* placed on the display. The monitor awareness makes it easy to scale and
405+
* translate the rectangles between pixels and points.
406+
*
407+
* @since 3.131
408+
* @noreference This class is not intended to be referenced by clients
409+
*/
410+
public static final class WithMonitor extends Rectangle {
411+
412+
private static final long serialVersionUID = 5041911840525116925L;
413+
414+
private final Monitor monitor;
415+
416+
/**
417+
* Constructs a new Rectangle.WithMonitor
418+
*
419+
* @param x the x coordinate of the top left corner of the rectangle
420+
* @param y the y coordinate of the top left corner of the rectangle
421+
* @param width the width of the rectangle
422+
* @param height the height of the rectangle
423+
* @param monitor the monitor with whose context the rectangle is created
424+
*/
425+
public WithMonitor(int x, int y, int width, int height, Monitor monitor) {
426+
super(x, y, width, height);
427+
this.monitor = monitor;
428+
}
429+
430+
/**
431+
* {@return the monitor with whose context the instance is created}
432+
*/
433+
public Monitor getMonitor() {
434+
return monitor;
435+
}
436+
437+
@Override
438+
public Rectangle.WithMonitor clone() {
439+
return new Rectangle.WithMonitor(x, y, width, height, monitor);
440+
}
441+
442+
}
399443
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,19 @@ public Point translateFromDisplayCoordinates(Point point, int zoom) {
9898

9999
@Override
100100
public Point translateToDisplayCoordinates(Point point, int zoom) {
101-
Monitor monitor = point instanceof MonitorAwarePoint monitorAwarePoint ? monitorAwarePoint.getMonitor() : null;
101+
Monitor monitor = point instanceof Point.WithMonitor pointWithMonitor ? pointWithMonitor.getMonitor() : null;
102102
return translateLocationInPointsToPixels(point.x, point.y, monitor);
103103
}
104104

105105
@Override
106106
public Rectangle translateFromDisplayCoordinates(Rectangle rect, int zoom) {
107-
Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect.getMonitor() : null;
107+
Monitor monitor = rect instanceof Rectangle.WithMonitor rectWithMonitor ? rectWithMonitor.getMonitor() : null;
108108
return translateRectangleInPixelsToPoints(rect.x, rect.y, rect.width, rect.height, monitor);
109109
}
110110

111111
@Override
112112
public Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom) {
113-
Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect.getMonitor() : null;
113+
Monitor monitor = rect instanceof Rectangle.WithMonitor rectWithMonitor ? rectWithMonitor.getMonitor() : null;
114114
return translateRectangleInPointsToPixels(rect.x, rect.y, rect.width, rect.height, monitor);
115115
}
116116

@@ -152,7 +152,7 @@ private Rectangle translateRectangleInPixelsToPoints(int x, int y, int widthInPi
152152
Point topLeft = getPointFromPixels(monitor, x, y);
153153
int width = DPIUtil.scaleDown(widthInPixels, zoom);
154154
int height = DPIUtil.scaleDown(heightInPixels, zoom);
155-
MonitorAwareRectangle rect = new MonitorAwareRectangle(topLeft.x, topLeft.y, width, height, monitor);
155+
Rectangle.WithMonitor rect = new Rectangle.WithMonitor(topLeft.x, topLeft.y, width, height, monitor);
156156
return rect;
157157
}
158158

@@ -264,7 +264,7 @@ private Point getPointFromPixels(Monitor monitor, int x, int y) {
264264
int zoom = getApplicableMonitorZoom(monitor);
265265
int mappedX = DPIUtil.scaleDown(x - monitor.clientX, zoom) + monitor.clientX;
266266
int mappedY = DPIUtil.scaleDown(y - monitor.clientY, zoom) + monitor.clientY;
267-
return new MonitorAwarePoint(mappedX, mappedY, monitor);
267+
return new Point.WithMonitor(mappedX, mappedY, monitor);
268268
}
269269

270270
private int getApplicableMonitorZoom(Monitor monitor) {
@@ -273,7 +273,7 @@ private int getApplicableMonitorZoom(Monitor monitor) {
273273

274274
@Override
275275
public Rectangle getContainingMonitorBoundsInPixels(Point point) {
276-
Monitor monitor = point instanceof MonitorAwarePoint monitorAwarePoint ? monitorAwarePoint.getMonitor()
276+
Monitor monitor = point instanceof Point.WithMonitor monitorAwarePoint ? monitorAwarePoint.getMonitor()
277277
: getContainingMonitorForPoints(point.x, point.y);
278278
return getMonitorClientAreaInPixels(monitor);
279279
}

0 commit comments

Comments
 (0)