1 19 package org.netbeans.modules.xml.refactoring.impl; 20 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.Enumeration ; 26 import java.util.HashSet ; 27 import java.util.List ; 28 import java.util.Set ; 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 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 <Project> getReferencingProjects(Project project) { 68 Set <Project> result = new HashSet <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 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 <SourceGroup> findSourceRoots(Project project) { 89 SourceGroup[] groups = 91 ProjectUtils.getSources(project).getSourceGroups(Sources.TYPE_GENERIC); 92 return Arrays.asList(groups); 93 } 94 95 public static List <FileObject> findSourceFiles(FileObject folder) { 96 Enumeration children = folder.getChildren(true); 97 List <FileObject> ret = new ArrayList <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 msg = NbBundle.getMessage(RefactoringUtil.class, "MSG_ModelSourceNotWelformed"); 111 request.addError(new ErrorItem(model, msg)); 112 } 113 if (request.getAutosave() && ! RefactoringUtil.isWritable(model)) { 114 String 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 <Model> models = request.getUsages().getModels(); 121 for (Model model : models) { 122 if (model.getState() != Model.State.VALID) { 123 String msg = NbBundle.getMessage(RefactoringUtil.class, "MSG_ModelSourceNotWelformed"); 124 request.addError(new ErrorItem(model, msg)); 125 } 126 if (request.getAutosave() && ! RefactoringUtil.isWritable(model)) { 127 String 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 msg = NbBundle.getMessage(RefactoringUtil.class, "MSG_UnsafeDelete"); 136 request.addError(new ErrorItem(request.getTarget(), msg)); 137 } 138 } 139 140 public static String getDescription(RefactorRequest request) { 141 if (request instanceof RenameRequest) { 142 return NbBundle.getMessage(RefactoringUtil.class, "LBL_Rename"); } else if (request instanceof DeleteRequest) { 144 return NbBundle.getMessage(RefactoringUtil.class, "LBL_Safe_Delete"); } else if (request instanceof FileRenameRequest) { 146 return NbBundle.getMessage(RefactoringUtil.class, "LBL_File_Rename"); } else { 148 return ""; } 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 <Model> all = request.getUsages().getModels(); 167 all.remove(target); 168 save(request, all); 169 } 170 171 public static void save(RefactorRequest request, Set <Model> excludeds) { 172 Set <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 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 ioe) { 192 String 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 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 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 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 248 public static boolean checkDuplicateName(RenameRequest request) { 249 Nameable component = request.getNameableTarget(); 250 Component parent = component.getParent(); 251 Collection <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 |