KickJava   Java API By Example, From Geeks To Geeks.

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


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.refactoring;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Set JavaDoc;
26 import org.netbeans.modules.xml.refactoring.impl.RefactoringUtil;
27 import org.netbeans.modules.xml.refactoring.spi.ChangeExecutor;
28 import org.netbeans.modules.xml.xam.Component;
29 import org.netbeans.modules.xml.xam.EmbeddableRoot;
30 import org.netbeans.modules.xml.xam.Model;
31 import org.netbeans.modules.xml.xam.Referenceable;
32 import org.netbeans.modules.xml.xam.dom.DocumentComponent;
33 import org.netbeans.modules.xml.xam.dom.DocumentModel;
34 import org.openide.filesystems.FileObject;
35
36 /**
37  *
38  * @author Nam Nguyen
39  */

40 public abstract class RefactorRequest {
41     private Set JavaDoc<Component> scope;
42     private ChangeExecutor changeExecutor;
43     private UsageSet usages;
44     private boolean autosave = true;
45     private List JavaDoc<ErrorItem> errors;
46
47     public RefactorRequest() {
48         this(null);
49     }
50     
51     /**
52      * @param scope list of search root constitute the refactoring scope.
53      */

54     public RefactorRequest(Set JavaDoc<Component> scope) {
55         this.scope = scope;
56         errors = new ArrayList JavaDoc<ErrorItem>();
57     }
58     
59     public Set JavaDoc<Component> getScope() {
60         return scope;
61     }
62     
63     public void setScope(Set JavaDoc<Component> searchRoots) {
64         scope = searchRoots;
65     }
66
67     public void setScopeLocal() {
68         if (getTarget() instanceof DocumentModel) {
69             scope = new HashSet JavaDoc<Component>();
70             scope.add(((DocumentModel)getTarget()).getRootComponent());
71         } else if (getTarget() instanceof Component) {
72             scope = Collections.singleton(getRootOf((Component)getTarget()));
73         }
74         setAutosave(false);
75     }
76     
77     public boolean isScopeLocal() {
78         if (getScope() == null || getScope().size() > 1) {
79             return false;
80         }
81         return getScope().iterator().next() == getRootOf(getTargetComponent());
82     }
83     
84     public String JavaDoc getDescription() {
85         return RefactoringUtil.getDescription(this);
86     }
87     
88     /**
89      * Returns target component, i.e., change component, of this refactoring request.
90      */

91     public abstract Referenceable getTarget();
92
93     public abstract String JavaDoc getTargetName();
94     
95     public Model getTargetModel() {
96         return getModel(getTarget());
97     }
98     
99     public Component getTargetComponent() {
100         return getComponent(getTarget());
101     }
102     
103     public static Model getModel(Referenceable ref) {
104         if (ref instanceof Model) {
105             return (Model) ref;
106         } else if (ref instanceof Component) {
107             return ((Component)ref).getModel();
108         } else {
109             return null;
110         }
111     }
112     
113     public static Component getComponent(Referenceable ref) {
114         if (ref instanceof Component) {
115             return (Component) ref;
116         } else if (ref instanceof DocumentModel) {
117             return ((DocumentModel)ref).getRootComponent();
118         } else {
119             throw new IllegalArgumentException JavaDoc("Invalid target type "+ref.getClass().getName());
120         }
121     }
122     
123     public FileObject getFileObject() {
124         FileObject fo = (FileObject) getTargetModel().getModelSource().getLookup().lookup(FileObject.class);
125         assert fo != null : "Failed to lookup for file object in model source"; //NOI18N
126
return fo;
127     }
128     
129     /**
130      * Returns the executor applying the change or null if none found or
131      * RefactoringManager#precheck or RefactoringManager#process has not been called
132      * on this request.
133      */

134     public ChangeExecutor getChangeExecutor() {
135         if (changeExecutor == null) {
136             for (ChangeExecutor ce : RefactoringManager.getInstance().getExecutors()) {
137                 if (ce.canChange(getType(), getTarget())) {
138                     changeExecutor = ce;
139                     break;
140                 }
141             }
142         }
143         return changeExecutor;
144     }
145     
146     void setChangeExecutor(ChangeExecutor executor) {
147         changeExecutor = executor;
148     }
149     
150     /**
151      * Returns the usages included in this refactoring request.
152      */

153     public UsageSet getUsages() {
154         return usages;
155     }
156     
157     public void setUsages(UsageSet usageSet) {
158         usages = usageSet;
159     }
160     
161     /**
162      * Whether the refactoring changes should be automatically persisted.
163      */

164     public boolean getAutosave() {
165         return autosave;
166     }
167     
168     /**
169      * Set whether the refactoring changes should be automatically persist.
170      */

171     public void setAutosave(boolean v) {
172         autosave = v;
173     }
174     
175     /**
176      * Returns type of this refactoring request.
177      */

178     public abstract <T extends RefactorRequest> Class JavaDoc<T> getType();
179     
180     /**
181      * Check if requested change has been performed on the target.
182      */

183     public abstract boolean confirmChangePerformed();
184     
185     /**
186      * Returns errors on the change itself. This does not include error from
187      * usages.
188      */

189     public List JavaDoc<ErrorItem> getChangeErrors() {
190         return errors;
191     }
192     
193     /**
194      * Returns all the collected errors.
195      */

196     public List JavaDoc<ErrorItem> getErrors() {
197         List JavaDoc ret = new ArrayList JavaDoc<ErrorItem>();
198         ret.addAll(errors);
199         if (usages != null) {
200             for (UsageGroup u : usages.getUsages()) {
201                 if (u.getErrors() != null) {
202                     ret.addAll(u.getErrors());
203                 }
204             }
205         }
206         return ret;
207     }
208     
209     /**
210      * Report an error happen during pre-refactoring check or during refactoring
211      * processing.
212      */

213     public void addError(ErrorItem error) {
214         errors.add(error);
215     }
216
217     public void addError(String JavaDoc message) {
218         errors.add(new ErrorItem(getTarget(), message));
219     }
220     
221     public void precheckChange() {
222         clear();
223         RefactoringUtil.precheckTarget(this);
224     }
225     
226     public void precheckUsages() {
227         RefactoringUtil.precheckUsageModels(this);
228     }
229     
230     public boolean hasFatalErrors() {
231         for (ErrorItem e : getErrors()) {
232             if (e.getLevel() == ErrorItem.Level.FATAL) {
233                 return true;
234             }
235         }
236         return false;
237     }
238     
239     protected void clear() {
240         errors = new ArrayList JavaDoc<ErrorItem>();
241         usages = null;
242     }
243
244     public static Component getEffectiveParent(Component component) {
245         if (component instanceof EmbeddableRoot) {
246             return ((EmbeddableRoot) component).getForeignParent();
247         } else {
248             return component.getParent();
249         }
250     }
251
252     public static Component getRootOf(Referenceable referenceable) {
253         return getRootOf(referenceable);
254     }
255     
256     public static Component getRootOf(Component component) {
257         Component root = (Component) component;
258         while (root != null) {
259             Component parent = getEffectiveParent(root);
260             if (parent == null) {
261                 break;
262             }
263             root = parent;
264         }
265         return root;
266     }
267
268     public Set JavaDoc<Model> getDirtyModels() {
269         HashSet JavaDoc<Model> dirties = new HashSet JavaDoc<Model>();
270         if (RefactoringUtil.isDirty(getTargetModel())) {
271             dirties.add(getTargetModel());
272         }
273         if (getUsages() != null) {
274             for (Model model : getUsages().getModels()) {
275                 if (RefactoringUtil.isDirty(model)) {
276                     dirties.add(model);
277                 }
278             }
279         }
280         return dirties;
281     }
282     
283 }
284
Popular Tags