Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 511dfe203b10d02f4a8a0d52b73d031a25962cc9 (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
/*******************************************************************************
 * Copyright (c) 2007 Benjamin Muskalla 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:
 *     Benjamin Muskalla - initial implementation
 *     IBM Canada - review initial contribution and commit
 *******************************************************************************/
package org.eclipse.debug.internal.ui.stringsubstitution;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Prompts the user to input a string.  The dialog will use a password text field so all
 * typed characters are hidden on the screen.
 */
public class PasswordPrompt extends PromptingResolver {

	private String returnValue;

	/* (non-Javadoc)
	 * @see org.eclipse.debug.internal.ui.stringsubstitution.PromptingResolver#prompt()
	 */
	@Override
	public void prompt() {
		Dialog dialog = new Dialog((Shell)null){
			private Text text;

			/* (non-Javadoc)
			 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
			 */
			@Override
			protected Control createDialogArea(Composite parent) {
				 // create composite
		        Composite composite = (Composite) super.createDialogArea(parent);
		        // create message
		        if (dialogMessage != null) {
		            Label label = new Label(composite, SWT.WRAP);
		            label.setText(dialogMessage);
		            GridData data = new GridData(GridData.GRAB_HORIZONTAL
		                    | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL
		                    | GridData.VERTICAL_ALIGN_CENTER);
		            data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
		            label.setLayoutData(data);
		            label.setFont(parent.getFont());
		        }
		        text = new Text(composite, SWT.SINGLE | SWT.BORDER | SWT.PASSWORD);
		        text.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
		                | GridData.HORIZONTAL_ALIGN_FILL));
		        String value = defaultValue == null ? lastValue : defaultValue;
		        if (value != null){
		        	text.setText(value);
		        }
		        applyDialogFont(composite);
		        return composite;
			}

			/* (non-Javadoc)
			 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
			 */
			@Override
			protected void buttonPressed(int buttonId) {
				if (buttonId == IDialogConstants.OK_ID) {
					returnValue = text.getText();
		        } else {
		        	returnValue = null;
		        }
		        super.buttonPressed(buttonId);
			}

			/* (non-Javadoc)
			 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
			 */
			@Override
			protected void configureShell(Shell newShell) {
		        super.configureShell(newShell);
		        newShell.setText(StringSubstitutionMessages.StringPromptExpander_0);
			}

		};

		int dialogResult = dialog.open();
		if (dialogResult == Window.OK) {
			dialogResultString = returnValue;
		}
	}

}

Back to the top