KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > AppClientActionProvider


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.clientproject;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.event.MouseEvent JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.regex.Pattern JavaDoc;
33 import javax.swing.JButton JavaDoc;
34 import javax.swing.event.ChangeEvent JavaDoc;
35 import javax.swing.event.ChangeListener JavaDoc;
36 import org.apache.tools.ant.module.api.support.ActionUtils;
37 import org.netbeans.api.debugger.DebuggerManager;
38 import org.netbeans.api.debugger.Session;
39 import org.netbeans.api.debugger.jpda.AttachingDICookie;
40 import org.netbeans.api.fileinfo.NonRecursiveFolder;
41 import org.netbeans.api.java.classpath.ClassPath;
42 import org.netbeans.api.java.project.JavaProjectConstants;
43 import org.netbeans.api.java.source.SourceUtils;
44 import org.netbeans.api.project.ProjectInformation;
45 import org.netbeans.api.project.ProjectManager;
46 import org.netbeans.api.project.ProjectUtils;
47 import org.netbeans.modules.j2ee.api.ejbjar.EjbProjectConstants;
48 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientProjectProperties;
49 import org.netbeans.modules.j2ee.clientproject.ui.customizer.MainClassChooser;
50 import org.netbeans.modules.j2ee.clientproject.ui.customizer.MainClassWarning;
51 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
52 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
53 import org.netbeans.modules.j2ee.deployment.plugins.api.ServerDebugInfo;
54 import org.netbeans.spi.project.ActionProvider;
55 import org.netbeans.spi.project.support.ant.AntProjectHelper;
56 import org.netbeans.spi.project.support.ant.EditableProperties;
57 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
58 import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
59 import org.openide.DialogDescriptor;
60 import org.openide.DialogDisplayer;
61 import org.openide.ErrorManager;
62 import org.openide.NotifyDescriptor;
63 import org.openide.awt.MouseUtils;
64 import org.openide.filesystems.FileObject;
65 import org.openide.filesystems.FileUtil;
66 import org.openide.util.Lookup;
67 import org.openide.util.NbBundle;
68
69 /**
70  * Action provider of the Application Client project.
71  */

