Skip to content

Commit 60dbf84

Browse files
committed
Better API for handling MonitorAwareRectangle
This commit contributes to providing better interfaces and APIs for handling MontiorAware Rectangles with Abstraction. contributes to #62 and #128
1 parent 925a294 commit 60dbf84

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @since 3.129
2525
* @noreference This class is not intended to be referenced by clients
2626
*/
27-
public final class MonitorAwareRectangle extends Rectangle {
27+
public final class MonitorAwareRectangle extends Rectangle implements Cloneable {
2828

2929
private static final long serialVersionUID = 5041911840525116925L;
3030

@@ -61,4 +61,9 @@ public int hashCode() {
6161
return super.hashCode();
6262
}
6363

64+
@Override
65+
public MonitorAwareRectangle clone() {
66+
return new MonitorAwareRectangle(x, y, width, height, monitor);
67+
}
68+
6469
}

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
4646
*/
4747

48-
public sealed class Rectangle implements Serializable permits MonitorAwareRectangle {
48+
public sealed class Rectangle implements Serializable, Cloneable permits MonitorAwareRectangle {
4949

5050
/**
5151
* the x coordinate of the rectangle
@@ -356,4 +356,39 @@ public Rectangle union (Rectangle rect) {
356356
return new Rectangle (left, top, right - left, bottom - top);
357357
}
358358

359+
/**
360+
* Creates a new {@code Rectangle} using the specified top-left point and dimensions.
361+
* <p>
362+
* If the provided {@code Point} instance carries additional contextual information,
363+
* an extended {@code Rectangle} type may be returned to preserve that context.
364+
* Otherwise, a standard {@code Rectangle} is returned.
365+
* </p>
366+
*
367+
* @param topLeft the top-left corner of the rectangle
368+
* @param width the width of the rectangle
369+
* @param height the height of the rectangle
370+
* @return a new {@code Rectangle} instance appropriate for the given point and dimensions
371+
* @since 3.131
372+
*/
373+
public static Rectangle of(Point topLeft, int width, int height) {
374+
if (topLeft instanceof MonitorAwarePoint monitorAwareTopLeft) {
375+
return new MonitorAwareRectangle(topLeft.x, topLeft.y, width, height, monitorAwareTopLeft.getMonitor());
376+
}
377+
return new Rectangle(topLeft.x, topLeft.y, width, height);
378+
}
379+
380+
/**
381+
* Creates and returns a copy of this {@code Rectangle}.
382+
* <p>
383+
* This method performs a shallow copy of the rectangle's fields: {@code x}, {@code y}, {@code width}, and {@code height}.
384+
* It does not copy any subclass-specific fields, so subclasses should override this method if additional fields exist.
385+
* </p>
386+
*
387+
* @return a new {@code Rectangle} instance with the same position and size as this one
388+
* @since 3.131
389+
*/
390+
@Override
391+
public Rectangle clone() {
392+
return new Rectangle(x, y, width, height);
393+
}
359394
}

0 commit comments

Comments
 (0)