Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 571fa864b0240e1291b826ed64ef7a2187390908 (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
/*******************************************************************************
 * Copyright (c) 2016, 2017 Obeo.
 * 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:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.sirius.properties.core.internal;

import org.eclipse.eef.common.api.utils.Util;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.sirius.ext.base.Option;
import org.eclipse.sirius.ext.emf.edit.EditingDomainServices;
import org.eclipse.sirius.properties.core.api.preferences.SiriusPropertiesCorePreferences;
import org.eclipse.sirius.properties.impl.EditSupportImpl;

/**
 * Contains the actual implementation of the EditSupport EOperations.
 * 
 * @author pcdavid
 */
public class EditSupportSpec extends EditSupportImpl {

    private static final String JAVA_LANG_STRING = "java.lang.String"; //$NON-NLS-1$

    private static final String INT = "int"; //$NON-NLS-1$

    private static final String JAVA_LANG_INTEGER = "java.lang.Integer"; //$NON-NLS-1$

    private static final String DOUBLE = "double"; //$NON-NLS-1$

    private static final String JAVA_LANG_DOUBLE = "java.lang.Double"; //$NON-NLS-1$

    private static final String CHAR = "char"; //$NON-NLS-1$

    private static final String JAVA_LANG_CHARACTER = "java.lang.Character"; //$NON-NLS-1$

    private static final String SHORT = "short"; //$NON-NLS-1$

    private static final String JAVA_LANG_SHORT = "java.lang.Short"; //$NON-NLS-1$

    private static final String LONG = "long"; //$NON-NLS-1$

    private static final String JAVA_LANG_LONG = "java.lang.Long"; //$NON-NLS-1$

    private static final String FLOAT = "float"; //$NON-NLS-1$

    private static final String JAVA_LANG_FLOAT = "java.lang.Float"; //$NON-NLS-1$

    private static final String JAVA_UTIL_DATE = "java.util.Date"; //$NON-NLS-1$

    private static final String BOOLEAN = "boolean"; //$NON-NLS-1$

    private static final String JAVA_LANG_BOOLEAN = "java.lang.Boolean"; //$NON-NLS-1$

    private final EditingDomainServices editServices = new EditingDomainServices();

    private final SiriusContext context;

    private final Object self;

    /**
     * Creates an instance configured to work on the specified target element as
     * implicit "self" for all operations.
     * 
     * @param ctx
     *            the context
     * @param target
     *            the target element.
     */
    public EditSupportSpec(SiriusContext ctx, Object target) {
        this.context = ctx;
        this.self = target;
    }

    /**
     * Returns the actual target model element, unwrapping the
     * {@link EEFViewCategory} if needed.
     * 
     * @return the actual target model element, or <code>null</code> if self is
     *         not an {@link EObject}.
     */
    private EObject getTargetEObject() {
        EObject result = null;
        if (self instanceof EObject) {
            result = (EObject) self;
        }
        return result;
    }

    @Override
    public Object getImage() {
        EObject target = getTargetEObject();
        if (target != null) {
            return this.editServices.getLabelProviderImage(target);
        } else {
            return null;
        }
    }

    @Override
    public String getText() {
        EObject target = getTargetEObject();
        if (target != null) {
            return this.editServices.getLabelProviderText(target);
        } else {
            return String.valueOf(self);
        }
    }

    @Override
    public Object getText(EStructuralFeature feature) {
        EObject target = getTargetEObject();
        if (target != null) {
            String result = this.editServices.getPropertyDescriptorDisplayName(target, feature.getName());
            if (Util.isBlank(result)) {
                result = this.editServices.getLabelProviderText(feature);
            }
            if (Util.isBlank(result)) {
                result = feature.getName();
            }
            return result;
        } else {
            return ""; //$NON-NLS-1$
        }
    }

    @Override
    public String getTabName() {
        EObject target = getTargetEObject();
        final String result;
        if (target != null) {
            Option<EObject> mainSemanticElement = context.getMainSemanticElement();
            if (mainSemanticElement.some() && mainSemanticElement.get().equals(target)) {
                result = Messages.SiriusToolServices_MainTabLabel;
            } else {
                result = this.editServices.getLabelProviderText(target);
            }
        } else {
            result = String.valueOf(target);
        }
        return elide(result, SiriusPropertiesCorePreferences.INSTANCE.getMaxLengthTabName());
    }

    private String elide(String s, int maxLength) {
        final String dots = "..."; //$NON-NLS-1$
        if (dots.length() <= maxLength && maxLength < s.length()) {
            return s.substring(0, maxLength - dots.length()) + dots;
        } else {
            return s;
        }
    }

    @Override
    public EList<Object> getChoiceOfValues(EStructuralFeature feature) {
        BasicEList<Object> result = new BasicEList<Object>();
        EObject target = getTargetEObject();
        if (target != null) {
            result.addAll(this.editServices.getPropertyDescriptorChoiceOfValues(target, feature.getName()));
        }
        return result;

    }

    @Override
    public boolean isMultiline(EStructuralFeature eStructuralFeature) {
        EObject target = getTargetEObject();
        if (target != null) {
            return this.editServices.isPropertyDescriptorMultiLine(target, eStructuralFeature.getName());
        } else {
            return false;
        }
    }

    @Override
    public String getDescription(EStructuralFeature eStructuralFeature) {
        EObject target = getTargetEObject();
        if (target != null) {
            return this.editServices.getPropertyDescriptorDescription(target, eStructuralFeature.getName());
        } else {
            return ""; //$NON-NLS-1$
        }
    }

    @Override
    public boolean needsTextWidget(EStructuralFeature eStructuralFeature) {
        boolean needsTextWidget = false;

        needsTextWidget = needsTextWidget || JAVA_LANG_STRING.equals(eStructuralFeature.getEType().getInstanceTypeName());
        needsTextWidget = needsTextWidget || INT.equals(eStructuralFeature.getEType().getInstanceTypeName());
        needsTextWidget = needsTextWidget || JAVA_LANG_INTEGER.equals(eStructuralFeature.getEType().getInstanceTypeName());
        needsTextWidget = needsTextWidget || DOUBLE.equals(eStructuralFeature.getEType().getInstanceTypeName());
        needsTextWidget = needsTextWidget || JAVA_LANG_DOUBLE.equals(eStructuralFeature.getEType().getInstanceTypeName());
        needsTextWidget = needsTextWidget || CHAR.equals(eStructuralFeature.getEType().getInstanceTypeName());
        needsTextWidget = needsTextWidget || JAVA_LANG_CHARACTER.equals(eStructuralFeature.getEType().getInstanceTypeName());
        needsTextWidget = needsTextWidget || SHORT.equals(eStructuralFeature.getEType().getInstanceTypeName());
        needsTextWidget = needsTextWidget || JAVA_LANG_SHORT.equals(eStructuralFeature.getEType().getInstanceTypeName());
        needsTextWidget = needsTextWidget || LONG.equals(eStructuralFeature.getEType().getInstanceTypeName());
        needsTextWidget = needsTextWidget || JAVA_LANG_LONG.equals(eStructuralFeature.getEType().getInstanceTypeName());
        needsTextWidget = needsTextWidget || FLOAT.equals(eStructuralFeature.getEType().getInstanceTypeName());
        needsTextWidget = needsTextWidget || JAVA_LANG_FLOAT.equals(eStructuralFeature.getEType().getInstanceTypeName());
        needsTextWidget = needsTextWidget || JAVA_UTIL_DATE.equals(eStructuralFeature.getEType().getInstanceTypeName());

        return needsTextWidget && !eStructuralFeature.isMany();
    }

    @Override
    public boolean needsCheckboxWidget(EStructuralFeature eStructuralFeature) {
        boolean needsCheckboxWidget = false;

        needsCheckboxWidget = needsCheckboxWidget || BOOLEAN.equals(eStructuralFeature.getEType().getInstanceTypeName());
        needsCheckboxWidget = needsCheckboxWidget || JAVA_LANG_BOOLEAN.equals(eStructuralFeature.getEType().getInstanceTypeName());

        return needsCheckboxWidget && !eStructuralFeature.isMany();
    }

    @Override
    public EList<EStructuralFeature> getEStructuralFeatures() {
        EList<EStructuralFeature> visibleFeatures = new BasicEList<>();
        for (EStructuralFeature eStructuralFeature : this.getTargetEObject().eClass().getEAllStructuralFeatures()) {
            if (!eStructuralFeature.isDerived() && !eStructuralFeature.isTransient() && !(eStructuralFeature instanceof EReference && ((EReference) eStructuralFeature).isContainment())) {
                visibleFeatures.add(eStructuralFeature);
            }
        }
        return visibleFeatures;
    }

    @Override
    public Object setValue(EStructuralFeature feature, Object newValue) {
        EObject target = getTargetEObject();
        if (target != null) {
            Object finalValue = newValue;
            if (feature instanceof EAttribute && newValue instanceof String) {
                finalValue = EcoreUtil.createFromString(((EAttribute) feature).getEAttributeType(), (String) newValue);
            }
            target.eSet(feature, finalValue);
        }
        return self;
    }

}

Back to the top