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
|
/*****************************************************************************
* Copyright (c) 2008 CEA LIST.
*
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.uml.diagram.clazz.custom.parsers;
import java.util.List;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParserEditStatus;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.papyrus.uml.diagram.clazz.parsers.MessageFormatParser;
public class CustomMessageFormatParser extends MessageFormatParser {
/** this is the ref ereference in order to obtain the obtain which feature will be displayed **/
private EStructuralFeature eRefFeature;
/** this is the index of object that we look for **/
private int objectIndex;
public CustomMessageFormatParser(EAttribute[] features, EStructuralFeature eRefFeature, int objectIndex) {
super(features);
this.eRefFeature = eRefFeature;
this.objectIndex = objectIndex;
}
/**
* {@inheritDoc}
*/
@Override
public String getEditString(IAdaptable adapter, int flags) {
IAdaptable reachedObject = obtainObject(adapter);
if (reachedObject != null) {
return super.getEditString(reachedObject, flags);
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
public ICommand getParseCommand(IAdaptable adapter, String newString, int flags) {
IAdaptable reachedObject = obtainObject(adapter);
if (reachedObject != null) {
return super.getParseCommand(reachedObject, newString, flags);
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
public String getPrintString(IAdaptable adapter, int flags) {
IAdaptable reachedObject = obtainObject(adapter);
if (reachedObject != null) {
return super.getPrintString(reachedObject, flags);
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
public IParserEditStatus isValidEditString(IAdaptable adapter, String editString) {
IAdaptable reachedObject = obtainObject(adapter);
if (reachedObject != null) {
return super.isValidEditString(reachedObject, editString);
}
return null;
}
/**
* this method returns the object that is link to the elemtn by an Ereference
*
* @param adapter
* the element where we look for the good element
* @return the associated element
*/
protected IAdaptable obtainObject(IAdaptable adapter) {
EObject element = (EObject) adapter.getAdapter(EObject.class);
Object result = element.eGet(eRefFeature);
// this is a collection
if (result instanceof List<?>) {
// the index is coherent
if (((List<?>) result).size() > objectIndex) {
EObject object = (EObject) ((List<?>) result).get(objectIndex);
return new EObjectAdapter(object);
}
// the index is not coherent
return null;
} else {
// this not a collection
if (objectIndex == 0) {
return new EObjectAdapter((EObject) result);
}
}
return null;
}
}
|