KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > whereused > EntityWhereUsedRefactoring


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.refactoring.whereused;
21
22 import java.text.MessageFormat JavaDoc;
23 import javax.jmi.reflect.RefObject;
24 import org.netbeans.jmi.javamodel.Feature;
25 import org.netbeans.jmi.javamodel.Field;
26 import org.netbeans.jmi.javamodel.JavaClass;
27 import org.netbeans.jmi.javamodel.Method;
28 import org.netbeans.jmi.javamodel.Resource;
29 import org.netbeans.modules.j2ee.refactoring.EntityAnnotationReference;
30 import org.netbeans.modules.j2ee.refactoring.EntityAssociationResolver;
31 import org.netbeans.modules.j2ee.refactoring.PersistenceRefactoringUtil;
32 import org.netbeans.modules.j2ee.refactoring.jaxwssupport.JaxWsXmlRefactoringSupport;
33 import org.netbeans.modules.javacore.api.JavaModel;
34 import org.netbeans.modules.refactoring.api.AbstractRefactoring;
35 import org.netbeans.modules.refactoring.api.Problem;
36 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
37 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
38 import org.openide.filesystems.FileObject;
39 import org.openide.util.NbBundle;
40
41 /**
42  * This class collects 'mappedBy' annotation references to classes.
43  *
44  * @author Erno Mononen
45  */

46 public class EntityWhereUsedRefactoring extends JaxWsXmlRefactoringSupport {
47     
48     /**
49      * Creates a new instance of EntityWhereUsedRefactoring
50      */

51     public EntityWhereUsedRefactoring() {
52     }
53     
54     /**
55      * Collects 'mappedBy' annotations references to the given property.
56      */

57     public Problem prepare(AbstractRefactoring refactoring, RefObject refObject, RefactoringElementsBag refactoringElements) {
58         
59         if (!(refObject instanceof Method || refObject instanceof Field)){
60             return null;
61         }
62         
63         Feature property = (Feature) refObject;
64         JavaClass javaClass = (JavaClass) property.getDeclaringClass();
65         if (!PersistenceRefactoringUtil.isEntity(javaClass)){
66             return null;
67         }
68         EntityAssociationResolver resolver
69                 = new EntityAssociationResolver(javaClass, PersistenceRefactoringUtil.getEntitiesInProject(javaClass));
70         
71         for (EntityAnnotationReference reference : resolver.getMappedByReferences(property)) {
72             Resource res = reference.getReferring().getResource();
73             FileObject fo = JavaModel.getFileObject(res);
74             RefactoringElementImplementation elem = new EntityWhereUsedElement(reference, fo);
75             refactoringElements.add(refactoring, elem);
76             
77         }
78         
79         return null;
80     }
81     
82     private static class EntityWhereUsedElement extends AbstractWhereUsedRefactoringElement {
83         
84         private EntityAnnotationReference reference;
85         
86         public EntityWhereUsedElement(EntityAnnotationReference reference, FileObject parentFile) {
87             this.reference = reference;
88             this.parentFile = parentFile;
89         }
90         
91         public String JavaDoc getDisplayText() {
92             Object JavaDoc[] args = new Object JavaDoc [] {reference.getAnnotation().getType().getName(), reference.getReferringProperty().getName()};
93             return MessageFormat.format(NbBundle.getMessage(SunJaxWsXmlWhereUsedRefactoring.class, "TXT_EntityWhereUsed"), args);
94         }
95     }
96 }
Popular Tags