1 19 20 package org.netbeans.modules.refactoring.spi; 21 22 import java.io.IOException ; 23 import java.util.*; 24 import javax.swing.text.Position ; 25 import org.netbeans.api.editor.guards.GuardedSection; 26 import org.netbeans.api.editor.guards.GuardedSectionManager; 27 import org.netbeans.modules.refactoring.api.impl.APIAccessor; 28 import org.netbeans.modules.refactoring.api.impl.SPIAccessor; 29 import org.netbeans.modules.refactoring.api.*; 30 import org.openide.cookies.EditorCookie; 31 import org.openide.filesystems.FileObject; 32 import org.openide.loaders.DataObject; 33 import org.openide.loaders.DataObjectNotFoundException; 34 35 39 public final class RefactoringElementsBag { 40 ArrayList<Transaction> commits; 41 ArrayList<Transaction> fileChanges; 42 43 static { 44 SPIAccessor.DEFAULT = new AccessorImpl(); 45 } 46 47 private final List<RefactoringElementImplementation> delegate; 48 private final RefactoringSession session; 49 private Collection<FileObject> readOnlyFiles = new HashSet(); 50 51 54 RefactoringElementsBag(RefactoringSession session, List<RefactoringElementImplementation> delegate) { 55 this.session = session; 56 this.delegate = delegate; 57 this.commits = new ArrayList(); 58 this.fileChanges = new ArrayList(); 59 } 60 61 74 public Problem add(AbstractRefactoring refactoring, RefactoringElementImplementation el) { 75 Problem p = null; 76 if (isReadOnly(el)) { 77 FileObject file = el.getParentFile(); 78 readOnlyFiles.add(file); 79 el.setEnabled(false); 80 el.setStatus(el.READ_ONLY); 81 delegate.add(el); 82 } else if (isGuarded(el)) { 83 ArrayList<RefactoringElementImplementation> proposedChanges = new ArrayList(); 84 ArrayList<Transaction> transactions = new ArrayList(); 85 for (GuardedBlockHandler gbHandler: APIAccessor.DEFAULT.getGBHandlers(refactoring)) { 86 el.setEnabled(false); 87 p = APIAccessor.DEFAULT.chainProblems(gbHandler.handleChange(el, proposedChanges, transactions), p); 88 89 if (p != null && p.isFatal()) 90 return p; 91 92 delegate.addAll(proposedChanges); 93 94 for (Transaction transaction:transactions) { 95 registerTransaction(transaction); 96 } 97 98 if (!proposedChanges.isEmpty() || !transactions.isEmpty()) 99 return p; 100 101 } 102 el.setEnabled(false); 103 el.setStatus(el.GUARDED); 104 delegate.add(el); 105 } else { 106 delegate.add(el); 107 } 108 return p; 109 } 110 111 117 public Problem addAll(AbstractRefactoring refactoring, Collection<RefactoringElementImplementation> elements) { 118 Problem p = null; 119 for (RefactoringElementImplementation rei:elements) { 120 p = APIAccessor.DEFAULT.chainProblems(p, add(refactoring, rei)); 121 if (p!=null && p.isFatal()) 122 return p; 123 } 124 return p; 125 } 126 127 128 132 public RefactoringSession getSession() { 133 return session; 134 } 135 136 Collection<FileObject> getReadOnlyFiles() { 137 return readOnlyFiles; 138 } 139 140 146 public void registerTransaction(Transaction commit) { 147 if (APIAccessor.DEFAULT.isCommit(session)) 148 if (!commits.contains(commit)) 149 commits.add(commit); 150 } 151 152 153 159 public void registerFileChange(Transaction changes) { 160 if (APIAccessor.DEFAULT.isCommit(session)) 161 fileChanges.add(changes); 162 } 163 164 private boolean isReadOnly(RefactoringElementImplementation rei) { 165 return !rei.getParentFile().canWrite(); 166 } 167 168 172 private boolean isGuarded(RefactoringElementImplementation el) { 173 if (el.getPosition()==null) 174 return false; 175 try { 176 DataObject dob = DataObject.find(el.getParentFile()); 177 EditorCookie e = dob.getCookie(EditorCookie.class); 178 if (e!=null) { 179 GuardedSectionManager manager = GuardedSectionManager.getInstance(e.openDocument()); 180 if (manager != null) { 181 Position elementStart = el.getPosition().getBegin().getPosition(); 182 Position elementEnd = el.getPosition().getEnd().getPosition(); 183 for(GuardedSection section:manager.getGuardedSections()) { 184 if (section.contains(elementStart, false) || 185 section.contains(elementEnd, false)) { 186 return true; 187 } 188 } 189 } 190 } 191 } catch (DataObjectNotFoundException ex) { 192 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, 193 ex.getMessage(), ex); 194 } catch (IOException ex) { 195 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, 196 ex.getMessage(), ex); 197 } 198 return false; 199 } 200 } 201 | Popular Tags |