KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > bluej > BluejActionProvider


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.bluej;
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.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Properties JavaDoc;
30 import java.util.regex.Pattern JavaDoc;
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.event.ChangeEvent JavaDoc;
33 import javax.swing.event.ChangeListener JavaDoc;
34 import org.apache.tools.ant.module.api.support.ActionUtils;
35 import org.netbeans.api.fileinfo.NonRecursiveFolder;
36 import org.netbeans.api.java.project.JavaProjectConstants;
37 import org.netbeans.api.java.source.SourceUtils;
38 import org.netbeans.api.project.ProjectManager;
39 import org.netbeans.api.project.ProjectUtils;
40 import org.netbeans.spi.project.ActionProvider;
41 import org.netbeans.spi.project.support.ant.AntProjectHelper;
42 import org.netbeans.spi.project.support.ant.EditableProperties;
43 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
44 import org.openide.DialogDescriptor;
45 import org.openide.DialogDisplayer;
46 import org.openide.ErrorManager;
47 import org.openide.NotifyDescriptor;
48 import org.openide.awt.MouseUtils;
49 import org.openide.execution.ExecutorTask;
50 import org.openide.filesystems.FileObject;
51 import org.openide.filesystems.FileUtil;
52 import org.openide.util.Lookup;
53 import org.openide.util.NbBundle;
54
55 /** Action provider of the J2SE project. This is the place where to do
56  * strange things to J2SE actions. E.g. compile-single.
57  */

58 class BluejActionProvider implements ActionProvider {
59     
60     // Commands available from J2SE project
61
private static final String JavaDoc[] supportedActions = {
62         COMMAND_BUILD,
63         COMMAND_CLEAN,
64         COMMAND_REBUILD,
65         COMMAND_COMPILE_SINGLE,
66         COMMAND_RUN,
67         COMMAND_RUN_SINGLE,
68         COMMAND_DEBUG,
69         COMMAND_DEBUG_SINGLE,
70         JavaProjectConstants.COMMAND_JAVADOC,
71         COMMAND_TEST,
72         COMMAND_TEST_SINGLE,
73         COMMAND_DEBUG_TEST_SINGLE,
74         JavaProjectConstants.COMMAND_DEBUG_FIX,
75         COMMAND_DEBUG_STEP_INTO,
76     };
77     
78     
79     // Project
80
BluejProject project;
81     
82 //// // Ant project helper of the project
83
private UpdateHelper updateHelper;
84     
85     
86     /** Map from commands to ant targets */
87     Map JavaDoc/*<String,String[]>*/ commands;
88     
89     public BluejActionProvider(BluejProject project, UpdateHelper updateHelper) {
90         
91         commands = new HashMap JavaDoc();
92         commands.put(COMMAND_BUILD, new String JavaDoc[] {"jar"}); // NOI18N
93
commands.put(COMMAND_CLEAN, new String JavaDoc[] {"clean"}); // NOI18N
94
commands.put(COMMAND_REBUILD, new String JavaDoc[] {"clean", "jar"}); // NOI18N
95
commands.put(COMMAND_COMPILE_SINGLE, new String JavaDoc[] {"compile-single"}); // NOI18N
96
// commands.put(COMMAND_COMPILE_TEST_SINGLE, new String[] {"compile-test-single"}); // NOI18N
97
commands.put(COMMAND_RUN, new String JavaDoc[] {"run"}); // NOI18N
98
commands.put(COMMAND_RUN_SINGLE, new String JavaDoc[] {"run-single"}); // NOI18N
99
commands.put(COMMAND_DEBUG, new String JavaDoc[] {"debug"}); // NOI18N
100
commands.put(COMMAND_DEBUG_SINGLE, new String JavaDoc[] {"debug-single"}); // NOI18N
101
commands.put(JavaProjectConstants.COMMAND_JAVADOC, new String JavaDoc[] {"javadoc"}); // NOI18N
102
commands.put(COMMAND_TEST, new String JavaDoc[] {"test"}); // NOI18N
103
commands.put(COMMAND_TEST_SINGLE, new String JavaDoc[] {"test-single"}); // NOI18N
104
commands.put(COMMAND_DEBUG_TEST_SINGLE, new String JavaDoc[] {"debug-test"}); // NOI18N
105
commands.put(JavaProjectConstants.COMMAND_DEBUG_FIX, new String JavaDoc[] {"debug-fix"}); // NOI18N
106
commands.put(COMMAND_DEBUG_STEP_INTO, new String JavaDoc[] {"debug-stepinto"}); // NOI18N
107

108         this.updateHelper = updateHelper;
109         this.project = project;
110     }
111     
112     private FileObject findBuildXml() {
113         return project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
114     }
115     
116     public String JavaDoc[] getSupportedActions() {
117         return supportedActions;
118     }
119     
120     public void invokeAction( final String JavaDoc command, final Lookup context ) throws IllegalArgumentException JavaDoc {
121         Runnable JavaDoc action = new Runnable JavaDoc() {
122             public void run() {
123                 Properties JavaDoc p = new Properties JavaDoc();
124                 String JavaDoc[] targetNames;
125                 
126                 targetNames = getTargetNames(command, context, p);
127                 if (targetNames == null) {
128                     return;
129                 }
130                 if (targetNames.length == 0) {
131                     targetNames = null;
132                 }
133                 if (p.keySet().size() == 0) {
134                     p = null;
135                 }
136                 try {
137                     FileObject buildFo = findBuildXml();
138                     if (buildFo == null || !buildFo.isValid()) {
139                         //The build.xml was deleted after the isActionEnabled was called
140
NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(BluejActionProvider.class,
141                                 "LBL_No_Build_XML_Found"), NotifyDescriptor.WARNING_MESSAGE);
142                         DialogDisplayer.getDefault().notify(nd);
143                     } else {
144                         ExecutorTask task = ActionUtils.runTarget(buildFo, targetNames, p);
145                     }
146                 } catch (IOException JavaDoc e) {
147                     ErrorManager.getDefault().notify(e);
148                 }
149             }
150         };
151         
152         action.run();
153     }
154     
155     /**
156      * @return array of targets or null to stop execution; can return empty array
157      */

