Merge Fix: Default callback handler not working in RAP
[lgpl/argeo-commons.git] / security / plugins / org.argeo.security.ui / src / main / java / org / argeo / security / ui / SecurityUiPlugin.java
index b7c4ef45f59caa5a9af9ed744e8c6cde0755cda0..b6bab756b653a0d8beaa18fd0d4c3791a12a69c0 100644 (file)
@@ -44,33 +44,19 @@ public class SecurityUiPlugin extends AbstractUIPlugin {
 
        private static SecurityUiPlugin plugin;
 
+       public static InheritableThreadLocal<Display> display = new InheritableThreadLocal<Display>() {
+
+               @Override
+               protected Display initialValue() {
+                       return Display.getCurrent();
+               }
+       };
+
        public void start(BundleContext context) throws Exception {
                super.start(context);
                plugin = this;
 
-               final Display display = Display.getCurrent();
-               defaultCallbackHandler = new CallbackHandler() {
-                       public void handle(final Callback[] callbacks) throws IOException,
-                                       UnsupportedCallbackException {
-
-                               if (display != null) // RCP
-                                       display.syncExec(new Runnable() {
-                                               public void run() {
-                                                       DefaultLoginDialog dialog = new DefaultLoginDialog();
-                                                       try {
-                                                               dialog.handle(callbacks);
-                                                       } catch (IOException e) {
-                                                               throw new ArgeoException("Cannot open dialog",
-                                                                               e);
-                                                       }
-                                               }
-                                       });
-                               else {// RAP
-                                       DefaultLoginDialog dialog = new DefaultLoginDialog();
-                                       dialog.handle(callbacks);
-                               }
-                       }
-               };
+               defaultCallbackHandler = new DefaultCallbackHandler();
                defaultCallbackHandlerReg = context.registerService(
                                CallbackHandler.class.getName(), defaultCallbackHandler, null);
        }
@@ -93,4 +79,28 @@ public class SecurityUiPlugin extends AbstractUIPlugin {
        public static ImageDescriptor getImageDescriptor(String path) {
                return imageDescriptorFromPlugin(PLUGIN_ID, path);
        }
+
+       protected class DefaultCallbackHandler implements CallbackHandler {
+               public void handle(final Callback[] callbacks) throws IOException,
+                               UnsupportedCallbackException {
+
+                       // if (display != null) // RCP
+                       display.get().syncExec(new Runnable() {
+                               public void run() {
+                                       DefaultLoginDialog dialog = new DefaultLoginDialog(display
+                                                       .get().getActiveShell());
+                                       try {
+                                               dialog.handle(callbacks);
+                                       } catch (IOException e) {
+                                               throw new ArgeoException("Cannot open dialog", e);
+                                       }
+                               }
+                       });
+                       // else {// RAP
+                       // DefaultLoginDialog dialog = new DefaultLoginDialog();
+                       // dialog.handle(callbacks);
+                       // }
+               }
+
+       }
 }