72 class AppClientActionProvider implements ActionProvider {
73     
74     private static final String JavaDoc COMMAND_COMPILE = "compile"; //NOI18N
75
private static final String JavaDoc COMMAND_VERIFY = "verify"; //NOI18N
76

77     // Commands available from Application Client project
78
private static final String JavaDoc[] supportedActions = {
79         COMMAND_BUILD,
80         COMMAND_CLEAN,
81         COMMAND_REBUILD,
82         COMMAND_COMPILE_SINGLE,
83         COMMAND_RUN,
84         COMMAND_RUN_SINGLE,
85         COMMAND_DEBUG,
86         COMMAND_DEBUG_SINGLE,
87         EjbProjectConstants.COMMAND_REDEPLOY,
88         JavaProjectConstants.COMMAND_JAVADOC,
89         COMMAND_TEST,
90         COMMAND_TEST_SINGLE,
91         COMMAND_DEBUG_TEST_SINGLE,
92         JavaProjectConstants.COMMAND_DEBUG_FIX,
93         COMMAND_DEBUG_STEP_INTO,
94         COMMAND_COMPILE,
95         COMMAND_VERIFY,
96         COMMAND_DELETE,
97         COMMAND_COPY,
98         COMMAND_MOVE,
99         COMMAND_RENAME,
100     };
101     
102     
103     private static final String JavaDoc[] platformSensitiveActions = {
104         COMMAND_BUILD,
105         COMMAND_REBUILD,
106         COMMAND_COMPILE_SINGLE,
107         COMMAND_RUN,
108         COMMAND_RUN_SINGLE,
109         COMMAND_DEBUG,
110         COMMAND_DEBUG_SINGLE,
111         JavaProjectConstants.COMMAND_JAVADOC,
112         COMMAND_TEST,
113         COMMAND_TEST_SINGLE,
114         COMMAND_DEBUG_TEST_SINGLE,
115         JavaProjectConstants.COMMAND_DEBUG_FIX,
116         COMMAND_DEBUG_STEP_INTO,
117     };
118     
119     // Project
120
AppClientProject project;
121     
122     // Ant project helper of the project
123
private final AntProjectHelper antProjectHelper;
124     private final UpdateHelper updateHelper;
125     
126     /** Map from commands to ant targets */
127     Map JavaDoc<String JavaDoc,String JavaDoc[]> commands;
128     
129     /**Set of commands which are affected by background scanning*/
130     final Set JavaDoc<String JavaDoc> bkgScanSensitiveActions;
131     
132     public AppClientActionProvider( AppClientProject project, AntProjectHelper antProjectHelper, UpdateHelper updateHelper ) {
133         commands = new HashMap JavaDoc<String JavaDoc, String JavaDoc[]>();
134         commands.put(COMMAND_BUILD, new String JavaDoc[] {"dist"}); // NOI18N
135
commands.put(COMMAND_CLEAN, new String JavaDoc[] {"clean"}); // NOI18N
136
commands.put(COMMAND_REBUILD, new String JavaDoc[] {"clean", "dist"}); // NOI18N
137
commands.put(COMMAND_COMPILE_SINGLE, new String JavaDoc[] {"compile-single"}); // NOI18N
138
// commands.put(COMMAND_COMPILE_TEST_SINGLE, new String[] {"compile-test-single"}); // NOI18N
139
commands.put(COMMAND_RUN, new String JavaDoc[] {"run"}); // NOI18N
140
commands.put(COMMAND_RUN_SINGLE, new String JavaDoc[] {"run-single"}); // NOI18N
141
commands.put(EjbProjectConstants.COMMAND_REDEPLOY, new String JavaDoc[] {"run-deploy"}); // NOI18N
142
commands.put(COMMAND_DEBUG, new String JavaDoc[] {"debug"}); // NOI18N
143
//commands.put(COMMAND_DEBUG_SINGLE, new String[] {"debug-single"}); // NOI18N
144
commands.put(JavaProjectConstants.COMMAND_JAVADOC, new String JavaDoc[] {"javadoc"}); // NOI18N
145
commands.put(COMMAND_TEST, new String JavaDoc[] {"test"}); // NOI18N
146
commands.put(COMMAND_TEST_SINGLE, new String JavaDoc[] {"test-single"}); // NOI18N
147
commands.put(COMMAND_DEBUG_TEST_SINGLE, new String JavaDoc[] {"debug-test"}); // NOI18N
148
commands.put(JavaProjectConstants.COMMAND_DEBUG_FIX, new String JavaDoc[] {"debug-fix"}); // NOI18N
149
commands.put(COMMAND_DEBUG_STEP_INTO, new String JavaDoc[] {"debug-stepinto"}); // NOI18N
150
commands.put(COMMAND_COMPILE, new String JavaDoc[] {"compile"}); // NOI18N
151
commands.put(COMMAND_VERIFY, new String JavaDoc[] {"verify"}); // NOI18N
152

153         this.bkgScanSensitiveActions = new HashSet JavaDoc<String JavaDoc>(Arrays.asList(new String JavaDoc[] {
154             COMMAND_RUN,
155             COMMAND_RUN_SINGLE,
156             COMMAND_DEBUG,
157             COMMAND_DEBUG_SINGLE,
158             COMMAND_DEBUG_STEP_INTO
159         }));
160         
161         this.antProjectHelper = antProjectHelper;
162         this.updateHelper = updateHelper;
163         this.project = project;
164     }
165     
166     private FileObject findBuildXml() {
167         return project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
168     }
169     
170     public String JavaDoc[] getSupportedActions() {
171         return supportedActions;
172     }
173     
174     public void invokeAction( final String JavaDoc command, final Lookup context ) throws IllegalArgumentException JavaDoc {
175         if (COMMAND_DELETE.equals(command)) {
176             DefaultProjectOperations.performDefaultDeleteOperation(project);
177             return ;
178         }
179         
180         if (COMMAND_COPY.equals(command)) {
181             DefaultProjectOperations.performDefaultCopyOperation(project);
182             return ;
183         }
184         
185         if (COMMAND_MOVE.equals(command)) {
186             DefaultProjectOperations.performDefaultMoveOperation(project);
187             return ;
188         }
189         
190         if (COMMAND_RENAME.equals(command)) {
191             DefaultProjectOperations.performDefaultRenameOperation(project, null);
192             return ;
193         }
194         
195         Runnable JavaDoc action = new Runnable JavaDoc() {
196             public void run() {
197                 Properties JavaDoc p = new Properties JavaDoc();
198                 String JavaDoc[] targetNames;
199                 
200                 targetNames = getTargetNames(command, context, p);
201                 if (targetNames == null) {
202                     return;
203                 }
204                 if (targetNames.length == 0) {
205                     targetNames = null;
206                 }
207                 if (p.keySet().size() == 0) {
208                     p = null;
209                 }
210                 try {
211                     FileObject buildFo = findBuildXml();
212                     if (buildFo == null || !buildFo.isValid()) {
213                         //The build.xml was deleted after the isActionEnabled was called
214
NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(AppClientActionProvider.class,
215                                 "LBL_No_Build_XML_Found"), NotifyDescriptor.WARNING_MESSAGE); // NOI18N
216
DialogDisplayer.getDefault().notify(nd);
217                     } else {
218                         ActionUtils.runTarget(buildFo, targetNames, p);
219                     }
220                 } catch (IOException JavaDoc e) {
221                     ErrorManager.getDefault().notify(e);
222                 }
223             }
224         };
225         
226 // if (this.bkgScanSensitiveActions.contains(command)) {
227
//TODO: RETOUCHE waitScanFinished
228
// JMManager.getManager().invokeAfterScanFinished(action, NbBundle.getMessage(AppClientActionProvider.class,"ACTION_"+command)); //NOI18N
229
// } else {
230
action.run();
231 // }
232
}
233     
234     /**
235      * @return array of targets or null to stop execution; can return empty array
236      */

