1 11 package org.eclipse.team.internal.ccvs.ui; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 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 user; 36 String password = ""; List numXs = new ArrayList (); 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 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 [] { user })); 82 l.setLayoutData(new GridData()); 83 l = new Label(middleComposite, SWT.NULL); 84 l.setText(""); 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 if (password.length() > 0) { 100 password = password.substring(0, password.length() - 1); 101 } 102 int numX = ((Integer )numXs.get(numXs.size() - 1)).intValue(); 104 numXs.remove(numXs.size() - 1); 105 String oldText = passwordText.getText(); 106 String newText = oldText.substring(0, oldText.length() - numX); 107 passwordText.setText(newText); 108 passwordText.setSelection(newText.length()); 109 break; 110 } 111 default: { 112 String oldText = passwordText.getText(); 113 String x = getXs(); 114 numXs.add(numXs.size(), new Integer (x.length())); 115 String 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 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 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; 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(); } 199 FontData fd = new FontData(); 200 fd.setStyle(SWT.BOLD); 201 fd.setHeight(10); 202 if (System.getProperty("os.name").indexOf("Windows") != -1) { fd.setName("Microsoft Sans Serif"); } 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 user) { 214 this.user = user; 215 } 216 String getXs() { 217 double random = Math.random(); 218 random *= 2; 219 random += 2; 220 long num = Math.round(random); 221 switch ((int)num) { 223 case 2: 224 return "XX"; case 3: 226 return "XXX"; case 4: 228 return "XXXX"; } 230 return "X"; } 232 protected void cancelPressed() { 233 password = null; 234 super.cancelPressed(); 235 } 236 } 237 | Popular Tags |