KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbfreeform > LookupProviderImpl


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.ejbfreeform;
21
22 import org.netbeans.api.project.Project;
23 import org.netbeans.modules.ant.freeform.spi.HelpIDFragmentProvider;
24 import org.netbeans.modules.ant.freeform.spi.ProjectAccessor;
25 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
26 import org.netbeans.modules.j2ee.spi.ejbjar.support.EjbEnterpriseReferenceContainerSupport;
27 import org.netbeans.spi.project.AuxiliaryConfiguration;
28 import org.netbeans.spi.project.LookupProvider;
29 import org.netbeans.spi.project.support.ant.AntProjectEvent;
30 import org.netbeans.spi.project.support.ant.AntProjectHelper;
31 import org.netbeans.spi.project.support.ant.AntProjectListener;
32 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
33 import org.netbeans.spi.project.ui.PrivilegedTemplates;
34 import org.netbeans.spi.project.ui.ProjectOpenedHook;
35 import org.netbeans.spi.project.ui.RecommendedTemplates;
36 import org.openide.util.Lookup;
37 import org.openide.util.lookup.Lookups;
38 import org.openide.util.lookup.ProxyLookup;
39
40 /**
41  *
42  * @author mkleint
43  */

44 public class LookupProviderImpl implements LookupProvider {
45     
46     private static final String JavaDoc HELP_ID_FRAGMENT = "ejb"; // NOI18N
47

48     /** Creates a new instance of LookupProviderImpl */
49     public LookupProviderImpl() {
50     }
51     
52     public Lookup createAdditionalLookup(Lookup baseContext) {
53         Project prj = (Project)baseContext.lookup(Project.class);
54         ProjectAccessor acc = (ProjectAccessor)baseContext.lookup(ProjectAccessor.class);
55         AuxiliaryConfiguration aux = (AuxiliaryConfiguration)baseContext.lookup(AuxiliaryConfiguration.class);
56         assert aux != null;
57         assert prj != null;
58         assert acc != null;
59         return new ProjectLookup(prj, acc.getHelper(), acc.getEvaluator(), aux);
60     }
61     
62     private static Lookup initLookup(Project project, AntProjectHelper projectHelper, PropertyEvaluator projectEvaluator, AuxiliaryConfiguration aux) {
63         
64         EJBFreeformProvider ejbFFProvider = new EJBFreeformProvider(project, projectHelper, projectEvaluator);
65         EJBFreeformModule ejbFFModule = new EJBFreeformModule(project, projectHelper, projectEvaluator);
66         ejbFFProvider.setJ2eeModule(ejbFFModule);
67                 
68         return Lookups.fixed(new Object JavaDoc[] {
69             ejbFFProvider,
70             ejbFFModule,
71             new EJBModules(project, projectHelper, projectEvaluator), // EJBModuleProvider, ClassPathProvider
72
new PrivilegedTemplatesImpl(), // List of templates in New action popup
73
EjbEnterpriseReferenceContainerSupport.createEnterpriseReferenceContainer(project, projectHelper),
74             new EjbFreeFormActionProvider(project, projectHelper, aux),
75             new ProjectOpenedHookImpl(project),
76             new LookupMergerImpl(),
77             new HelpIDFragmentProviderImpl(),
78         });
79     }
80     
81     private static final class HelpIDFragmentProviderImpl implements HelpIDFragmentProvider {
82         public String JavaDoc getHelpIDFragment() {
83             return HELP_ID_FRAGMENT;
84         }
85     }
86     
87     private static final class ProjectLookup extends ProxyLookup implements AntProjectListener {
88
89         private AntProjectHelper helper;
90         private PropertyEvaluator evaluator;
91         private Project project;
92         private AuxiliaryConfiguration aux;
93         private boolean isMyProject;
94         
95         public ProjectLookup(Project project, AntProjectHelper helper, PropertyEvaluator evaluator, AuxiliaryConfiguration aux) {
96             super(new Lookup[0]);
97             this.project = project;
98             this.helper = helper;
99             this.evaluator = evaluator;
100             this.aux = aux;
101             this.isMyProject = EJBProjectNature.isMyProject(aux);
102             updateLookup();
103             helper.addAntProjectListener(this);
104         }
105         
106         private void updateLookup() {
107             Lookup l = Lookup.EMPTY;
108             if (isMyProject) {
109                 l = initLookup(project, helper, evaluator, aux);
110             }
111             setLookups(new Lookup[]{l});
112         }
113         
114         public void configurationXmlChanged(AntProjectEvent ev) {
115             if (EJBProjectNature.isMyProject(aux) != isMyProject) {
116                 isMyProject = !isMyProject;
117                 updateLookup();
118             }
119         }
120         
121         public void propertiesChanged(AntProjectEvent ev) {
122             // ignore
123
}
124         
125     }
126     
127     private static final class ProjectOpenedHookImpl extends ProjectOpenedHook {
128         
129         private Project project;
130         
131         public ProjectOpenedHookImpl(Project project) {
132             this.project = project;
133         }
134         
135         protected void projectOpened() {
136             // initialize the server configuration
137
J2eeModuleProvider j2eeModule = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class);
138             j2eeModule.getConfigSupport().ensureConfigurationReady();
139         }
140
141         protected void projectClosed() {
142         }
143     }
144
145     private static final class PrivilegedTemplatesImpl implements PrivilegedTemplates, RecommendedTemplates {
146         
147         private static final String JavaDoc[] PRIVILEGED_NAMES = new String JavaDoc[] {
148             "Templates/J2EE/Session", // NOI18N
149
"Templates/J2EE/Entity", // NOI18N
150
"Templates/J2EE/ServiceLocator.java", // NOI18N
151
"Templates/Classes/Class.java" // NOI18N
152
};
153         
154         private static final String JavaDoc[] RECOMENDED_TYPES = new String JavaDoc[] {
155             "java-classes", // NOI18N
156
"ejb-types", // NOI18N
157
"ejb-types_2_1", // NOI18N
158
"j2ee-types", // NOI18N
159
"java-beans", // NOI18N
160
"oasis-XML-catalogs", // NOI18N
161
"XML", // NOI18N
162
"wsdl", // NOI18N
163
"ant-script", // NOI18N
164
"ant-task", // NOI18N
165
"junit", // NOI18N
166
"simple-files" // NOI18N
167
};
168         
169         public String JavaDoc[] getPrivilegedTemplates() {
170             return PRIVILEGED_NAMES;
171         }
172
173         public String JavaDoc[] getRecommendedTypes() {
174             return RECOMENDED_TYPES;
175         }
176      
177     
178     
179     }
180     
181
182 }
183
Popular Tags