237     /*private*/ String JavaDoc[] getTargetNames(String JavaDoc command, Lookup context, Properties JavaDoc p) throws IllegalArgumentException JavaDoc {
238         if (Arrays.asList(platformSensitiveActions).contains(command)) {
239             final String JavaDoc activePlatformId = this.project.evaluator().getProperty(AppClientProjectProperties.JAVA_PLATFORM); //NOI18N
240
if (AppClientProjectUtil.getActivePlatform(activePlatformId) == null) {
241                 showPlatformWarning();
242                 return null;
243             }
244         }
245         String JavaDoc[] targetNames = new String JavaDoc[0];
246         if ( command.equals( COMMAND_COMPILE_SINGLE ) ) {
247             FileObject[] sourceRoots = project.getSourceRoots().getRoots();
248             FileObject[] files = findSourcesAndPackages( context, sourceRoots);
249             boolean recursive = (context.lookup(NonRecursiveFolder.class) == null);
250             if (files != null) {
251                 p.setProperty("javac.includes", ActionUtils.antIncludesList(files, getRoot(sourceRoots,files[0]), recursive)); // NOI18N
252
targetNames = new String JavaDoc[] {"compile-single"}; // NOI18N
253
} else {
254                 FileObject[] testRoots = project.getTestSourceRoots().getRoots();
255                 files = findSourcesAndPackages(context, testRoots);
256                 p.setProperty("javac.includes", ActionUtils.antIncludesList(files, getRoot(testRoots,files[0]), recursive)); // NOI18N
257
targetNames = new String JavaDoc[] {"compile-test-single"}; // NOI18N
258
}
259         } else if ( command.equals( COMMAND_TEST_SINGLE ) ) {
260             FileObject[] files = findTestSourcesForSources(context);
261             targetNames = setupTestSingle(p, files);
262         } else if ( command.equals( COMMAND_DEBUG_TEST_SINGLE ) ) {
263             FileObject[] files = findTestSourcesForSources(context);
264             targetNames = setupDebugTestSingle(p, files);
265         } else if ( command.equals( JavaProjectConstants.COMMAND_DEBUG_FIX ) ) {
266             //this is maybe not needed
267
FileObject[] files = findSources( context );
268             String JavaDoc path = null;
269             if (files != null) {
270                 path = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(),files[0]), files[0]);
271                 targetNames = new String JavaDoc[] {"debug-fix"}; // NOI18N
272
} else {
273                 files = findTestSources(context, false);
274                 path = FileUtil.getRelativePath(getRoot(project.getTestSourceRoots().getRoots(),files[0]), files[0]);
275                 targetNames = new String JavaDoc[] {"debug-fix-test"}; // NOI18N
276
}
277             // Convert foo/FooTest.java -> foo/FooTest
278
if (path.endsWith(".java")) { // NOI18N
279
path = path.substring(0, path.length() - 5);
280             }
281             p.setProperty("fix.includes", path); // NOI18N
282
} else if (command.equals(COMMAND_RUN) || command.equals(EjbProjectConstants.COMMAND_REDEPLOY) || command.equals(COMMAND_DEBUG) /*|| command.equals(COMMAND_DEBUG_STEP_INTO)*/) {
283             EditableProperties ep = updateHelper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
284             //check server
285
if (!isSelectedServer()) {
286                 return null;
287             }
288             
289             //see issue 83056
290
if (command.equals(COMMAND_DEBUG)) {
291                 NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(AppClientActionProvider.class, "MSG_Server_State_Question"), NotifyDescriptor.QUESTION_MESSAGE);
292                 nd.setOptionType(NotifyDescriptor.YES_NO_OPTION);
293                 nd.setOptions(new Object JavaDoc[] {NotifyDescriptor.YES_OPTION, NotifyDescriptor.NO_OPTION});
294                 if (DialogDisplayer.getDefault().notify(nd) == NotifyDescriptor.YES_OPTION) {
295                     nd = new NotifyDescriptor.Message(NbBundle.getMessage(AppClientActionProvider.class, "MSG_Server_State"), NotifyDescriptor.INFORMATION_MESSAGE);
296                     Object JavaDoc o = DialogDisplayer.getDefault().notify(nd);
297                     return null;
298                 }
299             }
300             
301             // check project's main class
302
String JavaDoc mainClass = (String JavaDoc)ep.get("main.class"); // NOI18N
303
MainClassStatus result = isSetMainClass(project.getSourceRoots().getRoots(), mainClass);
304             if (result != MainClassStatus.SET_AND_VALID) {
305                 do {
306                     // show warning, if cancel then return
307
if (showMainClassWarning(mainClass, ProjectUtils.getInformation(project).getDisplayName(), ep,result)) {
308                         return null;
309                     }
310                     mainClass = (String JavaDoc)ep.get("main.class"); // NOI18N
311
result=isSetMainClass(project.getSourceRoots().getRoots(), mainClass);
312                 } while (result != MainClassStatus.SET_AND_VALID);
313                 try {
314                     if (updateHelper.requestSave()) {
315                         updateHelper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,ep);
316                         ProjectManager.getDefault().saveProject(project);
317                     } else {
318                         return null;
319                     }
320                 } catch (IOException JavaDoc ioe) {
321                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Error while saving project: " + ioe);
322                 }
323             }
324             
325             if (command.equals (EjbProjectConstants.COMMAND_REDEPLOY)) {
326                 p.setProperty("forceRedeploy", "true"); //NOI18N
327
} else {
328                 p.setProperty("forceRedeploy", "false"); //NOI18N
329
}
330             
331             targetNames = (String JavaDoc[])commands.get(command);
332             if (targetNames == null) {
333                 throw new IllegalArgumentException JavaDoc(command);
334             }
335         } else if (command.equals(COMMAND_RUN_SINGLE)) {
336             FileObject[] files = findTestSources(context, false);
337             if (files != null) {
338                 targetNames = setupTestSingle(p, files);
339             } else {
340                 //run java
341
FileObject file = findSources(context)[0];
342                 String JavaDoc clazz = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(),file), file);
343                 p.setProperty("javac.includes", clazz); // NOI18N
344
// Convert foo/FooTest.java -> foo.FooTest
345
if (clazz.endsWith(".java")) { // NOI18N
346
clazz = clazz.substring(0, clazz.length() - 5);
347                 }
348                 clazz = clazz.replace('/','.');
349                 
350                 if (!AppClientProjectUtil.hasMainMethod(file)) {
351                     NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(AppClientActionProvider.class, "LBL_No_Main_Classs_Found", clazz), NotifyDescriptor.INFORMATION_MESSAGE);
352                     DialogDisplayer.getDefault().notify(nd);
353                     return null;
354                 } else {
355                     p.setProperty("run.class", clazz); // NOI18N
356
targetNames = (String JavaDoc[])commands.get(COMMAND_RUN_SINGLE);
357                         /*
358                     } else {
359                         p.setProperty("debug.class", clazz); // NOI18N
360                         targetNames = (String[])commands.get(COMMAND_DEBUG_SINGLE);
361                     }
362                          */

363                 }
364             }
365         //DEBUGGING PART
366
} else if (command.equals(COMMAND_DEBUG_SINGLE)) {
367             if (!isSelectedServer ()) {
368                 return null;
369             }
370             if (isDebugged()) {
371                 NotifyDescriptor nd;
372                 nd = new NotifyDescriptor.Confirmation(
373                             NbBundle.getMessage(AppClientActionProvider.class, "MSG_FinishSession"),
374                             NotifyDescriptor.OK_CANCEL_OPTION);
375                 Object JavaDoc o = DialogDisplayer.getDefault().notify(nd);
376                 if (o.equals(NotifyDescriptor.OK_OPTION)) {
377                     DebuggerManager.getDebuggerManager().getCurrentSession().kill();
378                 } else {
379                     return null;
380                 }
381             }
382         } else {
383             targetNames = (String JavaDoc[])commands.get(command);
384             if (targetNames == null) {
385                 throw new IllegalArgumentException JavaDoc(command);
386             }
387         }
388         return targetNames;
389     }
390     
391     private String JavaDoc[] setupTestSingle(Properties JavaDoc p, FileObject[] files) {
392         FileObject[] testSrcPath = project.getTestSourceRoots().getRoots();
393         FileObject root = getRoot(testSrcPath, files[0]);
394         p.setProperty("test.includes", ActionUtils.antIncludesList(files, root)); // NOI18N
395
p.setProperty("javac.includes", ActionUtils.antIncludesList(files, root)); // NOI18N
396
return new String JavaDoc[] {"test-single"}; // NOI18N
397
}
398     
399     private String JavaDoc[] setupDebugTestSingle(Properties JavaDoc p, FileObject[] files) {
400         FileObject[] testSrcPath = project.getTestSourceRoots().getRoots();
401         FileObject root = getRoot(testSrcPath, files[0]);
402         String JavaDoc path = FileUtil.getRelativePath(root, files[0]);
403         // Convert foo/FooTest.java -> foo.FooTest
404
p.setProperty("test.class", path.substring(0, path.length() - 5).replace('/', '.')); // NOI18N
405
return new String JavaDoc[] {"debug-test"}; // NOI18N
406
}
407     
408     public boolean isActionEnabled( String JavaDoc command, Lookup context ) {
409         FileObject buildXml = findBuildXml();
410         if ( buildXml == null || !buildXml.isValid()) {
411             return false;
412         }
413         if ( command.equals( COMMAND_VERIFY ) ) {
414             return project.getCarModule().hasVerifierSupport();
415         }
416         if ( command.equals( COMMAND_COMPILE_SINGLE ) ) {
417             return findSourcesAndPackages( context, project.getSourceRoots().getRoots()) != null
418                     || findSourcesAndPackages( context, project.getTestSourceRoots().getRoots()) != null;
419         } else if ( command.equals( COMMAND_TEST_SINGLE ) ) {
420             return findTestSourcesForSources(context) != null;
421         } else if ( command.equals( COMMAND_DEBUG_TEST_SINGLE ) ) {
422             FileObject[] files = findTestSourcesForSources(context);
423             return files != null && files.length == 1;
424         } else if (command.equals(COMMAND_RUN_SINGLE) ||
425                 command.equals(COMMAND_DEBUG_SINGLE) ||
426                 command.equals(JavaProjectConstants.COMMAND_DEBUG_FIX)) {
427             FileObject fos[] = findSources(context);
428             if (fos != null && fos.length == 1) {
429                 return true;
430             }
431             fos = findTestSources(context, false);
432             return fos != null && fos.length == 1;
433         } else {
434             // other actions are global
435
return true;
436         }
437     }
438     
439     
440     
441     // Private methods -----------------------------------------------------
442

