]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/plugins/org.argeo.security.ui/src/main/java/org/argeo/security/ui/dialogs/DefaultLoginDialog.java
8c8554c6b77a0ef8ff2700acc4590f190617a4ba
[lgpl/argeo-commons.git] / security / plugins / org.argeo.security.ui / src / main / java / org / argeo / security / ui / dialogs / DefaultLoginDialog.java
1 package org.argeo.security.ui.dialogs;
2
3 import javax.security.auth.callback.Callback;
4 import javax.security.auth.callback.NameCallback;
5 import javax.security.auth.callback.PasswordCallback;
6 import javax.security.auth.callback.TextOutputCallback;
7
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.events.ModifyEvent;
10 import org.eclipse.swt.events.ModifyListener;
11 import org.eclipse.swt.graphics.Point;
12 import org.eclipse.swt.layout.GridData;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Control;
16 import org.eclipse.swt.widgets.Display;
17 import org.eclipse.swt.widgets.Label;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.swt.widgets.Text;
20
21 public class DefaultLoginDialog extends AbstractLoginDialog {
22
23 public DefaultLoginDialog() {
24 this(Display.getCurrent().getActiveShell());
25 }
26
27 protected DefaultLoginDialog(Shell parentShell) {
28 super(parentShell);
29 // setBlockOnOpen(false);
30 }
31
32 protected Point getInitialSize() {
33 return new Point(300, 180);
34 }
35
36 @Override
37 protected Control createContents(Composite parent) {
38 Control control = super.createContents(parent);
39 parent.pack();
40 return control;
41 }
42
43 protected Control createDialogArea(Composite parent) {
44 Composite dialogarea = (Composite) super.createDialogArea(parent);
45 Composite composite = new Composite(dialogarea, SWT.NONE);
46 composite.setLayout(new GridLayout(2, false));
47 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
48 createCallbackHandlers(composite);
49 // parent.pack();
50 return composite;
51 }
52
53 private void createCallbackHandlers(Composite composite) {
54 Callback[] callbacks = getCallbacks();
55 for (int i = 0; i < callbacks.length; i++) {
56 Callback callback = callbacks[i];
57 if (callback instanceof TextOutputCallback) {
58 createLabelTextoutputHandler(composite,
59 (TextOutputCallback) callback);
60 } else if (callback instanceof NameCallback) {
61 createNameHandler(composite, (NameCallback) callback);
62 } else if (callback instanceof PasswordCallback) {
63 createPasswordHandler(composite, (PasswordCallback) callback);
64 }
65 }
66 }
67
68 private void createPasswordHandler(Composite composite,
69 final PasswordCallback callback) {
70 Label label = new Label(composite, SWT.NONE);
71 label.setText(callback.getPrompt());
72 final Text passwordText = new Text(composite, SWT.SINGLE | SWT.LEAD
73 | SWT.PASSWORD | SWT.BORDER);
74 passwordText
75 .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
76 passwordText.addModifyListener(new ModifyListener() {
77
78 public void modifyText(ModifyEvent event) {
79 // FIXME use getTextChars() in Eclipse 3.7
80 callback.setPassword(passwordText.getText().toCharArray());
81 }
82 });
83 }
84
85 private void createNameHandler(Composite composite,
86 final NameCallback callback) {
87 Label label = new Label(composite, SWT.NONE);
88 label.setText(callback.getPrompt());
89 final Text text = new Text(composite, SWT.SINGLE | SWT.LEAD
90 | SWT.BORDER);
91 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
92 text.addModifyListener(new ModifyListener() {
93
94 public void modifyText(ModifyEvent event) {
95 callback.setName(text.getText());
96 }
97 });
98 }
99
100 private void createLabelTextoutputHandler(Composite composite,
101 final TextOutputCallback callback) {
102 Label label = new Label(composite, SWT.NONE);
103 label.setText(callback.getMessage());
104 GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
105 data.horizontalSpan = 2;
106 label.setLayoutData(data);
107 // TODO: find a way to pass this information
108 // int messageType = callback.getMessageType();
109 // int dialogMessageType = IMessageProvider.NONE;
110 // switch (messageType) {
111 // case TextOutputCallback.INFORMATION:
112 // dialogMessageType = IMessageProvider.INFORMATION;
113 // break;
114 // case TextOutputCallback.WARNING:
115 // dialogMessageType = IMessageProvider.WARNING;
116 // break;
117 // case TextOutputCallback.ERROR:
118 // dialogMessageType = IMessageProvider.ERROR;
119 // break;
120 // }
121 // setMessage(callback.getMessage(), dialogMessageType);
122 }
123
124 public void internalHandle() {
125 }
126
127 // hack to simulate modal
128 // see
129 // http://dev.eclipse.org/mhonarc/newsLists/news.eclipse.platform.jface/msg00181.html
130 // protected void setShellStyle(int newShellStyle) {
131 // // turn off APPLICATION_MODAL
132 // int newstyle = newShellStyle & ~SWT.APPLICATION_MODAL;
133 // // turn on MODELESS
134 // newstyle |= SWT.MODELESS;
135 // super.setShellStyle(newstyle);
136 // }
137 //
138 // public int open() {
139 //
140 // int retVal = super.open();
141 // // this will let the caller wait till OK, Cancel is
142 // // pressed, but will let the other GUI responsive
143 // pumpMessages();
144 // return retVal;
145 // }
146 //
147 // protected void pumpMessages() {
148 // Shell sh = getShell();
149 // Display disp = sh.getDisplay();
150 // while (!sh.isDisposed()) {
151 // if (!disp.readAndDispatch())
152 // disp.sleep();
153 // }
154 // disp.update();
155 // }
156
157 }