158     /*private*/ String JavaDoc[] getTargetNames(String JavaDoc command, Lookup context, Properties JavaDoc p) throws IllegalArgumentException JavaDoc {
159         String JavaDoc[] targetNames = new String JavaDoc[0];
160         if ( command.equals( COMMAND_COMPILE_SINGLE ) ) {
161             FileObject[] sourceRoots = new FileObject[] { project.getProjectDirectory() };
162             FileObject[] files = findSourcesAndPackages( context, sourceRoots);
163             boolean recursive = (context.lookup(NonRecursiveFolder.class) == null);
164             if (files != null) {
165                 p.setProperty("javac.includes", ActionUtils.antIncludesList(files, getRoot(sourceRoots,files[0]), recursive)); // NOI18N
166
targetNames = new String JavaDoc[] {"compile-single"}; // NOI18N
167
}
168             //TODO what to do here if we have all source in one root..
169
//// else {
170
//// FileObject[] testRoots = project.getTestSourceRoots().getRoots();
171
//// files = findSourcesAndPackages(context, testRoots);
172
//// p.setProperty("javac.includes", ActionUtils.antIncludesList(files, getRoot(testRoots,files[0]), recursive)); // NOI18N
173
//// targetNames = new String[] {"compile-test-single"}; // NOI18N
174
//// }
175
} else if ( command.equals( COMMAND_TEST_SINGLE ) ) {
176             FileObject[] files = findTestSourcesForSources(context);
177             targetNames = setupTestSingle(p, files);
178         } else if ( command.equals( COMMAND_DEBUG_TEST_SINGLE ) ) {
179             FileObject[] files = findTestSourcesForSources(context);
180             targetNames = setupDebugTestSingle(p, files);
181         }
182 //// else if ( command.equals( JavaProjectConstants.COMMAND_DEBUG_FIX ) ) {
183
//// FileObject[] files = findSources( context );
184
//// String path = null;
185
//// if (files != null) {
186
//// path = FileUtil.getRelativePath(getRoot(project.getProjectDirectory(),files[0]), files[0]);
187
//// targetNames = new String[] {"debug-fix"}; // NOI18N
188
//// //TODO what to do here if we have all source in one root..
189
////
190
//// } else {
191
//// files = findTestSources(context, false);
192
//// path = FileUtil.getRelativePath(getRoot(project.getProjectDirectory(),files[0]), files[0]);
193
//// targetNames = new String[] {"debug-fix-test"}; // NOI18N
194
//// }
195
//// // Convert foo/FooTest.java -> foo/FooTest
196
//// if (path.endsWith(".java")) { // NOI18N
197
//// path = path.substring(0, path.length() - 5);
198
//// }
199
//// p.setProperty("fix.includes", path); // NOI18N
200
//// }
201
else if (command.equals(COMMAND_RUN) || command.equals(COMMAND_DEBUG) || command.equals(COMMAND_DEBUG_STEP_INTO)) {
202             EditableProperties ep = updateHelper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
203             EditableProperties eprivate = updateHelper.getProperties (AntProjectHelper.PRIVATE_PROPERTIES_PATH);
204             if (eprivate == null) {
205                 eprivate = new EditableProperties();
206                 updateHelper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, eprivate);
207             }
208             // check project's main class
209
String JavaDoc mainClass = (String JavaDoc)ep.get ("main.class"); // NOI18N
210

211 // if (!JMManager.getManager().isScanInProgress()) {
212
// // in case the value gets back in some reasonable time,
213
// // check if we have just one mainclass and use it then without a dialog.
214
// List lst = MainClassChooser.getMainClasses(new FileObject[] { project.getProjectDirectory() }, false);
215
// if (lst.size() == 1) {
216
// showDialog = false;
217
// ep.put ("main.class", lst.get(0) == null ? "" : (String)lst.get(0)); // NOI18N
218
// }
219
// }
220
if (mainClass == null) {
221                     // show warning, if cancel then return
222
if (showMainClassWarning(mainClass, ProjectUtils.getInformation(project).getDisplayName(), ep, eprivate, -1)) {
223                         return null;
224                     }
225                     mainClass = (String JavaDoc)ep.get("main.class"); // NOI18N
226
}
227 // result = isSetMainClass(project.getProjectDirectory(), mainClass);
228
try {
229                 if (updateHelper.requestSave()) {
230                     updateHelper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
231                     updateHelper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, eprivate);
232                     ProjectManager.getDefault().saveProject(project);
233                 }
234             } catch (IOException JavaDoc ioe) {
235                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Error while saving project: " + ioe);
236             }
237             if (!command.equals(COMMAND_RUN)) {
238                 p.setProperty("debug.class", mainClass); // NOI18N
239
}
240
241             targetNames = (String JavaDoc[])commands.get(command);
242             if (targetNames == null) {
243                 throw new IllegalArgumentException JavaDoc(command);
244             }
245         } else if (command.equals(COMMAND_RUN_SINGLE) || command.equals(COMMAND_DEBUG_SINGLE)) {
246             FileObject[] files = findTestSources(context, false);
247             if (files != null) {
248                 if (command.equals(COMMAND_RUN_SINGLE)) {
249                     targetNames = setupTestSingle(p, files);
250                 } else {
251                     targetNames = setupDebugTestSingle(p, files);
252                 }
253             } else {
254                 FileObject file = findSources(context)[0];
255                 String JavaDoc clazz = FileUtil.getRelativePath(project.getProjectDirectory(), file);
256                 p.setProperty("javac.includes", clazz); // NOI18N
257
// Convert foo/FooTest.java -> foo.FooTest
258
if (clazz.endsWith(".java")) { // NOI18N
259
clazz = clazz.substring(0, clazz.length() - 5);
260                 }
261                 clazz = clazz.replace('/','.');
262                 
263                 if (!hasMainMethod(file)) {
264                     NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(BluejActionProvider.class, "LBL_No_Main_Classs_Found", clazz), NotifyDescriptor.INFORMATION_MESSAGE);
265                     DialogDisplayer.getDefault().notify(nd);
266                     return null;
267                 } else {
268                     if (command.equals(COMMAND_RUN_SINGLE)) {
269                         p.setProperty("run.class", clazz); // NOI18N
270
targetNames = (String JavaDoc[])commands.get(COMMAND_RUN_SINGLE);
271                     } else {
272                         p.setProperty("debug.class", clazz); // NOI18N
273
targetNames = (String JavaDoc[])commands.get(COMMAND_DEBUG_SINGLE);
274                     }
275                 }
276             }
277         } else {
278             targetNames = (String JavaDoc[])commands.get(command);
279             if (targetNames == null) {
280                 throw new IllegalArgumentException JavaDoc(command);
281             }
282         }
283         return targetNames;
284     }
285     
286     private String JavaDoc[] setupTestSingle(Properties JavaDoc p, FileObject[] files) {
287         FileObject[] testSrcPath = new FileObject[] {project.getProjectDirectory()};
288         FileObject root = getRoot(testSrcPath, files[0]);
289         p.setProperty("test.includes", ActionUtils.antIncludesList(files, root)); // NOI18N
290
p.setProperty("javac.includes", ActionUtils.antIncludesList(files, root)); // NOI18N
291
return new String JavaDoc[] {"test-single"}; // NOI18N
292
}
293     
294     private String JavaDoc[] setupDebugTestSingle(Properties JavaDoc p, FileObject[] files) {
295         FileObject[] testSrcPath = new FileObject[] {project.getProjectDirectory()};
296         FileObject root = getRoot(testSrcPath, files[0]);
297         String JavaDoc path = FileUtil.getRelativePath(root, files[0]);
298         // Convert foo/FooTest.java -> foo.FooTest
299
p.setProperty("test.class", path.substring(0, path.length() - 5).replace('/', '.')); // NOI18N
300
return new String JavaDoc[] {"debug-test"}; // NOI18N
301
}
302     
303     public boolean isActionEnabled( String JavaDoc command, Lookup context ) {
304         FileObject buildXml = findBuildXml();
305         if ( buildXml == null || !buildXml.isValid()) {
306             return false;
307         }
308         if ( command.equals( COMMAND_COMPILE_SINGLE ) ) {
309             return findSourcesAndPackages( context, project.getProjectDirectory()) != null
310                     || findSourcesAndPackages( context, project.getProjectDirectory()) != null;
311         } else if ( command.equals( COMMAND_TEST_SINGLE ) ) {
312             return findTestSourcesForSources(context) != null;
313         } else if ( command.equals( COMMAND_DEBUG_TEST_SINGLE ) ) {
314             FileObject[] files = findTestSourcesForSources(context);
315             return files != null && files.length == 1;
316         } else if (command.equals(COMMAND_RUN_SINGLE) ||
317                 command.equals(COMMAND_DEBUG_SINGLE) ||
318                 command.equals(JavaProjectConstants.COMMAND_DEBUG_FIX)) {
319             FileObject fos[] = findSources(context);
320             if (fos != null && fos.length == 1) {
321                 return true;
322             }
323             fos = findTestSources(context, false);
324             return fos != null && fos.length == 1;
325         } else {
326             // other actions are global
327
return true;
328         }
329     }
330     
331     
332     
333     // Private methods -----------------------------------------------------
334

