Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5217e946ee3658c4131ce38d2466dec6ef3b8d02 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/****************************************************************************
 * Copyright (c) 2007, 2008 Remy Suen and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Remy Suen <remy.suen@gmail.com> - initial API and implementation
 *****************************************************************************/
package org.eclipse.ecf.internal.provider.msn.ui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.ecf.ui.SharedImages;
import org.eclipse.ecf.ui.util.PasswordCacheHelper;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

final class MSNConnectWizardPage extends WizardPage {

	private Combo emailText;

	private Text passwordText;

	private String username;

	private static Pattern emailPattern = Pattern.compile(".+@.+.[a-z]+"); //$NON-NLS-1$

	MSNConnectWizardPage() {
		super(MSNConnectWizardPage.class.getName());
		setTitle(Messages.MSNConnectWizardPage_Title);
		setDescription(Messages.MSNConnectWizardPage_WIZARD_PAGE_DESCRIPTION);
		setPageComplete(false);
		setImageDescriptor(SharedImages
				.getImageDescriptor(SharedImages.IMG_CHAT_WIZARD));
	}

	MSNConnectWizardPage(String username) {
		this();
		this.username = username;
	}

	/**
	 * Verifies the user's input to the wizard. Optionally sets the password for
	 * the specified email if one has been stored and is recognized.
	 * 
	 * @param restorePassword
	 * 		<tt>true</tt> if the password field should be set if a password can be
	 * 		found
	 */
	private void verify(boolean restorePassword) {
		String email = emailText.getText().trim();
		if (email.equals("")) { //$NON-NLS-1$
			setErrorMessage(Messages.MSNConnectWizardPage_EmailAddressRequired);
		} else {
			Matcher matcher = emailPattern.matcher(email);
			if (!matcher.matches()) {
				setErrorMessage(Messages.MSNConnectWizardPage_EmailAddressInvalid);
			} else {
				if (restorePassword) {
					restorePassword(email);
				}
				if (passwordText.getText().equals("")) { //$NON-NLS-1$
					setErrorMessage(Messages.MSNConnectWizardPage_PasswordRequired);
				} else {
					setErrorMessage(null);
				}
			}
		}
	}

	private void restorePassword(String username) {
		PasswordCacheHelper pwStorage = new PasswordCacheHelper(username);
		String pw = pwStorage.retrievePassword();
		if (pw != null) {
			passwordText.setText(pw);
		}
	}

	private void addListeners() {
		emailText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				verify(true);
			}
		});
		emailText.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
				verify(true);
			}

			public void widgetSelected(SelectionEvent e) {
				verify(true);
			}
		});
		passwordText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				verify(false);
			}
		});
	}

	public void createControl(Composite parent) {

		parent = new Composite(parent, SWT.NONE);

		parent.setLayout(new GridLayout(2, false));

		GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);

		Label label = new Label(parent, SWT.LEFT);
		label.setText(Messages.MSNConnectWizardPage_EmailAddressLabel);
		emailText = new Combo(parent, SWT.SINGLE | SWT.BORDER | SWT.DROP_DOWN);
		emailText.setLayoutData(data);

		label = new Label(parent, SWT.LEFT);
		label.setText(Messages.MSNConnectWizardPage_PasswordLabel);
		passwordText = new Text(parent, SWT.SINGLE | SWT.PASSWORD | SWT.BORDER);
		passwordText.setLayoutData(data);

		addListeners();
		restoreCombo();

		if (username != null) {
			emailText.setText(username);
			restorePassword(username);
			passwordText.setFocus();
		}

		Dialog.applyDialogFont(parent);
		setControl(parent);
	}

	String getEmail() {
		return emailText.getText();
	}

	String getPassword() {
		return passwordText.getText();
	}

	public void setErrorMessage(String message) {
		super.setErrorMessage(message);
		setPageComplete(message == null);
	}

	private static final String PAGE_SETTINGS = MSNConnectWizardPage.class
			.getName();
	private static final int MAX_COMBO_VALUES = 40;
	private static final String COMBO_TEXT_KEY = "connectTextValue"; //$NON-NLS-1$
	private static final String COMBO_BOX_ITEMS_KEY = "comboValues"; //$NON-NLS-1$

	protected void saveComboText() {
		IDialogSettings pageSettings = getPageSettings();
		if (pageSettings != null)
			pageSettings.put(COMBO_TEXT_KEY, emailText.getText());
	}

	protected void saveComboItems() {
		IDialogSettings pageSettings = getPageSettings();
		if (pageSettings != null) {
			String connectTextValue = emailText.getText();
			List rawItems = Arrays.asList(emailText.getItems());
			// If existing text item is not in combo box then add it
			List items = new ArrayList();
			if (!rawItems.contains(connectTextValue))
				items.add(connectTextValue);
			items.addAll(rawItems);
			int itemsToSaveLength = items.size();
			if (itemsToSaveLength > MAX_COMBO_VALUES)
				itemsToSaveLength = MAX_COMBO_VALUES;
			String[] itemsToSave = new String[itemsToSaveLength];
			System.arraycopy(items.toArray(new String[] {}), 0, itemsToSave, 0,
					itemsToSaveLength);
			pageSettings.put(COMBO_BOX_ITEMS_KEY, itemsToSave);
		}
	}

	public IDialogSettings getDialogSettings() {
		return Activator.getDefault().getDialogSettings();
	}

	private IDialogSettings getPageSettings() {
		IDialogSettings pageSettings = null;
		IDialogSettings dialogSettings = this.getDialogSettings();
		if (dialogSettings != null) {
			pageSettings = dialogSettings.getSection(PAGE_SETTINGS);
			if (pageSettings == null)
				pageSettings = dialogSettings.addNewSection(PAGE_SETTINGS);
			return pageSettings;
		}
		return null;
	}

	protected void restoreCombo() {
		IDialogSettings pageSettings = getPageSettings();
		if (pageSettings != null) {
			String[] items = pageSettings.getArray(COMBO_BOX_ITEMS_KEY);
			if (items != null)
				emailText.setItems(items);
			String text = pageSettings.get(COMBO_TEXT_KEY);
			if (text != null)
				emailText.setText(text);
		}
	}

}

Back to the top