Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b3b40b74607858e2cd524d9a8d70e6a08cdf4e5d (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
/*****************************************************************************
 * Copyright (c) 2010, 2014 CEA LIST, Christian W. Damus, 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
 *  Christian W. Damus - bug 455075
 *
 *****************************************************************************/
package org.eclipse.papyrus.views.properties.modelelement;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.LabelProviderChangedEvent;
import org.eclipse.papyrus.infra.properties.contexts.View;
import org.eclipse.papyrus.infra.widgets.creation.ReferenceValueFactory;
import org.eclipse.papyrus.infra.widgets.providers.EmptyContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.EncapsulatedContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.IStaticContentProvider;
import org.eclipse.papyrus.infra.widgets.util.INameResolutionHelper;
import org.eclipse.papyrus.infra.widgets.util.IPapyrusConverter;
import org.eclipse.papyrus.views.properties.Activator;
import org.eclipse.swt.graphics.Image;

/**
 * A DataSource is an object encapsulating one or more {@link ModelElement}s.
 * It contains methods to resolve property paths, and forward the methods to
 * the right ModelElement.
 *
 * For example, a UML class stereotyped with the SysML::Blocks::Block will have
 * two ModelElements : one for UML, and one for the Block stereotype.
 *
 * It will be able to resolve paths such as UML:Class:name or
 * SysML:Blocks:Block:isEncapsulated
 *
 * The methods such as isUnique, isEditable or getContentProvider will be
 * delegated to the resolved ModelElement, with a truncated property path.
 *
 * For example, a call to DataSource#isEditable("UML:Class:name") will be
 * forwarded to UMLModelElement#isEditable("name")
 *
 * @author Camille Letavernier
 */
public class DataSource implements IChangeListener {

	private final ListenerList changeListeners = new ListenerList(ListenerList.IDENTITY);

	private final ListenerList dataSourceListeners = new ListenerList(ListenerList.IDENTITY);

	private View view;

	private IStructuredSelection selection;

	private Map<String, ModelElement> elements = new HashMap<String, ModelElement>();

	/**
	 * Constructs a new DataSource from the given view and selection
	 *
	 * @param realm
	 * @param view
	 * @param selection
	 *
	 * @see DataSourceFactory#createDataSourceFromSelection(IStructuredSelection, View)
	 */
	protected DataSource(View view, IStructuredSelection selection) {
		this.view = view;
		this.selection = selection;
	}

	/**
	 * Return the instance of ModelElement associated to the given path
	 *
	 * @param propertyPath
	 *            The propertyPath to lookup
	 * @return
	 *         The ModelElement associated to the given propertyPath
	 */
	public ModelElement getModelElement(String propertyPath) {
		// ConfigurationManager.instance.getProperty(propertyPath)
		String key = propertyPath.substring(0, propertyPath.lastIndexOf(":")); //$NON-NLS-1$
		if (!elements.containsKey(key)) { // Try to resolve the modelElements on-the-fly
			ModelElement element = DataSourceFactory.instance.getModelElementFromPropertyPath(this, propertyPath);
			if (element == null) {
				Activator.log.warn("Unable to find a ModelElement for " + propertyPath + ". Elements : " + elements); //$NON-NLS-1$ //$NON-NLS-2$
			}
			elements.put(key, element);
		}
		return elements.get(key);
	}

	private String getLocalPropertyPath(String propertyPath) {
		return propertyPath.substring(propertyPath.lastIndexOf(":") + 1); //$NON-NLS-1$
	}

	/**
	 * Returns an IObservable corresponding to the given property path
	 * The observable may be either an IObservableValue or an IObservableList
	 * The call to this method is delegated to the corresponding ModelElement
	 * The IObservable objects returned by this method may be shared by
	 * many instances, which means they should not be disposed directly.
	 * They will be disposed when this DataSource is disposed.
	 *
	 * @param propertyPath
	 *            The property path for which we want to retrieve an ObservableValue
	 * @return
	 *         The IObservable corresponding to the given propertyPath
	 */
	public IObservable getObservable(String propertyPath) {
		String localPropertyPath = getLocalPropertyPath(propertyPath);
		ModelElement element = getModelElement(propertyPath);

		if (element == null) {
			return null;
		}

		IObservable observable = element.getObservable(localPropertyPath);
		if (observable != null) {
			observable.addChangeListener(this);
		}

		return observable;
	}

	@Override
	public String toString() {
		return "[DataSource] " + super.toString(); //$NON-NLS-1$
	}

	/**
	 * Returns an IStaticContentProvider corresponding to the given property path
	 * The call to this method is delegated to the corresponding ModelElement
	 *
	 * @param propertyPath
	 *            The property path for which we want to retrieve a ContentProvider
	 * @return
	 *         The IStaticContentProvider corresponding to the given propertyPath
	 */
	public IStaticContentProvider getContentProvider(final String propertyPath) {
		class Delegator extends EncapsulatedContentProvider implements IDataSourceListener {

			{
				createDelegate();
				DataSource.this.addDataSourceListener(this);
			}

			@Override
			public void dispose() {
				disposeDelegate();
				DataSource.this.removeDataSourceListener(this);
			}

			private void disposeDelegate() {
				if (encapsulated != null) {
					encapsulated.dispose();
					encapsulated = null;
				}

				// If I had any temporary elements, then they cannot now be relevant
				clearTemporaryElements();
			}

			private void createDelegate() {
				encapsulate(doGetContentProvider(propertyPath));
			}

			public void dataSourceChanged(DataSourceChangedEvent event) {
				disposeDelegate();
				createDelegate();
			}
		}

		return new Delegator();
	}

	protected IStaticContentProvider doGetContentProvider(String propertyPath) {
		ModelElement element = getModelElement(propertyPath);
		if (element == null) {
			return EmptyContentProvider.instance;
		}

		String localPropertyPath = getLocalPropertyPath(propertyPath);
		return element.getContentProvider(localPropertyPath);
	}

	/**
	 * Returns an ILabelProvider corresponding to the given property path
	 * The call to this method is delegated to the corresponding ModelElement
	 *
	 * @param propertyPath
	 *            The property path for which we want to retrieve an ILabelProvider
	 * @return
	 *         The ILabelProvider corresponding to the given propertyPath
	 */
	public ILabelProvider getLabelProvider(final String propertyPath) {
		class Delegator extends LabelProvider implements IDataSourceListener, ILabelProviderListener {
			private ILabelProvider delegate;

			private final CopyOnWriteArrayList<ILabelProviderListener> listeners = new CopyOnWriteArrayList<ILabelProviderListener>();

			{
				DataSource.this.addDataSourceListener(this);
			}

			@Override
			public void dispose() {
				disposeDelegate();
				super.dispose();
			}

			private void disposeDelegate() {
				if (delegate != null) {
					delegate.removeListener(this);
					delegate.dispose();
					delegate = null;
				}
			}

			public void dataSourceChanged(DataSourceChangedEvent event) {
				disposeDelegate();
			}

			@Override
			public void addListener(ILabelProviderListener listener) {
				listeners.addIfAbsent(listener);
			}

			@Override
			public void removeListener(ILabelProviderListener listener) {
				listeners.remove(listener);
			}

			public void labelProviderChanged(LabelProviderChangedEvent event) {
				if (!listeners.isEmpty()) {
					LabelProviderChangedEvent forward = new LabelProviderChangedEvent(this, event.getElements());
					for (ILabelProviderListener next : listeners) {
						try {
							next.labelProviderChanged(forward);
						} catch (Exception e) {
							Activator.log.error("Uncaught exception in label provider listener.", e); //$NON-NLS-1$
						}
					}
				}
			}

			ILabelProvider getDelegate() {
				if (delegate == null) {
					delegate = doGetLabelProvider(propertyPath);
					if (delegate == null) {
						delegate = new LabelProvider();
					}
					delegate.addListener(this);
				}

				return delegate;
			}

			@Override
			public Image getImage(Object element) {
				return getDelegate().getImage(element);
			}

			@Override
			public String getText(Object element) {
				return getDelegate().getText(element);
			}

			@Override
			public boolean isLabelProperty(Object element, String property) {
				return getDelegate().isLabelProperty(element, property);
			}
		}

		return new Delegator();
	}

	protected ILabelProvider doGetLabelProvider(String propertyPath) {
		ModelElement element = getModelElement(propertyPath);
		if (element == null) {
			return null;
		}
		String localPropertyPath = getLocalPropertyPath(propertyPath);
		return element.getLabelProvider(localPropertyPath);
	}

	/**
	 * Adds a change listener to this DataSource. The listener will be notified
	 * each time a change occurs on one of the IObservable produced by this DataSource
	 *
	 * @see DataSource#getObservable(String)
	 * @param listener
	 *            The Change listener
	 */
	public void addChangeListener(IChangeListener listener) {
		changeListeners.add(listener);
	}

	/**
	 * Removes a change listener from this DataSource.
	 *
	 * @param listener
	 *            The listener to remove
	 * @see DataSource#addChangeListener(IChangeListener)
	 */
	public void removeChangeListener(IChangeListener listener) {
		changeListeners.remove(listener);
	}

	public void addDataSourceListener(IDataSourceListener listener) {
		dataSourceListeners.add(listener);
	}

	public void removeDataSourceListener(IDataSourceListener listener) {
		dataSourceListeners.remove(listener);
	}

	public void handleChange(ChangeEvent event) {
		Object[] listeners = changeListeners.getListeners();
		for (int i = 0; i < listeners.length; i++) {
			try {
				((IChangeListener) listeners[i]).handleChange(event);
			} catch (Exception e) {
				Activator.log.error("Uncaught exception in observable change listener.", e); //$NON-NLS-1$
			}
		}
	}

	protected void fireDataSourceChanged() {
		Object[] listeners = dataSourceListeners.getListeners();
		if (listeners.length > 0) {
			DataSourceChangedEvent event = new DataSourceChangedEvent(this);
			for (int i = 0; i < listeners.length; i++) {
				try {
					((IDataSourceListener) listeners[i]).dataSourceChanged(event);
				} catch (Exception e) {
					Activator.log.error("Uncaught exception in data-source listener.", e); //$NON-NLS-1$
				}
			}
		}
	}

	/**
	 * @return The view associated to this DataSource
	 */
	public View getView() {
		return view;
	}

	/**
	 * @return the selection associated to this DataSource
	 */
	public IStructuredSelection getSelection() {
		return selection;
	}

	/**
	 * @param selection
	 *            the selection to set
	 */
	public void setSelection(IStructuredSelection selection) {
		if (!selection.equals(this.selection)) {
			this.selection = selection;

			fireDataSourceChanged();
		}
	}

	/**
	 * @param propertyPath
	 * @return
	 *         true if the property represented by this propertyPath is ordered
	 */
	public boolean isOrdered(String propertyPath) {
		ModelElement element = getModelElement(propertyPath);
		if (element == null) {
			return false;
		}
		return element.isOrdered(getLocalPropertyPath(propertyPath));
	}

	/**
	 * @param propertyPath
	 * @return
	 *         true if the property represented by this propertyPath is unique
	 */
	public boolean isUnique(String propertyPath) {
		ModelElement element = getModelElement(propertyPath);
		if (element == null) {
			return false;
		}
		return element.isUnique(getLocalPropertyPath(propertyPath));
	}

	/**
	 * @param propertyPath
	 * @return
	 *         true if the property represented by this propertyPath is mandatory
	 */
	public boolean isMandatory(String propertyPath) {
		ModelElement element = getModelElement(propertyPath);
		if (element == null) {
			return false;
		}
		return element.isMandatory(getLocalPropertyPath(propertyPath));
	}

	/**
	 * @param propertyPath
	 * @return
	 *         true if the property represented by this propertyPath is editable
	 */
	public boolean isEditable(String propertyPath) {
		ModelElement element = getModelElement(propertyPath);
		if (element == null) {
			return false;
		}
		return element.isEditable(getLocalPropertyPath(propertyPath));
	}

	/**
	 * Returns true if the given property should be refresh each time a change
	 * occurs in the property view. May help when the IObservable doesn't
	 * catch some change events (For example, for some Ecore derived
	 * properties).
	 *
	 * @param propertyPath
	 * @return true if the refresh should be forced
	 */
	public boolean forceRefresh(String propertyPath) {
		ModelElement element = getModelElement(propertyPath);
		if (element == null) {
			return false;
		}
		return element.forceRefresh(getLocalPropertyPath(propertyPath));
	}

	/**
	 * Return the value factory associated to the given path. May be null
	 *
	 * @param propertyPath
	 *            The property path to lookup
	 * @return
	 *         The factory used to edit and/or instantiate values for this property path
	 */
	public ReferenceValueFactory getValueFactory(String propertyPath) {
		ModelElement element = getModelElement(propertyPath);
		if (element == null) {
			return null;
		}
		return element.getValueFactory(getLocalPropertyPath(propertyPath));
	}

	/**
	 * Return the default value for the given property path
	 *
	 * @param propertyPath
	 * @return
	 *         The default value for the given property
	 */
	public Object getDefaultValue(String propertyPath) {
		ModelElement element = getModelElement(propertyPath);
		if (element == null) {
			return null;
		}
		return element.getDefaultValue(getLocalPropertyPath(propertyPath));
	}

	/**
	 * Indicates if the widget should use the direct creation.
	 * The direct edition will disable the possibility to browse
	 * existing elements when the "add" button is pressed.
	 *
	 * This is essentially relevant for containment references : this method
	 * should return false if the widget should only allow creation of new
	 * elements.
	 *
	 * @param propertyPath
	 * @return
	 *         True if the widget should use the direct edition option for the given property
	 */
	public boolean getDirectCreation(String propertyPath) {
		ModelElement element = getModelElement(propertyPath);
		if (element == null) {
			return true;
		}
		return element.getDirectCreation(getLocalPropertyPath(propertyPath));
	}

	/**
	 * Disposes this data source.
	 * This will dispose all ModelElements and IObservable created by this DataSource
	 */
	public void dispose() {
		for (ModelElement element : elements.values()) {
			if (element != null) {
				element.dispose();
			}
		}
		elements.clear();
	}

	/**
	 * return the IValidator for a property path
	 *
	 * @param propertyPath
	 * @return
	 */
	public IValidator getValidator(String propertyPath) {
		ModelElement element = getModelElement(propertyPath);
		if (element == null) {
			return null;
		}
		return element.getValidator(getLocalPropertyPath(propertyPath));
	}

	/**
	 * return the NameResolutionHelper to use for completion
	 * 
	 * @param propertyPath
	 * @return
	 */
	public INameResolutionHelper getNameResolutionHelper(String propertyPath) {
		ModelElement element = getModelElement(propertyPath);
		if (element == null) {
			return null;
		}
		return element.getNameResolutionHelper(getLocalPropertyPath(propertyPath));
	}
	
	/**
	 * return the Papyrus Converter to convert the object to edit or display string and to find the object from a string
	 * 
	 * @param propertyPath
	 * @return
	 */
	public IPapyrusConverter getPapyrusConverter(String propertyPath){
		ModelElement element = getModelElement(propertyPath);
		if (element == null) {
			return null;
		}
		return element.getPapyrusConverter(getLocalPropertyPath(propertyPath));
	}
}

Back to the top