335     
336     private static final Pattern JavaDoc SRCDIRJAVA = Pattern.compile("\\.java$"); // NOI18N
337
private static final String JavaDoc SUBST = "Test.java"; // NOI18N
338

339     /** Find selected sources, the sources has to be under single source root,
340      * @param context the lookup in which files should be found
341      */

342     private FileObject[] findSources(Lookup context) {
343         FileObject[] srcPath = new FileObject[] {project.getProjectDirectory()};
344         for (int i=0; i< srcPath.length; i++) {
345             FileObject[] files = ActionUtils.findSelectedFiles(context, srcPath[i], ".java", true); // NOI18N
346
if (files != null) {
347                 return files;
348             }
349         }
350         return null;
351     }
352     
353     private FileObject[] findSourcesAndPackages(Lookup context, FileObject srcDir) {
354         if (srcDir != null) {
355             FileObject[] files = ActionUtils.findSelectedFiles(context, srcDir, null, true); // NOI18N
356
//Check if files are either packages of java files
357
if (files != null) {
358                 for (int i = 0; i < files.length; i++) {
359                     if (!files[i].isFolder() && !"java".equals(files[i].getExt())) { //NOI18N
360
return null;
361                     }
362                 }
363             }
364             return files;
365         } else {
366             return null;
367         }
368     }
369     
370     private FileObject[] findSourcesAndPackages(Lookup context, FileObject[] srcRoots) {
371         for (int i=0; i<srcRoots.length; i++) {
372             FileObject[] result = findSourcesAndPackages(context, srcRoots[i]);
373             if (result != null) {
374                 return result;
375             }
376         }
377         return null;
378     }
379     
380     /** Find either selected tests or tests which belong to selected source files
381      */

