Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4bb3a6a509532b933473338de75697d8a0fcdd23 (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*******************************************************************************
 * Copyright (c) 2010 protos software gmbh (http://www.protos.de).
 * 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:
 * 		Thomas Schuetz and Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.ui.common.dialogs;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;

import org.eclipse.core.databinding.AggregateValidationStatus;
import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.ValidationStatusProvider;
import org.eclipse.core.databinding.conversion.Converter;
import org.eclipse.core.databinding.conversion.IConverter;
import org.eclipse.core.databinding.observable.IObservableCollection;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.core.databinding.observable.value.ComputedValue;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.common.util.Enumerator;
import org.eclipse.emf.databinding.EMFProperties;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
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;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.ui.forms.FormDialog;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.xtext.resource.IEObjectDescription;


public abstract class AbstractPropertyDialog extends FormDialog {
	
	static class DescriptionBased_Reference2StringConverter extends Converter {

		private EAttribute nameAttr;

		DescriptionBased_Reference2StringConverter(Object type, EAttribute nameAttr) {
			super(type, String.class);
			this.nameAttr = nameAttr;
		}
		
		@Override
		public Object convert(Object fromObject) {
			if (fromObject instanceof IEObjectDescription)
				return ((IEObjectDescription)fromObject).getName();
			else if (fromObject instanceof EObject) {
				return ((EObject)fromObject).eGet(nameAttr);
			}
			
			return null;
		}
		
	}
	
	static class DescriptionBased_String2ReferenceConverter extends Converter {
		private EObject obj;
		private List<IEObjectDescription> candidates;

		DescriptionBased_String2ReferenceConverter(Object type, EObject obj, List<IEObjectDescription> candidates) {
			super(String.class, type);
			this.obj = obj;
			this.candidates = candidates;
		}

		@Override
		public Object convert(Object fromObject) {
			for (IEObjectDescription desc : candidates) {
				if (desc.getName().toString().equals(fromObject)) {
					EObject refObj = desc.getEObjectOrProxy();
					if (refObj.eIsProxy())
						refObj = EcoreUtil.resolve(refObj, obj);
					return refObj;
				}
			}
			return null;
		}
	}

	static class Reference2StringConverter extends Converter {

		private EAttribute nameAttr;

		Reference2StringConverter(Object type, EAttribute nameAttr) {
			super(type, String.class);
			this.nameAttr = nameAttr;
		}
		
		@Override
		public Object convert(Object fromObject) {
			return ((EObject)fromObject).eGet(nameAttr);
		}
		
	}
	
	static class String2ReferenceConverter extends Converter {
		private List<? extends EObject> candidates;
		private EAttribute nameAttr;

		String2ReferenceConverter(Object type, List<? extends EObject> candidates, EAttribute nameAttr) {
			super(String.class, type);
			this.candidates = candidates;
			this.nameAttr = nameAttr;
		}

		@Override
		public Object convert(Object fromObject) {
			for (EObject obj : candidates) {
				if (obj.eGet(nameAttr).equals(fromObject))
					return obj;
			}
			return null;
		}
	}

	static class Enum2StringConverter extends Converter {

		Enum2StringConverter() {
			super(Enumerator.class, String.class);
		}
		
		@Override
		public Object convert(Object fromObject) {
			return ((Enumerator)fromObject).getLiteral();
		}
	}

	static class String2EnumConverter extends Converter {

		private List<? extends Enumerator> choices;

		String2EnumConverter(Object type, List<? extends Enumerator> choices) {
			super(String.class, type);
			this.choices = choices;
		}
		
		@Override
		public Object convert(Object fromObject) {
			for (Enumerator choice : choices) {
				if (choice.getLiteral().equals(fromObject))
					return choice;
			}
			return null;
		}
	}
	
	private static String DATA_KEY_STATUS = "etrice.status";
	
	private String title;
	private FormToolkit toolkit;
	private DataBindingContext bindingContext;
	
	// decoration -> validation status
	private HashMap<ControlDecoration, IObservableValue> decoratorMap = new HashMap<ControlDecoration, IObservableValue>();
	
	// top validation message
	private IObservableValue displayedValidationStatus;
	private Label validationLabel;
	private Label validationText;

	public AbstractPropertyDialog(Shell shell, String title) {
		super(shell);
		this.title = title;
	}

	abstract protected Image getImage();
	
	@Override
	protected void createFormContent(IManagedForm mform) {
		toolkit = mform.getToolkit();
		bindingContext = new DataBindingContext();

		Form form = mform.getForm().getForm();
		form.setText(title);

		form.setImage(getImage());
		mform.getToolkit().decorateFormHeading(form);

		Composite body = form.getBody();
		body.setLayout(new GridLayout(2, false));
		body.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        validationLabel = toolkit.createLabel(body, "", SWT.NONE);
        validationLabel.setText("ERROR:");
        validationLabel.setForeground(getShell().getDisplay().getSystemColor(SWT.COLOR_RED));

        validationText = toolkit.createLabel(
                body, "", SWT.NONE);
        validationText
                .setLayoutData(new GridData(
                        GridData.FILL_HORIZONTAL));

		createContent(mform, body, bindingContext);
				
		displayedValidationStatus = new ComputedValue(IStatus.class) {
			
			Collection<ControlDecoration> decorations = decoratorMap.keySet();
			
			@Override
			protected Object calculate() {
				boolean ok = true;
				IStatus newStatus = ValidationStatus.ok();
				
				// iterate over all decoration and there validation status
				for(ControlDecoration decoration: decorations){
					IObservableValue observableValue = decoratorMap.get(decoration);
					IStatus status = (IStatus) observableValue.getValue();
					
					if (!status.isOK()) {
						// validation error
						ok = false;
						decoration.setDescriptionText(status.getMessage());
						decoration.show();
						// select severest and meaningful message
						if(status.getSeverity() > newStatus.getSeverity())
							if(!status.getMessage().isEmpty())
								newStatus = status;
					} else
						decoration.hide();
				}
				
				updateValidationFeedback(ok && newStatus.isOK());
				return newStatus;
			}
		};
		
		bindingContext.bindValue(WidgetProperties.text().observe(validationText), displayedValidationStatus);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.forms.FormDialog#createButtonBar(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createButtonBar(Composite parent) {
		Control bar = super.createButtonBar(parent);

		Object value = displayedValidationStatus.getValue();
		if (value instanceof IStatus) {
			boolean ok = ((IStatus) value).isOK();
			updateValidationFeedback(ok);
		}

		return bar;
	}

	protected void setValidationFeedbackOff() {
		if (validationLabel.isDisposed())
			return;
		
		validationLabel.setVisible(false);
		validationText.setVisible(false);
		
		Button okButton = getButton(IDialogConstants.OK_ID);
		if (okButton!=null)
			okButton.setEnabled(false);
	}

	protected void updateValidationFeedback(boolean ok) {
		if (validationLabel.isDisposed())
			return;
		
		validationLabel.setVisible(!ok);
		validationText.setVisible(!ok);
		
		Button okButton = getButton(IDialogConstants.OK_ID);
		if (okButton!=null)
			okButton.setEnabled(ok);
	}
	
	protected void setValidationText (String text) {
		validationText.setText(text);
	}
	
	protected abstract void createContent(IManagedForm mform, Composite body,
			DataBindingContext bindingContext);
	
	protected Text createText(Composite parent, String label, EObject obj, EAttribute att) {
		return createText(parent, label, obj, att, null);
	}
	
	protected Text createText(Composite parent, String label, EObject obj, EAttribute att, IValidator validator) {
		return createText(parent, label, obj, att, validator, false);
	}
	
	protected Text createText(Composite parent, String label, EObject obj, EAttribute att, IValidator validator, boolean multiline) {
		return createText(parent, label, obj, att, validator, null, null, multiline);
	}
	
	protected Text createText(Composite parent, String label, EObject obj, EStructuralFeature feat, IValidator validator, IConverter s2m, IConverter m2s, boolean multiline) {
		return createText(parent, label, obj, feat, validator, null, s2m, m2s, multiline);
	}
	
	protected Text createText(Composite parent, String label, EObject obj, EStructuralFeature feat, IValidator singleValidator, MultiValidator2 multiValidator, IConverter s2m, IConverter m2s, boolean multiline) {
		Label l = toolkit.createLabel(parent, label, SWT.NONE);
		l.setLayoutData(new GridData(SWT.NONE));
		
		int style = SWT.BORDER;
		if (multiline)
			style |= SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL;
		Text text = toolkit.createText(parent, "", style);
		GridData gd = new GridData(multiline?GridData.FILL_BOTH:GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		
		UpdateValueStrategy t2m = null;
		UpdateValueStrategy m2t = null;
		if (singleValidator!=null || s2m!=null || m2s!=null) {
			t2m = new UpdateValueStrategy();
			if (s2m!=null)
				t2m.setConverter(s2m);
			if (singleValidator!=null) {
				t2m.setAfterConvertValidator(singleValidator);
				t2m.setBeforeSetValidator(singleValidator);
			}
			m2t = new UpdateValueStrategy();
			if (m2s!=null)
				m2t.setConverter(m2s);
			if (singleValidator!=null) {
				m2t.setAfterConvertValidator(singleValidator);
				m2t.setBeforeSetValidator(singleValidator);
			}
		}
		
		Object type = (s2m!= null)?s2m.getToType():String.class;
		createBinding(text, obj, feat, type, t2m, m2t, multiValidator);
		
		return text;
	}
	
	protected Text createFixedText(Composite parent, String label, String txt, boolean multiline) {
		Label l = toolkit.createLabel(parent, label, SWT.NONE);
		l.setLayoutData(new GridData(SWT.NONE));
		
		int style = SWT.BORDER;
		if (multiline)
			style |= SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL;
		Text text = toolkit.createText(parent, txt, style);
		GridData gd = new GridData(multiline?GridData.FILL_BOTH:GridData.FILL_HORIZONTAL);
		text.setLayoutData(gd);
		text.setEnabled(false);
		
		return text;
	}
	
	protected Button createCheck(Composite parent, String label, EObject obj, EAttribute att) {
		return createCheck(parent, label, obj, att, null, null);
	}
	
	protected Button createCheck(Composite parent, String label, EObject obj, EAttribute att, IValidator validator, MultiValidator2 multiValidator) {
		Label l = toolkit.createLabel(parent, label, SWT.NONE);
		l.setLayoutData(new GridData(SWT.NONE));
		
		Button check = toolkit.createButton(parent, "", SWT.CHECK);
		check.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		UpdateValueStrategy t2m = null;
		UpdateValueStrategy m2t = null;
		if (validator!=null) {
			t2m = new UpdateValueStrategy();
			t2m.setAfterConvertValidator(validator);
			t2m.setBeforeSetValidator(validator);
			m2t = new UpdateValueStrategy();
			m2t.setAfterConvertValidator(validator);
			m2t.setBeforeSetValidator(validator);
		}
		createBinding(check, obj, att, Boolean.class, t2m, m2t, multiValidator);
		
		return check;
	}
	
	protected Combo createCombo(Composite parent, String label, EObject obj, Object type, EReference ref, List<? extends EObject> candidates, EAttribute nameAttr) {
		return createCombo(parent, label, obj, type, ref, candidates, nameAttr, null, null);
	}
	
	protected Combo createCombo(Composite parent, String label, EObject obj, Object type, EReference ref, List<? extends EObject> candidates, EAttribute nameAttr, IValidator validator, MultiValidator2 multiValidator) {
		Label l = toolkit.createLabel(parent, label, SWT.NONE);
		l.setLayoutData(new GridData(SWT.NONE));

		Combo combo = new Combo(parent, SWT.READ_ONLY);
		combo.setLayoutData(new GridData(SWT.HORIZONTAL));
		combo.setVisibleItemCount(10);
		toolkit.adapt(combo, true, true);
		
		Reference2StringConverter r2s = new Reference2StringConverter(type, nameAttr);
		for (EObject o : candidates) {
			combo.add((String) r2s.convert(o));
		}
		
		String2ReferenceConverter s2r = new String2ReferenceConverter(type, candidates, nameAttr);
		UpdateValueStrategy t2m = new UpdateValueStrategy().setConverter(s2r);
		UpdateValueStrategy m2t = new UpdateValueStrategy().setConverter(r2s);
		if (validator!=null) {
			t2m.setAfterConvertValidator(validator);
			t2m.setBeforeSetValidator(validator);
			m2t.setAfterConvertValidator(validator);
			m2t.setBeforeSetValidator(validator);
		}
		createBinding(combo, obj, ref, type, t2m, m2t, multiValidator);
		
		return combo;
	}
	
	protected Combo createComboUsingDesc(Composite parent, String label, EObject obj, Object type, EReference ref, List<IEObjectDescription> candidates, EAttribute nameAttr, IValidator validator){
		return createComboUsingDesc(parent, label, obj, type, ref, candidates, nameAttr, validator, null);
	}
	
	protected Combo createComboUsingDesc(Composite parent, String label, EObject obj, Object type, EReference ref, List<IEObjectDescription> candidates, EAttribute nameAttr, IValidator validator, MultiValidator2 multiValidator) {
		Label l = toolkit.createLabel(parent, label, SWT.NONE);
		l.setLayoutData(new GridData(SWT.NONE));

		Combo combo = new Combo(parent, SWT.READ_ONLY);
		combo.setLayoutData(new GridData(SWT.HORIZONTAL));
		combo.setVisibleItemCount(10);
		toolkit.adapt(combo, true, true);
		
		DescriptionBased_Reference2StringConverter r2s = new DescriptionBased_Reference2StringConverter(type, nameAttr);
		for (IEObjectDescription desc : candidates) {
			combo.add(r2s.convert(desc).toString());
		}
		
		DescriptionBased_String2ReferenceConverter s2r = new DescriptionBased_String2ReferenceConverter(type, obj, candidates);
		UpdateValueStrategy t2m = new UpdateValueStrategy().setConverter(s2r);
		UpdateValueStrategy m2t = new UpdateValueStrategy().setConverter(r2s);
		if (validator!=null) {
			t2m.setAfterConvertValidator(validator);
			t2m.setBeforeSetValidator(validator);
			m2t.setAfterConvertValidator(validator);
			m2t.setBeforeSetValidator(validator);
		}
		
		createBinding(combo, obj, ref, type, t2m, m2t, multiValidator);
		
		return combo;
	}
	
	protected Combo createCombo(Composite parent, String label, EObject obj, Object type, EAttribute att, List<? extends Enumerator> choices) {
		return createCombo(parent, label, obj, type, att, choices, null, null);
	}
	
	protected Combo createCombo(Composite parent, String label, EObject obj, Object type, EAttribute att, List<? extends Enumerator> choices, IValidator singleValidator, MultiValidator2 multiValidator) {
		Label l = toolkit.createLabel(parent, label, SWT.NONE);
		l.setLayoutData(new GridData(SWT.NONE));

		Combo combo = new Combo(parent, SWT.READ_ONLY);
		combo.setLayoutData(new GridData(SWT.HORIZONTAL));
		combo.setVisibleItemCount(10);
		toolkit.adapt(combo, true, true);
		
		for (Enumerator o : choices) 
			combo.add(o.getLiteral());
		
		UpdateValueStrategy t2m = new UpdateValueStrategy().setConverter(new String2EnumConverter(type, choices));
		UpdateValueStrategy m2t = new UpdateValueStrategy().setConverter(new Enum2StringConverter());
		if (singleValidator!=null) {
			t2m.setAfterConvertValidator(singleValidator);
			t2m.setBeforeSetValidator(singleValidator);
			m2t.setAfterConvertValidator(singleValidator);
			m2t.setBeforeSetValidator(singleValidator);
		}
		
		createBinding(combo, obj, att, type, t2m, m2t, multiValidator);
		
		return combo;
	}
	
	private void createBinding(Widget widget, EObject obj, EStructuralFeature feature, Object objType, UpdateValueStrategy t2m, UpdateValueStrategy m2t, MultiValidator2 multiValidator){
		IObservableValue observableWidget = null;
		if(widget instanceof Text)
			observableWidget = WidgetProperties.text(SWT.Modify).observe(widget);
		else if(widget instanceof Button || widget instanceof Combo)
			observableWidget = WidgetProperties.selection().observe(widget);
		IObservableValue observableObj = EMFProperties.value(feature).observe(obj);
		
		if(multiValidator != null){
			IObservableValue convertedObservable = new WritableValue(null, objType);
			Binding convertBinding = bindingContext.bindValue(observableWidget, convertedObservable, t2m, m2t);
			multiValidator.installValidator(convertBinding, observableObj);
		
			List<ValidationStatusProvider> valProviders = new ArrayList<ValidationStatusProvider>();
			valProviders.add(convertBinding);
			valProviders.add(multiValidator);
			widget.setData(DATA_KEY_STATUS, createAggregateValidationStatus(valProviders));
		} else {
			Binding binding = bindingContext.bindValue(observableWidget, observableObj, t2m, m2t);
			widget.setData(DATA_KEY_STATUS, binding.getValidationStatus());
		}
	}
	
	protected ControlDecoration createDecorator(Control ctrl, String message) {
		ControlDecoration controlDecoration = new ControlDecoration(ctrl, SWT.LEFT | SWT.TOP);
		controlDecoration.setDescriptionText(message);
		FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(
				FieldDecorationRegistry.DEC_ERROR);
		controlDecoration.setImage(fieldDecoration.getImage());
		controlDecoration.hide();
       
		IObservableValue status = getObservableStatus(ctrl);
		if(status != null)
			decoratorMap.put(controlDecoration, status);

        return controlDecoration;
	}

	protected ControlDecoration createInfoDecorator(Control ctrl, String message) {
		ControlDecoration controlDecoration = new ControlDecoration(ctrl, SWT.LEFT | SWT.TOP);
		controlDecoration.setDescriptionText(message);
		FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(
				FieldDecorationRegistry.DEC_INFORMATION);
		controlDecoration.setImage(fieldDecoration.getImage());

        return controlDecoration;
	}

	protected DataBindingContext getBindingContext() {
		return bindingContext;
	}
	
	/**
	 * Returns the observable validation status for the given widget.
	 */
	protected IObservableValue getObservableStatus(Widget widget){
		return (IObservableValue) widget.getData(DATA_KEY_STATUS);
	}

	/**
	 * @return the toolkit
	 */
	public FormToolkit getToolkit() {
		return toolkit;
	}

	protected void setTitle(String title) {
		this.title = title;
	}
	
	private IObservableValue createAggregateValidationStatus(Collection<ValidationStatusProvider> valProvider){
		IObservableCollection observableStatusProvider = new WritableList(valProvider, ValidationStatusProvider.class);
		return new AggregateValidationStatus(observableStatusProvider, AggregateValidationStatus.MAX_SEVERITY);
	}

	/**
	 * Selects the string {@code selectSring(...)} inside the Text & shifts the
	 * keyboard focus to it. If the select string is empty or not present inside
	 * the text, it does nothing.
	 * 
	 * @param text
	 * @param selectString
	 * 
	 * @author jayant
	 */
	protected void setTextSelectionAndFocus(Text text, String selectString) {
		if (selectString.isEmpty())
			return;
		String content = text.getText();
		int start = content.indexOf(selectString+'(');
		if (start != -1) {

			// find the index of closing brace
			int end = start + selectString.length() + 1;
			Stack<Character> parenthesis = new Stack<Character>();
			parenthesis.push('(');
			while (end < content.length() && !parenthesis.isEmpty()) {
				if (content.charAt(end) == '(')
					parenthesis.push('(');
				else if (content.charAt(end) == ')')
					parenthesis.pop();
				end++;
			}
			text.setFocus();
			text.setSelection(start, end);
		}
	}
}

Back to the top