Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 407aeaa04aafaa42c85f63e4b2d2f85c4349ba1a (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
/*****************************************************************************
 * Copyright (c) 2011 CEA LIST.
 *
 * 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Thibault Le Ouay t.leouay@sherpa-eng.com - Add binding implementation
 *****************************************************************************/
package org.eclipse.papyrus.uml.properties.widgets;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.papyrus.infra.properties.ui.modelelement.ModelElement;
import org.eclipse.papyrus.infra.widgets.editors.AbstractValueEditor;
import org.eclipse.papyrus.uml.properties.expression.ExpressionList.Expression;
import org.eclipse.papyrus.uml.properties.preferences.LanguageRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;


/**
 * An editor to edit an expression body.
 * Depending on the matching language, a different widget will be used to edit
 * the expression body.
 *
 * @author Camille Letavernier
 *
 */
public class DynamicBodyEditor extends AbstractValueEditor implements Listener {

	private BodyEditor currentEditor;

	private final Composite bodyEditorContainer;

	private final Set<Listener> changeListeners = new HashSet<Listener>();

	private boolean readOnly = false;

	private ModelElement context = null;

	/**
	 *
	 * Constructor.
	 *
	 * @param parent
	 *            The composite in which the widget will be displayed
	 * @param style
	 *            The widget's composite style
	 */
	public DynamicBodyEditor(Composite parent, int style) {
		super(parent, style);

		bodyEditorContainer = new Composite(this, style);
		bodyEditorContainer.setLayout(new FillLayout());
		bodyEditorContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		currentEditor = new NullBodyEditor();
		currentEditor.createWidget(bodyEditorContainer, SWT.NONE);
	}

	/**
	 * Sets the {@link Expression} to edit.
	 * This will refresh the widget used to edit the expression body.
	 *
	 * @param expression
	 *            the expression to edit
	 */
	public void display(Expression expression) {
		if (currentEditor != null) {
			disposeBodyEditor();
		}

		BodyEditor editor;

		if (expression == null) {
			editor = new NullBodyEditor();
			editor.createWidget(bodyEditorContainer, SWT.NONE);
			bodyEditorContainer.layout();
			return;
		}

		String language = expression.getLanguage();
		String initialText = expression.getBody();

		editor = getEditor(language);
		editor.createWidget(bodyEditorContainer, SWT.NONE);
		if (context != null) {
			editor.setContext(context);
		}

		editor.setInput(initialText);
		editor.addChangeListener(this);

		editor.setReadOnly(readOnly);

		bodyEditorContainer.layout();

		currentEditor = editor;
	}

	/**
	 * Adds a listener to this object
	 * Events will be fired when the body has changed
	 *
	 * @param listener
	 */
	// TODO : isn't it simply a commit listener ?
	public void addChangeListener(Listener listener) {
		changeListeners.add(listener);
	}

	/**
	 * Removes a listener from this object
	 *
	 * @param listener
	 */
	public void removeChangeListener(Listener listener) {
		changeListeners.remove(listener);
	}

	private BodyEditor getEditor(String language) {
		return LanguageRegistry.instance.getEditorFor(language);
	}

	private void disposeBodyEditor() {
		currentEditor.removeChangeListener(this);
		currentEditor.dispose();
		if (null != bodyEditorContainer && !bodyEditorContainer.isDisposed()) {
			for (Control control : bodyEditorContainer.getChildren()) {
				control.dispose();
			}
		}
	}

	@Override
	public String getValue() {
		if (currentEditor != null) {
			return currentEditor.getValue();
		}
		return null;
	}

	@Override
	public Object getEditableType() {
		return String.class;
	}

	@Override
	public void setReadOnly(boolean readOnly) {
		if (currentEditor != null) {
			currentEditor.setReadOnly(readOnly);
		}

		this.readOnly = readOnly;
	}

	@Override
	public boolean isReadOnly() {
		return readOnly;
	}

	@Override
	public void setToolTipText(String text) {
		super.setLabelToolTipText(text);
	}

	public void handleEvent(Event event) {
		for (Listener listener : changeListeners) {
			listener.handleEvent(event);
		}
	}

	@Override
	protected GridData getLabelLayoutData() {
		GridData result = super.getLabelLayoutData();
		result.verticalAlignment = SWT.BEGINNING;
		return result;
	}

	/**
	 * Sets the ModelElement used by this editor
	 *
	 * @param modelElement
	 */
	public void setContext(ModelElement modelElement) {
		this.context = modelElement;
		if (currentEditor != null) {
			currentEditor.setContext(context);
		}
	}

	@Override
	public void updateStatus(IStatus status) {
		
	}


	@Override
	public void changeColorField() {
		

	}
}

Back to the top