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
|
/*****************************************************************************
* Copyright (c) 2009 Atos Origin.
*
*
* 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:
* Emilien Perico (Atos Origin) emilien.perico@atosorigin.com - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.model;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.papyrus.infra.core.resource.IModel;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
import org.eclipse.papyrus.infra.core.utils.ServiceUtilsForActionHandlers;
import org.eclipse.papyrus.infra.gmfdiag.common.Activator;
import org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramUtils;
/**
* Utilities method to manage notation models. Should be moved in a more
* suitable plugin
*/
public class NotationUtils {
/**
* Get the notation Resource.
*
* @return
*
* @deprecated Usage of the internal Resource is discouraged.
*/
@Deprecated
public static Resource getNotationResource() {
return getNotationModel().getResource();
}
/**
* Gets the NotationModel for the currently selected editor. <br>
* Warning: This method is designed to be call from ui.handlers. It is not
* designed to be call from Editors. This method can return null if called
* during the MultiEditor initialization.
*
* @see ServiceUtilsForActionHandlers.getInstance().getModelSet()
*
* @return The {@link NotationModel} of the current editor, or null if not
* found.
*/
public static NotationModel getNotationModel() {
try {
return (NotationModel)ServiceUtilsForActionHandlers.getInstance().getModelSet().getModel(NotationModel.MODEL_ID);
} catch (ServiceException e) {
return null;
}
}
/**
* Gets the NotationModel for the currently selected editor. <br>
* Warning: This method is designed to be call from ui.handlers. It is not
* designed to be call from Editors. This method can return null if called
* during the MultiEditor initialization.
*
* @see ServiceUtilsForActionHandlers.getInstance().getModelSet()
*
* @return The {@link NotationModel} of the current editor, or null if not
* found.
* @throws ServiceException
* If an error occurs while getting or starting the service.
*/
public static NotationModel getNotationModelChecked() throws ServiceException {
return (NotationModel)ServiceUtilsForActionHandlers.getInstance().getModelSet().getModel(NotationModel.MODEL_ID);
}
/**
* Gets the NotationModel from the {@link ModelSet}. <br>
*
* @param modelsManager
* The modelManager containing the requested model.
*
* @return The {@link NotationModel} registered in modelManager, or null if
* not found.
*/
public static NotationModel getNotationModel(ModelSet modelsManager) {
return (NotationModel)modelsManager.getModel(NotationModel.MODEL_ID);
}
/**
* Gets the direct associated diagram of the specified eObject.
*
* @param eObject
* @param notationResource
*
* @return the associated diagram
*/
public static Diagram getAssociatedDiagram(Resource notationResource, EObject eObject) {
if(notationResource != null) {
for(EObject obj : notationResource.getContents()) {
if(obj instanceof Diagram) {
Diagram diagram = (Diagram)obj;
if (DiagramUtils.getOwner(diagram) == eObject)
return diagram;
}
}
}
return null;
}
/**
* Gets the direct associated diagram of the specified eObject.
*
* @param eObject
* @param notationResource
* @param resolve
* the resource if true
*
* @return the associated diagram
*/
public static Diagram getAssociatedDiagram(Resource notationResource, EObject eObject, boolean resolve) {
if(notationResource != null && resolve) {
EcoreUtil.resolveAll(notationResource);
}
return getAssociatedDiagram(notationResource, eObject);
}
/**
* Gets the all the diagrams contained in the specified ancestor eObject
*
* @param notationResource
* @param eObject
*
* @return all the contained diagrams
*
*/
public static List<Diagram> getDiagrams(Resource notationResource, EObject eObject) {
List<Diagram> diagrams = new LinkedList<Diagram>();
if(notationResource != null) {
for(EObject obj : notationResource.getContents()) {
if(obj instanceof Diagram) {
Diagram diagram = (Diagram)obj;
if (EcoreUtil.isAncestor(eObject, DiagramUtils.getOwner(diagram)))
diagrams.add(diagram);
}
}
}
return diagrams;
}
/**
* Gets the all the diagrams contained in the specified ancestor eObject
*
* @param notationResource
* @param eObject
* @param resolve
* the resource if true
*
* @return all the contained diagrams
*/
public static List<Diagram> getDiagrams(Resource notationResource, EObject eObject, boolean resolve) {
if(notationResource != null && resolve) {
EcoreUtil.resolveAll(notationResource);
}
return getDiagrams(notationResource, eObject);
}
/**
* Helper to retrieve the Notation resource associated to a ModelSet. May be null.
*
* @param from
* @return
*/
public static Resource getNotationResource(ModelSet from) {
IModel notationModel = from.getModel(NotationModel.MODEL_ID);
if(notationModel instanceof NotationModel) {
return ((NotationModel)notationModel).getResource();
}
return null;
}
/**
* Helper to retrieve the Notation resource associated to a ServicesRegistry. May be null.
*
* @param from
* @return
*/
public static Resource getNotationResource(ServicesRegistry registry) {
try {
ModelSet modelSet = ServiceUtils.getInstance().getModelSet(registry);
return getNotationResource(modelSet);
} catch (ServiceException ex) {
Activator.log.error(ex);
return null;
}
}
}
|