443     
444     private static final Pattern JavaDoc SRCDIRJAVA = Pattern.compile("\\.java$"); // NOI18N
445
private static final String JavaDoc SUBST = "Test.java"; // NOI18N
446

447     /** Find selected sources, the sources has to be under single source root,
448      * @param context the lookup in which files should be found
449      */

450     private FileObject[] findSources(Lookup context) {
451         FileObject[] srcPath = project.getSourceRoots().getRoots();
452         for (int i=0; i< srcPath.length; i++) {
453             FileObject[] files = ActionUtils.findSelectedFiles(context, srcPath[i], ".java", true); // NOI18N
454
if (files != null) {
455                 return files;
456             }
457         }
458         return null;
459     }
460     
461     private FileObject[] findSourcesAndPackages(Lookup context, FileObject srcDir) {
462         if (srcDir != null) {
463             FileObject[] files = ActionUtils.findSelectedFiles(context, srcDir, null, true); // NOI18N
464
//Check if files are either packages of java files
465
if (files != null) {
466                 for (int i = 0; i < files.length; i++) {
467                     if (!files[i].isFolder() && !"java".equals(files[i].getExt())) { // NOI18N
468
return null;
469                     }
470                 }
471             }
472             return files;
473         } else {
474             return null;
475         }
476     }
477     
478     private FileObject[] findSourcesAndPackages(Lookup context, FileObject[] srcRoots) {
479         for (int i=0; i<srcRoots.length; i++) {
480             FileObject[] result = findSourcesAndPackages(context, srcRoots[i]);
481             if (result != null) {
482                 return result;
483             }
484         }
485         return null;
486     }
487     
488     /** Find either selected tests or tests which belong to selected source files
489      */

