Added xstream handiling for Wikipedia upload/download
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / preferences / AlternateUserValidationDialog.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpeclipse.wiki.preferences;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
17
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;
34
35 public class AlternateUserValidationDialog extends Dialog {
36         String user;
37         String password = ""; //$NON-NLS-1$
38         List numXs = new ArrayList();
39         Label icon1;
40         Label icon2;
41         Label icon3;
42         Label icon4;
43         Text passwordText;
44         boolean inUpdate = false;
45         
46         Image[] images;
47         
48         public AlternateUserValidationDialog(Shell parentShell, String user) {
49                 super(parentShell);
50                 this.user = user;
51                 initializeImages();
52         }
53         
54         protected void configureShell(Shell newShell) {
55                 super.configureShell(newShell);
56                 newShell.setText(Messages.bind("AlternateUserValidationDialog.Enter_Password_2")); //$NON-NLS-1$
57         }
58         
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));
65
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());
71                 
72                 icon1 = createLabel(iconComposite);
73                 icon2 = createLabel(iconComposite);
74                 icon3 = createLabel(iconComposite);
75                 icon4 = createLabel(iconComposite);
76                 
77                 Composite middleComposite = new Composite(main, SWT.NONE);
78                 middleComposite.setLayout(new GridLayout());
79                 middleComposite.setLayoutData(new GridData());
80                 
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();
89                 data.widthHint = 250;
90                 passwordText.setLayoutData(data);
91                 
92                 passwordText.addVerifyListener(new VerifyListener() {
93                         public void verifyText(VerifyEvent e) {
94                                 if (inUpdate) return;
95                                 e.doit = false;
96                                 inUpdate = true;
97                                 switch ((int)e.character) {
98                                         case 8: {
99                                                 // backspace pressed
100                                                 if (password.length() > 0) {
101                                                         password = password.substring(0, password.length() - 1);
102                                                 }
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());
110                                                 break;
111                                         }
112                                         default: {
113                                                 String oldText = passwordText.getText();
114                                                 String x = getXs();
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;
120                                         }
121                                 }
122                                 inUpdate = false;
123                                 updateImages();
124                         }
125                 });
126                 /*passwordText.addTraverseListener(new TraverseListener() {
127                         public void keyTraversed(TraverseEvent e) {
128                                 switch (e.detail) {
129                                         case SWT.TRAVERSE_ARROW_NEXT:
130                                         case SWT.TRAVERSE_ARROW_PREVIOUS:
131                                                 e.detail = SWT.TRAVERSE_NONE;
132                                                 e.doit = false;
133                                                 break;
134                                 }
135                         }
136                 });*/
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();
143                 data.widthHint = 70;
144                 b.setLayoutData(data);
145                 b.addListener(SWT.Selection, new Listener() {
146                         public void handleEvent(Event event) {
147                                 okPressed();
148                         }
149                 });
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();
154                 data.widthHint = 70;
155                 b.setLayoutData(data);
156                 b.addListener(SWT.Selection, new Listener() {
157                         public void handleEvent(Event event) {
158                                 cancelPressed();
159                         }
160                 });
161         Dialog.applyDialogFont(parent);
162                 return main;
163         }
164
165         public boolean close() {
166                 boolean result = super.close();
167                 if (images != null) {
168                         for (int i = 0; i < images.length; i++) {
169                                 images[i].dispose();
170                                 images[i] = null;
171                         }
172                         images = null;
173                 }
174                 return result;
175         }
176         public String getPassword() {
177                 return password;
178         }
179         
180         Label createLabel(Composite parent) {
181                 Label result = new Label(parent, SWT.NULL);
182                 GridData data = new GridData();
183                 data.widthHint = 22;
184                 data.heightHint = 22;
185                 result.setLayoutData(data);
186                 result.setImage(getImage());
187                 return result;
188         }
189         Image 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];
194         }
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$
199                 }
200                 FontData fd = new FontData();
201                 fd.setStyle(SWT.BOLD);
202                 fd.setHeight(10);
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$
206                 }
207         }
208         void updateImages() {
209                 icon1.setImage(getImage());
210                 icon2.setImage(getImage());
211                 icon3.setImage(getImage());
212                 icon4.setImage(getImage());
213         }
214         public void setUsername(String user) {
215                 this.user = user;
216         }
217         String getXs() {
218                 double random = Math.random();
219                 random *= 2;
220                 random += 2;
221                 long num = Math.round(random);
222                 // Random number between 2 and 4
223                 switch ((int)num) {
224                         case 2:
225                                 return "XX"; //$NON-NLS-1$
226                         case 3:
227                                 return "XXX"; //$NON-NLS-1$
228                         case 4:
229                                 return "XXXX"; //$NON-NLS-1$
230                 }
231                 return "X"; //$NON-NLS-1$
232         }
233         protected void cancelPressed() {
234                 password = null;
235                 super.cancelPressed();
236         }
237 }