1 19 20 package org.netbeans.modules.xml.refactoring; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.concurrent.ExecutionException ; 28 import org.netbeans.modules.refactoring.api.AbstractRefactoring; 29 import org.netbeans.modules.refactoring.api.Problem; 30 import org.netbeans.modules.refactoring.api.ProgressEvent; 31 import org.netbeans.modules.refactoring.api.RenameRefactoring; 32 import org.netbeans.modules.refactoring.spi.ProgressProviderAdapter; 33 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation; 34 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 35 import org.netbeans.modules.refactoring.api.WhereUsedQuery; 36 import org.netbeans.modules.refactoring.spi.RefactoringPlugin; 37 import org.netbeans.modules.refactoring.spi.Transaction; 38 import org.netbeans.modules.xml.refactoring.impl.RefactoringUtil; 39 import org.netbeans.modules.xml.refactoring.spi.UIHelper; 40 import org.netbeans.modules.xml.refactoring.ui.util.AnalysisUtilities; 41 import org.netbeans.modules.xml.xam.Component; 42 import org.netbeans.modules.xml.xam.Model; 43 import org.netbeans.modules.xml.xam.Nameable; 44 import org.netbeans.modules.xml.xam.Named; 45 import org.netbeans.modules.xml.xam.Referenceable; 46 import org.netbeans.modules.xml.xam.dom.DocumentModel; 47 import org.openide.DialogDisplayer; 48 import org.openide.ErrorManager; 49 import org.openide.NotifyDescriptor; 50 import org.openide.filesystems.FileObject; 51 52 53 54 58 public class XMLRenameRefactoringPlugin extends ProgressProviderAdapter implements RefactoringPlugin { 59 60 private RenameRefactoring rename; 61 private RefactorRequest request; 62 63 public void cancelRequest() { 64 65 } 66 67 public Problem fastCheckParameters() { 68 Referenceable obj = rename.getRefactoringSource().lookup(Referenceable.class); 69 70 Problem problem = null; 71 ErrorItem error = null; 72 if(obj instanceof Model) { 73 FileRenameRequest fileRequest = new FileRenameRequest((Model)obj, "newFileName"); 74 fileRequest.setNewFileName(rename.getNewName()); 75 error = RefactoringUtil.precheck(fileRequest); 76 request = fileRequest; 77 } else if(obj instanceof Nameable) { 78 RenameRequest renameRequest = new RenameRequest((Nameable) obj, "newName"); 79 renameRequest.setNewName(rename.getNewName()); 80 error = RefactoringUtil.precheck(renameRequest); 81 request = renameRequest; 82 } 83 84 if (error != null) { 85 Problem p = new Problem(true, error.getMessage()); 86 if (problem == null) { 87 problem = p; 88 } else { 89 problem.setNext(p); 90 } 91 } 92 return problem; 93 94 95 } 96 97 98 99 100 103 public XMLRenameRefactoringPlugin(RenameRefactoring refactoring) { 104 this.rename = refactoring; 105 } 106 107 110 public Problem preCheck() { 111 return null; 112 } 113 114 117 public Problem checkParameters() { 118 Referenceable obj = rename.getRefactoringSource().lookup(Referenceable.class); 119 120 if( obj == null) 121 return null; 122 123 if( !((obj instanceof Model) || (obj instanceof Nameable)) ) 124 return null; 125 126 List <ErrorItem> allErrorItems = RefactoringManager.getInstance().precheckChange(request); 127 128 129 if (allErrorItems.size()> 0){ 130 return processErrors(allErrorItems); 131 } 132 133 return null; 134 135 136 } 137 138 144 public Problem prepare(RefactoringElementsBag refactoringElements) { 145 Referenceable obj = rename.getRefactoringSource().lookup(Referenceable.class); 146 if(obj == null) 147 return null; 148 if( !((obj instanceof Model) || (obj instanceof Nameable)) ) 149 return null; 150 Referenceable target = (Referenceable)obj; 151 152 fireProgressListenerStart(ProgressEvent.START, -1); 153 FindUsageResult fuResult = RefactoringManager.getInstance().findUsages(target); 154 UsageSet us = null; 155 try { 156 us = fuResult.get(); 157 158 request.setUsages(us); 160 } catch (InterruptedException ex) { 161 ErrorManager.getDefault().notify(ex); 162 } catch (ExecutionException ex) { 163 ErrorManager.getDefault().notify(ex); 164 } 165 Collection <UsageGroup> usageGroups = us.getUsages(); 166 167 List <ErrorItem> allErrorItems = null; 168 for (UsageGroup group:usageGroups){ 169 List <ErrorItem> errors = group.getErrors(); 170 if (errors != null && errors.size() > 0){ 171 if (allErrorItems == null){ 172 allErrorItems = new ArrayList <ErrorItem>(); 173 } 174 allErrorItems.addAll(errors); 175 } 176 } 177 178 if (( allErrorItems == null) || (allErrorItems.size() <= 0) ){ 179 allErrorItems = RefactoringManager.getInstance().precheckUsages(request); 180 if (allErrorItems != null && allErrorItems.size()> 0){ 181 return processErrors(allErrorItems); 182 } 183 } else { 184 return processErrors(allErrorItems); 185 } 186 187 refactoringElements.registerTransaction(new XMLRefactoringCommit(request)); 188 189 if( usageGroups.size()==0) { 190 refactoringElements.add(rename,createFauxElement(target)); 191 fireProgressListenerStop(); 192 return null; 193 194 } 195 196 for (UsageGroup ug:usageGroups){ 197 int count = ug.getItems().size(); 198 List <Usage> usage = ug.getItems(); 199 for(Usage u:usage){ 200 refactoringElements.add(rename, new XMLRefactoringElement(u)); 201 } 202 fireProgressListenerStep(); 203 } 204 205 fireProgressListenerStop(); 206 return null; 207 } 208 209 210 public Problem processErrors(List <ErrorItem> errorItems){ 211 212 if (errorItems == null || errorItems.size()== 0){ 213 return null; 214 } 215 Problem parent = null; 216 Problem child = null; 217 Problem head = null; 218 Iterator <ErrorItem> iterator = errorItems.iterator(); 219 220 while(iterator.hasNext()) { 221 ErrorItem error = iterator.next(); 222 if(parent == null ){ 223 parent = new Problem(isFatal(error), error.getMessage()); 224 child = parent; 225 head = parent; 226 continue; 227 } 228 child = new Problem(isFatal(error), error.getMessage()); 229 parent.setNext(child); 230 parent = child; 231 232 } 233 234 235 return head; 236 } 237 238 public boolean isFatal(ErrorItem error){ 239 if(error.getLevel() == ErrorItem.Level.FATAL) 240 return true; 241 else 242 return false; 243 } 244 245 public RefactoringElementImplementation createFauxElement(Referenceable ref){ 246 return new FauxRefactoringElement(ref); 247 } 248 249 250 251 } 252 253 | Popular Tags |