Lines 10-40
Link Here
|
10 |
*/ |
10 |
*/ |
11 |
package org.eclipse.gmf.internal.graphdef.util; |
11 |
package org.eclipse.gmf.internal.graphdef.util; |
12 |
|
12 |
|
|
|
13 |
import java.util.ArrayList; |
14 |
import java.util.Collection; |
13 |
import java.util.HashMap; |
15 |
import java.util.HashMap; |
|
|
16 |
import java.util.Iterator; |
14 |
import java.util.Map; |
17 |
import java.util.Map; |
15 |
|
18 |
|
16 |
import org.eclipse.emf.ecore.EObject; |
19 |
import org.eclipse.emf.ecore.EObject; |
17 |
import org.eclipse.emf.ecore.EReference; |
20 |
import org.eclipse.emf.ecore.EReference; |
18 |
import org.eclipse.emf.ecore.EStructuralFeature; |
21 |
import org.eclipse.emf.ecore.EStructuralFeature; |
19 |
import org.eclipse.emf.ecore.resource.Resource; |
22 |
import org.eclipse.emf.ecore.resource.Resource; |
20 |
import org.eclipse.emf.ecore.resource.impl.ResourceImpl; |
23 |
import org.eclipse.emf.ecore.util.EcoreUtil; |
21 |
import org.eclipse.gmf.gmfgraph.ChildAccess; |
24 |
import org.eclipse.gmf.gmfgraph.ChildAccess; |
22 |
import org.eclipse.gmf.gmfgraph.Compartment; |
25 |
import org.eclipse.gmf.gmfgraph.Compartment; |
|
|
26 |
import org.eclipse.gmf.gmfgraph.CustomFigure; |
27 |
import org.eclipse.gmf.gmfgraph.DecorationFigure; |
23 |
import org.eclipse.gmf.gmfgraph.DiagramElement; |
28 |
import org.eclipse.gmf.gmfgraph.DiagramElement; |
24 |
import org.eclipse.gmf.gmfgraph.DiagramLabel; |
29 |
import org.eclipse.gmf.gmfgraph.DiagramLabel; |
25 |
import org.eclipse.gmf.gmfgraph.Figure; |
30 |
import org.eclipse.gmf.gmfgraph.Figure; |
|
|
31 |
import org.eclipse.gmf.gmfgraph.FigureAccessor; |
26 |
import org.eclipse.gmf.gmfgraph.FigureDescriptor; |
32 |
import org.eclipse.gmf.gmfgraph.FigureDescriptor; |
27 |
import org.eclipse.gmf.gmfgraph.FigureGallery; |
33 |
import org.eclipse.gmf.gmfgraph.FigureGallery; |
28 |
import org.eclipse.gmf.gmfgraph.GMFGraphFactory; |
34 |
import org.eclipse.gmf.gmfgraph.GMFGraphFactory; |
29 |
import org.eclipse.gmf.gmfgraph.GMFGraphPackage; |
35 |
import org.eclipse.gmf.gmfgraph.GMFGraphPackage; |
30 |
import org.eclipse.gmf.gmfgraph.Node; |
36 |
import org.eclipse.gmf.gmfgraph.PolylineConnection; |
31 |
import org.eclipse.gmf.gmfgraph.RealFigure; |
37 |
import org.eclipse.gmf.gmfgraph.RealFigure; |
32 |
import org.eclipse.gmf.internal.common.migrate.MigrationDelegateImpl; |
38 |
import org.eclipse.gmf.internal.common.migrate.MigrationDelegateImpl; |
33 |
|
39 |
|
34 |
class MigrationDelegate extends MigrationDelegateImpl { |
40 |
class MigrationDelegate extends MigrationDelegateImpl { |
35 |
private EReference myFigure_RefElements; |
41 |
private EReference myFigure_RefElements; |
36 |
|
42 |
private Map<String, EObject> myId2EObject; |
37 |
//private FigureGallery myDefaultCreatedFigureGallery; |
43 |
private boolean isPostprocessingFinished; |
|
|
44 |
private Collection<EObject> myProxiesToResolve; |
38 |
|
45 |
|
39 |
MigrationDelegate() { |
46 |
MigrationDelegate() { |
40 |
} |
47 |
} |
Lines 45-62
Link Here
|
45 |
|
52 |
|
46 |
registerDeletedAttributes(GMFGraphPackage.eINSTANCE.getCustomClass(), "bundleName"); //$NON-NLS-1$ |
53 |
registerDeletedAttributes(GMFGraphPackage.eINSTANCE.getCustomClass(), "bundleName"); //$NON-NLS-1$ |
47 |
|
54 |
|
48 |
myFigure_RefElements = createNewReference("referencingElements", GMFGraphPackage.eINSTANCE.getDiagramElement(), false); //$NON-NLS-1$ |
55 |
myFigure_RefElements = createNewReference("referencingElementsFake", GMFGraphPackage.eINSTANCE.getDiagramElement(), false); //$NON-NLS-1$ |
49 |
registerRenamedParentAttribute(GMFGraphPackage.eINSTANCE.getFigure(), "referencingElements", myFigure_RefElements); //$NON-NLS-1$ |
56 |
registerRenamedParentAttribute(GMFGraphPackage.eINSTANCE.getFigure(), "referencingElements", myFigure_RefElements); //$NON-NLS-1$ |
|
|
57 |
registerRenamedAttribute(GMFGraphPackage.eINSTANCE.getFigureAccessor(), "referencingElements", myFigure_RefElements); //$NON-NLS-1$ |
58 |
|
59 |
myId2EObject = null; |
60 |
isPostprocessingFinished = false; |
50 |
} |
61 |
} |
51 |
|
62 |
|
52 |
@Override |
63 |
@Override |
53 |
public boolean setValue(EObject object, EStructuralFeature feature, Object value, int position) { |
64 |
public boolean setValue(EObject object, EStructuralFeature feature, Object value, int position) { |
54 |
if (myFigure_RefElements.equals(feature)) { |
65 |
// during load |
|
|
66 |
|
67 |
if (object instanceof Figure && "name".equals(feature.getName())) { |
68 |
// this feature used to be id in old versions, so need to emulate this during processing |
69 |
String name = (String) value; |
70 |
saveEObjectIdLocally(object, name); |
71 |
} |
72 |
|
73 |
// after end of document, handling forward references: |
74 |
|
75 |
if (GMFGraphPackage.eINSTANCE.getFigureAccessor_TypedFigure().equals(feature) && value instanceof CustomFigure) { |
76 |
CustomFigure custom = (CustomFigure) value; |
77 |
if (custom.eIsProxy()) { |
78 |
// this could happen in case of having "href" attribute, i.e. |
79 |
// in the case of reference, that now has changed to be containment |
80 |
saveReferenceToGetContainmentLater(custom); |
81 |
} |
82 |
} |
83 |
if (myFigure_RefElements.equals(feature) && object instanceof Figure) { |
55 |
Figure figure = (Figure) object; // can be FigureRef as well |
84 |
Figure figure = (Figure) object; // can be FigureRef as well |
|
|
85 |
FigureDescriptor figureDescriptor = figure.getDescriptor(); |
56 |
RealFigure topLevel = findTopLevelFigure(figure); |
86 |
RealFigure topLevel = findTopLevelFigure(figure); |
57 |
FigureDescriptor figureDescriptor = getOrCreateFigureDescriptorFor(topLevel); |
87 |
if (figureDescriptor == null) { |
58 |
if (figure instanceof RealFigure) { //XXX!!! think smth out! |
88 |
figureDescriptor = getOrCreateFigureDescriptorFor(topLevel); |
59 |
replaceFigureIdWithDescriptorIdInResolve(figureDescriptor, ((RealFigure) figure).getName()); |
|
|
60 |
} |
89 |
} |
61 |
if (!figure.equals(topLevel)) { |
90 |
if (!figure.equals(topLevel)) { |
62 |
ChildAccess access = getOrCreateChildAccessForNested(figure, figureDescriptor); |
91 |
ChildAccess access = getOrCreateChildAccessForNested(figure, figureDescriptor); |
Lines 64-69
Link Here
|
64 |
setNestedFigureAccessFor(node, access); |
93 |
setNestedFigureAccessFor(node, access); |
65 |
} |
94 |
} |
66 |
fireMigrationApplied(true); |
95 |
fireMigrationApplied(true); |
|
|
96 |
} else if (myFigure_RefElements.equals(feature) && object instanceof FigureAccessor) { |
97 |
FigureAccessor accessor = (FigureAccessor) object; |
98 |
CustomFigure customFigure = findParentCustomFigure(accessor); |
99 |
RealFigure figure = getOrCreateTypedFigure(accessor); |
100 |
FigureDescriptor figureDescriptor = figure.getDescriptor(); |
101 |
RealFigure topLevel = findTopLevelFigure(customFigure); |
102 |
if (figureDescriptor == null) { |
103 |
figureDescriptor = getOrCreateFigureDescriptorFor(topLevel); |
104 |
} |
105 |
if (!figure.equals(topLevel)) { |
106 |
ChildAccess access = getOrCreateChildAccessForNested(figure, figureDescriptor); |
107 |
DiagramElement node = (DiagramElement) value; |
108 |
setNestedFigureAccessFor(node, access); |
109 |
} |
110 |
fireMigrationApplied(true); |
111 |
} else if (GMFGraphPackage.eINSTANCE.getDiagramElement_Figure().equals(feature) && value instanceof Figure) { |
112 |
// as long as we have added ids for figure references in postProcess(), the id used here |
113 |
// can be resolved either as Figure (old version) or FigureDescriptor(new ones) - and this |
114 |
// should happen ONLY for old versions (or due to our wrong id reference resolving with |
115 |
// myId2EObject map) |
116 |
Figure figure = (Figure) value; |
117 |
if (figure.eIsProxy()) { |
118 |
figure = (Figure) EcoreUtil.resolve(figure, object); |
119 |
} |
120 |
FigureDescriptor figureDescriptor = figure.getDescriptor(); |
121 |
if (figureDescriptor == null) { |
122 |
RealFigure topLevel = findTopLevelFigure(figure); |
123 |
figureDescriptor = getOrCreateFigureDescriptorFor(topLevel); |
124 |
} |
125 |
DiagramElement diagramElement = (DiagramElement) object; |
126 |
diagramElement.setFigure(figureDescriptor); |
127 |
fireMigrationApplied(true); |
128 |
} else if (GMFGraphPackage.eINSTANCE.getDiagramElement_Figure().equals(feature) && value instanceof FigureAccessor) { |
129 |
// as FigureAccess could be referenced before in DiagramLabels and Containments along with Figures, |
130 |
// but, since now being not a Figure, do not have a Descriptor, so we use its TypedFigure's one |
131 |
// (creating default one if none exists) - and this should happen ONLY for old versions (or due to our |
132 |
// wrong id reference resolving with myId2EObject map). |
133 |
FigureAccessor accessor = (FigureAccessor) value; |
134 |
if (accessor.eIsProxy()) { |
135 |
accessor = (FigureAccessor) EcoreUtil.resolve(accessor, object); |
136 |
} |
137 |
RealFigure figure = getOrCreateTypedFigure(accessor); |
138 |
FigureDescriptor figureDescriptor = figure.getDescriptor(); |
139 |
if (figureDescriptor == null) { |
140 |
CustomFigure customFigure = findParentCustomFigure(accessor); |
141 |
RealFigure topLevel = findTopLevelFigure(customFigure); |
142 |
figureDescriptor = getOrCreateFigureDescriptorFor(topLevel); |
143 |
} |
144 |
DiagramElement diagramElement = (DiagramElement) object; |
145 |
diagramElement.setFigure(figureDescriptor); |
146 |
ChildAccess access = getOrCreateChildAccessForNested(figure, figureDescriptor); |
147 |
setNestedFigureAccessFor(diagramElement, access); |
148 |
fireMigrationApplied(true); |
149 |
} else if (GMFGraphPackage.eINSTANCE.getPolylineConnection_SourceDecoration().equals(feature) && value instanceof FigureDescriptor) { |
150 |
// as in old version a name is used for id of referenced figure, and now we set this name |
151 |
// for its figure descriptor, if the figure is directly nested into it, - we could receive |
152 |
// an instanceof descriptor resolved for this id value |
153 |
FigureDescriptor figureDescriptor = (FigureDescriptor) value; |
154 |
// ClassCast here will signal us of our id resolving mistakes: |
155 |
DecorationFigure toplevelFigureInstead = (DecorationFigure) figureDescriptor.getActualFigure(); |
156 |
PolylineConnection connection = (PolylineConnection) object; |
157 |
connection.setSourceDecoration(toplevelFigureInstead); |
158 |
fireMigrationApplied(true); |
159 |
} else if (GMFGraphPackage.eINSTANCE.getPolylineConnection_TargetDecoration().equals(feature) && value instanceof FigureDescriptor) { |
160 |
// as in old version a name is used for id of referenced figure, and now we set this name |
161 |
// for its figure descriptor, if the figure is directly nested into it, - we could receive |
162 |
// an instanceof descriptor resolved for this id value |
163 |
FigureDescriptor figureDescriptor = (FigureDescriptor) value; |
164 |
// ClassCast here will signal us of our id resolving mistakes: |
165 |
DecorationFigure toplevelFigureInstead = (DecorationFigure) figureDescriptor.getActualFigure(); |
166 |
PolylineConnection connection = (PolylineConnection) object; |
167 |
connection.setTargetDecoration(toplevelFigureInstead); |
168 |
fireMigrationApplied(true); |
67 |
} else { |
169 |
} else { |
68 |
// other cases are would be processed as defaults |
170 |
// other cases are would be processed as defaults |
69 |
return super.setValue(object, feature, value, position); |
171 |
return super.setValue(object, feature, value, position); |
Lines 71-104
Link Here
|
71 |
return true; |
173 |
return true; |
72 |
} |
174 |
} |
73 |
|
175 |
|
|
|
176 |
@Override |
177 |
public boolean setManyReference(EObject object, EStructuralFeature feature, Object[] values) { |
178 |
return myFigure_RefElements.equals(feature); |
179 |
} |
180 |
|
181 |
@Override |
182 |
public void postProcess() { |
183 |
super.postProcess(); |
184 |
Resource resource = getResource(); |
185 |
// this is run BEFORE forward references handling |
186 |
for (Iterator<EObject> it=resource.getAllContents();it.hasNext();) { |
187 |
EObject next = it.next(); |
188 |
// we have changed metamodel structure, so URI-fragment references for Accessor could get |
189 |
// invalid after resolving references in postprocessing, so we need to save them to preserve |
190 |
if (next instanceof FigureAccessor) { |
191 |
FigureAccessor customAccessor = (FigureAccessor) next; |
192 |
String accessorUri = resource.getURIFragment(customAccessor); |
193 |
saveEObjectIdLocally(customAccessor, accessorUri); |
194 |
} |
195 |
} |
196 |
if (myId2EObject == null) { |
197 |
isPostprocessingFinished = true; |
198 |
return; |
199 |
} |
200 |
for (Iterator<Map.Entry<String, EObject>> it = myId2EObject.entrySet().iterator(); it.hasNext();) { |
201 |
Map.Entry<String, EObject> next = it.next(); |
202 |
String id = next.getKey(); |
203 |
EObject found = resource.getEObject(id); |
204 |
if (found != null) { |
205 |
next.setValue(found); |
206 |
} |
207 |
} |
208 |
if (myProxiesToResolve == null) { |
209 |
isPostprocessingFinished = true; |
210 |
return; |
211 |
} |
212 |
for (EObject proxy : myProxiesToResolve) { |
213 |
// these proxies are expected just to be references, used to be non-containment in the past, |
214 |
// and interpreted by loader as proxies because of having "href" attribute |
215 |
String last = EcoreUtil.getURI(proxy).lastSegment(); |
216 |
EObject saved = myId2EObject.get(last); |
217 |
if (proxy.eContainer() instanceof FigureAccessor) { |
218 |
FigureAccessor accessor = (FigureAccessor) proxy.eContainer(); |
219 |
// saved could be FigureDescriptor for migrated resources with proxy customFigure references |
220 |
if (saved instanceof RealFigure) { |
221 |
RealFigure copyOfResolved = (RealFigure) EcoreUtil.copy(saved); |
222 |
accessor.setTypedFigure(copyOfResolved); |
223 |
} else if (saved instanceof FigureDescriptor) { |
224 |
FigureDescriptor descriptor = (FigureDescriptor) saved; |
225 |
RealFigure copyOfResolved = (RealFigure) EcoreUtil.copy(descriptor.getActualFigure()); |
226 |
accessor.setTypedFigure(copyOfResolved); |
227 |
} |
228 |
} |
229 |
} |
230 |
isPostprocessingFinished = true; |
231 |
} |
232 |
|
233 |
@Override |
234 |
public EObject findEObject(String uriFragment) { |
235 |
if (!isPostprocessingFinished || myId2EObject == null) { |
236 |
return null; |
237 |
} |
238 |
EObject result = myId2EObject.get(uriFragment); |
239 |
return result; |
240 |
} |
241 |
|
242 |
private void saveEObjectIdLocally(EObject object, String id) { |
243 |
if (myId2EObject == null) { |
244 |
myId2EObject = new HashMap<String, EObject>(); |
245 |
} |
246 |
if (myId2EObject.get(id) == null) { |
247 |
myId2EObject.put(id, object); |
248 |
} else { |
249 |
// collision with figure name expected only in new versions, as old ones used this as id |
250 |
} |
251 |
} |
252 |
|
253 |
private void saveReferenceToGetContainmentLater(EObject custom) { |
254 |
if (myProxiesToResolve == null) { |
255 |
myProxiesToResolve = new ArrayList<EObject>(); |
256 |
} |
257 |
myProxiesToResolve.add(custom); |
258 |
} |
259 |
|
260 |
private RealFigure getOrCreateTypedFigure(FigureAccessor accessor) { |
261 |
RealFigure result = accessor.getTypedFigure(); |
262 |
if (result == null) { |
263 |
CustomFigure custom = GMFGraphFactory.eINSTANCE.createCustomFigure(); |
264 |
// @see org.eclipse.gmf.codegen/templates/xpt/diagram/editparts/TextAware.xpt::labelSetterFigureClassName |
265 |
custom.setQualifiedClassName("org.eclipse.draw2d.IFigure"); //$NON-NLS-1$ |
266 |
accessor.setTypedFigure(custom); |
267 |
result = custom; |
268 |
} |
269 |
return result; |
270 |
} |
271 |
|
272 |
private CustomFigure findParentCustomFigure(FigureAccessor accessor) { |
273 |
CustomFigure result = null; |
274 |
if (accessor.eContainer() instanceof CustomFigure) { |
275 |
result = (CustomFigure) accessor.eContainer(); |
276 |
} |
277 |
return result; |
278 |
} |
279 |
|
74 |
private void setNestedFigureAccessFor(DiagramElement dElem, ChildAccess access) { |
280 |
private void setNestedFigureAccessFor(DiagramElement dElem, ChildAccess access) { |
75 |
if (dElem instanceof DiagramLabel) { |
281 |
if (dElem instanceof DiagramLabel) { |
76 |
DiagramLabel label = (DiagramLabel) dElem; |
282 |
DiagramLabel label = (DiagramLabel) dElem; |
77 |
label.setAccessor(access); |
283 |
label.setAccessor(access); |
78 |
} else if (dElem instanceof Node) { |
|
|
79 |
Node node = (Node) dElem; |
80 |
node.setContentPane(access); |
81 |
} else if (dElem instanceof Compartment) { |
284 |
} else if (dElem instanceof Compartment) { |
82 |
Compartment bag = (Compartment) dElem; |
285 |
Compartment bag = (Compartment) dElem; |
83 |
bag.setAccessor(access); |
286 |
bag.setAccessor(access); |
84 |
} |
287 |
} |
85 |
} |
288 |
} |
86 |
|
289 |
|
87 |
private void replaceFigureIdWithDescriptorIdInResolve(EObject figure, String name) { |
|
|
88 |
//XXX: there is still problem with references for figures, when not only |
89 |
// figureDescriptors are needed. For ex., see PolylineConnection.targetDecoration |
90 |
Resource resource = getResource(); |
91 |
if (resource instanceof ResourceImpl) { |
92 |
ResourceImpl resourceImpl = (ResourceImpl) resource; |
93 |
Map<String, EObject> idMap = resourceImpl.getIntrinsicIDToEObjectMap(); |
94 |
if (idMap == null) { |
95 |
idMap = new HashMap<String, EObject>(); |
96 |
resourceImpl.setIntrinsicIDToEObjectMap(idMap); |
97 |
} |
98 |
idMap.put(name, figure); |
99 |
} |
100 |
} |
101 |
|
102 |
private RealFigure findTopLevelFigure(Figure figure) { |
290 |
private RealFigure findTopLevelFigure(Figure figure) { |
103 |
RealFigure result = figure instanceof RealFigure ? (RealFigure) figure : null; |
291 |
RealFigure result = figure instanceof RealFigure ? (RealFigure) figure : null; |
104 |
while (result.eContainer() instanceof RealFigure) { |
292 |
while (result.eContainer() instanceof RealFigure) { |
Lines 135-151
Link Here
|
135 |
} |
323 |
} |
136 |
return descriptor; |
324 |
return descriptor; |
137 |
} |
325 |
} |
138 |
|
|
|
139 |
// private FigureGallery getOrCreateFigureGalleryOnce(Figure figure) { |
140 |
// if (myDefaultCreatedFigureGallery == null) { |
141 |
// myDefaultCreatedFigureGallery = GMFGraphFactory.eINSTANCE.createFigureGallery(); |
142 |
// if (figure.eContainer() instanceof Canvas) { |
143 |
// Canvas canvas = (Canvas) figure.eContainer(); |
144 |
// canvas.getFigures().add(myDefaultCreatedFigureGallery); |
145 |
// } else { |
146 |
// figure.eResource().getContents().add(myDefaultCreatedFigureGallery); |
147 |
// } |
148 |
// } |
149 |
// return myDefaultCreatedFigureGallery; |
150 |
// } |
151 |
} |
326 |
} |