001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.util;
011:
012: import java.util.Arrays;
013: import java.util.Comparator;
014: import java.util.HashMap;
015: import java.util.Map;
016: import java.util.Set;
017:
018: import org.eclipse.jdt.core.ICompilationUnit;
019:
020: import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange;
021: import org.eclipse.ltk.core.refactoring.TextChange;
022:
023: /**
024: * A <code>TextChangeManager</code> manages associations between <code>ICompilationUnit</code>
025: * or <code>IFile</code> and <code>TextChange</code> objects.
026: */
027: public class TextChangeManager {
028:
029: private Map/*<ICompilationUnit, TextChange>*/fMap = new HashMap(10);
030:
031: private final boolean fKeepExecutedTextEdits;
032:
033: public TextChangeManager() {
034: this (false);
035: }
036:
037: public TextChangeManager(boolean keepExecutedTextEdits) {
038: fKeepExecutedTextEdits = keepExecutedTextEdits;
039: }
040:
041: /**
042: * Adds an association between the given compilation unit and the passed
043: * change to this manager.
044: *
045: * @param cu the compilation unit (key)
046: * @param change the change associated with the compilation unit
047: */
048: public void manage(ICompilationUnit cu, TextChange change) {
049: fMap.put(cu, change);
050: }
051:
052: /**
053: * Returns the <code>TextChange</code> associated with the given compilation unit.
054: * If the manager does not already manage an association it creates a one.
055: *
056: * @param cu the compilation unit for which the text buffer change is requested
057: * @return the text change associated with the given compilation unit.
058: */
059: public TextChange get(ICompilationUnit cu) {
060: TextChange result = (TextChange) fMap.get(cu);
061: if (result == null) {
062: result = new CompilationUnitChange(cu.getElementName(), cu);
063: result.setKeepPreviewEdits(fKeepExecutedTextEdits);
064: fMap.put(cu, result);
065: }
066: return result;
067: }
068:
069: /**
070: * Removes the <tt>TextChange</tt> managed under the given key
071: * <code>unit<code>.
072: *
073: * @param unit the key determining the <tt>TextChange</tt> to be removed.
074: * @return the removed <tt>TextChange</tt>.
075: */
076: public TextChange remove(ICompilationUnit unit) {
077: return (TextChange) fMap.remove(unit);
078: }
079:
080: /**
081: * Returns all text changes managed by this instance.
082: *
083: * @return all text changes managed by this instance
084: */
085: public TextChange[] getAllChanges() {
086: Set cuSet = fMap.keySet();
087: ICompilationUnit[] cus = (ICompilationUnit[]) cuSet
088: .toArray(new ICompilationUnit[cuSet.size()]);
089: // sort by cu name:
090: Arrays.sort(cus, new Comparator() {
091: public int compare(Object o1, Object o2) {
092: String name1 = ((ICompilationUnit) o1).getElementName();
093: String name2 = ((ICompilationUnit) o2).getElementName();
094: return name1.compareTo(name2);
095: }
096: });
097:
098: TextChange[] textChanges = new TextChange[cus.length];
099: for (int i = 0; i < cus.length; i++) {
100: textChanges[i] = (TextChange) fMap.get(cus[i]);
101: }
102: return textChanges;
103: }
104:
105: /**
106: * Returns all compilation units managed by this instance.
107: *
108: * @return all compilation units managed by this instance
109: */
110: public ICompilationUnit[] getAllCompilationUnits() {
111: return (ICompilationUnit[]) fMap.keySet().toArray(
112: new ICompilationUnit[fMap.keySet().size()]);
113: }
114:
115: /**
116: * Clears all associations between resources and text changes.
117: */
118: public void clear() {
119: fMap.clear();
120: }
121:
122: /**
123: * Returns if any text changes are managed for the specified compilation unit.
124: *
125: * @param cu the compilation unit
126: * @return <code>true</code> if any text changes are managed for the specified compilation unit and <code>false</code> otherwise
127: */
128: public boolean containsChangesIn(ICompilationUnit cu) {
129: return fMap.containsKey(cu);
130: }
131: }
|