Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: defa490adca5a67d88bbe678cd9fd2020991fda0 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*******************************************************************************
 * Copyright (c) 2007, 2014 Symbian Software Limited 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:
 * Bala Torati (Symbian) - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.ui.templateengine.pages;

import org.eclipse.cdt.core.templateengine.TemplateEngineUtil;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * Creates a JFace Dialog for the user to get name-value pair to perform
 * SharedDefaults settings. The class takes care of user input validation.
 */

public class TemplateInputDialog extends Dialog {

	/**
	 * Controls settings in the GUI
	 */
	private static final String NAME = Messages.getString("TemplateInputDialog.0");// To be externalised //$NON-NLS-1$
	private static final String VALUE = Messages.getString("TemplateInputDialog.1");// To be externalised //$NON-NLS-1$

	/**
	 * Shell display messages for ADD and EDIT functionality
	 */

	private static final String ADD_SHELL_MESSAGE = Messages.getString("TemplateInputDialog.2"); //$NON-NLS-1$
	private static final String EDIT_SHELL_MESSAGE = Messages.getString("TemplateInputDialog.3"); //$NON-NLS-1$

	/**
	 * Label Error Message
	 */
	private Label errMessageLabel;
	private String labelMessage = Messages.getString("TemplateInputDialog.4"); //$NON-NLS-1$

	/**
	 * Text fields properties
	 */
	private static final int TEXT_LIMIT = 100;

	/**
	 * Dialog creation instances for display
	 */
	private TemplatePreferencePage templatePreferencePage;
	private TemplateInputDialog sharedDialog;
	private Shell shell;
	private Display display;

	/**
	 * Dialog control instances
	 */
	private Label valueLabel;
	private Label nameLabel;
	private Text valueText;
	private Text nameText;
	private Button oKButton;

	/**
	 * Indentifies ADD/EDIT function
	 */
	private int option;

	/**
	 * Parent composite
	 */
	private Composite parent;

	/**
	 * JFace Dialog Constructor, constructs controls of the super class
	 *
	 * @param parentShell
	 */

	protected TemplateInputDialog(Shell parentShell) {
		super(parentShell);
		setShellStyle(getShellStyle() | SWT.RESIZE);
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		this.shell = shell;
		display = shell.getDisplay();
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		this.parent = parent;
		Composite composite = (Composite) super.createDialogArea(parent);
		GridLayout gridLayout = new GridLayout(2, false);
		composite.setLayout(gridLayout);
		createControls(composite);
		return composite;
	}

	/**
	 * Opens the Dialog to accept user input for shared values.
	 *
	 * @param myDialog
	 * @param dataOption
	 */

	public void open(TemplateInputDialog myDialog, int dataOption) {
		this.option = dataOption;
		sharedDialog = myDialog;
		sharedDialog.create();
		Button oK = getButton(IDialogConstants.OK_ID);
		oK.setEnabled(false);
		if (option == TemplatePreferencePage.OPTION_ADD) {

			shell.setText(ADD_SHELL_MESSAGE);
		} else if (option == TemplatePreferencePage.OPTION_EDIT) {

			shell.setText(EDIT_SHELL_MESSAGE);
		}

		sharedDialog.open();
	}

	/**
	 * Creates control under the parent composite
	 *
	 * @param composite
	 */

