KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > common > Util


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.common;
21
22 import java.awt.Component JavaDoc;
23 import javax.swing.JLabel JavaDoc;
24 import java.awt.Container JavaDoc;
25 import java.io.File JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import javax.swing.JComponent JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Set JavaDoc;
37 import org.netbeans.api.java.classpath.ClassPath;
38 import org.netbeans.api.java.project.JavaProjectConstants;
39 import org.netbeans.api.java.queries.UnitTestForSourceQuery;
40 import org.netbeans.api.project.Project;
41 import org.netbeans.api.project.ProjectUtils;
42 import org.netbeans.api.project.SourceGroup;
43 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
44 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
45 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
46 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
47 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
48 import org.netbeans.spi.java.queries.SourceLevelQueryImplementation;
49 import org.openide.ErrorManager;
50
51 import org.openide.filesystems.FileObject;
52 import org.openide.filesystems.URLMapper;
53
54 public class Util {
55     
56     /*
57      * Changes the text of a JLabel in component from oldLabel to newLabel
58      */

59     public static void changeLabelInComponent(JComponent JavaDoc component, String JavaDoc oldLabel, String JavaDoc newLabel) {
60         JLabel JavaDoc label = findLabel(component, oldLabel);
61         if(label != null) {
62             label.setText(newLabel);
63         }
64     }
65     
66     /*
67      * Hides a JLabel and the component that it is designated to labelFor, if any
68      */

69     public static void hideLabelAndLabelFor(JComponent JavaDoc component, String JavaDoc lab) {
70         JLabel JavaDoc label = findLabel(component, lab);
71         if(label != null) {
72             label.setVisible(false);
73             Component JavaDoc c = label.getLabelFor();
74             if(c != null) {
75                 c.setVisible(false);
76             }
77         }
78     }
79     
80     /*
81      * Recursively gets all components in the components array and puts it in allComponents
82      */

83     public static void getAllComponents( Component JavaDoc[] components, Collection JavaDoc<Component JavaDoc> allComponents ) {
84         for( int i = 0; i < components.length; i++ ) {
85             if( components[i] != null ) {
86                 allComponents.add( components[i] );
87                 if( ( ( Container JavaDoc )components[i] ).getComponentCount() != 0 ) {
88                     getAllComponents( ( ( Container JavaDoc )components[i] ).getComponents(), allComponents );
89                 }
90             }
91         }
92     }
93     
94     /*
95      * Recursively finds a JLabel that has labelText in comp
96      */

97     public static JLabel JavaDoc findLabel(JComponent JavaDoc comp, String JavaDoc labelText) {
98         List JavaDoc<Component JavaDoc> allComponents = new ArrayList JavaDoc<Component JavaDoc>();
99         getAllComponents(comp.getComponents(), allComponents);
100         Iterator JavaDoc<Component JavaDoc> iterator = allComponents.iterator();
101         while(iterator.hasNext()) {
102             Component JavaDoc c = iterator.next();
103             if(c instanceof JLabel JavaDoc) {
104                 JLabel JavaDoc label = (JLabel JavaDoc)c;
105                 if(label.getText().equals(labelText)) {
106                     return label;
107                 }
108             }
109         }
110         return null;
111     }
112     
113     /**
114      * Returns Java source groups for all source packages in given project.<br>
115      * Doesn't include test packages.
116      *
117      * @param project Project to search
118      * @return Array of SourceGroup. It is empty if any probelm occurs.
119      */

120     public static SourceGroup[] getJavaSourceGroups(Project project) {
121         SourceGroup[] sourceGroups = ProjectUtils.getSources(project).getSourceGroups(
122                 JavaProjectConstants.SOURCES_TYPE_JAVA);
123         Set JavaDoc<SourceGroup> testGroups = getTestSourceGroups(sourceGroups);
124         List JavaDoc<SourceGroup> result = new ArrayList JavaDoc<SourceGroup>();
125         for (int i = 0; i < sourceGroups.length; i++) {
126             if (!testGroups.contains(sourceGroups[i])) {
127                 result.add(sourceGroups[i]);
128             }
129         }
130         return result.toArray(new SourceGroup[result.size()]);
131     }
132     
133     private static Set JavaDoc<SourceGroup> getTestSourceGroups(SourceGroup[] sourceGroups) {
134         Map JavaDoc<FileObject, SourceGroup> foldersToSourceGroupsMap = createFoldersToSourceGroupsMap(sourceGroups);
135         Set JavaDoc<SourceGroup> testGroups = new HashSet JavaDoc<SourceGroup>();
136         for (int i = 0; i < sourceGroups.length; i++) {
137             testGroups.addAll(getTestTargets(sourceGroups[i], foldersToSourceGroupsMap));
138         }
139         return testGroups;
140     }
141     
142     private static Map JavaDoc<FileObject, SourceGroup> createFoldersToSourceGroupsMap(final SourceGroup[] sourceGroups) {
143         Map JavaDoc<FileObject, SourceGroup> result;
144         if (sourceGroups.length == 0) {
145             result = Collections.<FileObject, SourceGroup>emptyMap();
146         } else {
147             result = new HashMap JavaDoc<FileObject, SourceGroup>(2 * sourceGroups.length, .5f);
148             for (int i = 0; i < sourceGroups.length; i++) {
149                 SourceGroup sourceGroup = sourceGroups[i];
150                 result.put(sourceGroup.getRootFolder(), sourceGroup);
151             }
152         }
153         return result;
154     }
155     
156     private static List JavaDoc<FileObject> getFileObjects(URL JavaDoc[] urls) {
157         List JavaDoc<FileObject> result = new ArrayList JavaDoc<FileObject>();
158         for (int i = 0; i < urls.length; i++) {
159             FileObject sourceRoot = URLMapper.findFileObject(urls[i]);
160             if (sourceRoot != null) {
161                 result.add(sourceRoot);
162             } else {
163                 int severity = ErrorManager.INFORMATIONAL;
164                 if (ErrorManager.getDefault().isNotifiable(severity)) {
165                     ErrorManager.getDefault().notify(severity, new IllegalStateException JavaDoc(
166                             "No FileObject found for the following URL: " + urls[i])); //NOI18N
167
}
168             }
169         }
170         return result;
171     }
172     
173     private static List JavaDoc<SourceGroup> getTestTargets(SourceGroup sourceGroup, Map JavaDoc<FileObject, SourceGroup> foldersToSourceGroupsMap) {
174         final URL JavaDoc[] rootURLs = UnitTestForSourceQuery.findUnitTests(sourceGroup.getRootFolder());
175         if (rootURLs.length == 0) {
176             return new ArrayList JavaDoc<SourceGroup>();
177         }
178         List JavaDoc<SourceGroup> result = new ArrayList JavaDoc<SourceGroup>();
179         List JavaDoc<FileObject> sourceRoots = getFileObjects(rootURLs);
180         for (int i = 0; i < sourceRoots.size(); i++) {
181             FileObject sourceRoot = sourceRoots.get(i);
182             SourceGroup srcGroup = foldersToSourceGroupsMap.get(sourceRoot);
183             if (srcGroup != null) {
184                 result.add(srcGroup);
185             }
186         }
187         return result;
188     }
189     
190     public static ClassPath getFullClasspath(FileObject fo) {
191         if (fo == null) {
192             return null;
193         }
194         return ClassPathSupport.createProxyClassPath(new ClassPath[]{
195             ClassPath.getClassPath(fo, ClassPath.SOURCE),
196             ClassPath.getClassPath(fo, ClassPath.BOOT),
197             ClassPath.getClassPath(fo, ClassPath.COMPILE)
198         });
199     }
200     
201     /**
202      * Is J2EE version of a given project JavaEE 5 or higher?
203      *
204      * @param project J2EE project
205      * @return true if J2EE version is JavaEE 5 or higher; otherwise false
206      */

207     public static boolean isJavaEE5orHigher(Project project) {
208         if (project == null) {
209             return false;
210         }
211         J2eeModuleProvider j2eeModuleProvider = project.getLookup().lookup(J2eeModuleProvider.class);
212         if (j2eeModuleProvider != null) {
213             J2eeModule j2eeModule = j2eeModuleProvider.getJ2eeModule();
214             if (j2eeModule != null) {
215                 Object JavaDoc type = j2eeModule.getModuleType();
216                 double version = Double.parseDouble(j2eeModule.getModuleVersion());
217                 if (J2eeModule.EJB.equals(type) && (version > 2.1)) {
218                     return true;
219                 };
220                 if (J2eeModule.WAR.equals(type) && (version > 2.4)) {
221                     return true;
222                 }
223                 if (J2eeModule.CLIENT.equals(type) && (version > 1.4)) {
224                     return true;
225                 }
226             }
227         }
228         return false;
229     }
230     
231     /**
232      * Returns source level of a given project
233      *
234      * @param project Project
235      * @return source level string representation, e.g. "1.6"
236      */

237     public static String JavaDoc getSourceLevel(Project project) {
238         SourceLevelQueryImplementation sl = project.getLookup().lookup(SourceLevelQueryImplementation.class);
239         return sl.getSourceLevel(project.getProjectDirectory());
240     }
241     
242     /**
243      * Is source level of a given project 1.4 or lower?
244      *
245      * @param project Project
246      * @return true if source level is 1.4 or lower; otherwise false
247      */

248     public static boolean isSourceLevel14orLower(Project project) {
249         String JavaDoc srcLevel = getSourceLevel(project);
250         if (srcLevel != null) {
251             double sourceLevel = Double.parseDouble(srcLevel);
252             return (sourceLevel <= 1.4);
253         } else
254             return false;
255     }
256     
257     /**
258      * Is source level of a given project 1.6 or higher?
259      *
260      * @param project Project
261      * @return true if source level is 1.6 or higher; otherwise false
262      */

263     public static boolean isSourceLevel16orHigher(Project project) {
264         String JavaDoc srcLevel = getSourceLevel(project);
265         if (srcLevel != null) {
266             double sourceLevel = Double.parseDouble(srcLevel);
267             return (sourceLevel >= 1.6);
268         } else
269             return false;
270     }
271     
272     /**
273      * Checks whether the given <code>project</code>'s target server instance
274      * is present.
275      *
276      * @param project the project to check; can not be null.
277      * @return true if the target server instance of the given project
278      * exists, false otherwise.
279      *
280      * @since 1.8
281      */

282     public static boolean isValidServerInstance(Project project) {
283         J2eeModuleProvider j2eeModuleProvider = project.getLookup().lookup(J2eeModuleProvider.class);
284         if (j2eeModuleProvider == null) {
285             return false;
286         }
287         return isValidServerInstance(j2eeModuleProvider);
288     }
289     
290     /**
291      * Checks whether the given <code>provider</code>'s target server instance
292      * is present.
293      *
294      * @param provider the provider to check; can not be null.
295      * @return true if the target server instance of the given provider
296      * exists, false otherwise.
297      *
298      * @since 1.10
299      */

300     public static boolean isValidServerInstance(J2eeModuleProvider j2eeModuleProvider) {
301         String JavaDoc serverInstanceID = j2eeModuleProvider.getServerInstanceID();
302         if (serverInstanceID == null) {
303             return false;
304         }
305         return Deployment.getDefault().getServerID(serverInstanceID) != null;
306     }
307     
308     public static File JavaDoc[] getJ2eePlatformClasspathEntries(Project project) {
309         List JavaDoc<FileObject> j2eePlatformRoots = new ArrayList JavaDoc<FileObject>();
310         if (project != null) {
311             J2eeModuleProvider j2eeModuleProvider = project.getLookup().lookup(J2eeModuleProvider.class);
312             if (j2eeModuleProvider != null) {
313                 J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(j2eeModuleProvider.getServerInstanceID());
314                 if (j2eePlatform != null) {
315                     return j2eePlatform.getClasspathEntries();
316                 }
317             }
318         }
319         return new File JavaDoc[0];
320     }
321     
322 }
323
Popular Tags