1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpeclipse.wiki.preferences;
13 import java.util.ArrayList;
14 import java.util.List;
16 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.VerifyEvent;
21 import org.eclipse.swt.events.VerifyListener;
22 import org.eclipse.swt.graphics.FontData;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Event;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Listener;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.swt.widgets.Text;
35 public class AlternateUserValidationDialog extends Dialog {
37 String password = ""; //$NON-NLS-1$
38 List numXs = new ArrayList();
44 boolean inUpdate = false;
48 public AlternateUserValidationDialog(Shell parentShell, String user) {
54 protected void configureShell(Shell newShell) {
55 super.configureShell(newShell);
56 newShell.setText(Messages.bind("AlternateUserValidationDialog.Enter_Password_2")); //$NON-NLS-1$
59 protected Control createContents(Composite parent) {
60 Composite main = new Composite(parent, SWT.NONE);
61 GridLayout layout = new GridLayout();
62 layout.numColumns = 3;
63 main.setLayout(layout);
64 main.setLayoutData(new GridData(GridData.FILL_BOTH));
66 Composite iconComposite = new Composite(main, SWT.NONE);
67 layout = new GridLayout();
68 layout.numColumns = 2;
69 iconComposite.setLayout(layout);
70 iconComposite.setLayoutData(new GridData());
72 icon1 = createLabel(iconComposite);
73 icon2 = createLabel(iconComposite);
74 icon3 = createLabel(iconComposite);
75 icon4 = createLabel(iconComposite);
77 Composite middleComposite = new Composite(main, SWT.NONE);
78 middleComposite.setLayout(new GridLayout());
79 middleComposite.setLayoutData(new GridData());
81 Label l = new Label(middleComposite, SWT.NULL);
82 l.setText(Messages.bind("AlternateUserValidationDialog.message", user)); //$NON-NLS-1$
83 l.setLayoutData(new GridData());
84 l = new Label(middleComposite, SWT.NULL);
85 l.setText(""); //$NON-NLS-1$
86 l.setLayoutData(new GridData());
87 passwordText = new Text(middleComposite, SWT.SINGLE | SWT.BORDER);
88 GridData data = new GridData();
90 passwordText.setLayoutData(data);
92 passwordText.addVerifyListener(new VerifyListener() {
93 public void verifyText(VerifyEvent e) {
97 switch ((int)e.character) {
100 if (password.length() > 0) {
101 password = password.substring(0, password.length() - 1);
103 // get rid of bogus Xs
104 int numX = ((Integer)numXs.get(numXs.size() - 1)).intValue();
105 numXs.remove(numXs.size() - 1);
106 String oldText = passwordText.getText();
107 String newText = oldText.substring(0, oldText.length() - numX);
108 passwordText.setText(newText);
109 passwordText.setSelection(newText.length());
113 String oldText = passwordText.getText();
115 numXs.add(numXs.size(), new Integer(x.length()));
116 String newText = oldText + x;
117 passwordText.setText(newText);
118 passwordText.setSelection(newText.length());
119 password += e.character;
126 /*passwordText.addTraverseListener(new TraverseListener() {
127 public void keyTraversed(TraverseEvent e) {
129 case SWT.TRAVERSE_ARROW_NEXT:
130 case SWT.TRAVERSE_ARROW_PREVIOUS:
131 e.detail = SWT.TRAVERSE_NONE;
137 Composite buttonComposite = new Composite(main, SWT.NONE);
138 buttonComposite.setLayout(new GridLayout());
139 buttonComposite.setLayoutData(new GridData());
140 Button b = new Button(buttonComposite, SWT.PUSH);
141 b.setText(Messages.bind("AlternateUserValidationDialog.OK_6")); //$NON-NLS-1$
142 data = new GridData();
144 b.setLayoutData(data);
145 b.addListener(SWT.Selection, new Listener() {
146 public void handleEvent(Event event) {
150 buttonComposite.getShell().setDefaultButton(b);
151 b = new Button(buttonComposite, SWT.PUSH);
152 b.setText(Messages.bind("AlternateUserValidationDialog.Cancel_7")); //$NON-NLS-1$
153 data = new GridData();
155 b.setLayoutData(data);
156 b.addListener(SWT.Selection, new Listener() {
157 public void handleEvent(Event event) {
161 Dialog.applyDialogFont(parent);
165 public boolean close() {
166 boolean result = super.close();
167 if (images != null) {
168 for (int i = 0; i < images.length; i++) {
176 public String getPassword() {
180 Label createLabel(Composite parent) {
181 Label result = new Label(parent, SWT.NULL);
182 GridData data = new GridData();
184 data.heightHint = 22;
185 result.setLayoutData(data);
186 result.setImage(getImage());
190 double random = Math.random();
191 random *= 7; // Random number between 0.0 and 7.0
192 long num = Math.round(random);
193 return images[(int)num];
195 void initializeImages() {
196 images = new Image[8];
197 for (int i = 0; i < images.length; i++) {
198 images[i] = WikiEditorPlugin.getDefault().getImageDescriptor("glyphs/glyph" + (i+1) + ".gif").createImage(); //$NON-NLS-1$ //$NON-NLS-2$
200 FontData fd = new FontData();
201 fd.setStyle(SWT.BOLD);
203 // On Windows, set the font to Sans Serif for an authentic look
204 if (System.getProperty("os.name").indexOf("Windows") != -1) { //$NON-NLS-1$ //$NON-NLS-2$
205 fd.setName("Microsoft Sans Serif"); //$NON-NLS-1$
208 void updateImages() {
209 icon1.setImage(getImage());
210 icon2.setImage(getImage());
211 icon3.setImage(getImage());
212 icon4.setImage(getImage());
214 public void setUsername(String user) {
218 double random = Math.random();
221 long num = Math.round(random);
222 // Random number between 2 and 4
225 return "XX"; //$NON-NLS-1$
227 return "XXX"; //$NON-NLS-1$
229 return "XXXX"; //$NON-NLS-1$
231 return "X"; //$NON-NLS-1$
233 protected void cancelPressed() {
235 super.cancelPressed();