490     private FileObject[] findTestSources(Lookup context, boolean checkInSrcDir) {
491         //XXX: Ugly, should be rewritten
492
FileObject[] testSrcPath = project.getTestSourceRoots().getRoots();
493         for (int i=0; i< testSrcPath.length; i++) {
494             FileObject[] files = ActionUtils.findSelectedFiles(context, testSrcPath[i], ".java", true); // NOI18N
495
if (files != null) {
496                 return files;
497             }
498         }
499         if (checkInSrcDir && testSrcPath.length>0) {
500             FileObject[] files = findSources(context);
501             if (files != null) {
502                 //Try to find the test under the test roots
503
FileObject srcRoot = getRoot(project.getSourceRoots().getRoots(),files[0]);
504                 for (int i=0; i<testSrcPath.length; i++) {
505                     FileObject[] files2 = ActionUtils.regexpMapFiles(files,srcRoot, SRCDIRJAVA, testSrcPath[i], SUBST, true);
506                     if (files2 != null) {
507                         return files2;
508                     }
509                 }
510             }
511         }
512         return null;
513     }
514     
515     
516     /** Find tests corresponding to selected sources.
517      */

518     private FileObject[] findTestSourcesForSources(Lookup context) {
519         FileObject[] sourceFiles = findSources(context);
520         if (sourceFiles == null) {
521             return null;
522         }
523         FileObject[] testSrcPath = project.getTestSourceRoots().getRoots();
524         if (testSrcPath.length == 0) {
525             return null;
526         }
527         FileObject[] srcPath = project.getSourceRoots().getRoots();
528         FileObject srcDir = getRoot(srcPath, sourceFiles[0]);
529         for (int i=0; i<testSrcPath.length; i++) {
530             FileObject[] files2 = ActionUtils.regexpMapFiles(sourceFiles, srcDir, SRCDIRJAVA, testSrcPath[i], SUBST, true);
531             if (files2 != null) {
532                 return files2;
533             }
534         }
535         return null;
536     }
537     
538     private FileObject getRoot(FileObject[] roots, FileObject file) {
539         assert file != null : "File can't be null"; //NOI18N
540
FileObject srcDir = null;
541         for (int i=0; i< roots.length; i++) {
542             assert roots[i] != null : "Source Path Root can't be null"; //NOI18N
543
if (FileUtil.isParentOf(roots[i],file) || roots[i].equals(file)) {
544                 srcDir = roots[i];
545                 break;
546             }
547         }
548         return srcDir;
549     }
550     
551     
552     private static enum MainClassStatus {
553         SET_AND_VALID,
554         SET_BUT_INVALID,
555         UNSET
556     }
557
558     /**
559      * Tests if the main class is set
560      * @param sourcesRoots source roots
561      * @param mainClass main class name
562      * @return status code
563      */

