1
/*******************************************************************************
2
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
3
* Rapperswil, University of applied sciences and others
4
* All rights reserved. This program and the accompanying materials
5
* are made available under the terms of the Eclipse Public License v1.0
6
* which accompanies this distribution, and is available at
7
* http://www.eclipse.org/legal/epl-v10.html
10
* Institute for Software - initial API and implementation
11
*******************************************************************************/
13
package org.eclipse.cdt.internal.ui.refactoring;
15
import java.util.ArrayList;
16
import java.util.Collection;
17
import java.util.HashMap;
20
import org.eclipse.ltk.core.refactoring.Change;
21
import org.eclipse.ltk.core.refactoring.CompositeChange;
23
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
24
import org.eclipse.cdt.core.dom.rewrite.ASTRewrite;
27
* A ModificationCollector can be passed through a refactoring and manages the rewriters
28
* and additional changes a refactoring can create.
30
* @author Mirko Stocker
32
public class ModificationCollector {
34
// Each Translationunit can have only one ASTRewrite
35
private final Map<IASTTranslationUnit, ASTRewrite> rewriters = new HashMap<IASTTranslationUnit, ASTRewrite>();
37
private Collection<CreateFileChange> changes;
39
public ASTRewrite rewriterForTranslationUnit(IASTTranslationUnit unit) {
40
if(! rewriters.containsKey(unit)) {
41
rewriters.put(unit, ASTRewrite.create(unit));
44
return rewriters.get(unit);
47
// Creating new files doesn't concern the rewriter, the refactorings can add them here as needed.
48
public void addFileChange(CreateFileChange change) {
50
changes = new ArrayList<CreateFileChange>();
55
public CompositeChange createFinalChange() {
56
// Synthetic changes aren't displayed and therefore don't need a name
57
CompositeChange result = new CompositeChange(""); //$NON-NLS-1$
58
result.markAsSynthetic();
61
result.addAll(changes.toArray(new Change[changes.size()]));
63
for (ASTRewrite each : rewriters.values()) {
64
result.add(each.rewriteAST());