Skip to main content
summaryrefslogtreecommitdiffstats
blob: de0298164f210e387a9d77079b66a179e4d0d5f4 (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
/**
 * <copyright> 
 *
 * Copyright (c) 2008 itemis AG 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: 
 *   itemis AG - Initial API and implementation
 *
 * </copyright>
 *
 */
package org.eclipse.emf.editor.ui.binding;

import java.util.List;

import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.databinding.EMFDataBindingContext;
import org.eclipse.emf.databinding.edit.EMFEditObservables;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.impl.EEnumImpl;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.emf.editor.EEPlugin;
import org.eclipse.emf.editor.oaw.OawFacade;
import org.eclipse.emf.editor.ui.ProposalCreator;
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.bindings.keys.ParseException;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ViewersObservables;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
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.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * @author Dennis Huebner
 * 
 */
public class EmfSwtBindingFactory {
	private Composite parent = null;
	private FormToolkit toolkit = null;
	private final EObject owner;
	private final EditingDomain domain;
	private final EMFDataBindingContext edbc;
	private final AdapterFactory adapterFactory;
	private final ProposalCreator proposalcreator;
	public static final String EOBJECT_KEY = EcorePackage.Literals.EOBJECT
			.getName();
	public static final String ESTRUCTURALFEATURE_KEY = EcorePackage.Literals.ESTRUCTURAL_FEATURE
			.getName();

	public EmfSwtBindingFactory(AdapterFactory adapterFactory,
			EditingDomain domain, EObject owner, Composite parent,
			FormToolkit toolkit, OawFacade facade) {
		this.adapterFactory = adapterFactory;
		this.edbc = new EMFDataBindingContext();
		this.domain = domain;
		this.proposalcreator = new ProposalCreator(domain, facade);
		this.owner = owner;
		this.parent = parent;
		this.toolkit = toolkit;
	}

	public Control create(EObject eObject) {
		Control retVal = null;
		if (eObject instanceof EStructuralFeature) {
			retVal = createForEStructuralFeature((EStructuralFeature) eObject);
		}
		return retVal;
	}

	private Control createForEStructuralFeature(EStructuralFeature feature) {
		Control retVal = null;
		if (feature.isMany()) {
			retVal = bindList(feature);
		} else {
			retVal = bindValue(feature);
		}
		setupControl(feature, retVal);
		return retVal;
	}

	private Control bindList(EStructuralFeature feature) {
		IObservableList source = EMFEditObservables.observeList(domain, owner,
				feature);

		List<?> choice = proposalcreator.proposals(owner, feature);
		MultipleFeatureControl mfc = new MultipleFeatureControl(parent,
				toolkit, new AdapterFactoryLabelProvider(adapterFactory),
				owner, feature, choice);

		IObservableList target = new SelectionProviderMultipleSelectionObservableList(
				SWTObservables.getRealm(Display.getDefault()), mfc
						.getInternalSelectionProvider(), Object.class);
		Binding binding = edbc.bindList(target, source, null, null);
		binding.updateModelToTarget();
		return mfc;
	}

	private Control bindValue(EStructuralFeature feature) {
		Control retVal = null;
		IObservableValue source = EMFEditObservables.observeValue(domain,
				owner, feature);
		IObservableValue target = null;
		if (feature.getEType().equals(EcorePackage.Literals.EBOOLEAN)
				|| feature.getEType().equals(
						EcorePackage.Literals.EBOOLEAN_OBJECT)
				|| (feature.getEType() instanceof EDataType && (feature
						.getEType().getInstanceClass() == Boolean.class || feature
						.getEType().getInstanceClass() == Boolean.TYPE))) {
			Button b = toolkit.createButton(parent, "", SWT.CHECK);
			target = SWTObservables.observeSelection(b);
			retVal = b;
		} else if (feature instanceof EReference
				|| feature.getEType() instanceof EEnumImpl) {
			ComboViewer combo = new ComboViewer(parent, SWT.READ_ONLY);
			toolkit.adapt(combo.getCombo());
			combo.setContentProvider(new ArrayContentProvider());
			combo.setLabelProvider(new AdapterFactoryLabelProvider(
					adapterFactory));
			combo.setInput(proposalcreator.proposals(owner, feature));
			target = ViewersObservables.observeSingleSelection(combo);
			retVal = combo.getCombo();
		} else {
			Text t = toolkit.createText(parent, new String());
			t.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TREE_BORDER);
			List<?> proposals = proposalcreator.proposals(owner, feature);
			if (proposals != null && !proposals.isEmpty()) {
				// TODO prevert adding null to a list, for example a Collection
				// Type
				while (proposals.remove(null)) {
					// clear null entries
				}
				ControlDecoration field = new ControlDecoration(t, SWT.BORDER);
				FieldDecoration requiredFieldIndicator = FieldDecorationRegistry
						.getDefault().getFieldDecoration(
								FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
				field.setImage(requiredFieldIndicator.getImage());
				field.setDescriptionText(requiredFieldIndicator
						.getDescription());
				KeyStroke keyStroke = null;
				String string = new String();
				try {
					string = "Ctrl+Space";
					keyStroke = KeyStroke.getInstance(string);
				} catch (ParseException e) {
					EEPlugin.getDefault().getLog().log(
							new Status(IStatus.ERROR, EEPlugin.PLUGIN_ID,
									"Error while parse: " + string, e));
				}
				new ContentProposalAdapter(t, new TextContentAdapter(),
						new SimpleContentProposalProvider(proposals
								.toArray(new String[] {})), keyStroke, null);
			}
			target = SWTObservables.observeText(t, SWT.Modify);
			retVal = t;

		}
		edbc.bindValue(target, source, null, null);
		return retVal;
	}

	private void setupControl(EStructuralFeature f, Control c) {
		// disable unchangeable and unserializable
		c.setEnabled(f.isChangeable()
				&& (!(f.getEType() instanceof EDataType && !((EDataType) f
						.getEType()).isSerializable())));
		c.setData(EmfSwtBindingFactory.ESTRUCTURALFEATURE_KEY, f);
		c.setData(EmfSwtBindingFactory.EOBJECT_KEY, owner);
		c.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	}

	public void dispose() {
		edbc.dispose();
		parent.dispose();
		// toolkit.dispose();
	}

}

Back to the top