001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * Util.java
044: *
045: * Created on August 22, 2005, 8:44 PM
046: *
047: */
048: package org.netbeans.modules.mobility.end2end.util;
049:
050: import java.io.File;
051: import java.io.IOException;
052: import javax.swing.JFileChooser;
053: import org.netbeans.api.project.Project;
054: import org.netbeans.api.project.ProjectInformation;
055: import org.netbeans.api.project.ProjectManager;
056: import org.netbeans.api.project.SourceGroup;
057: import org.netbeans.api.project.libraries.Library;
058: import org.netbeans.api.project.libraries.LibraryManager;
059: import org.netbeans.api.project.ui.OpenProjects; //import org.netbeans.jmi.javamodel.JavaClass;
060: //import org.netbeans.jmi.javamodel.Resource;
061: import org.netbeans.modules.mobility.end2end.client.config.Configuration;
062: import org.netbeans.modules.j2ee.dd.api.web.DDProvider;
063: import org.netbeans.modules.j2ee.dd.api.web.Servlet;
064: import org.netbeans.modules.j2ee.dd.api.web.ServletMapping;
065: import org.netbeans.modules.j2ee.dd.api.web.WebApp;
066: import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
067: import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; //import org.netbeans.modules.javacore.api.JavaModel;
068: import org.netbeans.modules.mobility.javon.JavonMapping;
069: import org.netbeans.modules.mobility.project.DefaultPropertiesDescriptor;
070: import org.netbeans.modules.mobility.project.J2MEProject;
071: import org.netbeans.modules.web.api.webmodule.WebModule;
072: import org.netbeans.modules.web.spi.webmodule.WebModuleProvider;
073: import org.netbeans.modules.mobility.project.ProjectConfigurationsHelper;
074: import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender;
075: import org.netbeans.spi.project.support.ant.AntProjectHelper;
076: import org.netbeans.spi.project.support.ant.EditableProperties;
077: import org.netbeans.spi.project.ui.support.ProjectChooser;
078: import org.openide.DialogDisplayer;
079: import org.openide.ErrorManager;
080: import org.openide.NotifyDescriptor;
081: import org.openide.filesystems.FileObject;
082: import org.openide.filesystems.FileUtil;
083: import org.openide.util.Exceptions;
084: import org.openide.util.NbBundle;
085: import org.openide.windows.WindowManager;
086:
087: /**
088: *
089: * @author suchys
090: */
091: public final class Util {
092:
093: private Util() {
094: //to avoid instantiation
095: }
096:
097: public static SourceGroup getPreselectedGroup(
098: final SourceGroup[] groups, final String preselectedFolder) {
099: if (preselectedFolder != null) {
100: for (int i = 0; i < groups.length; i++) {
101: if (groups[i].getName().equals(preselectedFolder)) {
102: return groups[i];
103: }
104: }
105: }
106: return groups.length >= 0 ? groups[0] : null;
107: }
108:
109: public static Project openProject() {
110: return openProject(null);
111: }
112:
113: public static Project openProject(final String projectFolder) {
114: Project project = null;
115: if (projectFolder != null) {
116: final File folder = new File(projectFolder);
117: if (folder.exists() && folder.isDirectory()) {
118: ProjectChooser.setProjectsFolder(folder);
119: }
120: }
121: final JFileChooser chooser = ProjectChooser.projectChooser();
122: final NotifyDescriptor.Message message1 = new NotifyDescriptor.Message(
123: NbBundle.getMessage(Util.class, "MSG_notProjectDir"), // NOI18N
124: NotifyDescriptor.WARNING_MESSAGE);
125: final NotifyDescriptor.Message message2 = new NotifyDescriptor.Message(
126: NbBundle.getMessage(Util.class, "ERR_NoWebProject"), // NOI18N
127: NotifyDescriptor.WARNING_MESSAGE);
128:
129: while (true) { // Cycle while users does some reasonable action e.g.
130: // select project dir or cancel the chooser
131: final int option = chooser.showOpenDialog(WindowManager
132: .getDefault().getMainWindow()); // Sow the chooser
133:
134: if (option == JFileChooser.APPROVE_OPTION) {
135: final File projectDir = FileUtil.normalizeFile(chooser
136: .getSelectedFile());
137:
138: project = fileToProject(projectDir);
139: if (project == null) {
140: DialogDisplayer.getDefault().notify(message1);
141: } else {
142: if (!isWebProject(project)) {
143: DialogDisplayer.getDefault().notify(message2);
144: } else {
145: OpenProjects.getDefault().open(
146: new Project[] { project }, true);
147: break; // and exit the loop
148: }
149: }
150: } else {
151: return null; // OK user changed his mind and won't open anything
152: // Don't remeber the last selected dir
153: }
154: }
155:
156: return project;
157: }
158:
159: public static boolean isWebProject(final Project p) {
160: if (p == null) {
161: return false;
162: }
163: final WebModuleProvider provider = p.getLookup().lookup(
164: WebModuleProvider.class);
165: if (provider != null) {
166: return true;
167: }
168: return false;
169: }
170:
171: public static Project fileToProject(final File projectDir) {
172: try {
173: final FileObject fo = FileUtil.toFileObject(FileUtil
174: .normalizeFile(projectDir));
175: if (fo != null) {
176: return ProjectManager.getDefault().findProject(fo);
177: }
178: return null;
179: } catch (IOException e) {
180: return null;
181: }
182:
183: }
184:
185: public static Project getServerProject(
186: final Configuration configuration) {
187: Project result = null;
188: final OpenProjects openProject = OpenProjects.getDefault();
189: final Project[] openedProjects = openProject.getOpenProjects();
190: final String serverProjectName = configuration
191: .getServerConfigutation().getProjectName();
192: //for( int i = 0; i < openedProjects.length; i++ ) {
193: for (final Project p : openedProjects) {
194: final ProjectInformation pi = p.getLookup().lookup(
195: ProjectInformation.class);
196: final String webProjectName = pi.getName();
197: if (serverProjectName.equals(webProjectName)) {
198: result = p;
199: break;
200: }
201: }
202: return result;
203: }
204:
205: public static void addServletToWebProject(final Project project,
206: JavonMapping mapping) {
207: /* mark Servlet */
208: try {
209: boolean servlet = false;
210:
211: String servletName = mapping.getServerMapping()
212: .getClassName();
213: String packageName = mapping.getServerMapping()
214: .getPackageName();
215: String servletClassFQN = packageName.length() != 0 ? packageName
216: + '.' + servletName
217: : servletName;
218: FileObject fo = null;
219: final WebModuleProvider provider = project.getLookup()
220: .lookup(WebModuleProvider.class);
221: final WebModule wm = provider.findWebModule(project
222: .getProjectDirectory());
223: final WebApp webApp = DDProvider.getDefault()
224: .getDDRootCopy(fo = wm.getDeploymentDescriptor());
225: final Servlet[] servlets = webApp.getServlet();
226: for (int i = 0; i < servlets.length; i++) {
227: if (servlets[i].getServletClass().equals(
228: servletClassFQN)) {
229: servlet = true; //already contains
230: }
231: }
232:
233: if (!servlet) {
234: servletName = findFreeName(servletName, webApp);
235: final Servlet newServlet = (Servlet) webApp
236: .createBean("Servlet"); //NOI18N
237: newServlet.setServletName(servletName);
238: newServlet.setServletClass(servletClassFQN);
239: newServlet.setDescription(NbBundle.getMessage(
240: Util.class, "TXT_servletElementDescription"));
241: newServlet.setDisplayName("Javon service for : "
242: + servletClassFQN); // NOI18N
243: webApp.addServlet(newServlet);
244:
245: final ServletMapping newServletMapping = (ServletMapping) webApp
246: .createBean("ServletMapping"); //NOI18N
247: newServletMapping.setServletName(servletName);
248: newServletMapping.setUrlPattern("/servlet/"
249: + servletClassFQN); //NOI18N
250: webApp.addServletMapping(newServletMapping);
251: }
252:
253: if (!servlet) {
254: webApp.write(fo);
255: }
256: } catch (Exception ex) {
257: ErrorManager.getDefault().notify(ex);
258: }
259: }
260:
261: private static String findFreeName(String servletName, WebApp webApp) {
262: int nameIndex = 0;
263: final Servlet[] servlets = webApp.getServlet();
264: for (int i = 0; i < servlets.length; i++) {
265: if (!servlets[i].getServletName().equals(servletName)) {
266: break;
267: }
268: servletName = servletName + (++nameIndex);
269: }
270: final ServletMapping[] servletsMapping = webApp
271: .getServletMapping();
272: for (int i = 0; i < servletsMapping.length; i++) {
273: if (!servletsMapping[i].getServletName()
274: .equals(servletName)) {
275: break;
276: }
277: servletName = servletName + (++nameIndex);
278: }
279: return servletName;
280: }
281:
282: // public static JavaClass resolveWebServiceClass( final FileObject projectFolder, final String fqn ) {
283: // assert projectFolder != null;
284: // assert fqn != null;
285: // final FileObject resObj = projectFolder.getFileObject( "build/generated/wsimport/client/" + fqn.replace('.','/') + ".java" ); //NOI18N
286: // if( resObj == null ) return null;
287: // resObj.refresh( false );
288: // final Resource res = JavaModel.getResource(resObj);
289: // return (JavaClass)res.getClassifiers().get(0);
290: // }
291:
292: public static String getServerURL(final Project p,
293: final Configuration configuration) {
294: String port = "8080"; //NOI18N
295: final J2eeModuleProvider provider = p.getLookup().lookup(
296: J2eeModuleProvider.class);
297: if (provider != null) {
298: final InstanceProperties ip = provider
299: .getInstanceProperties();
300: if (ip != null) {
301: port = ip
302: .getProperty(InstanceProperties.HTTP_PORT_NUMBER) != null ? ip
303: .getProperty(InstanceProperties.HTTP_PORT_NUMBER)
304: : "8080";//NOI18N
305: }
306: }
307: return "http://localhost:"
308: + port
309: + "/"
310: + configuration.getServerConfigutation()
311: .getProjectName()
312: + "/servlet/"
313: + //NOI18N
314: configuration.getServerConfigutation()
315: .getClassDescriptor().getType();
316:
317: }
318:
319: public static boolean isSuitableProjectConfiguration(
320: final Project project) {
321: if (!(project instanceof J2MEProject)) {
322: return false;
323: }
324: final String profile = evaluateProjectProperty(
325: (J2MEProject) project,
326: DefaultPropertiesDescriptor.PLATFORM_PROFILE);
327:
328: if ("MIDP-2.0".equals(profile)) { //NOI18N
329: return true;
330: }
331: return false;
332: }
333:
334: /**
335: * Gets currently used device screen size from J2ME project
336: * @param project
337: * @return
338: */
339: private static String evaluateProjectProperty(
340: final J2MEProject project, final String property) {
341: final AntProjectHelper helper = project.getLookup().lookup(
342: AntProjectHelper.class);
343: final EditableProperties ep = helper
344: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
345: final ProjectConfigurationsHelper confs = project
346: .getConfigurationHelper();
347: final String activeConfiguration = confs
348: .getActiveConfiguration() != confs
349: .getDefaultConfiguration() ? confs
350: .getActiveConfiguration().getDisplayName() : null;
351:
352: return evaluateProperty(ep, property, activeConfiguration);
353: }
354:
355: private static String evaluateProperty(final EditableProperties ep,
356: final String propertyName, final String configuration) {
357: if (configuration == null)
358: return ep.getProperty(propertyName);
359: final String value = ep.getProperty("configs." + configuration
360: + "." + propertyName); // NOI18N
361: return value != null ? value : evaluateProperty(ep,
362: propertyName, null);
363: }
364:
365: /**
366: *
367: * @param project
368: * @return
369: */
370: public static String getServerLocation(final Project project) {
371: return "localhost";
372: }
373:
374: public static String getServerPort(final Project project) {
375: String port = "8080"; //NOI18N
376: final J2eeModuleProvider provider = project.getLookup().lookup(
377: J2eeModuleProvider.class);
378: if (provider != null) {
379: final InstanceProperties ip = provider
380: .getInstanceProperties();
381: if (ip != null) {
382: port = ip
383: .getProperty(InstanceProperties.HTTP_PORT_NUMBER) != null ? ip
384: .getProperty(InstanceProperties.HTTP_PORT_NUMBER)
385: : "8080";//NOI18N
386: }
387: }
388: return port;
389: }
390:
391: /**
392: * Appends DataBinding library to the libraries
393: *
394: */
395: public static boolean registerDataBindingLibrary(Project p) {
396: ProjectClassPathExtender pcpe = p.getLookup().lookup(
397: ProjectClassPathExtender.class);
398: Library[] libraries = LibraryManager.getDefault()
399: .getLibraries();
400: for (Library library : libraries) {
401: if (library.getName().equals("DataBindingME")) {
402: try {
403: pcpe.addLibrary(library);
404: return true;
405: } catch (IOException ex) {
406: Exceptions.printStackTrace(ex);
407: return false;
408: }
409: }
410: }
411: return false;
412: }
413: }
|