382     private FileObject[] findTestSources(Lookup context, boolean checkInSrcDir) {
383         //XXX: Ugly, should be rewritten
384
FileObject[] files = ActionUtils.findSelectedFiles(context, project.getProjectDirectory(), "Test.java", true); // NOI18N
385
if (files != null) {
386             return files;
387         }
388         if (checkInSrcDir) {
389             files = findSources(context);
390             if (files != null) {
391                 //Try to find the test under the test roots
392
FileObject[] files2 = ActionUtils.regexpMapFiles(files, project.getProjectDirectory(), SRCDIRJAVA, project.getProjectDirectory(), SUBST, true);
393                 if (files2 != null) {
394                     return files2;
395                 }
396             }
397         }
398         return null;
399     }
400     
401     
402     /** Find tests corresponding to selected sources.
403      */

404     private FileObject[] findTestSourcesForSources(Lookup context) {
405         FileObject[] sourceFiles = findSources(context);
406         if (sourceFiles == null) {
407             return null;
408         }
409         FileObject srcDir = project.getProjectDirectory();
410             FileObject[] files2 = ActionUtils.regexpMapFiles(sourceFiles, srcDir, SRCDIRJAVA, project.getProjectDirectory(), SUBST, true);
411             if (files2 != null) {
412                 return files2;
413             }
414         return null;
415     }
416     
417     private FileObject getRoot(FileObject[] roots, FileObject file) {
418         assert file != null : "File can't be null"; //NOI18N
419
FileObject srcDir = null;
420         for (int i=0; i< roots.length; i++) {
421             assert roots[i] != null : "Source Path Root can't be null"; //NOI18N
422
if (FileUtil.isParentOf(roots[i],file) || roots[i].equals(file)) {
423                 srcDir = roots[i];
424                 break;
425             }
426         }
427         return srcDir;
428     }
429     
430     
431 //// /**
432
//// * Tests if the main class is set
433
//// * @param sourcesRoots source roots
434
//// * @param mainClass main class name
435
//// * @return 0 if the main class is set and is valid
436
//// * -1 if the main class is not set
437
//// * -2 if the main class is set but is not valid
438
//// */
439
//// private int isSetMainClass (FileObject[] sourcesRoots, String mainClass) {
440
////
441
//// // support for unit testing
442
//// if (MainClassChooser.unitTestingSupport_hasMainMethodResult != null) {
443
//// return MainClassChooser.unitTestingSupport_hasMainMethodResult.booleanValue () ? 0 : -2;
444
//// }
445
////
446
//// if (mainClass == null || mainClass.length () == 0) {
447
//// return -1;
448
//// }
449
////
450
//// ClassPath classPath = ClassPath.getClassPath (sourcesRoots[0], ClassPath.EXECUTE); //Single compilation unit
451
//// if (J2SEProjectUtil.isMainClass (mainClass, classPath)) {
452
//// return 0;
453
//// }
454
//// return -2;
455
//// }
456

