1515
1616
1717import java .util .*;
18+ import java .util .function .*;
1819import java .util .stream .*;
1920
2021import org .eclipse .swt .*;
@@ -597,6 +598,29 @@ private void storeAndApplyOperationForAllHandles(Operation operation) {
597598 zoomToHandle .forEach ((zoom , handle ) -> operation .apply (handle , zoom ));
598599}
599600
601+ private static <T > T applyUsingTemporaryHandle (int zoom , List <Operation > operations , Function <Long , T > function ) {
602+ long temporaryHandle = newRegionHandle (operations , zoom );
603+ try {
604+ return function .apply (temporaryHandle );
605+ } finally {
606+ OS .DeleteObject (temporaryHandle );
607+ }
608+ }
609+
610+ private static long newEmptyRegionHandle () {
611+ long newHandle = OS .CreateRectRgn (0 , 0 , 0 , 0 );
612+ if (newHandle == 0 ) SWT .error (SWT .ERROR_NO_HANDLES );
613+ return newHandle ;
614+ }
615+
616+ private static long newRegionHandle (List <Operation > operations , int zoom ) {
617+ long newHandle = newEmptyRegionHandle ();
618+ for (Operation operation : operations ) {
619+ operation .apply (newHandle , zoom );
620+ }
621+ return newHandle ;
622+ }
623+
600624/**
601625 * <b>IMPORTANT:</b> This method is not part of the public
602626 * API for Image. It is marked public only so that it
@@ -615,9 +639,9 @@ private void storeAndApplyOperationForAllHandles(Operation operation) {
615639 * @noreference This method is not intended to be referenced by clients.
616640 */
617641public static long win32_getHandle (Region region , int zoom ) {
618- if (!region .zoomToHandle .containsKey (zoom )) {
619- long handle = OS . CreateRectRgn ( 0 , 0 , 0 , 0 );
620- for (Operation operation : region .operations ) {
642+ if (!region .zoomToHandle .containsKey (zoom )) {
643+ long handle = newEmptyRegionHandle ( );
644+ for (Operation operation : region .operations ) {
621645 operation .apply (handle , zoom );
622646 }
623647 region .zoomToHandle .put (zoom , handle );
@@ -642,7 +666,7 @@ private interface OperationStrategy {
642666 void apply (Operation operation , long handle , int zoom );
643667}
644668
645- private abstract class Operation {
669+ private abstract static class Operation {
646670 private OperationStrategy operationStrategy ;
647671
648672 Operation (OperationStrategy operationStrategy ) {
@@ -662,7 +686,7 @@ void apply(long handle, int zoom) {
662686 abstract void translate (long handle , int zoom );
663687}
664688
665- private class OperationWithRectangle extends Operation {
689+ private static class OperationWithRectangle extends Operation {
666690
667691 Rectangle data ;
668692
@@ -721,7 +745,7 @@ private Rectangle getScaledRectangle(int zoom) {
721745
722746}
723747
724- private class OperationWithArray extends Operation {
748+ private static class OperationWithArray extends Operation {
725749
726750 int [] data ;
727751
@@ -769,7 +793,7 @@ private int[] getScaledPoints(int zoom) {
769793 }
770794}
771795
772- private class OperationWithPoint extends Operation {
796+ private static class OperationWithPoint extends Operation {
773797
774798 Point data ;
775799
@@ -801,41 +825,38 @@ void translate(long handle, int zoom) {
801825
802826}
803827
804- private class OperationWithRegion extends Operation {
805-
806- Region data ;
828+ private static class OperationWithRegion extends Operation {
829+ private final List <Operation > operations ;
807830
808831 OperationWithRegion (OperationStrategy operationStrategy , Region data ) {
809832 super (operationStrategy );
810- this .data = data ;
833+ this .operations = data . operations ;
811834 }
812835
813836 @ Override
814837 void add (long handle , int zoom ) {
815- long scaledHandle = getHandleForScaledRegion (zoom );
816- OS .CombineRgn (handle , handle , scaledHandle , OS .RGN_OR );
838+ applyUsingTemporaryHandle (zoom , operations , regionHandle -> {
839+ return OS .CombineRgn (handle , handle , regionHandle , OS .RGN_OR );
840+ });
817841 }
818842
819843 @ Override
820844 void subtract (long handle , int zoom ) {
821- long scaledHandle = getHandleForScaledRegion (zoom );
822- OS .CombineRgn (handle , handle , scaledHandle , OS .RGN_DIFF );
845+ applyUsingTemporaryHandle (zoom , operations , regionHandle -> {
846+ return OS .CombineRgn (handle , handle , regionHandle , OS .RGN_DIFF );
847+ });
823848 }
824849
825850 @ Override
826851 void intersect (long handle , int zoom ) {
827- long scaledHandle = getHandleForScaledRegion (zoom );
828- OS .CombineRgn (handle , handle , scaledHandle , OS .RGN_AND );
852+ applyUsingTemporaryHandle (zoom , operations , regionHandle -> {
853+ return OS .CombineRgn (handle , handle , regionHandle , OS .RGN_AND );
854+ });
829855 }
830856
831857 @ Override
832858 void translate (long handle , int zoom ) {
833859 throw new UnsupportedOperationException ();
834860 }
835-
836- private long getHandleForScaledRegion (int zoom ) {
837- if (data .isDisposed () || data == Region .this ) SWT .error (SWT .ERROR_INVALID_ARGUMENT );
838- return win32_getHandle (data , zoom );
839- }
840861}
841862}
0 commit comments