KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > refactoring > impl > RefactoringUtil


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.impl;
20
21 import java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29 import org.netbeans.api.project.FileOwnerQuery;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.api.project.ProjectUtils;
32 import org.netbeans.api.project.SourceGroup;
33 import org.netbeans.api.project.Sources;
34 import org.netbeans.api.project.ui.OpenProjects;
35 import org.netbeans.modules.xml.refactoring.DeleteRequest;
36 import org.netbeans.modules.xml.refactoring.ErrorItem;
37 import org.netbeans.modules.xml.refactoring.FileRenameRequest;
38 import org.netbeans.modules.xml.refactoring.RefactorRequest;
39 import org.netbeans.modules.xml.refactoring.RenameRequest;
40 import org.netbeans.modules.xml.refactoring.Usage;
41 import org.netbeans.modules.xml.retriever.catalog.ProjectCatalogSupport;
42 import org.netbeans.modules.xml.xam.Component;
43 import org.netbeans.modules.xml.xam.Model;
44 import org.netbeans.modules.xml.xam.ModelSource;
45 import org.netbeans.modules.xml.xam.Nameable;
46 import org.netbeans.modules.xml.xam.Referenceable;
47 import org.netbeans.modules.xml.xam.dom.Utils;
48 import org.netbeans.spi.project.SubprojectProvider;
49 import org.openide.cookies.SaveCookie;
50 import org.openide.filesystems.FileObject;
51 import org.openide.loaders.DataObject;
52 import org.openide.util.NbBundle;
53
54 /**
55  *
56  * @author Nam Nguyen
57  */

