KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > AlternateUserValidationDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.jface.dialogs.Dialog;
17 import org.eclipse.osgi.util.NLS;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.VerifyEvent;
20 import org.eclipse.swt.events.VerifyListener;
21 import org.eclipse.swt.graphics.FontData;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Event;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Listener;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.swt.widgets.Text;
33
34 public class AlternateUserValidationDialog extends Dialog {
35     String JavaDoc user;
36     String JavaDoc password = ""; //$NON-NLS-1$
37
List JavaDoc numXs = new ArrayList JavaDoc();
38     Label icon1;
39     Label icon2;
40     Label icon3;
41     Label icon4;
42     Text passwordText;
43     boolean inUpdate = false;
44     
45     Image[] images;
46     
47     public AlternateUserValidationDialog(Shell parentShell, String JavaDoc user) {
48         super(parentShell);
49         this.user = user;
50         initializeImages();
51     }
52     
53     protected void configureShell(Shell newShell) {
54         super.configureShell(newShell);
55         newShell.setText(CVSUIMessages.AlternateUserValidationDialog_Enter_Password_2);
56     }
57     
58     protected Control createContents(Composite parent) {
59         Composite main = new Composite(parent, SWT.NONE);
60         GridLayout layout = new GridLayout();
61         layout.numColumns = 3;
62         main.setLayout(layout);
63         main.setLayoutData(new GridData(GridData.FILL_BOTH));
64
65         Composite iconComposite = new Composite(main, SWT.NONE);
66         layout = new GridLayout();
67         layout.numColumns = 2;
68         iconComposite.setLayout(layout);
69         iconComposite.setLayoutData(new GridData());
70         
71         icon1 = createLabel(iconComposite);
72         icon2 = createLabel(iconComposite);
73         icon3 = createLabel(iconComposite);
74         icon4 = createLabel(iconComposite);
75         
76         Composite middleComposite = new Composite(main, SWT.NONE);
77         middleComposite.setLayout(new GridLayout());
78         middleComposite.setLayoutData(new GridData());
79         
80         Label l = new Label(middleComposite, SWT.NULL);
81         l.setText(NLS.bind(CVSUIMessages.AlternateUserValidationDialog_message, new String JavaDoc[] { user }));
82         l.setLayoutData(new GridData());
83         l = new Label(middleComposite, SWT.NULL);
84         l.setText(""); //$NON-NLS-1$
85
l.setLayoutData(new GridData());
86         passwordText = new Text(middleComposite, SWT.SINGLE | SWT.BORDER);
87         GridData data = new GridData();
88         data.widthHint = 250;
89         passwordText.setLayoutData(data);
90         
91         passwordText.addVerifyListener(new VerifyListener() {
92             public void verifyText(VerifyEvent e) {
93                 if (inUpdate) return;
94                 e.doit = false;
95                 inUpdate = true;
96                 switch (e.character) {
97                     case 8: {
98                         // backspace pressed
99
if (password.length() > 0) {
100                             password = password.substring(0, password.length() - 1);
101                         }
102                         // get rid of bogus Xs
103
int numX = ((Integer JavaDoc)numXs.get(numXs.size() - 1)).intValue();
104                         numXs.remove(numXs.size() - 1);
105                         String JavaDoc oldText = passwordText.getText();
106                         String JavaDoc newText = oldText.substring(0, oldText.length() - numX);
107                         passwordText.setText(newText);
108                         passwordText.setSelection(newText.length());
109                         break;
110                     }
111                     default: {
112                         String JavaDoc oldText = passwordText.getText();
113                         String JavaDoc x = getXs();
114                         numXs.add(numXs.size(), new Integer JavaDoc(x.length()));
115                         String JavaDoc newText = oldText + x;
116                         passwordText.setText(newText);
117                         passwordText.setSelection(newText.length());
118                         password += e.character;
119                     }
120                 }
121                 inUpdate = false;
122                 updateImages();
123             }
124         });
125         /*passwordText.addTraverseListener(new TraverseListener() {
126             public void keyTraversed(TraverseEvent e) {
127                 switch (e.detail) {
128                     case SWT.TRAVERSE_ARROW_NEXT:
129                     case SWT.TRAVERSE_ARROW_PREVIOUS:
130                         e.detail = SWT.TRAVERSE_NONE;
131                         e.doit = false;
132                         break;
133                 }
134             }
135         });*/

136         Composite buttonComposite = new Composite(main, SWT.NONE);
137         buttonComposite.setLayout(new GridLayout());
138         buttonComposite.setLayoutData(new GridData());
139         Button b = new Button(buttonComposite, SWT.PUSH);
140         b.setText(CVSUIMessages.AlternateUserValidationDialog_OK_6);
141         data = new GridData();
142         data.widthHint = 70;
143         b.setLayoutData(data);
144         b.addListener(SWT.Selection, new Listener() {
145             public void handleEvent(Event event) {
146                 okPressed();
147             }
148         });
149         buttonComposite.getShell().setDefaultButton(b);
150         b = new Button(buttonComposite, SWT.PUSH);
151         b.setText(CVSUIMessages.AlternateUserValidationDialog_Cancel_7);
152         data = new GridData();
153         data.widthHint = 70;
154         b.setLayoutData(data);
155         b.addListener(SWT.Selection, new Listener() {
156             public void handleEvent(Event event) {
157                 cancelPressed();
158             }
159         });
160         Dialog.applyDialogFont(parent);
161         return main;
162     }
163
164     public boolean close() {
165         boolean result = super.close();
166         if (images != null) {
167             for (int i = 0; i < images.length; i++) {
168                 images[i].dispose();
169                 images[i] = null;
170             }
171             images = null;
172         }
173         return result;
174     }
175     public String JavaDoc getPassword() {
176         return password;
177     }
178     
179     Label createLabel(Composite parent) {
180         Label result = new Label(parent, SWT.NULL);
181         GridData data = new GridData();
182         data.widthHint = 22;
183         data.heightHint = 22;
184         result.setLayoutData(data);
185         result.setImage(getImage());
186         return result;
187     }
188     Image getImage() {
189         double random = Math.random();
190         random *= 7; // Random number between 0.0 and 7.0
191
long num = Math.round(random);
192         return images[(int)num];
193     }
194     void initializeImages() {
195         images = new Image[8];
196         for (int i = 0; i < images.length; i++) {
197             images[i] = CVSUIPlugin.getPlugin().getImageDescriptor("glyphs/glyph" + (i+1) + ".gif").createImage(); //$NON-NLS-1$ //$NON-NLS-2$
198
}
199         FontData fd = new FontData();
200         fd.setStyle(SWT.BOLD);
201         fd.setHeight(10);
202         // On Windows, set the font to Sans Serif for an authentic look
203
if (System.getProperty("os.name").indexOf("Windows") != -1) { //$NON-NLS-1$ //$NON-NLS-2$
204
fd.setName("Microsoft Sans Serif"); //$NON-NLS-1$
205
}
206     }
207     void updateImages() {
208         icon1.setImage(getImage());
209         icon2.setImage(getImage());
210         icon3.setImage(getImage());
211         icon4.setImage(getImage());
212     }
213     public void setUsername(String JavaDoc user) {
214         this.user = user;
215     }
216     String JavaDoc getXs() {
217         double random = Math.random();
218         random *= 2;
219         random += 2;
220         long num = Math.round(random);
221         // Random number between 2 and 4
222
switch ((int)num) {
223             case 2:
224                 return "XX"; //$NON-NLS-1$
225
case 3:
226                 return "XXX"; //$NON-NLS-1$
227
case 4:
228                 return "XXXX"; //$NON-NLS-1$
229
}
230         return "X"; //$NON-NLS-1$
231
}
232     protected void cancelPressed() {
233         password = null;
234         super.cancelPressed();
235     }
236 }
237
Popular Tags