1 19 package org.netbeans.modules.xml.refactoring.spi; 20 21 import java.io.IOException ; 22 import java.net.URI ; 23 import java.util.logging.Level ; 24 import java.util.logging.Logger ; 25 import org.netbeans.api.project.FileOwnerQuery; 26 import org.netbeans.api.project.Project; 27 import org.netbeans.modules.xml.refactoring.DeleteRequest; 28 import org.netbeans.modules.xml.refactoring.ErrorItem; 29 import org.netbeans.modules.xml.refactoring.FileRenameRequest; 30 import org.netbeans.modules.xml.refactoring.RenameRequest; 31 import org.netbeans.modules.xml.refactoring.Usage; 32 import org.netbeans.modules.xml.refactoring.UsageGroup; 33 import org.netbeans.modules.xml.refactoring.impl.RefactoringUtil; 34 import org.netbeans.modules.xml.retriever.catalog.ProjectCatalogSupport; 35 import org.netbeans.modules.xml.xam.Component; 36 import org.netbeans.modules.xml.xam.Model; 37 import org.netbeans.modules.xml.xam.locator.CatalogModel; 38 import org.openide.filesystems.FileLock; 39 import org.openide.filesystems.FileObject; 40 import org.openide.filesystems.FileUtil; 41 import org.openide.util.NbBundle; 42 43 48 public class SharedUtils { 49 50 public static void renameTarget(RenameRequest request) throws IOException { 51 if (request == null || request.getTargetModel() == null) return; 52 Model model = request.getTargetModel(); 53 boolean startTransaction = ! model.isIntransaction(); 54 try { 55 if (startTransaction) { 56 model.startTransaction(); 57 } 58 request.getNameableTarget().setName(request.getNewName()); 59 } finally { 60 if (startTransaction && model.isIntransaction()) { 61 model.endTransaction(); 62 } 63 } 64 65 request.setRenamedTarget(request.getNameableTarget()); 66 } 67 68 public static void deleteTarget(DeleteRequest request) throws IOException { 69 if (request == null || request.getTargetModel() == null) return; 70 Model model = request.getTargetModel(); 71 boolean startTransaction = ! model.isIntransaction(); 72 try { 73 if (startTransaction) { 74 model.startTransaction(); 75 } 76 model.removeChildComponent(request.getTarget()); 77 } finally { 78 if (startTransaction && model.isIntransaction()) { 79 model.endTransaction(); 80 } 81 } 82 request.setDone(true); 83 } 84 85 public static void addCascadeDeleteErrors(DeleteRequest request, Class <? extends Model> referencingModelType) { 86 for (UsageGroup usage : request.getUsages().getUsages()) { 87 if (! (referencingModelType.isAssignableFrom(usage.getModel().getClass()))) { 88 continue; 89 } 90 String msg = NbBundle.getMessage(RefactoringUtil.class, "MSG_CascadeDeleteNotSupported"); 91 for (Object o : usage.getItems()) { 92 Usage i = (Usage) o; request.addError(new ErrorItem(i.getComponent(), msg)); 94 } 95 } 96 } 97 98 public static void addCascadeDeleteErrors(DeleteRequest request, RefactoringEngine engine) { 99 for (UsageGroup usage : request.getUsages().getUsages()) { 100 if (! usage.getEngine().equals(engine)) { 101 continue; 102 } 103 String msg = NbBundle.getMessage(RefactoringUtil.class, "MSG_CascadeDeleteNotSupported"); 104 for (Object o : usage.getItems()) { 105 Usage i = (Usage) o; request.addError(new ErrorItem(i.getComponent(), msg)); 107 } 108 } 109 } 110 111 public static boolean isWritable(FileObject fo) { 112 boolean canLock = false; 113 FileLock lock = null; 114 try { 115 lock = fo.lock(); 116 canLock = true; 117 } catch(IOException ioe) { 118 if (lock != null) lock.releaseLock(); 119 } 120 return fo != null && fo.canWrite() && canLock; 121 } 122 123 public static String getURI(FileObject fo) { 124 return FileUtil.toFile(fo).toURI().toString(); 125 } 126 127 public static void renameFile(FileRenameRequest request) throws IOException { 128 FileObject referencedFO = request.getFileObject(); 129 RefactoringUtil.saveTargetFile(request); 130 referencedFO = renameFile(referencedFO, request.getNewFileName()); 131 refreshCatalogModel(request, referencedFO); 132 } 133 134 public static ProjectCatalogSupport getCatalogSupport(FileObject file) { 135 Project p = FileOwnerQuery.getOwner(file); 136 if (p != null) { 137 return (ProjectCatalogSupport) p.getLookup().lookup(ProjectCatalogSupport.class); 138 } 139 return null; 140 } 141 142 public static void undoRenameFile(FileRenameRequest request) throws IOException { 143 CatalogModel cat = (CatalogModel) 144 request.getTargetModel().getModelSource().getLookup().lookup(CatalogModel.class); 145 FileObject fo = request.getFileObject(); 146 fo = renameFile(fo, request.getOldFileName()); 147 refreshCatalogModel(request, fo); 148 } 149 150 public static FileObject renameFile(FileObject fo, String newName) throws IOException { 151 String extension = fo.getExt(); 152 FileLock lock = null; 153 try { 154 lock = fo.lock(); 155 fo.rename(lock, newName, extension); 156 return fo; 157 } finally { 158 if (lock != null) lock.releaseLock(); 159 } 160 } 161 162 public static void refreshCatalogModel(FileRenameRequest request, FileObject referencedFO) { 163 for (UsageGroup ug : request.getUsages().getUsages()) { 164 FileObject referencingFO = ug.getFileObject(); 165 ProjectCatalogSupport pcs = getCatalogSupport(referencingFO); 166 if (pcs == null) continue; 167 for (Component uc : ug.getRefactorComponents()) { 168 String reference = ug.getEngine().getModelReference(uc); 169 if (reference == null) continue; 170 try { 171 if (pcs != null && pcs.removeCatalogEntry(new URI (reference))) { 172 pcs.createCatalogEntry(referencingFO, referencedFO); 173 } 174 } catch(Exception ex) { 175 Logger.getLogger(SharedUtils.class.getName()).log(Level.FINE, ex.getMessage()); 176 } 177 } 178 } 179 } 180 } 181 | Popular Tags |