	private void createControls(Composite composite) {

		// Name Label
		nameLabel = new Label(composite, SWT.NONE);
		nameLabel.setText(NAME);

		// Name Text Field
		nameText = new Text(composite, SWT.BORDER | SWT.SINGLE);
		nameText.setTextLimit(TEXT_LIMIT);
		GridData textData = new GridData(GridData.FILL_HORIZONTAL);
		textData.horizontalSpan = GridData.BEGINNING;
		nameText.setLayoutData(textData);
		addTextListener(nameText);

		// Value Label
		valueLabel = new Label(composite, SWT.NONE);
		valueLabel.setText(VALUE);

		// Value Text Field
		valueText = new Text(composite, SWT.BORDER | SWT.SINGLE);
		valueText.setTextLimit(TEXT_LIMIT);
		GridData valueData = new GridData(GridData.FILL_HORIZONTAL);
		valueData.horizontalSpan = GridData.BEGINNING;
		valueData.verticalSpan = 5;
		valueText.setLayoutData(valueData);
		addTextListener(valueText);

		// Label for Error Message
		Color color = display.getSystemColor(SWT.COLOR_RED); // Get a red Color
		Composite labelComposite = new Composite(parent, SWT.NONE);
		labelComposite.setLayout(new GridLayout());
		labelComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		errMessageLabel = new Label(labelComposite, SWT.COLOR_DARK_RED);
		errMessageLabel.setForeground(color);
		errMessageLabel.setText(labelMessage);
		errMessageLabel.setVisible(false);

		if (option == TemplatePreferencePage.OPTION_EDIT) {
			nameLabel.setEnabled(false);
			nameText.setEnabled(false);

			String name = TemplatePreferencePage.getSelectedItemNameFromTable();
			if (name != null) {
				nameText.setText(name);
			}
		}
	}

	/**
	 * Adds Modify listeners to the Text fields
	 *
	 * @param aText
	 */
	public void addTextListener(final Text aText) {

		ModifyListener mListener = new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				String nameField = aText.getText();
				textChanged(nameField);
			}
		};

		aText.addModifyListener(mListener);
	}

	@Override
	protected void okPressed() {
		if (option == TemplatePreferencePage.OPTION_ADD) {
			String name = nameText.getText();
			String value = valueText.getText();

			if (name != TemplatePreferencePage.Blank && value != TemplatePreferencePage.Blank) {
				templatePreferencePage = new TemplatePreferencePage(name, value);
				templatePreferencePage.addNewDataIntoTable();

				if (TemplatePreferencePage.isDup) {
					nameText.setText(TemplatePreferencePage.Blank);
					nameText.setFocus();
					TemplatePreferencePage.isDup = false;
				} else if (!TemplatePreferencePage.isDup) {
					nameText.setFocus();
					sharedDialog.close();
				}
			}
		}

		if (option == TemplatePreferencePage.OPTION_EDIT) {
			String name = nameText.getText();
			String value = valueText.getText();

			if (!value.equals(TemplatePreferencePage.Blank)) {
				templatePreferencePage = new TemplatePreferencePage(name, value);
				templatePreferencePage.updateDataInTheTable();
			}
			sharedDialog.close();
		}
	}

	/**
	 * Pops up Message dialog if duplicate entry is found and returns
	 * confirmation.
	 *
	 * @return result
	 */

	public int popDuplicate() {

		final int[] result = new int[] { 0 };
		Display.getDefault().syncExec(new Runnable() {
			@Override
			public void run() {
				MessageBox mBox = new MessageBox(Display.getCurrent().getActiveShell(), SWT.ICON_INFORMATION);
				mBox.setText(TemplatePreferencePage.Message);
				mBox.setMessage(TemplatePreferencePage.DuplicateEntry);
				result[0] = mBox.open();
			}
		});
		return result[0];
	}

	@Override
	protected void cancelPressed() {

		sharedDialog.close();
	}

	/**
	 *
	 * Implements the modify listener for the text field Name and Value fields.
	 *
	 * @param textField
	 */

	private void textChanged(String textField) {

		errMessageLabel.setVisible(false);
		try {

			// Diable OK button if special characters are entererd.
			if (textField.matches(TemplatePreferencePage.Blank)) {

				oKButton = getButton(IDialogConstants.OK_ID);
				errMessageLabel.setText(labelMessage);
				errMessageLabel.setVisible(true);
				oKButton.setEnabled(false);

			}

			// Enable OK button if and only if data is entered.
			else if (!nameText.getText().equals(TemplatePreferencePage.Blank)
					&& !valueText.getText().equals(TemplatePreferencePage.Blank)) {

				oKButton = getButton(IDialogConstants.OK_ID);
				oKButton.setEnabled(true);
			}

		} catch (Exception exp) {
			TemplateEngineUtil.log(exp);
		}

	}
}

Back to the top