564     private MainClassStatus isSetMainClass(FileObject[] sourcesRoots, String JavaDoc mainClass) {
565
566         // support for unit testing
567
if (MainClassChooser.unitTestingSupport_hasMainMethodResult != null) {
568             return MainClassChooser.unitTestingSupport_hasMainMethodResult ? MainClassStatus.SET_AND_VALID : MainClassStatus.SET_BUT_INVALID;
569         }
570
571         if (mainClass == null || mainClass.length () == 0) {
572             return MainClassStatus.UNSET;
573         }
574         
575         ClassPath bootPath = ClassPath.getClassPath (sourcesRoots[0], ClassPath.BOOT); //Single compilation unit
576
ClassPath compilePath = ClassPath.getClassPath (sourcesRoots[0], ClassPath.COMPILE);
577         ClassPath sourcePath = ClassPath.getClassPath(sourcesRoots[0], ClassPath.SOURCE);
578         if (AppClientProjectUtil.isMainClass (mainClass, bootPath, compilePath, sourcePath)) {
579             return MainClassStatus.SET_AND_VALID;
580         }
581         return MainClassStatus.SET_BUT_INVALID;
582     }
583     
584     /** Checks if given file object contains the main method.
585      *
586      * @param classFO file object represents java
587      * @return false if parameter is null or doesn't contain class with main method
588      */

