KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 /**
55  *
56  * @author Sonali Kochar
57  */

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

103     public XMLRenameRefactoringPlugin(RenameRefactoring refactoring) {
104         this.rename = refactoring;
105     }
106     
107     /** Checks pre-conditions of the refactoring and returns problems.
108      * @return Problems found or null (if no problems were identified)
109      */

110     public Problem preCheck() {
111         return null;
112     }
113     
114     /** Checks parameters of the refactoring.
115      * @return Problems found or null (if no problems were identified)
116      */

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 JavaDoc<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     /** Collects refactoring elements for a given refactoring.
139      * @param refactoringElements Collection of refactoring elements - the implementation of this method
140      * should add refactoring elements to this collections. It should make no assumptions about the collection
141      * content.
142      * @return Problems found or null (if no problems were identified)
143      */

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 should not be null..but check for null and throw problem
159
request.setUsages(us);
160          } catch (InterruptedException JavaDoc ex) {
161                ErrorManager.getDefault().notify(ex);
162          } catch (ExecutionException JavaDoc ex) {
163                ErrorManager.getDefault().notify(ex);
164          }
165         Collection JavaDoc<UsageGroup> usageGroups = us.getUsages();
166         
167         List JavaDoc<ErrorItem> allErrorItems = null;
168         for (UsageGroup group:usageGroups){
169                 List JavaDoc<ErrorItem> errors = group.getErrors();
170                 if (errors != null && errors.size() > 0){
171                     if (allErrorItems == null){
172                         allErrorItems = new ArrayList JavaDoc<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 JavaDoc<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 JavaDoc<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 JavaDoc<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