Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3d3148bf4062816499e8cfaafe4d565b80d565f8 (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
/**
 * Copyright (c) 2012 Mia-Software.
 *
 * 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:
 *  	Alban Ménager (Soft-Maint) - Bug 387470 - [EFacet][Custom] Editors
 *  	Grégoire Dupé (Mia-Software) - Bug 387470 - [EFacet][Custom] Editors
 */
package org.eclipse.papyrus.emf.facet.query.ocl.sdk.ui.internal.widget.oclexpression;

import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.papyrus.emf.facet.query.ocl.core.util.OclQueryUtil;
import org.eclipse.papyrus.emf.facet.query.ocl.sdk.ui.internal.Messages;
import org.eclipse.papyrus.emf.facet.query.ocl.sdk.ui.widget.oclexpression.IOCLExpressionWidget;
import org.eclipse.papyrus.emf.facet.util.ui.internal.exported.util.widget.AbstractWidget;
import org.eclipse.papyrus.emf.facet.util.ui.utils.PropertyElement;
import org.eclipse.ocl.ParserException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

/**
 * Simple widget for the edition of a complexe OCl expression.
 */
public class OclExpressionWidgetSimple extends AbstractWidget implements
		IOCLExpressionWidget {

	private static final String UNREC_VAR = "Unrecognized variable"; //$NON-NLS-1$
	private static final int TEXT_HEIGHT = 100;
	private final EClassifier context;
	private final String expression;
	private Text text;
	private final PropertyElement oclExpressionProp;

	public OclExpressionWidgetSimple(final Composite parent,
			final EClassifier context, final String expression,
			final PropertyElement oclExpressionProp) {
		super(parent);
		this.context = context;
		this.expression = expression;
		this.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		this.oclExpressionProp = oclExpressionProp;
	}

	@Override
	protected void addSubWidgets() {
		this.text = new Text(this, SWT.MULTI | SWT.V_SCROLL);
		final GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.heightHint = TEXT_HEIGHT;
		this.text.setLayoutData(gridData);

		if (this.expression != null) {
			this.text.setText(this.expression);
		}
	}

	@Override
	public String getError() {
		String result = null;

		final String textExpression = this.text.getText().trim();
		if ("".equals(textExpression)) { //$NON-NLS-1$
			result = Messages.OclExpression_Error;
		} else {
			try {
				OclQueryUtil.createOCLExpression(this.context, textExpression);
				this.oclExpressionProp.setValue(textExpression);
			} catch (final ParserException exception) {
				if (exception.getMessage().contains(UNREC_VAR)) {
					this.oclExpressionProp.setValue(textExpression);
				} else {
					result = Messages.OclExpression_Error
							+ " :\n - " + exception.getMessage(); //$NON-NLS-1$
				}
			}
		}

		return result;
	}

	@Override
	public void notifyChanged() {
		// Nothing to do.
	}

	public void setExpression(final String expression) {
		this.text.setText(expression);
	}

}

Back to the top