KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > refactoring > XMLSafeDeleteRefactoringPlugin


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
20 package org.netbeans.modules.xml.refactoring;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.concurrent.ExecutionException JavaDoc;
28 import javax.swing.Action JavaDoc;
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 /**
63  *
64  * @author Sonali Kochar
65  */

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     /**
84      * Creates a new instance of XMLWhereUsedRefactoringPlugin
85      */

86     public XMLSafeDeleteRefactoringPlugin(SafeDeleteRefactoring refactoring) {
87         delete = refactoring;
88     }
89     
90     /** Checks pre-conditions of the refactoring and returns problems.
91      * @return Problems found or null (if no problems were identified)
92      */

93     public Problem preCheck() {
94         return null;
95     }
96     
97     /** Checks parameters of the refactoring.
98      * @return Problems found or null (if no problems were identified)
99      */

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 JavaDoc<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 JavaDoc<ErrorItem> checkErrors(NamedReferenceable obj) {
118           
119         if(request == null)
120             request = new DeleteRequest(obj);
121      
122             List JavaDoc<ErrorItem> allErrorItems = null;
123             //NamedReferenceable ref = (NamedReferenceable)obj;
124
FindUsageResult fuResult = RefactoringManager.getInstance().findUsages(obj);
125             try {
126                 request.setUsages(fuResult.get());
127             } catch (InterruptedException JavaDoc ex) {
128                     ErrorManager.getDefault().notify(ex);
129             } catch (ExecutionException JavaDoc ex) {
130                      ErrorManager.getDefault().notify(ex);
131             }
132             Collection JavaDoc<UsageGroup> usageGroups = null;
133             try {
134                             usageGroups = fuResult.get().getUsages();
135                             
136             } catch (InterruptedException JavaDoc ex) {
137                             ErrorManager.getDefault().notify(ex);
138             } catch (ExecutionException JavaDoc ex) {
139                 ErrorManager.getDefault().notify(ex);
140             }
141             for (UsageGroup group:usageGroups){
142                    
143                 List JavaDoc<ErrorItem> errors = group.getErrors();
144                 if (errors != null && errors.size() > 0){
145                     if (allErrorItems == null){
146                         allErrorItems = new ArrayList JavaDoc<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         //}
162
return null;
163        
164     }
165     
166     /** Collects refactoring elements for a given refactoring.
167      * @param refactoringElements Collection of refactoring elements - the implementation of this method
168      * should add refactoring elements to this collections. It should make no assumptions about the collection
169      * content.
170      * @return Problems found or null (if no problems were identified)
171      */

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 JavaDoc<ErrorItem> error = checkErrors((NamedReferenceable)obj);
188         
189         us = request.getUsages();
190         
191         Collection JavaDoc<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 JavaDoc<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 JavaDoc<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 JavaDoc<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                 //the 5.5.1 code shows only the first error
239
//the comments from original code
240
//TODO straighten out usability issue of Safe Delete whether to allow
241
// cascade delete where possible
242
// for now just a hack to show only first one assuming all entailed cascade delete not supported.
243
break;
244                 //continue;
245
}
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 JavaDoc<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 JavaDoc<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 JavaDoc callback, Cancellable parent) {
308             parent.cancel();
309             UI.openRefactoringUI(ui, rs, callback);
310         }
311         
312         public String JavaDoc getDetailsHint() {
313             return NbBundle.getMessage(XMLSafeDeleteRefactoringPlugin.class, "LBL_ShowUsages");
314                         
315         }
316     
317 }
318     
319     
320 }
321
322
Popular Tags