KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > util > TextEditBasedChangeManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring.util;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.Comparator JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.jdt.core.ICompilationUnit;
20
21 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange;
22 import org.eclipse.ltk.core.refactoring.TextEditBasedChange;
23
24 /**
25  * A <code>TextChangeManager</code> manages associations between <code>ICompilationUnit</code>
26  * or <code>IFile</code> and <code>TextEditBasedChange</code> objects.
27  */

28 public class TextEditBasedChangeManager {
29     
30     private Map JavaDoc/*<ICompilationUnit, TextEditBasedChange>*/ fMap= new HashMap JavaDoc(10);
31     
32     private final boolean fKeepExecutedTextEdits;
33     
34     public TextEditBasedChangeManager() {
35         this(false);
36     }
37
38     public TextEditBasedChangeManager(boolean keepExecutedTextEdits) {
39         fKeepExecutedTextEdits= keepExecutedTextEdits;
40     }
41     
42     /**
43      * Adds an association between the given compilation unit and the passed
44      * change to this manager.
45      *
46      * @param cu the compilation unit (key)
47      * @param change the change associated with the compilation unit
48      */

49     public void manage(ICompilationUnit cu, TextEditBasedChange change) {
50         fMap.put(cu, change);
51     }
52     
53     /**
54      * Returns the <code>TextEditBasedChange</code> associated with the given compilation unit.
55      * If the manager does not already manage an association it creates a one.
56      *
57      * @param cu the compilation unit for which the text buffer change is requested
58      * @return the text change associated with the given compilation unit.
59      */

60     public TextEditBasedChange get(ICompilationUnit cu) {
61         TextEditBasedChange result= (TextEditBasedChange)fMap.get(cu);
62         if (result == null) {
63             result= new CompilationUnitChange(cu.getElementName(), cu);
64             result.setKeepPreviewEdits(fKeepExecutedTextEdits);
65             fMap.put(cu, result);
66         }
67         return result;
68     }
69     
70     /**
71      * Removes the <tt>TextEditBasedChange</tt> managed under the given key
72      * <code>unit<code>.
73      *
74      * @param unit the key determining the <tt>TextEditBasedChange</tt> to be removed.
75      * @return the removed <tt>TextEditBasedChange</tt>.
76      */

77     public TextEditBasedChange remove(ICompilationUnit unit) {
78         return (TextEditBasedChange)fMap.remove(unit);
79     }
80     
81     /**
82      * Returns all text changes managed by this instance.
83      *
84      * @return all text changes managed by this instance
85      */

86     public TextEditBasedChange[] getAllChanges(){
87         Set JavaDoc cuSet= fMap.keySet();
88         ICompilationUnit[] cus= (ICompilationUnit[]) cuSet.toArray(new ICompilationUnit[cuSet.size()]);
89         // sort by cu name:
90
Arrays.sort(cus, new Comparator JavaDoc() {
91             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
92                 String JavaDoc name1= ((ICompilationUnit) o1).getElementName();
93                 String JavaDoc name2= ((ICompilationUnit) o2).getElementName();
94                 return name1.compareTo(name2);
95             }
96         });
97         
98         TextEditBasedChange[] textChanges= new TextEditBasedChange[cus.length];
99         for (int i= 0; i < cus.length; i++) {
100             textChanges[i]= (TextEditBasedChange) 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(new ICompilationUnit[fMap.keySet().size()]);
112     }
113     
114     /**
115      * Clears all associations between resources and text changes.
116      */

117     public void clear() {
118         fMap.clear();
119     }
120
121     /**
122      * Returns if any text changes are managed for the specified compilation unit.
123      *
124      * @param cu the compilation unit
125      * @return <code>true</code> if any text changes are managed for the specified compilation unit and <code>false</code> otherwise
126      */

127     public boolean containsChangesIn(ICompilationUnit cu){
128         return fMap.containsKey(cu);
129     }
130 }
131
132
Popular Tags