1 19 20 package org.netbeans.modules.j2ee.refactoring.rename; 21 22 import java.text.MessageFormat ; 23 import javax.jmi.reflect.RefObject; 24 import org.netbeans.jmi.javamodel.JavaClass; 25 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 26 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil; 27 import org.netbeans.modules.j2ee.persistence.unit.PUDataObject; 28 import org.netbeans.modules.j2ee.refactoring.PersistenceRefactoringUtil; 29 import org.netbeans.modules.j2ee.refactoring.Utility; 30 import org.netbeans.modules.javacore.internalapi.ExternalChange; 31 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 32 import org.netbeans.modules.refactoring.api.AbstractRefactoring; 33 import org.netbeans.modules.refactoring.api.Problem; 34 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation; 35 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 36 import org.openide.filesystems.FileObject; 37 import org.openide.util.NbBundle; 38 39 44 public class PersistenceXmlRenameRefactoring { 45 46 47 public PersistenceXmlRenameRefactoring() { 48 } 49 50 53 public Problem prepare(AbstractRefactoring refactoring, RefObject refObject, 54 String newName, RefactoringElementsBag refactoringElements) { 55 56 Problem problem = null; 57 if (!(refObject instanceof JavaClass)) { 58 return null; 59 } 60 61 JavaClass jClass = (JavaClass)refObject; 62 newName = Utility.renameClass(jClass.getName(), newName); 63 64 for (FileObject ddFile : PersistenceRefactoringUtil.getPersistence(jClass)) { 65 66 PUDataObject dataObject = ProviderUtil.getPUDataObject(ddFile); 67 68 if (!ProviderUtil.isValid(dataObject)){ 69 Problem newProblem = 70 new Problem(false, NbBundle.getMessage(PersistenceXmlRenameRefactoring.class, "TXT_PersistenceXmlInvalidProblem", ddFile.getPath())); 71 problem = Utility.addProblemsToEnd(problem, newProblem); 72 continue; 73 } 74 75 PersistenceUnit[] persistenceUnits = ProviderUtil.getPersistenceUnits(dataObject); 76 77 78 for (int i = 0; i < persistenceUnits.length; i++) { 79 PersistenceUnit unit = persistenceUnits[i]; 80 81 for (int j = 0; j < unit.getClass2().length; j++) { 82 83 if (unit.getClass2()[j].equals(jClass.getName())){ 84 85 RefactoringElementImplementation refactoringElem = 86 new PersistenceXmlClassRenameRefactoringElement(unit, jClass.getName(), newName, dataObject, ddFile); 87 88 refactoringElements.add(refactoring, refactoringElem); 89 } 90 91 } 92 } 93 } 94 95 return problem; 96 } 97 98 101 private static class PersistenceXmlClassRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 102 103 private PersistenceUnit persistenceUnit; 104 private PUDataObject puDataObject; 105 106 107 public PersistenceXmlClassRenameRefactoringElement(PersistenceUnit persistenceUnit, 108 String oldName, String newName, PUDataObject puDataObject, FileObject parentFile) { 109 this.oldName = oldName; 110 this.newName = newName; 111 this.persistenceUnit = persistenceUnit; 112 this.puDataObject = puDataObject; 113 this.parentFile = parentFile; 114 } 115 116 120 public String getDisplayText() { 121 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 122 return MessageFormat.format(NbBundle.getMessage(PersistenceXmlRenameRefactoring.class, "TXT_PersistenceXmlRename"), args); 123 } 124 125 public void performExternalChange() { 126 ProviderUtil.renameManagedClass(persistenceUnit, newName, oldName, puDataObject); 127 } 128 129 public void undoExternalChange() { 130 ProviderUtil.renameManagedClass(persistenceUnit, oldName, newName, puDataObject); 131 } 132 133 135 public void performChange() { 136 JavaMetamodel.getManager().registerExtChange(this); 137 } 138 139 } 140 } 141 | Popular Tags |