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 javax.swing.Action ; 29 import org.netbeans.modules.refactoring.api.AbstractRefactoring; 30 import org.netbeans.modules.refactoring.api.Problem; 31 import org.netbeans.modules.refactoring.api.ProgressEvent; 32 import org.netbeans.modules.refactoring.api.RefactoringSession; 33 import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring; 34 import org.netbeans.modules.refactoring.spi.ProblemDetailsFactory; 35 import org.netbeans.modules.refactoring.spi.ProblemDetailsImplementation; 36 import org.netbeans.modules.refactoring.spi.ProgressProviderAdapter; 37 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation; 38 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 39 import org.netbeans.modules.refactoring.api.WhereUsedQuery; 40 import org.netbeans.modules.refactoring.spi.RefactoringPlugin; 41 import org.netbeans.modules.refactoring.spi.Transaction; 42 import org.netbeans.modules.refactoring.spi.ui.RefactoringUI; 43 import org.netbeans.modules.refactoring.spi.ui.UI; 44 import org.netbeans.modules.xml.refactoring.impl.RefactoringUtil; 45 import org.netbeans.modules.xml.refactoring.ui.j.spi.ui.WhereUsedQueryUI; 46 import org.netbeans.modules.xml.refactoring.ui.views.WhereUsedView; 47 import org.netbeans.modules.xml.xam.Component; 48 import org.netbeans.modules.xml.xam.Model; 49 import org.netbeans.modules.xml.xam.Nameable; 50 import org.netbeans.modules.xml.xam.Named; 51 import org.netbeans.modules.xml.xam.NamedReferenceable; 52 import org.netbeans.modules.xml.xam.Referenceable; 53 import org.openide.DialogDisplayer; 54 import org.openide.ErrorManager; 55 import org.openide.NotifyDescriptor; 56 import org.openide.util.Cancellable; 57 import org.openide.util.NbBundle; 58 import org.openide.util.lookup.Lookups; 59 60 61 62 66 public class XMLSafeDeleteRefactoringPlugin extends ProgressProviderAdapter implements RefactoringPlugin { 67 68 private SafeDeleteRefactoring delete; 69 private DeleteRequest request; 70 71 public void cancelRequest() { 72 73 } 74 75 public Problem fastCheckParameters() { 76 return null; 77 78 } 79 80 81 82 83 86 public XMLSafeDeleteRefactoringPlugin(SafeDeleteRefactoring refactoring) { 87 delete = refactoring; 88 } 89 90 93 public Problem preCheck() { 94 return null; 95 } 96 97 100 public Problem checkParameters() { 101 NamedReferenceable obj = delete.getRefactoringSource().lookup(NamedReferenceable.class); 102 103 if(obj == null) 104 return null; 105 request = new DeleteRequest((NamedReferenceable)obj); 106 107 List <ErrorItem> allErrorItems = RefactoringManager.getInstance().precheckChange(request); 108 109 if (allErrorItems.size()> 0){ 110 return processErrors(allErrorItems); 111 } 112 113 return null; 114 115 } 116 117 public List <ErrorItem> checkErrors(NamedReferenceable obj) { 118 119 if(request == null) 120 request = new DeleteRequest(obj); 121 122 List <ErrorItem> allErrorItems = null; 123 FindUsageResult fuResult = RefactoringManager.getInstance().findUsages(obj); 125 try { 126 request.setUsages(fuResult.get()); 127 } catch (InterruptedException ex) { 128 ErrorManager.getDefault().notify(ex); 129 } catch (ExecutionException ex) { 130 ErrorManager.getDefault().notify(ex); 131 } 132 Collection <UsageGroup> usageGroups = null; 133 try { 134 usageGroups = fuResult.get().getUsages(); 135 136 } catch (InterruptedException ex) { 137 ErrorManager.getDefault().notify(ex); 138 } catch (ExecutionException ex) { 139 ErrorManager.getDefault().notify(ex); 140 } 141 for (UsageGroup group:usageGroups){ 142 143 List <ErrorItem> errors = group.getErrors(); 144 if (errors != null && errors.size() > 0){ 145 if (allErrorItems == null){ 146 allErrorItems = new ArrayList <ErrorItem>(); 147 } 148 allErrorItems.addAll(errors); 149 } 150 } 151 152 if( (allErrorItems == null) || (allErrorItems.size() <= 0) ){ 153 154 allErrorItems = RefactoringManager.getInstance().precheckUsages(request); 155 if (allErrorItems != null && allErrorItems.size()> 0){ 156 return allErrorItems; 157 } 158 } else { 159 return allErrorItems; 160 } 161 return null; 163 164 } 165 166 172 public Problem prepare(RefactoringElementsBag refactoringElements) { 173 NamedReferenceable obj = delete.getRefactoringSource().lookup(NamedReferenceable.class); 174 175 if( obj == null ) 176 return null; 177 UsageSet us = null; 178 179 fireProgressListenerStart(ProgressEvent.START, -1); 180 RefactoringSession inner = RefactoringSession.create("delete"); 181 Referenceable ref = (Referenceable)obj; 182 WhereUsedQuery query = new WhereUsedQuery(Lookups.singleton(ref)); 183 query.prepare(inner); 184 WhereUsedView wuv = new WhereUsedView((Referenceable) obj); 185 WhereUsedQueryUI ui = new WhereUsedQueryUI(wuv, (Referenceable) obj); 186 187 List <ErrorItem> error = checkErrors((NamedReferenceable)obj); 188 189 us = request.getUsages(); 190 191 Collection <UsageGroup> usageGroups = us.getUsages(); 192 refactoringElements.registerTransaction(new XMLRefactoringCommit(request)); 193 194 if( usageGroups.size()==0) { 195 refactoringElements.add(delete,createFauxElement((Referenceable)obj)); 196 fireProgressListenerStop(); 197 return null; 198 199 } 200 201 for (UsageGroup ug:usageGroups){ 202 int count = ug.getItems().size(); 203 204 List <Usage> usage = ug.getItems(); 205 for(Usage u:usage){ 206 refactoringElements.add(delete, new XMLRefactoringElement(u)); 207 fireProgressListenerStep(); 208 } 209 } 210 211 if(error != null && error.size() > 0) { 212 Problem problem = processErrors(error, ui, inner); 213 fireProgressListenerStop(); 214 return problem; 215 } 216 217 fireProgressListenerStop(); 218 219 return null; 220 } 221 222 223 public Problem processErrors(List <ErrorItem> errorItems, WhereUsedQueryUI ui, RefactoringSession inner) { 224 if (errorItems == null || errorItems.size()== 0){ 225 return null; 226 } 227 Problem parent = null; 228 Problem child = null; 229 Problem head = null; 230 Iterator <ErrorItem> iterator = errorItems.iterator(); 231 232 while(iterator.hasNext()) { 233 ErrorItem error = iterator.next(); 234 if(parent == null ){ 235 parent = new Problem(isFatal(error), error.getMessage(),ProblemDetailsFactory.createProblemDetails(new ProblemDetailsImplemen(ui, inner))); 236 child = parent; 237 head = parent; 238 break; 244 } 246 child = new Problem(isFatal(error), error.getMessage()); 247 parent.setNext(child); 248 parent = child; 249 250 } 251 252 253 return head; 254 255 256 } 257 258 public Problem processErrors(List <ErrorItem> errorItems){ 259 260 if (errorItems == null || errorItems.size()== 0){ 261 return null; 262 } 263 Problem parent = null; 264 Problem child = null; 265 Problem head = null; 266 Iterator <ErrorItem> iterator = errorItems.iterator(); 267 268 while(iterator.hasNext()) { 269 ErrorItem error = iterator.next(); 270 if(parent == null ){ 271 parent = new Problem(isFatal(error), error.getMessage()); 272 child = parent; 273 head = parent; 274 continue; 275 } 276 child = new Problem(isFatal(error), error.getMessage()); 277 parent.setNext(child); 278 parent = child; 279 280 } 281 282 283 return head; 284 } 285 286 public boolean isFatal(ErrorItem error){ 287 if(error.getLevel() == ErrorItem.Level.FATAL) 288 return true; 289 else 290 return false; 291 } 292 293 public RefactoringElementImplementation createFauxElement(Referenceable ref){ 294 return new FauxRefactoringElement(ref); 295 } 296 297 private class ProblemDetailsImplemen implements ProblemDetailsImplementation { 298 299 private RefactoringUI ui; 300 private RefactoringSession rs; 301 302 public ProblemDetailsImplemen(RefactoringUI ui, RefactoringSession rs) { 303 this.ui = ui; 304 this.rs = rs; 305 } 306 307 public void showDetails(Action callback, Cancellable parent) { 308 parent.cancel(); 309 UI.openRefactoringUI(ui, rs, callback); 310 } 311 312 public String getDetailsHint() { 313 return NbBundle.getMessage(XMLSafeDeleteRefactoringPlugin.class, "LBL_ShowUsages"); 314 315 } 316 317 } 318 319 320 } 321 322 | Popular Tags |