Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 14e0415872becbb084cda56c8d5b1211228e2960 (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
/*******************************************************************************
 * Copyright (c) 2008 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.security.ui.storage;

import org.eclipse.equinox.internal.security.ui.nls.SecUIMessages;
import org.eclipse.jface.dialogs.*;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.PlatformUI;

public class ChallengeResponseDialog extends TitleAreaDialog {

	private static final String HELP_ID = "org.eclipse.equinox.security.ui.challenge_response_dialog"; //$NON-NLS-1$

	protected Text[] questions;
	protected Text[] answers;

	protected String[] questionsText = null;
	protected String[] answersText = null;

	protected Button okButton;
	final protected int size;

	public ChallengeResponseDialog(int numberOfQuestions, Shell parentShell) {
		super(parentShell);
		this.size = numberOfQuestions;
		questions = new Text[this.size];
		answers = new Text[this.size];
	}

	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText(SecUIMessages.passwordRecoveryTitle);
		PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, HELP_ID);
	}

	protected void createButtonsForButtonBar(Composite parent) {
		okButton = createButton(parent, IDialogConstants.OK_ID, SecUIMessages.passwordButtonOK, true);
		okButton.setEnabled(false);
		createButton(parent, IDialogConstants.CANCEL_ID, SecUIMessages.passwordButtonCancel, false);
	}

	protected boolean isResizable() {
		return true;
	}

	protected Control createDialogArea(Composite parent) {
		Composite compositeTop = (Composite) super.createDialogArea(parent);
		setTitle(SecUIMessages.passwordRecoveryTitleMsg);

		Composite composite = new Composite(compositeTop, SWT.NONE);

		setMessage(SecUIMessages.passwordMsg, IMessageProvider.NONE);

		Label storyLabel = new Label(composite, SWT.WRAP);
		GridData labelData = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
		labelData.widthHint = 500;
		storyLabel.setLayoutData(labelData);
		storyLabel.setText(SecUIMessages.passwordRecoveryLabel);

		for (int i = 0; i < size; i++) {
			Group group = new Group(composite, SWT.NONE);
			group.setText(NLS.bind(SecUIMessages.passwordGroup, Integer.toString(i + 1)));
			group.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));
			group.setLayout(new GridLayout(2, false));

			new Label(group, SWT.LEFT).setText(SecUIMessages.passwordQuestion);
			questions[i] = new Text(group, SWT.LEFT | SWT.BORDER);
			questions[i].setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));
			questions[i].addModifyListener(new ModifyListener() {
				public void modifyText(ModifyEvent event) {
					validateOK();
				}
			});

			new Label(group, SWT.LEFT).setText(SecUIMessages.passwordAnswer);
			answers[i] = new Text(group, SWT.LEFT | SWT.BORDER);
			answers[i].setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));
			answers[i].addModifyListener(new ModifyListener() {
				public void modifyText(ModifyEvent event) {
					validateOK();
				}
			});
		}

		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		GridLayoutFactory.swtDefaults().generateLayout(composite);

		return composite;
	}

	protected void validateOK() {
		boolean valid = true;
		for (int i = 0; i < size; i++) {
			if (questions[i] == null)
				continue;
			String question = questions[i].getText();
			if (question == null || question.length() == 0) {
				valid = false;
				break;
			}
			if (answers[i] == null)
				continue;
			String answer = answers[i].getText();
			if (answer == null || answer.length() == 0) {
				valid = false;
				break;
			}
		}
		if (valid)
			setMessage(SecUIMessages.passwordMsg, IMessageProvider.NONE);
		else
			setMessage(SecUIMessages.passwordErrMsg, IMessageProvider.WARNING);
		okButton.setEnabled(valid);
	}

	protected void okPressed() {
		questionsText = new String[size];
		answersText = new String[size];

		for (int i = 0; i < size; i++) {
			questionsText[i] = questions[i].getText();
			answersText[i] = answers[i].getText();
		}
		super.okPressed();
	}

	public String[][] getResult() {
		if (questionsText == null || answersText == null)
			return null;
		return new String[][] {questionsText, answersText};
	}

}

Back to the top