Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 792ef4055cb0e3a25194803533a40c7b7cb80d2f (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
/*******************************************************************************
 * Copyright (c) 2008, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.security.ui.storage.view;

import org.eclipse.equinox.internal.security.ui.Activator;
import org.eclipse.equinox.internal.security.ui.nls.SecUIMessages;
import org.eclipse.jface.dialogs.*;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.PlatformUI;

public class NewValueDialog extends TitleAreaDialog {

	private static final String HELP_ID = Activator.PLUGIN_ID + ".NewValueDialog"; //$NON-NLS-1$

	private static final ImageDescriptor dlgImageDescriptor = ImageDescriptor.createFromFile(NodesView.class, "/icons/storage/new_value_wiz.png"); //$NON-NLS-1$

	private final String[] existingKeys;

	protected Text keyText;
	protected Text valueText;
	protected Button encryptButton;
	protected Button okButton;
	protected String key;
	protected String value;
	protected boolean encrypt;

	private Image dlgTitleImage = null;

	public NewValueDialog(String[] existingKeys, Shell parentShell) {
		super(parentShell);
		this.existingKeys = existingKeys;
	}

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

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

	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setTitle(SecUIMessages.addValueTitle);
		setMessage(SecUIMessages.addValueMsg);
		dlgTitleImage = dlgImageDescriptor.createImage();
		setTitleImage(dlgTitleImage);
		return contents;
	}

	protected Control createDialogArea(Composite parent) {
		Composite compositeTop = (Composite) super.createDialogArea(parent);
		Composite composite = new Composite(compositeTop, SWT.NONE);

		new Label(composite, SWT.LEFT).setText(SecUIMessages.addValueKeyLabel);
		keyText = new Text(composite, SWT.LEFT | SWT.BORDER);
		keyText.addModifyListener(event -> okButton.setEnabled(validName()));

		new Label(composite, SWT.LEFT).setText(SecUIMessages.addValueValueLabel);
		valueText = new Text(composite, SWT.LEFT | SWT.BORDER);

		encryptButton = new Button(composite, SWT.CHECK);
		encryptButton.setText(SecUIMessages.addValueEncryptLabel);
		encryptButton.setSelection(true); // encrypt by default

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

		return composite;
	}

	protected boolean validName() {
		String tmp = keyText.getText();
		boolean valid;
		if ((tmp == null) || (tmp.length() == 0))
			valid = false;
		else {
			valid = true;
			for (String existingKey : existingKeys) {
				if (existingKey.equals(tmp)) {
					valid = false;
					break;
				}
			}
			valid = (tmp.indexOf('/') == -1);
		}
		if (valid)
			setMessage(SecUIMessages.addValueMsg, IMessageProvider.NONE);
		else
			setMessage(SecUIMessages.addValueInvalid, IMessageProvider.ERROR);
		return valid;
	}

	protected void okPressed() {
		key = keyText.getText();
		value = valueText.getText();
		encrypt = encryptButton.getSelection();
		super.okPressed();
	}

	public String getKey() {
		return key;
	}

	public String getValue() {
		return value;
	}

	public boolean encrypt() {
		return encrypt;
	}

}

Back to the top