589     public static boolean canBeRun(FileObject classFO) {
590         return !SourceUtils.getMainClasses(classFO).isEmpty();
591     }
592     
593     
594     /**
595      * Asks user for name of main class
596      * @param mainClass current main class
597      * @param projectName the name of project
598      * @param ep EditableProperties
599      * @param messgeType type of dialog -1 when the main class is not set, -2 when the main class in not valid
600      * @return true if user selected main class
601      */

602     private boolean showMainClassWarning(String JavaDoc mainClass, String JavaDoc projectName, EditableProperties ep, MainClassStatus messageType) {
603         boolean canceled;
604         final JButton JavaDoc okButton = new JButton JavaDoc(NbBundle.getMessage(MainClassWarning.class, "LBL_MainClassWarning_ChooseMainClass_OK")); // NOI18N
605
okButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MainClassWarning.class, "AD_MainClassWarning_ChooseMainClass_OK"));
606         
607         // main class goes wrong => warning
608
String JavaDoc message;
609         switch (messageType) {
610             case UNSET:
611                 message = MessageFormat.format(NbBundle.getMessage(MainClassWarning.class,"LBL_MainClassNotFound"), new Object JavaDoc[] {
612                     projectName
613                 });
614                 break;
615             case SET_BUT_INVALID:
616                 message = MessageFormat.format(NbBundle.getMessage(MainClassWarning.class,"LBL_MainClassWrong"), new Object JavaDoc[] {
617                     mainClass,
618                     projectName
619                 });
620                 break;
621             default:
622                 throw new IllegalArgumentException JavaDoc();
623         }
624         final MainClassWarning panel = new MainClassWarning(message,project.getSourceRoots().getRoots());
625         Object JavaDoc[] options = new Object JavaDoc[] {
626             okButton,
627             DialogDescriptor.CANCEL_OPTION
628         };
629         
630         panel.addChangeListener(new ChangeListener JavaDoc() {
631             public void stateChanged(ChangeEvent JavaDoc e) {
632                 if (e.getSource() instanceof MouseEvent JavaDoc && MouseUtils.isDoubleClick(((MouseEvent JavaDoc)e.getSource()))) {
633                     // click button and the finish dialog with selected class
634
okButton.doClick();
635                 } else {
636                     okButton.setEnabled(panel.getSelectedMainClass() != null);
637                 }
638             }
639         });
640         
641         okButton.setEnabled(false);
642         DialogDescriptor desc = new DialogDescriptor(panel,
643                 NbBundle.getMessage(MainClassWarning.class, "CTL_MainClassWarning_Title", ProjectUtils.getInformation(project).getDisplayName()), // NOI18N
644
true, options, options[0], DialogDescriptor.BOTTOM_ALIGN, null, null);
645         desc.setMessageType(DialogDescriptor.INFORMATION_MESSAGE);
646         Dialog JavaDoc dlg = DialogDisplayer.getDefault().createDialog(desc);
647         dlg.setVisible(true);
648         if (desc.getValue() != options[0]) {
649             canceled = true;
650         } else {
651             mainClass = panel.getSelectedMainClass();
652             canceled = false;
653             ep.put("main.class", mainClass == null ? "" : mainClass); // NOI18N
654
}
655         dlg.dispose();
656         
657         return canceled;
658     }
659     
660     private void showPlatformWarning() {
661         final JButton JavaDoc closeOption = new JButton JavaDoc(NbBundle.getMessage(AppClientActionProvider.class, "CTL_BrokenPlatform_Close"));
662         closeOption.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(AppClientActionProvider.class, "AD_BrokenPlatform_Close"));
663         final ProjectInformation pi = (ProjectInformation) this.project.getLookup().lookup(ProjectInformation.class);
664         final String JavaDoc projectDisplayName = pi == null ?
665             NbBundle.getMessage(AppClientActionProvider.class,"TEXT_BrokenPlatform_UnknownProjectName")
666             : pi.getDisplayName();
667         final DialogDescriptor dd = new DialogDescriptor(
668                 NbBundle.getMessage(AppClientActionProvider.class, "TEXT_BrokenPlatform", projectDisplayName),
669                 NbBundle.getMessage(AppClientActionProvider.class, "MSG_BrokenPlatform_Title"),
670                 true,
671                 new Object JavaDoc[] {closeOption},
672                 closeOption,
673                 DialogDescriptor.DEFAULT_ALIGN,
674                 null,
675                 null);
676         dd.setMessageType(DialogDescriptor.WARNING_MESSAGE);
677         final Dialog JavaDoc dlg = DialogDisplayer.getDefault().createDialog(dd);
678         dlg.setVisible(true);
679     }
680     
681     private boolean isSelectedServer () {
682         String JavaDoc instance = antProjectHelper.getStandardPropertyEvaluator ().getProperty (AppClientProjectProperties.J2EE_SERVER_INSTANCE);
683         if (instance != null) {
684             String JavaDoc id = Deployment.getDefault().getServerID(instance);
685             if (id != null) {
686                 return true;
687             }
688         }
689         
690         // if there is some server instance of the type which was used
691
// previously do not ask and use it
692
String JavaDoc serverType = antProjectHelper.getStandardPropertyEvaluator ().getProperty (AppClientProjectProperties.J2EE_SERVER_TYPE);
693         if (serverType != null) {
694             String JavaDoc[] servInstIDs = Deployment.getDefault().getInstancesOfServer(serverType);
695             if (servInstIDs.length > 0) {
696                 setServerInstance(servInstIDs[0]);
697                 return true;
698             }
699         }
700
701         // no selected server => warning
702
String JavaDoc msg = NbBundle.getMessage(AppClientActionProvider.class, "MSG_No_Server_Selected"); // NOI18N
703
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(msg, NotifyDescriptor.WARNING_MESSAGE));
704         return false;
705     }
706     
707     private void setServerInstance(final String JavaDoc serverInstanceId) {
708         AppClientProjectProperties.setServerInstance(project, antProjectHelper, serverInstanceId);
709     }
710     
711    private boolean isDebugged() {
712         J2eeModuleProvider jmp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class);
713         ServerDebugInfo sdi = jmp.getServerDebugInfo();
714         if (sdi == null) {
715             return false;
716         }
717         Session[] sessions = DebuggerManager.getDebuggerManager().getSessions();
718         
719         for (int i=0; i < sessions.length; i++) {
720             Session s = sessions[i];
721             if (s != null) {
722                 Object JavaDoc o = s.lookupFirst(null, AttachingDICookie.class);
723                 if (o != null) {
724                     AttachingDICookie attCookie = (AttachingDICookie)o;
725                     if (sdi.getTransport().equals(ServerDebugInfo.TRANSPORT_SHMEM)) {
726                         if (attCookie.getSharedMemoryName().equalsIgnoreCase(sdi.getShmemName())) {
727                             return true;
728                         }
729                     } else {
730                         if (attCookie.getHostName().equalsIgnoreCase(sdi.getHost()) &&
731                                 attCookie.getPortNumber() == sdi.getPort()) {
732                             return true;
733                         }
734                     }
735                 }
736             }
737         }
738         return false;
739     }
740 }
741
Popular Tags