Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 09df6e70c39c1f1f74d100720c8c5c095a7ed4b9 (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
/*******************************************************************************
 * Copyright (c) 2008, 2009 Institute for Software, HSR Hochschule fuer Technik
 * Rapperswil, University of applied sciences 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:
 *     Institute for Software - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.ltk.core.refactoring.Change;

import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.rewrite.ASTRewrite;

/**
 * A ModificationCollector can be passed through a refactoring and manages the rewriters
 * and additional changes a refactoring can create.
 *
 * @author Mirko Stocker
 */
public class ModificationCollector {
	// Each translation unit can have only one ASTRewrite
	private final Map<IASTTranslationUnit, ASTRewrite> rewriters =
			new HashMap<IASTTranslationUnit, ASTRewrite>();

	private Collection<CreateFileChange> changes;

	public ASTRewrite rewriterForTranslationUnit(IASTTranslationUnit ast) {
		if (!rewriters.containsKey(ast)) {
			rewriters.put(ast, ASTRewrite.create(ast));
		}
		return rewriters.get(ast);
	}

	// Creating new files doesn't concern the rewriter, the refactorings can add them here as needed.
	public void addFileChange(CreateFileChange change) {
		if (changes == null) {
			changes = new ArrayList<CreateFileChange>();
		}
		changes.add(change);
	}

	public CCompositeChange createFinalChange() {
		// Synthetic changes aren't displayed and therefore don't need a name
		CCompositeChange result = new CCompositeChange(""); //$NON-NLS-1$
		result.markAsSynthetic();

		if (changes != null)
			result.addAll(changes.toArray(new Change[changes.size()]));

		for (ASTRewrite each : rewriters.values()) {
			result.add(each.rewriteAST());
		}

		return result;
	}
}

Back to the top