1 19 20 package org.netbeans.modules.j2ee.refactoring.safedelete; 21 22 import java.text.MessageFormat ; 23 import org.netbeans.jmi.javamodel.Element; 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.DefaultPositionBoundsResolver; 29 import org.netbeans.modules.j2ee.refactoring.J2EERefactoring; 30 import org.netbeans.modules.j2ee.refactoring.PersistenceRefactoringUtil; 31 import org.netbeans.modules.j2ee.refactoring.Utility; 32 import org.netbeans.modules.javacore.internalapi.ExternalChange; 33 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 34 import org.netbeans.modules.refactoring.api.Problem; 35 import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring; 36 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation; 37 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 38 import org.openide.ErrorManager; 39 import org.openide.filesystems.FileObject; 40 import org.openide.loaders.DataObject; 41 import org.openide.loaders.DataObjectNotFoundException; 42 import org.openide.text.PositionBounds; 43 import org.openide.util.NbBundle; 44 45 51 public class PersistenceXmlSafeDeleteRefactoring implements J2EERefactoring { 52 53 private Element element; 54 private SafeDeleteRefactoring safeDeleteRefactoring; 55 56 59 public PersistenceXmlSafeDeleteRefactoring(Element element, SafeDeleteRefactoring safeDeleteRefactoring) { 60 this.element = element; 61 this.safeDeleteRefactoring = safeDeleteRefactoring; 62 } 63 64 public Problem preCheck() { 65 return null; 66 } 67 68 public Problem checkParameters() { 69 return null; 70 } 71 72 public Problem fastCheckParameters() { 73 return null; 74 } 75 76 public Problem prepare(RefactoringElementsBag refactoringElementsBag) { 77 78 Problem problem = null; 79 80 for (Object elem : element.getResource().getClassifiers()) { 81 JavaClass jClass = (JavaClass) elem; 82 83 for (FileObject ddFile : PersistenceRefactoringUtil.getPersistence(jClass)) { 84 PUDataObject dataObject = ProviderUtil.getPUDataObject(ddFile); 85 86 if (!ProviderUtil.isValid(dataObject)){ 87 Problem newProblem = 88 new Problem(false, NbBundle.getMessage(PersistenceXmlSafeDeleteRefactoring.class, "TXT_PersistenceXmlInvalidProblem", ddFile.getPath())); 89 problem = Utility.addProblemsToEnd(problem, newProblem); 90 continue; 91 } 92 93 94 PersistenceUnit[] persistenceUnits = ProviderUtil.getPersistenceUnits(dataObject); 95 96 for (int i = 0; i < persistenceUnits.length; i++) { 97 PersistenceUnit unit = persistenceUnits[i]; 98 99 for (int j = 0; j < unit.getClass2().length; j++) { 100 101 if (unit.getClass2()[j].equals(jClass.getName())){ 102 103 RefactoringElementImplementation refactoringElem = 104 new PersistenceXmlSafeDeleteRefactoringElement(unit, jClass.getName(), dataObject, ddFile); 105 106 refactoringElementsBag.add(safeDeleteRefactoring, refactoringElem); 107 } 108 109 } 110 } 111 112 } 113 } 114 return problem; 115 } 116 117 120 private static class PersistenceXmlSafeDeleteRefactoringElement extends AbstractSafeDeleteRefactoringElement implements ExternalChange { 121 122 private PersistenceUnit persistenceUnit; 123 private PUDataObject puDataObject; 124 private String clazz; 125 126 127 public PersistenceXmlSafeDeleteRefactoringElement(PersistenceUnit persistenceUnit, String clazz, PUDataObject puDataObject, FileObject parentFile) { 128 this.parentFile = parentFile; 129 this.clazz = clazz; 130 this.persistenceUnit = persistenceUnit; 131 this.puDataObject = puDataObject; 132 } 133 134 138 public String getDisplayText() { 139 Object [] args = new Object [] {parentFile.getNameExt(), clazz}; 140 return MessageFormat.format(NbBundle.getMessage(PersistenceXmlSafeDeleteRefactoringElement.class, "TXT_PersistenceXmlSafeDeleteClass"), clazz); 141 } 142 143 public void performExternalChange() { 144 ProviderUtil.removeManagedClass(persistenceUnit, clazz, puDataObject); 145 } 146 147 public void undoExternalChange() { 148 ProviderUtil.addManagedClass(persistenceUnit, clazz, puDataObject); 149 } 150 151 153 public void performChange() { 154 JavaMetamodel.getManager().registerExtChange(this); 155 } 156 157 public PositionBounds getPosition() { 158 try { 159 return new DefaultPositionBoundsResolver(DataObject.find(parentFile), clazz).getPositionBounds(); 160 } catch (DataObjectNotFoundException ex) { 161 ErrorManager.getDefault().notify(ex); 162 } 163 return null; 164 } 165 166 167 } 168 169 170 } 171 | Popular Tags |