fixed Bug 1024299
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / ui / dialog / PasswordDialog.java
1 /*
2  * Created on 28-ago-2003
3  *
4  */
5 package com.quantum.ui.dialog;
6
7 import com.quantum.Messages;
8 import com.quantum.model.PasswordFinder;
9
10 import org.eclipse.jface.dialogs.InputDialog;
11 import org.eclipse.swt.widgets.Composite;
12 import org.eclipse.swt.widgets.Control;
13 import org.eclipse.swt.widgets.Shell;
14 import org.eclipse.swt.widgets.Text;
15
16 /**
17  * @author panic
18  *
19  */
20 public class PasswordDialog extends InputDialog {
21
22     static class PasswordFinderImpl implements PasswordFinder {
23         private boolean passwordMeantToBeSaved = false;
24         private String password;
25         private boolean prompted = false;
26         private Shell shell;
27         
28         PasswordFinderImpl(Shell shell) {
29             this.shell = shell;
30         }
31
32         protected void prompt() {
33             PasswordDialog dialog =
34                 new PasswordDialog(
35                     this.shell,
36                     Messages.getString("MultiSQLServer.PasswordNotProvided"),
37                     Messages.getString("MultiSQLServer.Enter_Password"),
38                     "",
39                     this);
40             dialog.open();
41             this.prompted = true;
42         }
43
44         /**
45          * @return
46          */
47         public String getPassword() {
48             if (!this.prompted) {
49                 prompt();
50             }
51             return password;
52         }
53
54         /**
55          * @return
56          */
57         public boolean isPasswordMeantToBeSaved() {
58             if (!this.prompted) {
59                 prompt();
60             }
61             return passwordMeantToBeSaved;
62         }
63
64         /**
65          * @param string
66          */
67         public void setPassword(String string) {
68             password = string;
69         }
70
71         /**
72          * @param b
73          */
74         public void setPasswordMeantToBeSaved(boolean b) {
75             passwordMeantToBeSaved = b;
76         }
77
78     }
79
80     private PasswordFinderImpl passwordFinderImpl;
81
82     /**
83      * @param parentShell
84      * @param dialogTitle
85      * @param dialogMessage
86      * @param initialValue
87      * @param validator
88      */
89     protected PasswordDialog(
90         Shell parentShell,
91         String dialogTitle,
92         String dialogMessage,
93         String initialValue,
94         PasswordFinderImpl passwordFinderImpl) {
95         super(parentShell, dialogTitle, dialogMessage, initialValue, null);
96         this.passwordFinderImpl = passwordFinderImpl;
97     }
98
99     /* (non-Javadoc)
100      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
101      */
102     protected Control createDialogArea(Composite parent) {
103
104         Control control = super.createDialogArea(parent);
105         Text text = getText();
106         text.setEchoChar('*');
107         return control;
108     }
109
110     public static PasswordFinder createPasswordFinder(Shell parentShell) {
111         PasswordFinderImpl passwordFinder = new PasswordFinderImpl(parentShell);
112         return passwordFinder;
113     }
114     /**
115      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
116      */
117     protected void okPressed() {
118         this.passwordFinderImpl.setPassword(getText().getText());
119         super.okPressed();
120     }
121
122 }