457 //// /** Checks if given file object contains the main method.
458
//// *
459
//// * @param classFO file object represents java
460
//// * @return false if parameter is null or doesn't contain SourceCookie
461
//// * or SourceCookie doesn't contain the main method
462
//// */
463
//// public static boolean canBeRun (FileObject classFO) {
464
//// if (classFO == null) {
465
//// return false;
466
//// }
467
//// try {
468
//// DataObject classDO = DataObject.find (classFO);
469
//// Object obj = classDO.getCookie (SourceCookie.class);
470
//// if (obj == null || !(obj instanceof SourceCookie)) {
471
//// return false;
472
//// }
473
//// SourceCookie cookie = (SourceCookie) obj;
474
//// // check the main class
475
//// SourceElement source = cookie.getSource ();
476
//// ClassElement[] classes = source.getClasses();
477
//// boolean hasMain = false;
478
//// for (int i = 0; i < classes.length; i++) {
479
//// if (classes[i].hasMainMethod()) {
480
//// return true;
481
//// }
482
//// }
483
//// } catch (DataObjectNotFoundException ex) {
484
//// // can ignore it, classFO could be wrongly set
485
//// }
486
//// return false;
487
//// }
488

489     
490     
491 //// private void showPlatformWarning() {
492
//// final JButton closeOption = new JButton(NbBundle.getMessage(BluejActionProvider.class, "CTL_BrokenPlatform_Close"));
493
//// closeOption.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(BluejActionProvider.class, "AD_BrokenPlatform_Close"));
494
//// final ProjectInformation pi = (ProjectInformation) this.project.getLookup().lookup(ProjectInformation.class);
495
//// final String projectDisplayName = pi == null ?
496
//// NbBundle.getMessage(BluejActionProvider.class,"TEXT_BrokenPlatform_UnknownProjectName")
497
//// : pi.getDisplayName();
498
//// final DialogDescriptor dd = new DialogDescriptor(
499
//// NbBundle.getMessage(BluejActionProvider.class, "TEXT_BrokenPlatform", projectDisplayName),
500
//// NbBundle.getMessage(BluejActionProvider.class, "MSG_BrokenPlatform_Title"),
501
//// true,
502
//// new Object[] {closeOption},
503
//// closeOption,
504
//// DialogDescriptor.DEFAULT_ALIGN,
505
//// null,
506
//// null);
507
//// dd.setMessageType(DialogDescriptor.WARNING_MESSAGE);
508
//// final Dialog dlg = DialogDisplayer.getDefault().createDialog(dd);
509
//// dlg.setVisible(true);
510
//// }
511

