Skip to main content
summaryrefslogtreecommitdiffstats
blob: a1eda6f6f07ee983f589b649313bc8806565e59b (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
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.jsdt.internal.corext.refactoring.util;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.ltk.core.refactoring.TextEditBasedChange;
import org.eclipse.wst.jsdt.core.IJavaScriptUnit;
import org.eclipse.wst.jsdt.internal.corext.refactoring.changes.CompilationUnitChange;

/**
 * A <code>TextChangeManager</code> manages associations between <code>IJavaScriptUnit</code>
 * or <code>IFile</code> and <code>TextEditBasedChange</code> objects.
 */
public class TextEditBasedChangeManager {
	
	private Map/*<IJavaScriptUnit, TextEditBasedChange>*/ fMap= new HashMap(10);
	
	private final boolean fKeepExecutedTextEdits;
	
	public TextEditBasedChangeManager() {
		this(false);
	}

	public TextEditBasedChangeManager(boolean keepExecutedTextEdits) {
		fKeepExecutedTextEdits= keepExecutedTextEdits;
	}
	
	/**
	 * Adds an association between the given compilation unit and the passed
	 * change to this manager.
	 * 
	 * @param cu the compilation unit (key)
	 * @param change the change associated with the compilation unit
	 */
	public void manage(IJavaScriptUnit cu, TextEditBasedChange change) {
		fMap.put(cu, change);
	}
	
	/**
	 * Returns the <code>TextEditBasedChange</code> associated with the given compilation unit.
	 * If the manager does not already manage an association it creates a one.
	 * 
	 * @param cu the compilation unit for which the text buffer change is requested
	 * @return the text change associated with the given compilation unit. 
	 */
	public TextEditBasedChange get(IJavaScriptUnit cu) {
		TextEditBasedChange result= (TextEditBasedChange)fMap.get(cu);
		if (result == null) {
			result= new CompilationUnitChange(cu.getElementName(), cu);
			result.setKeepPreviewEdits(fKeepExecutedTextEdits);
			fMap.put(cu, result);
		}
		return result;
	}
	
	/**
	 * Removes the <tt>TextEditBasedChange</tt> managed under the given key
	 * <code>unit<code>.
	 * 
	 * @param unit the key determining the <tt>TextEditBasedChange</tt> to be removed.
	 * @return the removed <tt>TextEditBasedChange</tt>.
	 */
	public TextEditBasedChange remove(IJavaScriptUnit unit) {
		return (TextEditBasedChange)fMap.remove(unit);
	}
	
	/**
	 * Returns all text changes managed by this instance.
	 * 
	 * @return all text changes managed by this instance
	 */
	public TextEditBasedChange[] getAllChanges(){
		Set cuSet= fMap.keySet();
		IJavaScriptUnit[] cus= (IJavaScriptUnit[]) cuSet.toArray(new IJavaScriptUnit[cuSet.size()]);
		// sort by cu name:
		Arrays.sort(cus, new Comparator() {
			public int compare(Object o1, Object o2) {
				String name1= ((IJavaScriptUnit) o1).getElementName();
				String name2= ((IJavaScriptUnit) o2).getElementName();
				return name1.compareTo(name2);
			}
		});
		
		TextEditBasedChange[] textChanges= new TextEditBasedChange[cus.length];
		for (int i= 0; i < cus.length; i++) {
			textChanges[i]= (TextEditBasedChange) fMap.get(cus[i]);
		}
		return textChanges;
	}

	/**
	 * Returns all compilation units managed by this instance.
	 * 
	 * @return all compilation units managed by this instance
	 */	
	public IJavaScriptUnit[] getAllCompilationUnits(){
		return (IJavaScriptUnit[]) fMap.keySet().toArray(new IJavaScriptUnit[fMap.keySet().size()]);
	}
	
	/**
	 * Clears all associations between resources and text changes.
	 */
	public void clear() {
		fMap.clear();
	}

	/**
	 * Returns if any text changes are managed for the specified compilation unit.
	 * 
	 * @param cu the compilation unit
	 * @return <code>true</code> if any text changes are managed for the specified compilation unit and <code>false</code> otherwise
	 */		
	public boolean containsChangesIn(IJavaScriptUnit cu){
		return fMap.containsKey(cu);
	}
}

Back to the top