Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2eb0b4807ff2a8e1c4d6d27dbc94f91aeeff537d (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
314
315
316
317
318
319
320
321
/*******************************************************************************
 * Copyright (c) 2007, 2016 Symbian Software Limited 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:
 * Bala Torati (Symbian) - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.ui.templateengine.uitree.uiwidgets;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import org.eclipse.cdt.core.templateengine.TemplateEngineHelper;
import org.eclipse.cdt.ui.templateengine.Messages;
import org.eclipse.cdt.ui.templateengine.event.PatternEvent;
import org.eclipse.cdt.ui.templateengine.uitree.IPatternMatchingTable;
import org.eclipse.cdt.ui.templateengine.uitree.InputUIElement;
import org.eclipse.cdt.ui.templateengine.uitree.UIAttributes;
import org.eclipse.cdt.ui.templateengine.uitree.UIElement;


/**
 * This gives a Label and Text widget. The Text widget can be SINGLE type of
 * MULTI type. This depends on the input type in TemplateDescriptor. The data
 * entered by the user is verified against an expected pattern. If the user
 * entered data doesn't confirms to the expected pattern, a PatternEvent is
 * fired to UIComposite.
 *
 * The UI***Widget classes which needs to handle patterns, can inherit the same
 * from this class. The inheriting class need not cache UIComposite instance
 * but should set the same for UITextWidget(super).
 */
public class UITextWidget extends InputUIElement implements ModifyListener {

	/**
	 * Text widget.
	 */
	protected Text text;

	/**
	 * Label of this widget.
	 */
	protected Label label;
	private String patternValue;

	/**
	 * Composite to which this widget control is added. Classes extending this class, should make sure that they initialize this from the respective class createWidgets method.
	 */
	protected UIComposite uiComposite;

	protected String textValue;

	/**
	 * Constructor.
	 *
	 * @param uiAttribute
	 *            attribute associated with this widget.
	 */
	public UITextWidget(UIAttributes uiAttribute) {
		super(uiAttribute);
		this.textValue = ""; // $NON-NLS-1$
	}

	/**
	 * @return String, value contained in the Text Widget.
	 */
	@Override
	public Map<String, String> getValues() {
		Map<String, String> retMap = new HashMap<String, String>();
		retMap.put(uiAttributes.get(UIElement.ID), textValue);

		return retMap;
	}

	/**
	 * Set the Text widget with new value.
	 *
	 * @param valueMap
	 */
	@Override
	public void setValues(Map<String, String> valueMap) {
		String val = valueMap.get(uiAttributes.get(UIElement.ID));
		String key = null;
		String subString = null;
		if (val != null) {
			if (val.indexOf(TemplateEngineHelper.OPEN_MARKER) != -1) {
				key = TemplateEngineHelper.getFirstMarkerID(val);
				subString = val.substring(key.length() + 3, val.length());
				if (valueMap.get(key) != null)
					val = valueMap.get(key) + subString;
				else
					val = subString;
			}
			val = val.trim();
			textValue = val;
		}
	}

	/**
	 * create a Label and Text widget, add it to UIComposite. set Layout for the
	 * widgets to be added to UIComposite. set required parameters to the
	 * Widgets.
	 *
	 * @param uiComposite
	 */
	@Override
	public void createWidgets(UIComposite uiComposite) {

		GridData gd = new GridData();
		this.uiComposite = uiComposite;
		label = new Label(uiComposite, SWT.LEFT);

		label.setText(uiAttributes.get(InputUIElement.WIDGETLABEL));
		if ((uiAttributes.get(UIElement.TYPE)).equalsIgnoreCase(InputUIElement.MULTILINETYPE)) {
			gd = new GridData();
			gd.verticalAlignment = SWT.BEGINNING;
			gd.verticalIndent = 5;
			label.setLayoutData(gd);
		}

		if (uiAttributes.get(UIElement.DESCRIPTION) != null){
			String tipText = uiAttributes.get(UIElement.DESCRIPTION);
			tipText = tipText.replaceAll("\\\\r\\\\n", "\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
			label.setToolTipText(tipText);
		}
		text = getTextWidget(uiAttributes.get(UIElement.TYPE));
		text.addModifyListener(this);
		text.setData(".uid", uiAttributes.get(UIElement.ID)); //$NON-NLS-1$
		text.setText(textValue);
	}

	/**
	 * call the dispose method on the widgets. This is to ensure that the
	 * widgets are properly disposed.
	 */
	@Override
	public void disposeWidget() {
		label.dispose();
		text.dispose();
	}

	/**
	 * evaluate the text entered by the user against the pattern associated with
	 * this widget. checks the text entered. On violation of pattern associated
	 * for this widget, by the user entered text. A pattern event is fired to
	 * the container. If this widget has attribute 'checkproject' set to true,
	 * the value entered in this widget is treated as project name. The same is
	 * verified if there is a directory by the same name in workspace,
	 * PatternEvent is thrown to Container.
	 *
	 * @param pattern
	 */
	public void evaluatePattern(String labelText, String userInputText, String pattern) {
		String message = labelText + InputUIElement.CONTENTS;
		Pattern pattern2 = Pattern.compile(pattern);
		Matcher matcher = pattern2.matcher(userInputText);
		if (!matcher.matches()) {
			String[] failed = pattern2.split(userInputText);
			for (int i = 1; i < failed.length; i++)
				message = message + " " + failed[i]; //$NON-NLS-1$
			message += InputUIElement.ISINVALID;
			message += " Expected pattern is \"" + pattern + "\""; //$NON-NLS-1$ //$NON-NLS-2$
			if (uiComposite != null)
				uiComposite.firePatternEvent(new PatternEvent(this, message, false));

		} else {
			String checkproject = uiAttributes.get(InputUIElement.CHECKPROJECT);
			if ((checkproject != null) && (checkproject.equalsIgnoreCase(TemplateEngineHelper.BOOLTRUE))
					&& TemplateEngineHelper.checkDirectoryInWorkspace(userInputText)) {

				message = userInputText + Messages.getString("UITextWidget.0"); //$NON-NLS-1$
				uiComposite.firePatternEvent(new PatternEvent(this, message, false));
			} else {
				if (uiComposite != null)
					uiComposite.firePatternEvent(new PatternEvent(this, message, true));
			}
		}
	}

	/**
	 * Method from ModifyListener. Extracts the Text from the widget. calls
	 * evaluatePattern.
	 */
	@Override
	public void modifyText(ModifyEvent e) {
		String patternName = uiAttributes.get(InputUIElement.INPUTPATTERN);

		if (patternName == null) {
			patternValue = null;
		} else if (patternName.equals(IPatternMatchingTable.FREETEXT) ||
				 patternName.equals(IPatternMatchingTable.TEXT) ||
				 patternName.equals(IPatternMatchingTable.FILENAME)) {

			patternValue = getPatternValue(patternName);
		} else {
			patternValue = patternName;
		}

		// Get the source from event. This is done because this class can be
		// extended by
		// other classes, having Text widget. They can just make use of
		// modifyText,
		// evaluatePattern and isValid.
		textValue = text.getText();

		if ((patternValue == null) || (textValue == null))
			return;

		String mandatory = uiAttributes.get(InputUIElement.MANDATORY);
		if ((mandatory == null || !mandatory.equalsIgnoreCase("true")) && textValue.isEmpty()) { //$NON-NLS-1$
			return;
		}

		evaluatePattern(label.getText(), textValue, patternValue);
	}

	/**
	 * Returns the Pattern Value for the widget.
	 * @param patternName
	 * @return
	 */
	private String getPatternValue(String patternName) {

		if (patternName.equals(IPatternMatchingTable.TEXT)) {
			patternValue = IPatternMatchingTable.TEXTPATTERNVALUE;
		}

		if (patternName.equals(IPatternMatchingTable.FREETEXT)) {
			patternValue = IPatternMatchingTable.FREETEXTPATTERNVALUE;
		}

		if (patternName.equals(IPatternMatchingTable.FILENAME)) {
			patternValue = IPatternMatchingTable.FILEPATTERNVALUE;
		}
		return patternValue;

	}

	/**
	 * Based on the sate of this Widget return true or false. This return value
	 * will be used by the UIPage to update its(UIPage) state. Return value
	 * depends on the value contained in TextWidget. If value contained is null, ""
	 * and Mandatory value from attributes.
	 *
	 * @return boolean.
	 */
	@Override
	public boolean isValid() {
		boolean retVal = true;
		String mandatory = uiAttributes.get(InputUIElement.MANDATORY);

		if (((mandatory != null) && (mandatory.equalsIgnoreCase(TemplateEngineHelper.BOOLTRUE)))
				&& ((textValue == null) || (textValue.isEmpty()) ||
				(textValue.trim().length() < 1))) {

			retVal = false;
		}
		return retVal;
	}

	/**
	 * Based on Input Type Text widget is created. The Text widget created can
	 * be of Type SINGLE or MULTI.
	 *
	 * @param type
	 *            of Text widget required.
	 * @return Text.
	 */
	private Text getTextWidget(String type) {
		Text retTextWidget = null;

		Composite textConatiner = new Composite(uiComposite, SWT.NONE | SWT.NO_REDRAW_RESIZE);

		textConatiner.setLayout(new GridLayout());

		if (type.equalsIgnoreCase(InputUIElement.INPUTTYPE)) {
			GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
			gridData.widthHint = 70;
			textConatiner.setLayoutData(gridData);
			retTextWidget = new Text(textConatiner, SWT.SINGLE | SWT.BORDER);
			retTextWidget.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		}
		if (type.equalsIgnoreCase(InputUIElement.MULTILINETYPE)) {

			GridData multiTextData = new GridData(GridData.FILL_HORIZONTAL);
			multiTextData.widthHint = 70;
			String line = uiAttributes.get(InputUIElement.SIZE);
			int cnt = 1;
			if (line != null) {
				cnt = Integer.parseInt(line);
				if (cnt <= 0)
					cnt = 1;

			}
			multiTextData.heightHint = 30 + 12 * cnt;
			textConatiner.setLayoutData(multiTextData);

			retTextWidget = new Text(textConatiner, SWT.WRAP | SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
			GridData textData = new GridData(SWT.FILL, SWT.FILL, true, true);
			retTextWidget.setLayoutData(textData);

		}

		return retTextWidget;
	}

}

Back to the top