KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > api > RefactoringSession


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.refactoring.api;
20
21 import java.util.AbstractCollection JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.ListIterator JavaDoc;
27 import org.netbeans.modules.refactoring.api.impl.ProgressSupport;
28 import org.netbeans.modules.refactoring.api.impl.SPIAccessor;
29 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
30 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
31 import org.netbeans.modules.refactoring.spi.Transaction;
32 import org.netbeans.modules.refactoring.spi.impl.UndoManager;
33 import org.openide.LifecycleManager;
34
35
36 /** Class used to invoke refactorings.
37  *
38  * @author Martin Matula, Daniel Prusa, Jan Becicka
39  */

40 public final class RefactoringSession {
41     private final LinkedList JavaDoc<RefactoringElementImplementation> internalList;
42     private final RefactoringElementsBag bag;
43     private final Collection JavaDoc<RefactoringElement> refactoringElements;
44     private final String JavaDoc description;
45     private ProgressSupport progressSupport;
46     private UndoManager undoManager = UndoManager.getDefault();
47     boolean realcommit = true;
48     
49     private RefactoringSession(String JavaDoc description) {
50         internalList = new LinkedList JavaDoc();
51         bag = SPIAccessor.DEFAULT.createBag(this, internalList);
52         this.description = description;
53         this.refactoringElements = new ElementsCollection();
54     }
55     
56     /**
57      * Creates a new refactoring session.
58      * @param description textual description of this session
59      * @return instance of RefactoringSession
60      */

61     public static RefactoringSession create(String JavaDoc description) {
62         return new RefactoringSession(description);
63     }
64
65
66     /**
67      * process all elements from elements bags,
68      * do all fileChanges
69      * and call all commits
70      * @param saveAfterDone save all if true
71      * @return instance of Problem or null, if everything is OK
72      */

73     public Problem doRefactoring(boolean saveAfterDone) {
74         Iterator JavaDoc it = internalList.iterator();
75         fireProgressListenerStart(0, internalList.size()+1);
76         if (realcommit) {
77             undoManager.transactionStarted();
78             undoManager.setUndoDescription(description);
79         }
80         try {
81             try {
82                 while (it.hasNext()) {
83                     fireProgressListenerStep();
84                     RefactoringElementImplementation element = (RefactoringElementImplementation) it.next();
85                     if (element.isEnabled() && !((element.getStatus() == RefactoringElement.GUARDED) || (element.getStatus() == RefactoringElement.READ_ONLY))) {
86                         element.performChange();
87                     }
88                 }
89             } finally {
90                 for (Transaction commit:SPIAccessor.DEFAULT.getCommits(bag)) {
91                     commit.commit();
92                 }
93             }
94             if (saveAfterDone) {
95                 LifecycleManager.getDefault().saveAll();
96             }
97             for (Transaction fileChange:SPIAccessor.DEFAULT.getFileChanges(bag)) {
98                 fileChange.commit();
99             }
100             fireProgressListenerStep();
101         } finally {
102             fireProgressListenerStop();
103             if (realcommit) {
104                 undoManager.addItem(this);
105                 undoManager.transactionEnded(false);
106                 realcommit=false;
107             }
108         }
109         return null;
110     }
111     
112     /**
113      * do undo of previous doRefactoring()
114      * @param saveAfterDone save all if true
115      * @return instance of Problem or null, if everything is OK
116      */

117     public Problem undoRefactoring(boolean saveAfterDone) {
118         try {
119             ListIterator JavaDoc it = internalList.listIterator(internalList.size());
120             fireProgressListenerStart(0, internalList.size()+1);
121             ArrayList JavaDoc<Transaction> fileChanges = SPIAccessor.DEFAULT.getFileChanges(bag);
122             ArrayList JavaDoc<Transaction> commits = SPIAccessor.DEFAULT.getCommits(bag);
123             for (ListIterator JavaDoc<Transaction> fileChangeIterator = fileChanges.listIterator(fileChanges.size()); fileChangeIterator.hasPrevious();) {
124                 fileChangeIterator.previous().rollback();
125             }
126             for (ListIterator JavaDoc<Transaction> commitIterator = commits.listIterator(commits.size()); commitIterator.hasPrevious();) {
127                 commitIterator.previous().rollback();
128             }
129             
130             while (it.hasPrevious()) {
131                 fireProgressListenerStep();
132                 RefactoringElementImplementation element = (RefactoringElementImplementation) it.previous();
133                 if (element.isEnabled() && !((element.getStatus() == RefactoringElement.GUARDED) || (element.getStatus() == RefactoringElement.READ_ONLY))) {
134                     element.undoChange();
135                 }
136             }
137             if (saveAfterDone) {
138                 LifecycleManager.getDefault().saveAll();
139             }
140             fireProgressListenerStep();
141         } finally {
142             fireProgressListenerStop();
143         }
144         return null;
145     }
146     
147     /**
148      * get elements from session
149      * @return collection of RefactoringElements
150      */

151     public Collection JavaDoc<RefactoringElement> getRefactoringElements() {
152         return refactoringElements;
153     }
154     
155     /**
156      * Adds progress listener to this RefactoringSession
157      * @param listener to add
158      */

159     public synchronized void addProgressListener(ProgressListener listener) {
160         if (progressSupport == null ) {
161             progressSupport = new ProgressSupport();
162         }
163         progressSupport.addProgressListener(listener);
164     }
165
166     /**
167      * Remove progress listener from this RefactoringSession
168      * @param listener to remove
169      */

170     public synchronized void removeProgressListener(ProgressListener listener) {
171         if (progressSupport != null ) {
172             progressSupport.removeProgressListener(listener);
173         }
174     }
175
176     RefactoringElementsBag getElementsBag() {
177         return bag;
178     }
179
180     private void fireProgressListenerStart(int type, int count) {
181         if (progressSupport != null) {
182             progressSupport.fireProgressListenerStart(this, type, count);
183         }
184     }
185
186     private void fireProgressListenerStep() {
187         if (progressSupport != null) {
188             progressSupport.fireProgressListenerStep(this);
189         }
190     }
191
192     private void fireProgressListenerStop() {
193         if (progressSupport != null) {
194             progressSupport.fireProgressListenerStop(this);
195         }
196     }
197     
198     private class ElementsCollection extends AbstractCollection JavaDoc<RefactoringElement> {
199         public Iterator JavaDoc<RefactoringElement> iterator() {
200             return new Iterator JavaDoc() {
201                 private final Iterator JavaDoc<RefactoringElementImplementation> inner = internalList.iterator();
202
203                 public void remove() {
204                     throw new UnsupportedOperationException JavaDoc();
205                 }
206                 
207                 public RefactoringElement next() {
208                     return new RefactoringElement(inner.next());
209                 }
210                 
211                 public boolean hasNext() {
212                     return inner.hasNext();
213                 }
214             };
215         }
216
217         public int size() {
218             return internalList.size();
219         }
220     }
221 }
222
Popular Tags