Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aa31d913d32b92e1ba5dcbccbfcaef6302fbfd1c (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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*****************************************************************************
 * 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:
 *  <a href="mailto:thomas.szadel@atosorigin.com">Thomas Szadel</a> - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.navigator.internal.ltk;

import static org.eclipse.papyrus.navigator.internal.Activator.log;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.resource.RenameResourceChange;
import org.eclipse.papyrus.core.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.core.services.ServiceException;
import org.eclipse.papyrus.core.utils.DiResourceSet;
import org.eclipse.papyrus.core.utils.EditorUtils;
import org.eclipse.papyrus.resource.sasheditor.DiModelUtils;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.part.FileEditorInput;

/**
 * Rename the model.<BR>
 * <b>Note</b>: That change should be called inside a rename operation as it assumes that a {@link RenameResourceChange} occured.
 * 
 * @author tszadel
 * 
 */
public class RenameModelChange extends Change {

	private final Map<URI, URI> uriMap = new HashMap<URI, URI>();

	private final IFile oldFile;

	private final IFile newFile;

	private final Set<IResource> relatedFiles;

	/**
	 * Constructor.
	 * 
	 * @param resourceSet
	 *        The resource set being changed.
	 * @param oldFile
	 *        The old file.
	 * @param newFile
	 *        The new file.
	 */
	public RenameModelChange(IFile oldFile, IFile newFile) {
		this.oldFile = oldFile;
		this.newFile = newFile;

		IPath newPathWithoutExt = newFile.getFullPath().removeFileExtension();

		// Create the map of URI that are being modified in the resource set
		relatedFiles = ModelParticipantHelpers.getRelatedFiles(oldFile);
		relatedFiles.add(oldFile);
		for(IResource file : relatedFiles) {
			IPath path = file.getFullPath();
			URI oldURI = getPlatformURI(path);
			URI newURI = getPlatformURI(newPathWithoutExt.addFileExtension(path.getFileExtension()));
			uriMap.put(oldURI, newURI);
		}
	}

	/**
	 * Overrides getModifiedElement.
	 * 
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.ltk.core.refactoring.Change#getModifiedElement()
	 */
	@Override
	public Object getModifiedElement() {
		return oldFile;
	}

	/**
	 * Overrides getName.
	 * 
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.ltk.core.refactoring.Change#getName()
	 */
	@Override
	public String getName() {
		return "Update all resources related to " + oldFile.getName();
	}

	/**
	 * Overrides initializeValidationData.
	 * 
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.ltk.core.refactoring.Change#initializeValidationData(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void initializeValidationData(IProgressMonitor pm) {
		// Nothing
	}

	/**
	 * Overrides isValid.
	 * 
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.ltk.core.refactoring.Change#isValid(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException {
		return new RefactoringStatus();
	}

	/**
	 * Get a platform resource URI of the given path
	 * 
	 * @param path
	 *        the path
	 * @return the uri
	 */
	private URI getPlatformURI(IPath path) {
		return URI.createPlatformResourceURI(path.toString(), true);
	}


	/**
	 * Overrides perform.
	 * 
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.ltk.core.refactoring.Change#perform(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public Change perform(IProgressMonitor pm) throws CoreException {
		String lMsg = "Rename " + oldFile.getName() + " to " + newFile.getName();
		log.info(lMsg);
		pm.beginTask(lMsg, 30);
		try {
			// That change assumes that the model resource has already been renamed
			// So, the first thing to do is to get the new file, to restore it in order
			// to change all the resources.
			pm.subTask("Loading EMF model into memory");

			boolean isUndoOperation = oldFile.exists() && !newFile.exists();
			if(!isUndoOperation) {
				newFile.move(oldFile.getFullPath(), true, new SubProgressMonitor(pm, 1));
			}

			pm.subTask("Saving dirty editors");
			// We need to get the current workbench... so we have to use the UI-Thread!
			final List<IMultiDiagramEditor> openedEditors = new ArrayList<IMultiDiagramEditor>();
			Display.getDefault().syncExec(new Runnable() {

				public void run() {
					IMultiDiagramEditor[] multiEditors = EditorUtils.getRelatedEditors(oldFile);
					if(multiEditors != null) {
						for(IMultiDiagramEditor editor : multiEditors) {
							if(editor.isDirty()) {
								editor.doSave(new NullProgressMonitor());
							}
							openedEditors.add(editor);
						}
					}
				}
			});
			pm.worked(10);

			// Then, we can load the resource set as the file structure is now correct
			DiResourceSet resourceSet = new DiResourceSet();
			resourceSet.loadResources(oldFile);
			// Force EMF resolve and load all the resources
			EcoreUtil.resolveAll(resourceSet);
			pm.worked(4);

			// Change the URIs of modified resources
			pm.subTask("Modifying resources' URI");
			for(Resource res : resourceSet.getResources()) {
				URI newURI = uriMap.get(res.getURI());
				if(newURI != null) {
					if(log.isDebugEnabled()) {
						log.debug("Changing URI " + res.getURI() + " into " + newURI);
					}
					res.setURI(newURI);
				}
			}
			pm.worked(5);

			// Now, save all the resources
			pm.subTask("Saving resources");
			for(Resource res : resourceSet.getResources()) {
				try {
					res.save(null);
				} catch (IOException e) {
					log.error("Error while loading resource " + res.getURI(), e);
				}
			}
			pm.worked(5);

			// Do not forget to unload all the resources to avoid memory leak
			pm.subTask("Unloading model");
			resourceSet.unload();
			pm.worked(1);

			// Now, notify the editor of the change
			if(!openedEditors.isEmpty()) {
				Display.getDefault().syncExec(new Runnable() {

					public void run() {
						// Get the DI file as the rename could occur on any model's file.
						IFile newDiFile = DiModelUtils.getRelatedDiFile(newFile);
						for(IMultiDiagramEditor editor : openedEditors) {
							try {
								DiResourceSet diRes = editor.getServicesRegistry().getService(DiResourceSet.class);
								if(diRes != null) {
									diRes.saveAs(newFile.getFullPath());
								}
								editor.setEditorInput(new FileEditorInput(newDiFile));

							} catch (ServiceException e) {
								log.error(e);
							} catch (IOException e) {
								log.error(e);
							}
						}
					}
				});
			}

			// Then, remove the old model files
			pm.subTask("Removing old files");
			for(IResource file : relatedFiles) {
				if(file.exists()) {
					file.delete(true, new NullProgressMonitor());
				}
			}
			pm.worked(4);

			if(isUndoOperation) {
				newFile.move(oldFile.getFullPath(), true, new SubProgressMonitor(pm, 1));
			}

			return new RenameModelChange(newFile, oldFile);
		} finally {
			pm.done();
		}
	}
}

Back to the top