Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fd9e4d9bc596f1c71ae020c535cf24dced90d9d2 (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
/**
 * 
 */
package org.eclipse.papyrus.resource;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;


/**
 * Base class for models sharing a common {@link Resource}.
 * To share a common {@link Resource}, one of the model should be Master, while the other are slaves.
 * The Master is the one performing the save operation.
 * All the model should use the same file extension. So, {@link #getModelFileExtension()} should return 
 * the same value for all models.
 * 
 * @author cedric dumoulin
 *
 * @param T Type of the roots of the model.
 */
public abstract class AbstractModelWithSharedResource <T extends EObject> extends AbstractBaseModel {

	/**
	 * Possible type for this model: master or slave
	 */
    public enum ModelKind {master, slave}

    /**
     * Model kind.
     */
	private ModelKind modelKind;;
	
	/**
	 * 
	 * Constructor.
	 *
	 * @param modelKind
	 */
	public AbstractModelWithSharedResource(ModelKind modelKind) {
		this.modelKind = modelKind;
	}

	/**
	 * By default, we are a slave.
	 * Constructor.
	 *
	 * @param modelKind
	 */
	public AbstractModelWithSharedResource() {
		this.modelKind = ModelKind.slave;
	}

	/**
	 * Attach the model to its resource if this is not already done.
	 * @see org.eclipse.papyrus.resource.AbstractBaseModel#loadModel(org.eclipse.core.runtime.IPath)
	 *
	 * @param fullPathWithoutExtension
	 */
	@Override
	public void loadModel(IPath fullPathWithoutExtension) {
		
		// Look for the resource
		lookupResource(fullPathWithoutExtension);
		// Check if model is loaded.
		if(resourceIsSet())
			return;
		// model is not loaded, do it.
		super.loadModel(fullPathWithoutExtension);
	}

	/**
	 * Create the model if this is not already done.
	 * @see org.eclipse.papyrus.resource.AbstractBaseModel#createModel(org.eclipse.core.runtime.IPath)
	 *
	 * @param fullPath
	 */
	@Override
	public void createModel(IPath fullPath) {
		
		// Look for the resource
		lookupResource(fullPath);
		// Check if model is loaded.
		if(resourceIsSet())
			return;
		// model is not loaded, do it.
		super.createModel(fullPath);
	}
	
	/**
	 * Lookup for the resource in the resourceSet. Return the resource or null if not found.
	 * 
	 * @param fullPath
	 */
	private void lookupResource(IPath fullPath) {
		
		// Compute model URI
		resourceURI = getPlatformURI(fullPath.addFileExtension(getModelFileExtension()));
		
		resource = getResourceSet().getResource(resourceURI, false);
		
	}

	/**
	 * Do nothing as we are slave.
	 * The Resource is save by the master model.
	 * @see org.eclipse.papyrus.resource.AbstractBaseModel#saveModel()
	 *
	 * @throws IOException
	 */
	@Override
	public void saveModel() throws IOException {
		
		// Do nothing if we are a slave
		if( modelKind == ModelKind.slave)
			return;
		
		// Do the save
		super.saveModel();
	}
	
	/**
	 * Get the root of this model. Lookup in the associated {@link Resource} for the root.
	 * 
	 * @return The root of the model, or null if no root exist.
	 */
	@SuppressWarnings("unchecked")
	public T getModelRoot() {
		
		for( EObject object : getResource().getContents() )
		{
			
			if( isModelRoot(object) )
			{
				return (T)object;
			}
		}
		
		// Not found
		return null;
	}
	
	/**
	 * Get the roots of this model. Lookup in the associated {@link Resource} for the roots.
	 * 
	 * @return A list containing the roots of the model. The list is empty if there is no root.
	 */
	@SuppressWarnings("unchecked")
	public List<T> getModelRoots() {
		
		List<T> roots = new ArrayList<T>();
		
		for( EObject object : getResource().getContents() )
		{
			if( isModelRoot(object) )
				roots.add((T)object);
		}
		
		return roots;
	}
	
	/**
	 * Return true if the provided object is a root of the model, false otherwise.
	 * This method should be implemented by subclasses.
	 * 
	 * @param object
	 * @return
	 */
	protected abstract boolean isModelRoot(EObject object);

	/**
	 * Add a root to this model.
	 * 
	 * @param root
	 */
	public void addModelRoot(T root) {
		getResource().getContents().add(root);
	}
}

Back to the top