Bug 569946 - Replace deprecated API in MessageBox

Replace the deprecated method:
NSAlert.beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo:
with  NSAlert.beginSheetModalForWindow:completionHandler:

Follows similar pattern as File/Directory Dialog for the completion
handler.

Change-Id: I55d101400a547f9e10227825c9651c1839c5ce86
Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.swt/+/171339
Tested-by: Platform Bot <platform-bot@eclipse.org>
Reviewed-by: Lakshmi P Shanmugam <lshanmug@in.ibm.com>
diff --git a/bundles/org.eclipse.swt/Eclipse SWT PI/cocoa/org/eclipse/swt/internal/cocoa/OS.java b/bundles/org.eclipse.swt/Eclipse SWT PI/cocoa/org/eclipse/swt/internal/cocoa/OS.java
index fa1f248..9ac310b 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT PI/cocoa/org/eclipse/swt/internal/cocoa/OS.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT PI/cocoa/org/eclipse/swt/internal/cocoa/OS.java
@@ -155,7 +155,7 @@
 	 */
 	/** @method flags=no_gen*/
 	public static native void beginSheetModalForWindow(long id, long sel, long window, long handler);
-	public static void beginSheetModalForWindow(NSPanel id, NSWindow window, long handler) {
+	public static void beginSheetModalForWindow(NSObject id, NSWindow window, long handler) {
 		OS.beginSheetModalForWindow(id.id, OS.sel_beginSheetModalForWindow_completionHandler_, window != null ? window.id : 0, handler);
 	}
 
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java
index 79f4ae6..1c4cc42 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java
@@ -5787,11 +5787,6 @@
 	long [] jniRef = new long [1];
 	OS.object_getInstanceVariable(id, SWT_OBJECT, jniRef);
 	if (jniRef[0] == 0) return 0;
-	if (sel == OS.sel_panelDidEnd_returnCode_contextInfo_) {
-		MessageBox dialog = (MessageBox)OS.JNIGetObject(jniRef[0]);
-		if (dialog == null) return 0;
-		dialog.panelDidEnd_returnCode_contextInfo(id, sel, arg0, arg1, arg2);
-	}
 	if (sel == OS.sel_panel_userEnteredFilename_confirmed_) {
 		FileDialog dialog = (FileDialog)OS.JNIGetObject(jniRef[0]);
 		if (dialog == null) return 0;
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/MessageBox.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/MessageBox.java
index 733d7d3..0cba2bf 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/MessageBox.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/MessageBox.java
@@ -15,6 +15,7 @@
 
 
 import org.eclipse.swt.*;
+import org.eclipse.swt.internal.*;
 import org.eclipse.swt.internal.cocoa.*;
 
 /**
@@ -41,8 +42,9 @@
  * @noextend This class is not intended to be subclassed by clients.
  */
 public  class MessageBox extends Dialog {
+	Callback callback_completion_handler;
 	String message = "";
-	int returnCode;
+	int userResponse;
 
 /**
  * Constructs a new instance of this class given only its parent.
@@ -115,6 +117,12 @@
 	return style;
 }
 
+private int getBits () {
+	int mask = (SWT.YES | SWT.NO | SWT.OK | SWT.CANCEL | SWT.ABORT | SWT.RETRY | SWT.IGNORE);
+	int bits = style & mask;
+	return bits;
+}
+
 /**
  * Returns the dialog's message, or an empty string if it does not have one.
  * The message is a description of the purpose for which the dialog was opened.
@@ -166,8 +174,7 @@
 	}
 	alert.setAlertStyle(alertType);
 
-	int mask = (SWT.YES | SWT.NO | SWT.OK | SWT.CANCEL | SWT.ABORT | SWT.RETRY | SWT.IGNORE);
-	int bits = style & mask;
+	int bits = getBits();
 	NSString title;
 	switch (bits) {
 		case SWT.OK:
@@ -227,7 +234,7 @@
 	panel.setTitle(title);
 	NSString message = NSString.stringWith(this.message != null ? this.message : "");
 	alert.setMessageText(message);
-	int response = 0;
+
 	long jniRef = 0;
 	SWTPanelDelegate delegate = null;
 	Display display = parent != null ? parent.getDisplay() : Display.getCurrent();
@@ -236,26 +243,39 @@
 		jniRef = OS.NewGlobalRef(this);
 		if (jniRef == 0) error(SWT.ERROR_NO_HANDLES);
 		OS.object_setInstanceVariable(delegate.id, Display.SWT_OBJECT, jniRef);
-		alert.beginSheetModalForWindow(parent.view.window (), delegate, OS.sel_panelDidEnd_returnCode_contextInfo_, 0);
 		display.setModalDialog(this, panel);
+		callback_completion_handler = new Callback(this, "_completionHandler", 1);
+		long handler = callback_completion_handler.getAddress();
+		OS.beginSheetModalForWindow(alert, parent.window, handler);
+
 		if ((style & SWT.APPLICATION_MODAL) != 0) {
-			response = (int)alert.runModal();
+			alert.runModal();
 		} else {
-			this.returnCode = 0;
 			NSWindow window = alert.window();
 			while (window.isVisible()) {
 				if (!display.readAndDispatch()) display.sleep();
 			}
-			response = this.returnCode;
 		}
 	} else {
 		display.setModalDialog(this, panel);
-		response = (int)alert.runModal();
+		int response = (int)alert.runModal();
+		userResponse = handleResponse(bits, response);
 	}
 	display.setModalDialog(null);
 	if (delegate != null) delegate.release();
 	if (jniRef != 0) OS.DeleteGlobalRef(jniRef);
 	alert.release();
+	releaseHandler();
+	return userResponse;
+}
+
+long _completionHandler (long result) {
+	NSApplication.sharedApplication().stopModal();
+	userResponse = handleResponse(getBits(), (int)result);
+	return result;
+}
+
+int handleResponse (int bits, int response) {
 	switch (bits) {
 		case SWT.OK:
 			switch (response) {
@@ -329,10 +349,11 @@
 	return SWT.CANCEL;
 }
 
-void panelDidEnd_returnCode_contextInfo(long id, long sel, long alert, long returnCode, long contextInfo) {
-	this.returnCode = (int)returnCode;
-	NSApplication application = NSApplication.sharedApplication();
-	application.endSheet(new NSAlert(alert).window(), returnCode);
+void releaseHandler () {
+	if (callback_completion_handler != null) {
+		callback_completion_handler.dispose();
+		callback_completion_handler = null;
+	}
 }
 
 /**