1 19 20 package org.netbeans.modules.java.j2seproject; 21 22 import java.awt.Dialog ; 23 import java.awt.event.MouseEvent ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.net.URL ; 27 import java.text.MessageFormat ; 28 import java.util.ArrayList ; 29 import java.util.Arrays ; 30 import java.util.Enumeration ; 31 import java.util.HashMap ; 32 import java.util.HashSet ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Properties ; 36 import java.util.Set ; 37 import java.util.StringTokenizer ; 38 import java.util.regex.Pattern ; 39 import javax.swing.JButton ; 40 import javax.swing.event.ChangeEvent ; 41 import javax.swing.event.ChangeListener ; 42 import org.apache.tools.ant.module.api.support.ActionUtils; 43 import org.netbeans.api.fileinfo.NonRecursiveFolder; 44 import org.netbeans.api.java.classpath.ClassPath; 45 import org.netbeans.api.java.project.JavaProjectConstants; 46 import org.netbeans.api.project.ProjectInformation; 47 import org.netbeans.api.project.ProjectManager; 48 import org.netbeans.api.project.ProjectUtils; 49 import org.netbeans.modules.java.j2seproject.applet.AppletSupport; 50 import org.netbeans.modules.java.j2seproject.classpath.ClassPathProviderImpl; 51 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties; 52 import org.netbeans.modules.java.j2seproject.ui.customizer.MainClassChooser; 53 import org.netbeans.modules.java.j2seproject.ui.customizer.MainClassWarning; 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.FileStateInvalidException; 66 import org.openide.filesystems.FileUtil; 67 import org.openide.util.Lookup; 68 import org.openide.util.NbBundle; 69 70 73 class J2SEActionProvider implements ActionProvider { 74 75 private static final String [] supportedActions = { 77 COMMAND_BUILD, 78 COMMAND_CLEAN, 79 COMMAND_REBUILD, 80 COMMAND_COMPILE_SINGLE, 81 COMMAND_RUN, 82 COMMAND_RUN_SINGLE, 83 COMMAND_DEBUG, 84 COMMAND_DEBUG_SINGLE, 85 JavaProjectConstants.COMMAND_JAVADOC, 86 COMMAND_TEST, 87 COMMAND_TEST_SINGLE, 88 COMMAND_DEBUG_TEST_SINGLE, 89 JavaProjectConstants.COMMAND_DEBUG_FIX, 90 COMMAND_DEBUG_STEP_INTO, 91 COMMAND_DELETE, 92 COMMAND_COPY, 93 COMMAND_MOVE, 94 COMMAND_RENAME, 95 }; 96 97 98 private static final String [] platformSensitiveActions = { 99 COMMAND_BUILD, 100 COMMAND_REBUILD, 101 COMMAND_COMPILE_SINGLE, 102 COMMAND_RUN, 103 COMMAND_RUN_SINGLE, 104 COMMAND_DEBUG, 105 COMMAND_DEBUG_SINGLE, 106 JavaProjectConstants.COMMAND_JAVADOC, 107 COMMAND_TEST, 108 COMMAND_TEST_SINGLE, 109 COMMAND_DEBUG_TEST_SINGLE, 110 JavaProjectConstants.COMMAND_DEBUG_FIX, 111 COMMAND_DEBUG_STEP_INTO, 112 }; 113 114 J2SEProject project; 116 117 private UpdateHelper updateHelper; 119 120 121 122 Map <String ,String []> commands; 123 124 125 final Set <String > bkgScanSensitiveActions; 126 127 public J2SEActionProvider( J2SEProject project, UpdateHelper updateHelper ) { 128 129 commands = new HashMap <String ,String []>(); 130 commands.put(COMMAND_BUILD, new String [] {"jar"}); commands.put(COMMAND_CLEAN, new String [] {"clean"}); commands.put(COMMAND_REBUILD, new String [] {"clean", "jar"}); commands.put(COMMAND_COMPILE_SINGLE, new String [] {"compile-single"}); commands.put(COMMAND_RUN, new String [] {"run"}); commands.put(COMMAND_RUN_SINGLE, new String [] {"run-single"}); commands.put(COMMAND_DEBUG, new String [] {"debug"}); commands.put(COMMAND_DEBUG_SINGLE, new String [] {"debug-single"}); commands.put(JavaProjectConstants.COMMAND_JAVADOC, new String [] {"javadoc"}); commands.put(COMMAND_TEST, new String [] {"test"}); commands.put(COMMAND_TEST_SINGLE, new String [] {"test-single"}); commands.put(COMMAND_DEBUG_TEST_SINGLE, new String [] {"debug-test"}); commands.put(JavaProjectConstants.COMMAND_DEBUG_FIX, new String [] {"debug-fix"}); commands.put(COMMAND_DEBUG_STEP_INTO, new String [] {"debug-stepinto"}); 146 this.bkgScanSensitiveActions = new HashSet <String >(Arrays.asList(new String [] { 147 COMMAND_RUN, 148 COMMAND_RUN_SINGLE, 149 COMMAND_DEBUG, 150 COMMAND_DEBUG_SINGLE, 151 COMMAND_DEBUG_STEP_INTO 152 })); 153 154 this.updateHelper = updateHelper; 155 this.project = project; 156 } 157 158 private FileObject findBuildXml() { 159 return project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_XML_PATH); 160 } 161 162 public String [] getSupportedActions() { 163 return supportedActions; 164 } 165 166 public void invokeAction( final String command, final Lookup context ) throws IllegalArgumentException { 167 if (COMMAND_DELETE.equals(command)) { 168 DefaultProjectOperations.performDefaultDeleteOperation(project); 169 return ; 170 } 171 172 if (COMMAND_COPY.equals(command)) { 173 DefaultProjectOperations.performDefaultCopyOperation(project); 174 return ; 175 } 176 177 if (COMMAND_MOVE.equals(command)) { 178 DefaultProjectOperations.performDefaultMoveOperation(project); 179 return ; 180 } 181 182 if (COMMAND_RENAME.equals(command)) { 183 DefaultProjectOperations.performDefaultRenameOperation(project, null); 184 return ; 185 } 186 187 Runnable action = new Runnable () { 188 public void run () { 189 Properties p = new Properties (); 190 String [] targetNames; 191 192 targetNames = getTargetNames(command, context, p); 193 if (targetNames == null) { 194 return; 195 } 196 if (targetNames.length == 0) { 197 targetNames = null; 198 } 199 if (p.keySet().size() == 0) { 200 p = null; 201 } 202 try { 203 FileObject buildFo = findBuildXml(); 204 if (buildFo == null || !buildFo.isValid()) { 205 NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(J2SEActionProvider.class, 207 "LBL_No_Build_XML_Found"), NotifyDescriptor.WARNING_MESSAGE); 208 DialogDisplayer.getDefault().notify(nd); 209 } 210 else { 211 ActionUtils.runTarget(buildFo, targetNames, p); 212 } 213 } 214 catch (IOException e) { 215 ErrorManager.getDefault().notify(e); 216 } 217 } 218 }; 219 220 action.run(); 225 } 227 228 231 String [] getTargetNames(String command, Lookup context, Properties p) throws IllegalArgumentException { 232 if (Arrays.asList(platformSensitiveActions).contains(command)) { 233 final String activePlatformId = this.project.evaluator().getProperty("platform.active"); if (J2SEProjectUtil.getActivePlatform (activePlatformId) == null) { 235 showPlatformWarning (); 236 return null; 237 } 238 } 239 String [] targetNames = new String [0]; 240 Map <String ,String []> targetsFromConfig = loadTargetsFromConfig(); 241 if ( command.equals( COMMAND_COMPILE_SINGLE ) ) { 242 FileObject[] sourceRoots = project.getSourceRoots().getRoots(); 243 FileObject[] files = findSourcesAndPackages( context, sourceRoots); 244 boolean recursive = (context.lookup(NonRecursiveFolder.class) == null); 245 if (files != null) { 246 p.setProperty("javac.includes", ActionUtils.antIncludesList(files, getRoot(sourceRoots,files[0]), recursive)); String [] targets = targetsFromConfig.get(command); 248 targetNames = (targets != null) ? targets : commands.get(command); 249 } 250 else { 251 FileObject[] testRoots = project.getTestSourceRoots().getRoots(); 252 files = findSourcesAndPackages(context, testRoots); 253 p.setProperty("javac.includes", ActionUtils.antIncludesList(files, getRoot(testRoots,files[0]), recursive)); targetNames = new String [] {"compile-test-single"}; } 256 } 257 else if ( command.equals( COMMAND_TEST_SINGLE ) ) { 258 FileObject[] files = findTestSourcesForSources(context); 259 targetNames = setupTestSingle(p, files); 260 } 261 else if ( command.equals( COMMAND_DEBUG_TEST_SINGLE ) ) { 262 FileObject[] files = findTestSourcesForSources(context); 263 targetNames = setupDebugTestSingle(p, files); 264 } 265 else if ( command.equals( JavaProjectConstants.COMMAND_DEBUG_FIX ) ) { 266 FileObject[] files = findSources( context ); 267 String path = null; 268 if (files != null) { 269 path = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(),files[0]), files[0]); 270 targetNames = new String [] {"debug-fix"}; } else { 272 files = findTestSources(context, false); 273 path = FileUtil.getRelativePath(getRoot(project.getTestSourceRoots().getRoots(),files[0]), files[0]); 274 targetNames = new String [] {"debug-fix-test"}; } 276 if (path.endsWith(".java")) { path = path.substring(0, path.length() - 5); 279 } 280 p.setProperty("fix.includes", path); } 282 else if (command.equals (COMMAND_RUN) || command.equals(COMMAND_DEBUG) || command.equals(COMMAND_DEBUG_STEP_INTO)) { 283 String config = project.evaluator().getProperty(J2SEConfigurationProvider.PROP_CONFIG); 284 String path; 285 if (config == null || config.length() == 0) { 286 path = AntProjectHelper.PROJECT_PROPERTIES_PATH; 287 } else { 288 path = "nbproject/configs/" + config + ".properties"; } 291 EditableProperties ep = updateHelper.getProperties(path); 292 293 String mainClass = project.evaluator().getProperty(J2SEProjectProperties.MAIN_CLASS); 298 MainClassStatus result = isSetMainClass (project.getSourceRoots().getRoots(), mainClass); 299 if (context.lookup(J2SEConfigurationProvider.Config.class) != null) { 300 result = MainClassStatus.SET_AND_VALID; 305 } 306 if (result != MainClassStatus.SET_AND_VALID) { 307 do { 308 if (showMainClassWarning (mainClass, ProjectUtils.getInformation(project).getDisplayName(), ep,result)) { 310 return null; 311 } 312 mainClass = ep.get(J2SEProjectProperties.MAIN_CLASS); 314 result=isSetMainClass (project.getSourceRoots().getRoots(), mainClass); 315 } while (result != MainClassStatus.SET_AND_VALID); 316 try { 317 if (updateHelper.requestSave()) { 318 updateHelper.putProperties(path, ep); 319 ProjectManager.getDefault().saveProject(project); 320 } 321 else { 322 return null; 323 } 324 } catch (IOException ioe) { 325 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Error while saving project: " + ioe); 326 } 327 } 328 if (!command.equals(COMMAND_RUN) && mainClass != null) { 329 p.setProperty("debug.class", mainClass); } 331 String [] targets = targetsFromConfig.get(command); 332 targetNames = (targets != null) ? targets : commands.get(command); 333 if (targetNames == null) { 334 throw new IllegalArgumentException (command); 335 } 336 } else if (command.equals (COMMAND_RUN_SINGLE) || command.equals (COMMAND_DEBUG_SINGLE)) { 337 FileObject[] files = findTestSources(context, false); 338 if (files != null) { 339 if (command.equals(COMMAND_RUN_SINGLE)) { 340 targetNames = setupTestSingle(p, files); 341 } else { 342 targetNames = setupDebugTestSingle(p, files); 343 } 344 } else { 345 FileObject file = findSources(context)[0]; 346 String clazz = FileUtil.getRelativePath(getRoot(project.getSourceRoots().getRoots(),file), file); 347 p.setProperty("javac.includes", clazz); if (clazz.endsWith(".java")) { clazz = clazz.substring(0, clazz.length() - 5); 351 } 352 clazz = clazz.replace('/','.'); 353 354 if (!J2SEProjectUtil.hasMainMethod (file)) { 355 if (AppletSupport.isApplet(file)) { 356 357 EditableProperties ep = updateHelper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); 358 String jvmargs = ep.getProperty("run.jvmargs"); 359 360 URL url = null; 361 362 if ((jvmargs == null) || !(jvmargs.indexOf("java.security.policy") > 0)) { AppletSupport.generateSecurityPolicy(project.getProjectDirectory()); 365 if ((jvmargs == null) || (jvmargs.length() == 0)) { 366 ep.setProperty("run.jvmargs", "-Djava.security.policy=applet.policy"); } else { 368 ep.setProperty("run.jvmargs", jvmargs + " -Djava.security.policy=applet.policy"); } 370 updateHelper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep); 371 try { 372 ProjectManager.getDefault().saveProject(project); 373 } catch (Exception e) { 374 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Error while saving project: " + e); 375 } 376 } 377 378 if (file.existsExt("html") || file.existsExt("HTML")) { url = copyAppletHTML(file, "html"); } else { 381 url = generateAppletHTML(file); 382 } 383 if (url == null) { 384 return null; 385 } 386 p.setProperty("applet.url", url.toString()); if (command.equals (COMMAND_RUN_SINGLE)) { 388 targetNames = new String [] {"run-applet"}; } else { 390 p.setProperty("debug.class", clazz); targetNames = new String [] {"debug-applet"}; } 393 } else { 394 NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(J2SEActionProvider.class, "LBL_No_Main_Classs_Found", clazz), NotifyDescriptor.INFORMATION_MESSAGE); 395 DialogDisplayer.getDefault().notify(nd); 396 return null; 397 } 398 } else { 399 if (command.equals (COMMAND_RUN_SINGLE)) { 400 p.setProperty("run.class", clazz); String [] targets = targetsFromConfig.get(command); 402 targetNames = (targets != null) ? targets : commands.get(COMMAND_RUN_SINGLE); 403 } else { 404 p.setProperty("debug.class", clazz); String [] targets = targetsFromConfig.get(command); 406 targetNames = (targets != null) ? targets : commands.get(COMMAND_DEBUG_SINGLE); 407 } 408 } 409 } 410 } else { 411 String [] targets = targetsFromConfig.get(command); 412 targetNames = (targets != null) ? targets : commands.get(command); 413 if (targetNames == null) { 414 throw new IllegalArgumentException (command); 415 } 416 } 417 J2SEConfigurationProvider.Config c = context.lookup(J2SEConfigurationProvider.Config.class); 418 if (c != null) { 419 String config; 420 if (c.name != null) { 421 config = c.name; 422 } else { 423 config = ""; 425 } 426 p.setProperty(J2SEConfigurationProvider.PROP_CONFIG, config); 427 } 428 return targetNames; 429 } 430 431 private HashMap <String ,String []> loadTargetsFromConfig() { 434 HashMap <String ,String []> targets = new HashMap <String ,String []>(6); 435 String config = project.evaluator().getProperty(J2SEConfigurationProvider.PROP_CONFIG); 436 FileObject propFO = project.getProjectDirectory().getFileObject("nbproject/configs/" + config + ".properties"); 438 if (propFO == null) { 439 return targets; 440 } 441 Properties props = new Properties (); 442 try { 443 InputStream is = propFO.getInputStream(); 444 try { 445 props.load(is); 446 } finally { 447 is.close(); 448 } 449 } catch (IOException ex) { 450 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 451 return targets; 452 } 453 Enumeration propNames = props.propertyNames(); 454 while (propNames.hasMoreElements()) { 455 String propName = (String ) propNames.nextElement(); 456 if (propName.startsWith("$target.")) { 457 String tNameVal = props.getProperty(propName); 458 String cmdNameKey = null; 459 if (tNameVal != null && !tNameVal.equals("")) { 460 cmdNameKey = propName.substring("$target.".length()); 461 StringTokenizer stok = new StringTokenizer (tNameVal.trim(), " "); 462 List <String > targetNames = new ArrayList <String >(3); 463 while (stok.hasMoreTokens()) { 464 targetNames.add(stok.nextToken()); 465 } 466 targets.put(cmdNameKey, targetNames.toArray(new String [targetNames.size()])); 467 } 468 } 469 } 470 return targets; 471 } 472 473 private String [] setupTestSingle(Properties p, FileObject[] files) { 474 FileObject[] testSrcPath = project.getTestSourceRoots().getRoots(); 475 FileObject root = getRoot(testSrcPath, files[0]); 476 p.setProperty("test.includes", ActionUtils.antIncludesList(files, root)); p.setProperty("javac.includes", ActionUtils.antIncludesList(files, root)); return new String [] {"test-single"}; } 480 481 private String [] setupDebugTestSingle(Properties p, FileObject[] files) { 482 FileObject[] testSrcPath = project.getTestSourceRoots().getRoots(); 483 FileObject root = getRoot(testSrcPath, files[0]); 484 String path = FileUtil.getRelativePath(root, files[0]); 485 p.setProperty("test.class", path.substring(0, path.length() - 5).replace('/', '.')); return new String [] {"debug-test"}; } 489 490 public boolean isActionEnabled( String command, Lookup context ) { 491 FileObject buildXml = findBuildXml(); 492 if ( buildXml == null || !buildXml.isValid()) { 493 return false; 494 } 495 if ( command.equals( COMMAND_COMPILE_SINGLE ) ) { 496 return findSourcesAndPackages( context, project.getSourceRoots().getRoots()) != null 497 || findSourcesAndPackages( context, project.getTestSourceRoots().getRoots()) != null; 498 } 499 else if ( command.equals( COMMAND_TEST_SINGLE ) ) { 500 return findTestSourcesForSources(context) != null; 501 } 502 else if ( command.equals( COMMAND_DEBUG_TEST_SINGLE ) ) { 503 FileObject[] files = findTestSourcesForSources(context); 504 return files != null && files.length == 1; 505 } else if (command.equals(COMMAND_RUN_SINGLE) || 506 command.equals(COMMAND_DEBUG_SINGLE) || 507 command.equals(JavaProjectConstants.COMMAND_DEBUG_FIX)) { 508 FileObject fos[] = findSources(context); 509 if (fos != null && fos.length == 1) { 510 return true; 511 } 512 fos = findTestSources(context, false); 513 return fos != null && fos.length == 1; 514 } else { 515 return true; 517 } 518 } 519 520 521 522 524 525 private static final Pattern SRCDIRJAVA = Pattern.compile("\\.java$"); private static final String SUBST = "Test.java"; 528 531 private FileObject[] findSources(Lookup context) { 532 FileObject[] srcPath = project.getSourceRoots().getRoots(); 533 for (int i=0; i< srcPath.length; i++) { 534 FileObject[] files = ActionUtils.findSelectedFiles(context, srcPath[i], ".java", true); if (files != null) { 536 return files; 537 } 538 } 539 return null; 540 } 541 542 private FileObject[] findSourcesAndPackages (Lookup context, FileObject srcDir) { 543 if (srcDir != null) { 544 FileObject[] files = ActionUtils.findSelectedFiles(context, srcDir, null, true); if (files != null) { 547 for (int i = 0; i < files.length; i++) { 548 if (!files[i].isFolder() && !"java".equals(files[i].getExt())) { 549 return null; 550 } 551 } 552 } 553 return files; 554 } else { 555 return null; 556 } 557 } 558 559 private FileObject[] findSourcesAndPackages (Lookup context, FileObject[] srcRoots) { 560 for (int i=0; i<srcRoots.length; i++) { 561 FileObject[] result = findSourcesAndPackages(context, srcRoots[i]); 562 if (result != null) { 563 return result; 564 } 565 } 566 return null; 567 } 568 569 571 private FileObject[] findTestSources(Lookup context, boolean checkInSrcDir) { 572 FileObject[] testSrcPath = project.getTestSourceRoots().getRoots(); 574 for (int i=0; i< testSrcPath.length; i++) { 575 FileObject[] files = ActionUtils.findSelectedFiles(context, testSrcPath[i], ".java", true); if (files != null) { 577 return files; 578 } 579 } 580 if (checkInSrcDir && testSrcPath.length>0) { 581 FileObject[] files = findSources (context); 582 if (files != null) { 583 FileObject srcRoot = getRoot(project.getSourceRoots().getRoots(),files[0]); 585 for (int i=0; i<testSrcPath.length; i++) { 586 FileObject[] files2 = ActionUtils.regexpMapFiles(files,srcRoot, SRCDIRJAVA, testSrcPath[i], SUBST, true); 587 if (files2 != null) { 588 return files2; 589 } 590 } 591 } 592 } 593 return null; 594 } 595 596 597 599 private FileObject[] findTestSourcesForSources(Lookup context) { 600 FileObject[] sourceFiles = findSources(context); 601 if (sourceFiles == null) { 602 return null; 603 } 604 FileObject[] testSrcPath = project.getTestSourceRoots().getRoots(); 605 if (testSrcPath.length == 0) { 606 return null; 607 } 608 FileObject[] srcPath = project.getSourceRoots().getRoots(); 609 FileObject srcDir = getRoot(srcPath, sourceFiles[0]); 610 for (int i=0; i<testSrcPath.length; i++) { 611 FileObject[] files2 = ActionUtils.regexpMapFiles(sourceFiles, srcDir, SRCDIRJAVA, testSrcPath[i], SUBST, true); 612 if (files2 != null) { 613 return files2; 614 } 615 } 616 return null; 617 } 618 619 private FileObject getRoot (FileObject[] roots, FileObject file) { 620 assert file != null : "File can't be null"; FileObject srcDir = null; 622 for (int i=0; i< roots.length; i++) { 623 assert roots[i] != null : "Source Path Root can't be null"; if (FileUtil.isParentOf(roots[i],file) || roots[i].equals(file)) { 625 srcDir = roots[i]; 626 break; 627 } 628 } 629 return srcDir; 630 } 631 632 private static enum MainClassStatus { 633 SET_AND_VALID, 634 SET_BUT_INVALID, 635 UNSET 636 } 637 638 644 private MainClassStatus isSetMainClass(FileObject[] sourcesRoots, String mainClass) { 645 646 if (MainClassChooser.unitTestingSupport_hasMainMethodResult != null) { 648 return MainClassChooser.unitTestingSupport_hasMainMethodResult ? MainClassStatus.SET_AND_VALID : MainClassStatus.SET_BUT_INVALID; 649 } 650 651 if (mainClass == null || mainClass.length () == 0) { 652 return MainClassStatus.UNSET; 653 } 654 if (sourcesRoots.length > 0) { 655 ClassPath bootPath = ClassPath.getClassPath (sourcesRoots[0], ClassPath.BOOT); ClassPath compilePath = ClassPath.getClassPath (sourcesRoots[0], ClassPath.COMPILE); 657 ClassPath sourcePath = ClassPath.getClassPath(sourcesRoots[0], ClassPath.SOURCE); 658 if (J2SEProjectUtil.isMainClass (mainClass, bootPath, compilePath, sourcePath)) { 659 return MainClassStatus.SET_AND_VALID; 660 } 661 } 662 else { 663 ClassPathProviderImpl cpProvider = project.getLookup().lookup(ClassPathProviderImpl.class); 664 if (cpProvider != null) { 665 ClassPath bootPath = cpProvider.getProjectSourcesClassPath(ClassPath.BOOT); 666 ClassPath compilePath = cpProvider.getProjectSourcesClassPath(ClassPath.COMPILE); 667 ClassPath sourcePath = cpProvider.getProjectSourcesClassPath(ClassPath.SOURCE); if (J2SEProjectUtil.isMainClass (mainClass, bootPath, compilePath, sourcePath)) { 669 return MainClassStatus.SET_AND_VALID; 670 } 671 } 672 } 673 return MainClassStatus.SET_BUT_INVALID; 674 } 675 676 684 private boolean showMainClassWarning(String mainClass, String projectName, EditableProperties ep, MainClassStatus messageType) { 685 boolean canceled; 686 final JButton okButton = new JButton (NbBundle.getMessage (MainClassWarning.class, "LBL_MainClassWarning_ChooseMainClass_OK")); okButton.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage (MainClassWarning.class, "AD_MainClassWarning_ChooseMainClass_OK")); 688 689 String message; 691 switch (messageType) { 692 case UNSET: 693 message = MessageFormat.format (NbBundle.getMessage(MainClassWarning.class,"LBL_MainClassNotFound"), new Object [] { 694 projectName 695 }); 696 break; 697 case SET_BUT_INVALID: 698 message = MessageFormat.format (NbBundle.getMessage(MainClassWarning.class,"LBL_MainClassWrong"), new Object [] { 699 mainClass, 700 projectName 701 }); 702 break; 703 default: 704 throw new IllegalArgumentException (); 705 } 706 final MainClassWarning panel = new MainClassWarning (message,project.getSourceRoots().getRoots()); 707 Object [] options = new Object [] { 708 okButton, 709 DialogDescriptor.CANCEL_OPTION 710 }; 711 712 panel.addChangeListener (new ChangeListener () { 713 public void stateChanged (ChangeEvent e) { 714 if (e.getSource () instanceof MouseEvent && MouseUtils.isDoubleClick (((MouseEvent )e.getSource ()))) { 715 okButton.doClick (); 717 } else { 718 okButton.setEnabled (panel.getSelectedMainClass () != null); 719 } 720 } 721 }); 722 723 okButton.setEnabled (false); 724 DialogDescriptor desc = new DialogDescriptor (panel, 725 NbBundle.getMessage (MainClassWarning.class, "CTL_MainClassWarning_Title", ProjectUtils.getInformation(project).getDisplayName()), true, options, options[0], DialogDescriptor.BOTTOM_ALIGN, null, null); 727 desc.setMessageType (DialogDescriptor.INFORMATION_MESSAGE); 728 Dialog dlg = DialogDisplayer.getDefault ().createDialog (desc); 729 dlg.setVisible (true); 730 if (desc.getValue() != options[0]) { 731 canceled = true; 732 } else { 733 mainClass = panel.getSelectedMainClass (); 734 canceled = false; 735 ep.put(J2SEProjectProperties.MAIN_CLASS, mainClass == null ? "" : mainClass); 736 } 737 dlg.dispose(); 738 739 return canceled; 740 } 741 742 private void showPlatformWarning () { 743 final JButton closeOption = new JButton (NbBundle.getMessage(J2SEActionProvider.class, "CTL_BrokenPlatform_Close")); 744 closeOption.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(J2SEActionProvider.class, "AD_BrokenPlatform_Close")); 745 final ProjectInformation pi = (ProjectInformation) this.project.getLookup().lookup (ProjectInformation.class); 746 final String projectDisplayName = pi == null ? 747 NbBundle.getMessage (J2SEActionProvider.class,"TEXT_BrokenPlatform_UnknownProjectName") 748 : pi.getDisplayName(); 749 final DialogDescriptor dd = new DialogDescriptor( 750 NbBundle.getMessage(J2SEActionProvider.class, "TEXT_BrokenPlatform", projectDisplayName), 751 NbBundle.getMessage(J2SEActionProvider.class, "MSG_BrokenPlatform_Title"), 752 true, 753 new Object [] {closeOption}, 754 closeOption, 755 DialogDescriptor.DEFAULT_ALIGN, 756 null, 757 null); 758 dd.setMessageType(DialogDescriptor.WARNING_MESSAGE); 759 final Dialog dlg = DialogDisplayer.getDefault().createDialog(dd); 760 dlg.setVisible(true); 761 } 762 763 private URL generateAppletHTML(FileObject file) { 764 URL url = null; 765 try { 766 String buildDirProp = project.evaluator().getProperty("build.dir"); String classesDirProp = project.evaluator().getProperty("build.classes.dir"); FileObject buildDir = this.updateHelper.getAntProjectHelper().resolveFileObject(buildDirProp); 769 FileObject classesDir = this.updateHelper.getAntProjectHelper().resolveFileObject(classesDirProp); 770 771 if (buildDir == null) { 772 buildDir = FileUtil.createFolder(project.getProjectDirectory(), buildDirProp); 773 } 774 775 if (classesDir == null) { 776 classesDir = FileUtil.createFolder(project.getProjectDirectory(), classesDirProp); 777 } 778 String activePlatformName = project.evaluator().getProperty("platform.active"); url = AppletSupport.generateHtmlFileURL(file, buildDir, classesDir, activePlatformName); 780 } catch (FileStateInvalidException fe) { 781 } catch (IOException ioe) { 783 ErrorManager.getDefault().notify(ioe); 784 return null; 785 } 786 return url; 787 } 788 789 private URL copyAppletHTML(FileObject file, String ext) { 790 URL url = null; 791 try { 792 String buildDirProp = project.evaluator().getProperty("build.dir"); FileObject buildDir = updateHelper.getAntProjectHelper().resolveFileObject(buildDirProp); 794 795 if (buildDir == null) { 796 buildDir = FileUtil.createFolder(project.getProjectDirectory(), buildDirProp); 797 } 798 799 FileObject htmlFile = null; 800 htmlFile = file.getParent().getFileObject(file.getName(), "html"); if (htmlFile == null) { 802 htmlFile = file.getParent().getFileObject(file.getName(), "HTML"); } 804 if (htmlFile == null) { 805 return null; 806 } 807 808 FileObject existingFile = buildDir.getFileObject(htmlFile.getName(), htmlFile.getExt()); 809 if (existingFile != null) { 810 existingFile.delete(); 811 } 812 813 FileObject targetHtml = htmlFile.copy(buildDir, file.getName(), ext); 814 815 if (targetHtml != null) { 816 String activePlatformName = project.evaluator().getProperty("platform.active"); url = AppletSupport.getHTMLPageURL(targetHtml, activePlatformName); 818 } 819 } catch (FileStateInvalidException fe) { 820 } catch (IOException ioe) { 822 ErrorManager.getDefault().notify(ioe); 823 return null; 824 } 825 return url; 826 } 827 828 } 829 | Popular Tags |