512     /**
513      * Asks user for name of main class
514      * @param mainClass current main class
515      * @param projectName the name of project
516      * @param ep EditableProperties
517      * @param messgeType type of dialog -1 when the main class is not set, -2 when the main class in not valid
518      * @return true if user selected main class
519      */

520     private boolean showMainClassWarning (String JavaDoc mainClass, String JavaDoc projectName, EditableProperties ep, EditableProperties eprivate, int messageType) {
521         boolean canceled;
522         final JButton JavaDoc okButton = new JButton JavaDoc ("OK"); // NOI18N
523
// okButton.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage (MainClassWarning.class, "AD_MainClassWarning_ChooseMainClass_OK"));
524

525         // main class goes wrong => warning
526
String JavaDoc message;
527         switch (messageType) {
528             case -1:
529                 message = MessageFormat.format (NbBundle.getMessage(BluejActionProvider.class, "LBL_MainClassNotFound"), new Object JavaDoc[] {
530                     projectName
531                 });
532                 break;
533             case -2:
534                 message = MessageFormat.format (NbBundle.getMessage(BluejActionProvider.class, "LBL_MainClassWrong"), new Object JavaDoc[] {
535                     mainClass,
536                     projectName
537                 });
538                 break;
539             default:
540                 throw new IllegalArgumentException JavaDoc ();
541         }
542         final MainClassWarning panel = new MainClassWarning (message, new FileObject[] { project.getProjectDirectory() } );
543         panel.setArguments(eprivate.getProperty("application.args")); //NOI18N
544
panel.setSelectedMainClass(ep.getProperty("main.class")); //NOI18N
545
Object JavaDoc[] options = new Object JavaDoc[] {
546             okButton,
547             DialogDescriptor.CANCEL_OPTION
548         };
549         
550         panel.addChangeListener (new ChangeListener JavaDoc () {
551            public void stateChanged (ChangeEvent JavaDoc e) {
552                if (e.getSource () instanceof MouseEvent JavaDoc && MouseUtils.isDoubleClick (((MouseEvent JavaDoc)e.getSource ()))) {
553                    // click button and the finish dialog with selected class
554
okButton.doClick ();
555                } else {
556                    okButton.setEnabled (panel.getSelectedMainClass () != null);
557                }
558            }
559         });
560         
561         okButton.setEnabled (false);
562         DialogDescriptor desc = new DialogDescriptor (panel, "Run Project", // NOI18N
563
true, options, options[0], DialogDescriptor.BOTTOM_ALIGN, null, null);
564         desc.setMessageType (DialogDescriptor.INFORMATION_MESSAGE);
565         Dialog JavaDoc dlg = DialogDisplayer.getDefault ().createDialog (desc);
566         dlg.setVisible (true);
567         if (desc.getValue() != options[0]) {
568             canceled = true;
569         } else {
570             mainClass = panel.getSelectedMainClass ();
571             canceled = false;
572             ep.put ("main.class", mainClass == null ? "" : mainClass); // NOI18N
573
eprivate.put("application.args", panel.getArguments()); //NOI18N
574
}
575         dlg.dispose();
576
577         return canceled;
578     }
579     
580     
581     /** Check if the given file object represents a source with the main method.
582      *
583      * @param fo source
584      * @return true if the source contains the main method
585      */

586     public static boolean hasMainMethod(FileObject fo) {
587         if (fo == null) {
588             // ??? maybe better should be thrown IAE
589
return false;
590         }
591         return !SourceUtils.getMainClasses(fo).isEmpty();
592     }
593     
594 }
595
Popular Tags