]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/eclipse/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/dialogs/AbstractLoginDialog.java
Introduce Argeo user edition
[lgpl/argeo-commons.git] / security / eclipse / plugins / org.argeo.security.ui / src / main / java / org / argeo / security / ui / dialogs / AbstractLoginDialog.java
1 package org.argeo.security.ui.dialogs;
2
3 import java.io.IOException;
4
5 import javax.security.auth.callback.Callback;
6 import javax.security.auth.callback.CallbackHandler;
7
8 import org.eclipse.core.runtime.IProgressMonitor;
9 import org.eclipse.core.runtime.NullProgressMonitor;
10 import org.eclipse.jface.dialogs.IDialogConstants;
11 import org.eclipse.jface.dialogs.TitleAreaDialog;
12 import org.eclipse.jface.operation.IRunnableWithProgress;
13 import org.eclipse.jface.operation.ModalContext;
14 import org.eclipse.swt.events.SelectionEvent;
15 import org.eclipse.swt.events.SelectionListener;
16 import org.eclipse.swt.widgets.Button;
17 import org.eclipse.swt.widgets.Display;
18 import org.eclipse.swt.widgets.Shell;
19
20 public abstract class AbstractLoginDialog extends TitleAreaDialog implements
21 CallbackHandler {
22
23 boolean processCallbacks = false;
24 boolean isCancelled = false;
25 Callback[] callbackArray;
26
27 protected final Callback[] getCallbacks() {
28 return this.callbackArray;
29 }
30
31 public abstract void internalHandle();
32
33 public boolean isCancelled() {
34 return isCancelled;
35 }
36
37 protected AbstractLoginDialog(Shell parentShell) {
38 super(parentShell);
39 }
40
41 /*
42 * (non-Javadoc)
43 *
44 * @see
45 * javax.security.auth.callback.CallbackHandler#handle(javax.security.auth
46 * .callback.Callback[])
47 */
48 public void handle(final Callback[] callbacks) throws IOException {
49 this.callbackArray = callbacks;
50 final Display display = Display.getDefault();
51 display.syncExec(new Runnable() {
52
53 public void run() {
54 isCancelled = false;
55 setBlockOnOpen(false);
56 open();
57 final Button okButton = getButton(IDialogConstants.OK_ID);
58 okButton.setText("Login");
59 okButton.addSelectionListener(new SelectionListener() {
60
61 public void widgetSelected(final SelectionEvent event) {
62 processCallbacks = true;
63 }
64
65 public void widgetDefaultSelected(final SelectionEvent event) {
66 // nothing to do
67 }
68 });
69 final Button cancel = getButton(IDialogConstants.CANCEL_ID);
70 cancel.addSelectionListener(new SelectionListener() {
71
72 public void widgetSelected(final SelectionEvent event) {
73 isCancelled = true;
74 processCallbacks = true;
75 }
76
77 public void widgetDefaultSelected(final SelectionEvent event) {
78 // nothing to do
79 }
80 });
81 }
82 });
83 try {
84 ModalContext.setAllowReadAndDispatch(true); // Works for now.
85 ModalContext.run(new IRunnableWithProgress() {
86
87 public void run(final IProgressMonitor monitor) {
88 // Wait here until OK or cancel is pressed, then let it rip.
89 // The event
90 // listener
91 // is responsible for closing the dialog (in the
92 // loginSucceeded
93 // event).
94 while (!processCallbacks) {
95 try {
96 Thread.sleep(100);
97 } catch (final Exception e) {
98 // do nothing
99 }
100 }
101 processCallbacks = false;
102 // Call the adapter to handle the callbacks
103 if (!isCancelled())
104 internalHandle();
105 }
106 }, true, new NullProgressMonitor(), Display.getDefault());
107 } catch (final Exception e) {
108 final IOException ioe = new IOException();
109 ioe.initCause(e);
110 throw ioe;
111 }
112 }
113
114 protected void configureShell(Shell shell) {
115 super.configureShell(shell);
116 shell.setText("Login");
117 }
118 }