KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > Utils


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.ejbjarproject;
21 import java.util.ArrayList JavaDoc;
22 import org.netbeans.api.project.ui.OpenProjects;
23 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
24 import org.openide.DialogDisplayer;
25 import org.openide.NotifyDescriptor;
26 import org.openide.WizardDescriptor;
27 import javax.swing.*;
28 import java.io.File JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Set JavaDoc;
33 import org.netbeans.api.java.classpath.GlobalPathRegistry;
34 import org.netbeans.api.project.FileOwnerQuery;
35 import org.netbeans.api.project.Project;
36 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeAppProvider;
37 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
38 import org.openide.filesystems.FileObject;
39
40
41 public class Utils {
42
43     private static final String JavaDoc WIZARD_PANEL_CONTENT_DATA = "WizardPanel_contentData"; // NOI18N
44
private static final String JavaDoc WIZARD_PANEL_CONTENT_SELECTED_INDEX = "WizardPanel_contentSelectedIndex"; //NOI18N;
45

46     public static String JavaDoc toClasspathString(File JavaDoc[] classpathEntries) {
47         if (classpathEntries == null) {
48             return "";
49         }
50         StringBuffer JavaDoc classpath = new StringBuffer JavaDoc();
51         for (int i = 0; i < classpathEntries.length; i++) {
52             classpath.append(classpathEntries[i].getAbsolutePath());
53             if (i + 1 < classpathEntries.length) {
54                 classpath.append(":"); // NOI18N
55
}
56         }
57         return classpath.toString();
58     }
59
60     public static void notifyError(Exception JavaDoc ex) {
61         NotifyDescriptor ndd = new NotifyDescriptor.Message(ex.getMessage(), NotifyDescriptor.ERROR_MESSAGE);
62         DialogDisplayer.getDefault().notify(ndd);
63     }
64
65     public static void mergeSteps(WizardDescriptor wizard, WizardDescriptor.Panel[] panels, String JavaDoc[] steps) {
66         Object JavaDoc prop = wizard.getProperty (WIZARD_PANEL_CONTENT_DATA);
67         String JavaDoc[] beforeSteps;
68         int offset;
69         if (prop instanceof String JavaDoc[]) {
70             beforeSteps = (String JavaDoc[]) prop;
71             offset = beforeSteps.length;
72             if (offset > 0 && ("...".equals(beforeSteps[offset - 1]))) {// NOI18N
73
offset--;
74             }
75         } else {
76             beforeSteps = null;
77             offset = 0;
78         }
79         String JavaDoc[] resultSteps = new String JavaDoc[ (offset) + panels.length];
80         for (int i = 0; i < offset; i++) {
81             resultSteps[i] = beforeSteps[i];
82         }
83         setSteps(panels, steps, resultSteps, offset);
84     }
85
86     private static void setSteps(WizardDescriptor.Panel[] panels, String JavaDoc[] steps, String JavaDoc[] resultSteps, int offset) {
87         int n = steps == null ? 0 : steps.length;
88         for (int i = 0; i < panels.length; i++) {
89             final JComponent component = (JComponent) panels[i].getComponent();
90             String JavaDoc step = i < n ? steps[i] : null;
91             if (step == null) {
92                 step = component.getName();
93             }
94             component.putClientProperty (WIZARD_PANEL_CONTENT_DATA, resultSteps);
95             component.putClientProperty(WIZARD_PANEL_CONTENT_SELECTED_INDEX, new Integer JavaDoc(i));
96             component.getAccessibleContext().setAccessibleDescription (step);
97             resultSteps[i + offset] = step;
98         }
99     }
100
101     public static void setSteps(WizardDescriptor.Panel[] panels, String JavaDoc[] steps) {
102         setSteps(panels, steps, steps, 0);
103     }
104
105     public static boolean areInSameJ2EEApp(Project p1, Project p2) {
106         Set JavaDoc globalPath = GlobalPathRegistry.getDefault().getSourceRoots();
107         Iterator JavaDoc iter = globalPath.iterator();
108         while (iter.hasNext()) {
109             FileObject sourceRoot = (FileObject)iter.next();
110             Project project = FileOwnerQuery.getOwner(sourceRoot);
111             if (project != null) {
112                 Object JavaDoc j2eeAppProvider = project.getLookup().lookup(J2eeAppProvider.class);
113                 if (j2eeAppProvider != null) { // == it is j2ee app
114
J2eeAppProvider j2eeApp = (J2eeAppProvider)j2eeAppProvider;
115                     J2eeModuleProvider[] j2eeModules = j2eeApp.getChildModuleProviders();
116                     if ((j2eeModules != null) && (j2eeModules.length > 0)) { // == there are some modules in the j2ee app
117
J2eeModuleProvider affectedPrjProvider1 =
118                                 (J2eeModuleProvider)p1.getLookup().lookup(J2eeModuleProvider.class);
119                         J2eeModuleProvider affectedPrjProvider2 =
120                                 (J2eeModuleProvider)p2.getLookup().lookup(J2eeModuleProvider.class);
121                         if (affectedPrjProvider1 != null && affectedPrjProvider2 != null) {
122                             List JavaDoc childModules = Arrays.asList(j2eeModules);
123                             if (childModules.contains(affectedPrjProvider1) &&
124                                 childModules.contains(affectedPrjProvider2)) {
125                                 return true;
126                             }
127                         }
128                     }
129                 }
130             }
131         }
132         return false;
133     }
134
135     // =========================================================================
136

137     // utils for ejb code synchronization
138

139     
140     
141     /** Returns list of all EJB projects that can be called from the caller project.
142      *
143      * @param enterpriseProject the caller enterprise project
144      */

145     public static Project [] getCallableEjbProjects (Project enterpriseProject) {
146         Project[] allProjects = OpenProjects.getDefault().getOpenProjects();
147         
148         boolean isCallerEJBModule = false;
149         J2eeModuleProvider callerJ2eeModuleProvider = (J2eeModuleProvider) enterpriseProject.getLookup().lookup(J2eeModuleProvider.class);
150         if (callerJ2eeModuleProvider != null && callerJ2eeModuleProvider.getJ2eeModule().getModuleType().equals(J2eeModule.EJB)) {
151             // TODO: HACK - this should be set by calling AntArtifactQuery.findArtifactsByType(p, EjbProjectConstants.ARTIFACT_TYPE_EJBJAR)
152
// but now freeform doesn't implement this correctly
153
isCallerEJBModule = true;
154         }
155         // TODO: HACK - this must be solved by freeform's own implementation of EnterpriseReferenceContainer, see issue 57003
156
// call ejb should not make this check, all should be handled in EnterpriseReferenceContainer
157
boolean isCallerFreeform = enterpriseProject.getClass().getName().equals("org.netbeans.modules.ant.freeform.FreeformProject");
158         
159         List JavaDoc /*<Project>*/ filteredResults = new ArrayList JavaDoc(allProjects.length);
160         for (int i = 0; i < allProjects.length; i++) {
161             boolean isEJBModule = false;
162             J2eeModuleProvider j2eeModuleProvider = (J2eeModuleProvider) allProjects[i].getLookup().lookup(J2eeModuleProvider.class);
163             if (j2eeModuleProvider != null && j2eeModuleProvider.getJ2eeModule().getModuleType().equals(J2eeModule.EJB)) {
164                 isEJBModule = true;
165             }
166             if ((isEJBModule && !isCallerFreeform) ||
167                 (isCallerFreeform && enterpriseProject.equals(allProjects[i]))) {
168                 filteredResults.add(allProjects[i]);
169             }
170         }
171         return (Project []) filteredResults.toArray(new Project[filteredResults.size()]);
172     }
173
174 }
175
Popular Tags