Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 41f554ce51600bbb3b90fc65ec449fa38e337cf4 (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
/*****************************************************************************
 * Copyright (c) 2011, 2014 CEA LIST 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Thibault Le Ouay t.leouay@sherpa-eng.com - Add binding implementation
 *  Christian W. Damus (CEA) - bug 417409
 *
 *****************************************************************************/
package org.eclipse.papyrus.views.properties.modelelement;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.databinding.observable.DisposeEvent;
import org.eclipse.core.databinding.observable.IDisposeListener;
import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.tools.databinding.DelegatingObservable;
import org.eclipse.papyrus.infra.tools.databinding.IDelegatingObservable;
import org.eclipse.papyrus.infra.tools.databinding.ReferenceCountedObservable;
import org.eclipse.papyrus.infra.widgets.creation.ReferenceValueFactory;
import org.eclipse.papyrus.infra.widgets.providers.EmptyContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.IStaticContentProvider;
import org.eclipse.papyrus.views.properties.creation.PropertyEditorFactory;

/**
 * Provides a default implementation for ModelElement methods applied on the
 * modelElement's properties.
 *
 * @author Camille Letavernier
 */
public abstract class AbstractModelElement implements ModelElement, IDataSourceListener {

	/**
	 * The DataSource owning this ModelElement
	 */
	protected DataSource dataSource;

	private final Map<String, IObservable> observables = new HashMap<String, IObservable>();

	private IDisposeListener observableDisposeListener;

	AbstractModelElementFactory<AbstractModelElement> factory;

	/**
	 * Constructor.
	 */
	protected AbstractModelElement() {
		super();
	}

	@SuppressWarnings("unchecked")
	void setFactory(AbstractModelElementFactory<? extends AbstractModelElement> factory) {
		this.factory = (AbstractModelElementFactory<AbstractModelElement>) factory;
	}

	public IStaticContentProvider getContentProvider(String propertyPath) {
		return EmptyContentProvider.instance;
	}

	public ILabelProvider getLabelProvider(String propertyPath) {
		return null;
	}

	public boolean isOrdered(String propertyPath) {
		return true;
	}

	public boolean isUnique(String propertyPath) {
		return false;
	}

	public boolean isMandatory(String propertyPath) {
		return false;
	}

	public boolean isEditable(String propertyPath) {
		return true;
	}

	public boolean forceRefresh(String propertyPath) {
		return false;
	}

	public void setDataSource(DataSource source) {
		if (this.dataSource != source) {
			if (this.dataSource != null) {
				this.dataSource.removeDataSourceListener(this);
			}

			this.dataSource = source;

			if (this.dataSource != null) {
				this.dataSource.addDataSourceListener(this);
			}
		}
	}

	public final void dataSourceChanged(DataSourceChangedEvent event) {
		if (event.getDataSource() == dataSource) {
			// The data source changed. Update for the new selection
			IStructuredSelection selection = dataSource.getSelection();
			if (selection.isEmpty()) {
				factory.updateModelElement(this, null);
			} else if (selection.size() == 1) {
				factory.updateModelElement(this, selection.getFirstElement());
			} else {
				updateMultipleSelection(selection);
			}

			// Update our observables
			for (Map.Entry<String, IObservable> next : observables.entrySet()) {
				IDelegatingObservable wrapper = ((IDelegatingObservable) next.getValue());
				wrapper.setDelegate(doGetObservable(next.getKey()));
			}
		}
	}

	void updateMultipleSelection(IStructuredSelection selection) {
		throw new IllegalArgumentException("multiple selection"); //$NON-NLS-1$
	}

	/**
	 * @see org.eclipse.papyrus.views.properties.modelelement.ModelElement#getValueFactory(java.lang.String)
	 *
	 * @param propertyPath
	 * @return a default factory based on the property view configuration to
	 *         edit objects, as if they were selected in an editor
	 */
	public ReferenceValueFactory getValueFactory(String propertyPath) {
		return new PropertyEditorFactory();
	}

	public Object getDefaultValue(String propertyPath) {
		return null;
	}

	public boolean getDirectCreation(String propertyPath) {
		return false;
	}

	public final IObservable getObservable(String propertyPath) {
		if (!observables.containsKey(propertyPath)) {
			IObservable observable = doGetObservable(propertyPath);
			if (observable != null) {
				// Wrap it so that we may replace the delegate as needed
				observable = DelegatingObservable.wrap(observable);
				observable.addDisposeListener(getObservableDisposeListener());
				ReferenceCountedObservable.Util.retain(observable);
				observables.put(propertyPath, observable);
			}
		}
		return observables.get(propertyPath);
	}

	/**
	 * Creates the IObservable for the given propertyPath
	 *
	 * @param propertyPath
	 *            The path of the property we want to observe
	 * @return
	 *         The new IObservable
	 */
	protected abstract IObservable doGetObservable(String propertyPath);

	public void dispose() {
		for (IObservable observable : observables.values()) {
			if (observableDisposeListener != null) {
				// Don't let the listener concurrently modify the map in case releasing triggers dispose
				observable.removeDisposeListener(observableDisposeListener);
			}

			ReferenceCountedObservable.Util.release(observable);
		}

		observables.clear();
		observableDisposeListener = null;
	}

	public IValidator getValidator(String propertyPath) {
		return null;
	}

	private IDisposeListener getObservableDisposeListener() {
		if (observableDisposeListener == null) {
			observableDisposeListener = new IDisposeListener() {

				public void handleDispose(DisposeEvent event) {
					// Remove this property
					for (Iterator<Map.Entry<String, IObservable>> entries = observables.entrySet().iterator(); entries.hasNext();) {
						if (entries.next().getValue() == event.getObservable()) {
							entries.remove();
							break;
						}
					}
				}
			};
		}

		return observableDisposeListener;
	}

}

Back to the top