Skip to content

Commit 7990f08

Browse files
amartya4256HeikoKlare
authored andcommitted
Improve API for cloning/creating rectangles
This commit contributes to providing better interfaces and APIs for cloning/creating rectangles, including the proper consideration of subclasses like the monitor-aware implementation of Rectangle. Contributes to #62 and #128
1 parent e39c288 commit 7990f08

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 41 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,44 @@ 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
361+
* dimensions.
362+
* <p>
363+
* If the provided {@code Point} instance carries additional contextual
364+
* information, an extended {@code Rectangle} type may be returned to preserve
365+
* that context. Otherwise, a standard {@code Rectangle} is returned.
366+
* </p>
367+
*
368+
* @param topLeft the top-left corner of the rectangle
369+
* @param width the width of the rectangle
370+
* @param height the height of the rectangle
371+
* @return a new {@code Rectangle} instance appropriate for the given point and
372+
* dimensions
373+
* @since 3.131
374+
*/
375+
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());
378+
}
379+
return new Rectangle(topLeft.x, topLeft.y, width, height);
380+
}
381+
382+
/**
383+
* Creates and returns a copy of this {@code Rectangle}.
384+
* <p>
385+
* This method performs a shallow copy of the rectangle's fields: {@code x},
386+
* {@code y}, {@code width}, and {@code height}. It does not copy any
387+
* subclass-specific fields, so subclasses should override this method if
388+
* additional fields exist.
389+
* </p>
390+
*
391+
* @return a new {@code Rectangle} instance with the same position and size as
392+
* this one
393+
* @since 3.131
394+
*/
395+
@Override
396+
public Rectangle clone() {
397+
return new Rectangle(x, y, width, height);
398+
}
359399
}

0 commit comments

Comments
 (0)