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
|
/*****************************************************************************
* Copyright (c) 2013 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 (camille.letavernier@cea.fr) - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.uml.properties.widgets;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.papyrus.infra.emf.utils.HistoryUtil;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject;
import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
import org.eclipse.papyrus.infra.ui.emf.providers.EMFGraphicalContentProvider;
import org.eclipse.papyrus.infra.ui.emf.providers.EMFLabelProvider;
import org.eclipse.papyrus.infra.ui.emf.utils.ProviderHelper;
import org.eclipse.papyrus.uml.properties.Activator;
import org.eclipse.papyrus.uml.properties.creation.UMLPropertyEditorFactory;
import org.eclipse.papyrus.uml.tools.providers.UMLContainerContentProvider;
import org.eclipse.papyrus.uml.tools.providers.UMLContentProvider;
import org.eclipse.papyrus.uml.tools.providers.UMLFilteredLabelProvider;
import org.eclipse.papyrus.uml.tools.providers.UMLLabelProvider;
import org.eclipse.papyrus.views.properties.modelelement.DataSource;
import org.eclipse.papyrus.views.properties.providers.FeatureContentProvider;
import org.eclipse.papyrus.views.properties.widgets.AbstractPropertyEditor;
import org.eclipse.papyrus.views.properties.widgets.layout.PropertiesLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
/**
* A Property Editor to display an instance of DataType
* TODO: It could actually be used as a generic EObject property editor
*
* @author Camille Letavernier
*
*/
public class GenericUMLDatatypeEditor extends AbstractPropertyEditor {
protected Composite self;
public GenericUMLDatatypeEditor(Composite parent, int style) {
super();
self = new Composite(parent, SWT.NONE);
self.setLayout(new PropertiesLayout(false));
}
@Override
public void setProperty(String path) {
this.propertyPath = path;
// Do not check input, to avoid calling doBinding() twice
}
/**
* This widget is used to edit a full EObject. It does not need a property, nor a ModelElement.
*
* It retrieves all the EStructuralFeatures reflexively
*/
@Override
protected void checkInput() {
if (input != null) {
try {
doBinding();
} catch (Exception ex) {
// TODO : Handle the exception here. Display something ?
Activator.log.error(ex);
}
}
}
/**
* This widget is used to edit a full object. It does not need a property.
*/
@Override
protected void doBinding() {
DataSource input = getInput();
IStructuredSelection selection = input.getSelection();
if (selection.isEmpty()) {
return;
}
Object element = selection.getFirstElement();
if (element instanceof EObject) {
EObject dataTypeInstance = ((EObject) element);
EClass dataTypeDefinition = dataTypeInstance.eClass();
ILabelProvider labelProvider;
try {
labelProvider = ServiceUtilsForEObject.getInstance().getService(LabelProviderService.class, dataTypeInstance).getLabelProvider();
} catch (Exception ex) {
Activator.log.error(ex);
labelProvider = new UMLLabelProvider();
}
for (EStructuralFeature feature : dataTypeDefinition.getEAllStructuralFeatures()) {
EStructuralFeatureEditor propertyEditor = new EStructuralFeatureEditor(self, SWT.NONE);
propertyEditor.setProviders(new UMLContentProvider(dataTypeInstance, feature), labelProvider);
if (feature instanceof EReference) {
propertyEditor.setValueFactory(getUMLPropertyEditorFactory(dataTypeInstance, (EReference) feature));
}
propertyEditor.setFeatureToEdit(feature.getName(), feature, dataTypeInstance);
}
}
}
protected UMLPropertyEditorFactory getUMLPropertyEditorFactory(EObject dataTypeInstance, EReference reference) {
UMLPropertyEditorFactory factory = new UMLPropertyEditorFactory(reference);
EClass type = reference.getEReferenceType();
factory.setContainerLabelProvider(new UMLFilteredLabelProvider());
factory.setReferenceLabelProvider(new EMFLabelProvider());
ITreeContentProvider contentProvider = new UMLContainerContentProvider(dataTypeInstance, reference);
EMFGraphicalContentProvider provider = ProviderHelper.encapsulateProvider(contentProvider, dataTypeInstance.eResource().getResourceSet(), HistoryUtil.getHistoryID(dataTypeInstance, reference, "container"));
factory.setContainerContentProvider(provider);
factory.setReferenceContentProvider(new FeatureContentProvider(type));
return factory;
}
}
|