KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > base > login > LoginDialog


1 /*
2  * Created on Oct 5, 2004
3  * by Alexander Bieber
4  *
5  */

6 package com.nightlabs.ipanema.base.login;
7
8 import org.apache.commons.beanutils.BeanUtils;
9 import org.apache.log4j.Logger;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.events.SelectionAdapter;
12 import org.eclipse.swt.events.SelectionEvent;
13 import org.eclipse.swt.layout.GridData;
14 import org.eclipse.swt.layout.GridLayout;
15 import org.eclipse.swt.layout.RowLayout;
16 import org.eclipse.swt.widgets.Button;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Shell;
19
20 import com.nightlabs.config.Config;
21 import com.nightlabs.config.ConfigException;
22 import com.nightlabs.ipanema.base.IpanemaBasePlugin;
23 import com.nightlabs.rcp.dialog.ExpandableAreaDialog;
24
25 /**
26  * @author Alexander Bieber
27  */

28 public class LoginDialog extends ExpandableAreaDialog {
29     public static final Logger LOGGER = Logger.getLogger(LoginDialog.class);
30     
31     private LoginConfigModule persistentLoginModule = null;
32     private LoginConfigModule runtimeLoginModule = null;
33     /**
34      * @param parent
35      */

36     public LoginDialog(Shell parent) {
37         super(
38             parent,
39             IpanemaBasePlugin.getResourceString("login.label.login"),
40             IpanemaBasePlugin.getResourceString("login.label.details")
41         );
42         try {
43             persistentLoginModule = ((LoginConfigModule)Config.sharedInstance().createConfigModule(LoginConfigModule.class));
44         } catch (ConfigException e) {
45             throw new RuntimeException JavaDoc(e);
46         }
47 // setMaxWidth(350);
48
}
49     
50     public LoginDialog(Shell parent, Login.AsyncLoginResult loginResult, LoginConfigModule loginModule) {
51         this(parent);
52         setLoginResult(loginResult);
53         setLoginModule(loginModule);
54     }
55
56     
57     protected Composite createStaticArea(Composite parent)
58     {
59         return new LoginDialogStaticArea(parent,SWT.NONE,runtimeLoginModule);
60     }
61
62     protected Composite createExpandableArea(Composite
63      parent)
64     {
65         return new LoginDialogExpandableArea(parent,SWT.NONE,runtimeLoginModule);
66     }
67     
68     
69     public LoginDialogStaticArea getLoginStaticArea(){
70         return (LoginDialogStaticArea)super.getStaticArea();
71     }
72     
73     public LoginDialogExpandableArea getLoginDetailArea(){
74         return (LoginDialogExpandableArea)super.getExpandableArea();
75     }
76     
77     private Login.AsyncLoginResult loginResult = null;
78     public void setLoginResult(Login.AsyncLoginResult loginResult) {
79         this.loginResult = loginResult;
80     }
81     public Login.AsyncLoginResult getLoginResult() {
82         return loginResult;
83     }
84     
85     private IpanemaLoginContext loginContext = null;
86     public void setLoginContext(IpanemaLoginContext loginContext){
87         this.loginContext = loginContext;
88     }
89     public IpanemaLoginContext getLoginContext() {
90         return loginContext;
91     }
92     
93     public void setLoginModule(LoginConfigModule loginConfigModule) {
94         this.runtimeLoginModule = loginConfigModule;
95     }
96     public int open(){
97 // this.getShell().addControlListener()
98
return super.open();
99     }
100         
101     public void setActualValues(){
102         loginContext.setCredentials(
103                 getLoginStaticArea().getTextUserID().getText(),
104                 getLoginDetailArea().getTextOrganisationID().getText(),
105                 getLoginStaticArea().getTextPassword().getText()
106         );
107         
108         runtimeLoginModule.setUserID(getLoginStaticArea().getTextUserID().getText());
109         runtimeLoginModule.setOrganisationID(getLoginDetailArea().getTextOrganisationID().getText());
110         runtimeLoginModule.setInitialContextFactory(getLoginDetailArea().getTextInitialContextFactory().getText());
111         runtimeLoginModule.setServerURL(getLoginDetailArea().getTextServerURL().getText());
112         runtimeLoginModule.setSecurityProtocol(getLoginDetailArea().getTextSecurityProtocol().getText());
113     }
114
115     
116     
117     
118     protected static final int BUTTON_LOGIN = 100;
119     protected static final int BUTTON_OFFLINE = 200;
120     protected static final int BUTTON_QUIT = 300;
121     
122     private Button loginButton;
123     private Button offlineButton;
124     private Button quitButton;
125     
126     
127     private void createLoginButton(Composite parent) {
128         Button loginButton = new Button(parent, SWT.PUSH);
129 // GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
130
// loginButton.setLayoutData(gridData);
131
loginButton.setText(IpanemaBasePlugin.getResourceString("login.label.button.login"));
132         loginButton.setData(new Integer JavaDoc(BUTTON_LOGIN));
133         loginButton.addSelectionListener(new SelectionAdapter() {
134             public void widgetSelected(SelectionEvent event) {
135                 loginPressed(event);
136             }
137         });
138         Shell shell = parent.getShell();
139         if (shell != null)
140             shell.setDefaultButton(loginButton);
141     }
142     
143     private void createOfflineButton(Composite parent) {
144         Button offlineButton = new Button(parent, SWT.PUSH);
145 // GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
146
// offlineButton.setLayoutData(gridData);
147
offlineButton.setText(IpanemaBasePlugin.getResourceString("login.label.button.offline"));
148         offlineButton.setData(new Integer JavaDoc(BUTTON_OFFLINE));
149         offlineButton.addSelectionListener(new SelectionAdapter() {
150             public void widgetSelected(SelectionEvent event) {
151                 offlinePressed(event);
152             }
153         });
154     }
155     
156     private void createQuitButton(Composite parent) {
157         Button quitButton = new Button(parent, SWT.PUSH);
158 // GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
159
// quitButton.setLayoutData(gridData);
160
quitButton.setText(IpanemaBasePlugin.getResourceString("login.label.button.quit"));
161         quitButton.setData(new Integer JavaDoc(BUTTON_QUIT));
162         quitButton.addSelectionListener(new SelectionAdapter() {
163             public void widgetSelected(SelectionEvent event) {
164                 quitPressed(event);
165             }
166         });
167     }
168     
169     
170
171 // protected Control createButtonBar(Composite parent) {
172
// Composite composite = new Composite(parent, SWT.NONE);
173
// // create a layout with spacing and margins appropriate for the font
174
// // size.
175
// RowLayout layout = new RowLayout();
176
// layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
177
// layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
178
// composite.setLayout(layout);
179
//
180
// // Add the buttons to the button bar.
181
// createButtonsForButtonBar(composite);
182
// return composite;
183
// }
184

185     
186     protected void createButtonsForButtonBar(Composite parent) {
187         GridLayout parentLayout = (GridLayout) parent.getLayout();
188         parentLayout.numColumns = 2;
189         parentLayout.verticalSpacing = 0;
190         parentLayout.marginHeight = 0;
191 // parentLayout.marginWidth = 0;
192
Composite left = new Composite(parent,SWT.NONE);
193         GridData leftData = new GridData(GridData.FILL_HORIZONTAL);
194         leftData.grabExcessHorizontalSpace = true;
195         left.setLayoutData(leftData);
196       Composite right = new Composite(parent,SWT.NONE);
197         GridData rightData = new GridData(GridData.FILL_HORIZONTAL);
198         right.setLayoutData(rightData);
199         RowLayout layout = new RowLayout();
200         right.setLayout(layout);
201         createLoginButton(right);
202         createOfflineButton(right);
203 // createQuitButton(right);
204
}
205     
206     protected void loginPressed(SelectionEvent event) {
207         // check entries
208
if (getLoginStaticArea().getTextUserID().getText().equals("")) {
209             getLoginStaticArea().setErrMessage(IpanemaBasePlugin.getResourceString("login.error.missingUserID"));
210             return;
211         }
212         
213         if (getLoginDetailArea().getTextOrganisationID().getText().equals("")) {
214             getLoginStaticArea().setErrMessage(IpanemaBasePlugin.getResourceString("login.error.missingOrganisationID"));
215             return;
216         }
217         if (getLoginDetailArea().getTextInitialContextFactory().getText().equals("")) {
218             getLoginStaticArea().setErrMessage(IpanemaBasePlugin.getResourceString("login.error.missingContextFactory"));
219             return;
220         }
221         if (getLoginDetailArea().getTextServerURL().getText().equals("")) {
222             getLoginStaticArea().setErrMessage(IpanemaBasePlugin.getResourceString("login.error.missingServerURL"));
223             return;
224         }
225         if (getLoginDetailArea().getTextSecurityProtocol().getText().equals("")) {
226             getLoginStaticArea().setErrMessage(IpanemaBasePlugin.getResourceString("login.error.missingSecurityProtocol"));
227             return;
228         }
229
230         // use entries and log in
231
setActualValues();
232         if (getLoginDetailArea().getCheckBoxSaveSettings().getSelection()) {
233             try {
234                 BeanUtils.copyProperties(persistentLoginModule,runtimeLoginModule);
235                 persistentLoginModule.setChanged();
236 // persistentLoginModule._getConfig().saveConfFile();
237
} catch (Exception JavaDoc e) {
238                 LOGGER.error("Saving config failed!", e);
239             }
240         }
241         
242         Login.AsyncLoginResult testResult = Login.testLogin(loginContext);
243         testResult.copyValuesTo(loginResult);
244         
245 // Login login = null;
246
// try {
247
// login = Login.getLogin(false);
248
// } catch (LoginException e) {
249
// LOGGER.error("Obtaining shared instance of Login failed!", e);
250
// }
251
//
252
// if ( login != null)
253
// login.copyPropertiesFrom(loginContext);
254
// else
255
// throw new IllegalStateException("Shared instance of Login must not be null");
256
//
257
// boolean wasAuthenticationErr = false;
258
// boolean wasCommunicationErr = false;
259
// boolean wasSocketTimeout = false;
260
// loginResult.setSuccess(true);
261
// loginResult.setMessage(null);
262
// loginResult.setException(null);
263
// login.flushInitialContextProperties();
264
//
265
// // verify login
266
// IpanemaCLBackend ipanemaCLBackend = null;
267
//// LanguageManager languageManager = null;
268
// if (ipanemaCLBackend == null) {
269
// try {
270
// ipanemaCLBackend = IpanemaCLBackendUtil.getHome(
271
// login.getInitialContextProperties()).create();
272
//// languageManager = LanguageManagerUtil.getHome(
273
//// login.getInitialContextProperties()).create();
274
// } catch (RemoteException remoteException) {
275
// Throwable cause = remoteException.getCause();
276
// if (cause != null && cause.getCause() instanceof EJBException) {
277
// EJBException ejbE = (EJBException)cause.getCause();
278
// if (ejbE != null) {
279
// if (ejbE.getCausedByException() instanceof SecurityException) {
280
// // SecurityException authentication failure
281
// wasAuthenticationErr = true;
282
// }
283
// }
284
// }
285
// else {
286
// if (ExceptionUtils.indexOfThrowable(cause, SecurityException.class) >= 0)
287
// wasAuthenticationErr = true;
288
// }
289
// } catch (LoginException x) {
290
// LOGGER.warn("Login failed with a very weird LoginException!", x);
291
// // something went very wrong as we are in the login procedure
292
// IllegalStateException ill = new IllegalStateException("Caught LoginException although getLogin(FALSE) was executed. "+x.getMessage());
293
// ill.initCause(x);
294
// throw ill;
295
// } catch (Exception x) {
296
// if (x instanceof CommunicationException) {
297
// wasCommunicationErr = true;
298
// }
299
// if (x instanceof SocketTimeoutException) {
300
// wasSocketTimeout = true;
301
// }
302
// // cant create local bean stub
303
// LOGGER.warn("Login failed!", x);
304
// LoginException loginE = new LoginException(x.getMessage());
305
// loginE.initCause(x);
306
// loginResult.setSuccess(false);
307
// loginResult.setMessage(IpanemaBasePlugin.getResourceString("login.error.unhadledExceptionMessage"));
308
// loginResult.setException(loginE);
309
// }
310
// }
311
// System.out.println(Locale.getDefault().getDisplayLanguage());
312
// try {
313
// languageManager.createLanguage(Locale.getDefault().getLanguage(),Locale.getDefault().getDisplayLanguage());
314
// } catch (Exception e) {
315
// // setting language failed, but login ok
316
// LOGGER.error("Login OK, but creating lanugage failed.");
317
// }
318
// verify login done
319

320         if ((!loginResult.isWasAuthenticationErr()) && (loginResult.isSuccess()))
321             close();
322         else {
323             // login failed
324
if (loginResult.isWasAuthenticationErr()) {
325                 getLoginStaticArea().setErrMessage(IpanemaBasePlugin.getResourceString("login.error.authenticationFailed"),3);
326             }
327             else if (loginResult.isWasCommunicationErr()) {
328                 getLoginStaticArea().setErrMessage(IpanemaBasePlugin.getResourceString("login.error.communicatinError")+" "+loginResult.getException().getMessage(),3);
329             }
330             else {
331                 int lineNo = 2;
332                 String JavaDoc message = loginResult.getMessage();
333                 if (loginResult.getException() != null) {
334                     message += "\n"+loginResult.getException().getClass().getName()+": "+loginResult.getException().getLocalizedMessage();
335                     Throwable JavaDoc cause = loginResult.getException().getCause();
336                     while ( cause != null ) {
337                         message += "\n"+cause.getClass().getName()+": "+cause.getLocalizedMessage();
338                         lineNo++;
339                         cause = cause.getCause();
340                     }
341                     loginResult.getException().printStackTrace();
342                 }
343                 getLoginStaticArea().setErrMessage(message,lineNo);
344                 
345             }
346             // show a message to the user
347
}
348     }
349     
350     protected void offlinePressed(SelectionEvent event) {
351         loginResult.setSuccess(false);
352         loginResult.setWorkOffline(true);
353         loginResult.setMessage(IpanemaBasePlugin.getResourceString("login.error.offlineDecision"));
354         close();
355     }
356
357     protected void quitPressed(SelectionEvent event) {
358         loginResult.setSuccess(false);
359         close();
360 // Workbench.getInstance().close();
361
System.exit(0);
362     }
363     
364     
365     
366     protected void configureShell(Shell shell) {
367         super.configureShell(shell);
368 // shell.setImage(new Image(null,"/home/alex/Java/eclipse-SDK-3.0R3/workspace/com.nightlabs.ipanema.base.app/icons/Login_loggedin.gif"));
369
}
370     
371     
372     protected static LoginDialog sharedInstance = null;
373     
374     public static void registerSharedInstance(LoginDialog dialog) {
375         sharedInstance = dialog;
376     }
377     
378     public static void deregisterSharedInstance() {
379         sharedInstance = null;
380     }
381
382     public static LoginDialog getSharedInstace() {
383         return sharedInstance;
384     }
385 }
386
Popular Tags