1 19 package org.netbeans.modules.refactoring.api; 20 21 import java.util.AbstractCollection ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.LinkedList ; 26 import java.util.ListIterator ; 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 40 public final class RefactoringSession { 41 private final LinkedList <RefactoringElementImplementation> internalList; 42 private final RefactoringElementsBag bag; 43 private final Collection <RefactoringElement> refactoringElements; 44 private final String description; 45 private ProgressSupport progressSupport; 46 private UndoManager undoManager = UndoManager.getDefault(); 47 boolean realcommit = true; 48 49 private RefactoringSession(String description) { 50 internalList = new LinkedList (); 51 bag = SPIAccessor.DEFAULT.createBag(this, internalList); 52 this.description = description; 53 this.refactoringElements = new ElementsCollection(); 54 } 55 56 61 public static RefactoringSession create(String description) { 62 return new RefactoringSession(description); 63 } 64 65 66 73 public Problem doRefactoring(boolean saveAfterDone) { 74 Iterator 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 117 public Problem undoRefactoring(boolean saveAfterDone) { 118 try { 119 ListIterator it = internalList.listIterator(internalList.size()); 120 fireProgressListenerStart(0, internalList.size()+1); 121 ArrayList <Transaction> fileChanges = SPIAccessor.DEFAULT.getFileChanges(bag); 122 ArrayList <Transaction> commits = SPIAccessor.DEFAULT.getCommits(bag); 123 for (ListIterator <Transaction> fileChangeIterator = fileChanges.listIterator(fileChanges.size()); fileChangeIterator.hasPrevious();) { 124 fileChangeIterator.previous().rollback(); 125 } 126 for (ListIterator <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 151 public Collection <RefactoringElement> getRefactoringElements() { 152 return refactoringElements; 153 } 154 155 159 public synchronized void addProgressListener(ProgressListener listener) { 160 if (progressSupport == null ) { 161 progressSupport = new ProgressSupport(); 162 } 163 progressSupport.addProgressListener(listener); 164 } 165 166 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 <RefactoringElement> { 199 public Iterator <RefactoringElement> iterator() { 200 return new Iterator () { 201 private final Iterator <RefactoringElementImplementation> inner = internalList.iterator(); 202 203 public void remove() { 204 throw new UnsupportedOperationException (); 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 |