58 public class RefactoringUtil {
59     
60     public static Project findCurrentProject(Referenceable referenced) {
61         Model model = referenced instanceof Model ? (Model) referenced : ((Component)referenced).getModel();
62         if (model == null) return null;
63         return FileOwnerQuery.getOwner((FileObject)
64             model.getModelSource().getLookup().lookup(FileObject.class));
65     }
66     
67     public static Set JavaDoc<Project> getReferencingProjects(Project project) {
68         Set JavaDoc<Project> result = new HashSet JavaDoc<Project>();
69         for (Project p : OpenProjects.getDefault().getOpenProjects()) {
70             if (p.getLookup().lookup(ProjectCatalogSupport.class) == null) {
71                 continue;
72             }
73         
74             SubprojectProvider spp = (SubprojectProvider) p.getLookup().lookup(SubprojectProvider.class);
75             if (spp == null) continue;
76             for (Object JavaDoc o : spp.getSubprojects()) {
77                 Project sp = (Project) o;
78                 if (sp == project) {
79                     result.add(p);
80                     break;
81                 }
82             }
83         }
84
85         return result;
86     }
87     
88     public static List JavaDoc<SourceGroup> findSourceRoots(Project project) {
89     // get the generic roots so that all roots will be identified
90
SourceGroup[] groups =
91             ProjectUtils.getSources(project).getSourceGroups(Sources.TYPE_GENERIC);
92         return Arrays.asList(groups);
93     }
94     
95     public static List JavaDoc<FileObject> findSourceFiles(FileObject folder) {
96         Enumeration JavaDoc children = folder.getChildren(true);
97         List JavaDoc<FileObject> ret = new ArrayList JavaDoc<FileObject>();
98         while (children.hasMoreElements()) {
99             FileObject fo = (FileObject) children.nextElement();
100             if (fo.isData()) {
101                 ret.add(fo);
102             }
103         }
104         return ret;
105     }
106     
107     public static void precheckTarget(RefactorRequest request) {
108         Model model = request.getTargetModel();
109         if (model.getState() != Model.State.VALID) {
110             String JavaDoc msg = NbBundle.getMessage(RefactoringUtil.class, "MSG_ModelSourceNotWelformed");
111             request.addError(new ErrorItem(model, msg));
112         }
113         if (request.getAutosave() && ! RefactoringUtil.isWritable(model)) {
114             String JavaDoc msg = NbBundle.getMessage(RefactoringUtil.class, "MSG_ModelSourceNotWritable");
115             request.addError(new ErrorItem(model, msg));
116         }
117     }
118
119     public static void precheckUsageModels(RefactorRequest request) {
120         Set JavaDoc<Model> models = request.getUsages().getModels();
121         for (Model model : models) {
122             if (model.getState() != Model.State.VALID) {
123                 String JavaDoc msg = NbBundle.getMessage(RefactoringUtil.class, "MSG_ModelSourceNotWelformed");
124                 request.addError(new ErrorItem(model, msg));
125             }
126             if (request.getAutosave() && ! RefactoringUtil.isWritable(model)) {
127                 String JavaDoc msg = NbBundle.getMessage(RefactoringUtil.class, "MSG_ModelSourceNotWritable");
128                 request.addError(new ErrorItem(model, msg));
129             }
130         }
131     }
132     
133     public static void precheckForUnsafeDelete(DeleteRequest request) {
134         if (! request.getUsages().getUsages().isEmpty()) {
135             String JavaDoc msg = NbBundle.getMessage(RefactoringUtil.class, "MSG_UnsafeDelete");
136             request.addError(new ErrorItem(request.getTarget(), msg));
137         }
138     }
139     
140     public static String JavaDoc getDescription(RefactorRequest request) {
141         if (request instanceof RenameRequest) {
142             return NbBundle.getMessage(RefactoringUtil.class, "LBL_Rename"); //NOI18N
143
} else if (request instanceof DeleteRequest) {
144             return NbBundle.getMessage(RefactoringUtil.class, "LBL_Safe_Delete"); //NOI18N
145
} else if (request instanceof FileRenameRequest) {
146             return NbBundle.getMessage(RefactoringUtil.class, "LBL_File_Rename"); //NOI18N
147
} else {
148             return ""; //NOI18N
149
}
150     }
151
152     public static boolean isDirty(Model model) {
153         DataObject obj = (DataObject) model.getModelSource().getLookup().lookup(DataObject.class);
154         if (obj != null) {
155             return obj.isModified();
156         }
157         return false;
158     }
159
160     public static DataObject getDataObject(Model model) {
161         return (DataObject) model.getModelSource().getLookup().lookup(DataObject.class);
162     }
163     
164     public static void saveTargetFile(FileRenameRequest request) {
165         Model target = request.getTarget();
166         Set JavaDoc<Model> all = request.getUsages().getModels();
167         all.remove(target);
168         save(request, all);
169     }
170     
171     public static void save(RefactorRequest request, Set JavaDoc<Model> excludeds) {
172         Set JavaDoc<Model> all = request.getUsages().getModels();
173         all.add(request.getTargetModel());
174         for (Model model : all) {
175             if (excludeds.contains(model)) {
176                 continue;
177             }
178             DataObject obj = getDataObject(model);
179             if (obj == null) {
180                 String JavaDoc msg = NbBundle.getMessage(RefactoringUtil.class, "MSG_CannotFindDataObject");
181                 request.addError(new ErrorItem(model, msg));
182                 continue;
183             }
184             
185             SaveCookie save = (SaveCookie) obj.getCookie(SaveCookie.class);
186             FileObject fo = obj.getPrimaryFile();
187             if (save != null) {
188                 try {
189                     save.save();
190                     obj.setModified(false);
191                 } catch (IOException JavaDoc ioe) {
192                     String JavaDoc msg = NbBundle.getMessage(RefactoringUtil.class, "MSG_ErrorSave", fo.getPath(), ioe.getMessage());
193                     request.addError(new ErrorItem(model, msg));
194                     continue;
195                 }
196             } else {
197                 String JavaDoc msg = NbBundle.getMessage(RefactoringUtil.class, "MSG_CannotSave", fo.getPath());
198                 request.addError(new ErrorItem(model, msg));
199                 continue;
200             }
201         }
202             
203     }
204     
205     public static String JavaDoc getDescription(Usage.Type type) {
206         switch(type) {
207             case GENERALIZATION:
208                 return NbBundle.getMessage(RefactoringUtil.class, "LBL_Generalization");
209             case REFERENCE:
210                 return NbBundle.getMessage(RefactoringUtil.class, "LBL_Reference");
211             default:
212                 assert false : "Invalid type " + type;
213                 return "";
214         }
215     }
216     
217     public static ErrorItem precheck(RenameRequest request) {
218         if (request.getNewName() == null || !Utils.isValidNCName(request.getNewName())) {
219             return new ErrorItem(request.getTarget(),
220                     NbBundle.getMessage(RefactoringUtil.class, "MSG_NewNameNullEmpty"),
221                     ErrorItem.Level.FATAL);
222         } else if (! checkDuplicateName(request)) {
223             return new ErrorItem(request.getTarget(),
224                     NbBundle.getMessage(RefactoringUtil.class, "MSG_NewNameDuplicate"),
225                     ErrorItem.Level.FATAL);
226         }
227         return null;
228     }
229
230     public static ErrorItem precheck(FileRenameRequest request) {
231         FileObject current = request.getFileObject();
232         FileObject parent = current.getParent();
233         assert (parent != null) : "Source file has no parent folder";
234         String JavaDoc newName = request.getNewFileName();
235         if (newName == null || newName.trim().length() == 0 ||
236             parent.getFileObject(newName, current.getExt()) != null)
237         {
238             return new ErrorItem(request.getTarget(),
239                     NbBundle.getMessage(RefactoringUtil.class, "MSG_NewNameDuplicate"),
240                     ErrorItem.Level.FATAL);
241         }
242         return null;
243     }
244
245     /**
246      * Returns true if the check result is OK.
247      */

248     public static boolean checkDuplicateName(RenameRequest request) {
249         Nameable component = request.getNameableTarget();
250         Component parent = component.getParent();
251         Collection JavaDoc<Component> siblings = parent.getChildren(component.getClass());
252         for (Component c : siblings) {
253             Nameable nameable = (Nameable)c;
254             if (nameable.getName() != null && nameable.getName().equals(request.getNewName())) {
255                 return false;
256             }
257         }
258         return true;
259     }
260
261
262     public static boolean isWritable(Model model) {
263         if (model != null) {
264             ModelSource ms = model.getModelSource();
265             if (ms.isEditable()) {
266                 FileObject fo = (FileObject) ms.getLookup().lookup(FileObject.class);
267                 if (fo != null) {
268                     return fo.canWrite();
269                 }
270             }
271         }
272         return false;
273     }
274 }
275
Popular Tags