blob: 9ee222ffacac754b2badd8ec8241dda14563183a (
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
|
/*****************************************************************************
* Copyright (c) 2013, 2014 CEA LIST, Christian W. Damus, and others.
*
* 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 (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
* Christian W. Damus (CEA) - bug 437052
* Christian W. Damus - bug 399859
*
*****************************************************************************/
package org.eclipse.papyrus.infra.core.resource;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.papyrus.infra.core.Activator;
/**
* An IModel which is an abstraction for a set of consistent EMF Resources
*
* @author Camille Letavernier
*
*/
public abstract class EMFLogicalModel extends AbstractBaseModel implements IEMFModel {
protected final Set<Resource> resources = new HashSet<Resource>();
public Set<Resource> getResources() {
pruneDeletedResources();
return resources;
}
protected void pruneDeletedResources() {
ResourceSet rset = getModelManager();
for (Iterator<Resource> iter = resources.iterator(); iter.hasNext();) {
if (iter.next().getResourceSet() != rset) {
// This resource was deleted
iter.remove();
}
}
}
@Override
protected void configureResource(Resource resourceToConfigure) {
super.configureResource(resourceToConfigure);
if (resourceToConfigure != null) {
resources.add(resourceToConfigure);
}
}
protected boolean isRelatedResource(Resource resource) {
if (resource == null) {
return false;
}
return resource.getURI().fileExtension().equals(getModelFileExtension());
}
@Override
public void saveModel() throws IOException {
final ModelSet set = getModelManager();
for (Resource resource : getResources()) {
if (set.shouldSave(resource)) {
try {
resource.save(null);
} catch (IOException ex) {
// If an exception occurs, we should not prevent other resources from being saved.
// This would probably make things even worse. Catch and log.
Activator.log.error(ex);
}
}
}
}
@Override
public void handle(Resource resource) {
if (isRelatedResource(resource)) {
configureResource(resource);
}
}
@Override
public void unload() {
super.unload();
getResources().clear();
}
@Override
public void setModelURI(URI uriWithoutExtension) {
for (Resource resource : getResources()) {
if (isControlled(resource)) {
updateURI(resource, uriWithoutExtension);
}
}
super.setModelURI(uriWithoutExtension);
}
protected void updateURI(Resource resource, URI uriWithoutExtension) {
URI oldBaseURI = this.resource.getURI();
URI newBaseURI = uriWithoutExtension.appendFileExtension(getModelFileExtension());
URI currentFullURI = resource.getURI();
URI currentRelativeURI = currentFullURI.deresolve(oldBaseURI);
URI newFullURI = currentRelativeURI.resolve(newBaseURI);
resource.setURI(newFullURI);
}
@Override
public boolean isModelFor(Object element) {
if (element instanceof EObject) {
return resources.contains(((EObject) element).eResource());
}
return resources.contains(element);
}
}
|