1 19 20 package org.netbeans.modules.junit; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Color ; 24 import java.awt.Component ; 25 import java.awt.Container ; 26 import java.awt.Font ; 27 import java.awt.GridBagConstraints ; 28 import java.awt.GridBagLayout ; 29 import java.awt.Insets ; 30 import java.awt.event.ActionListener ; 31 import java.awt.event.ItemEvent ; 32 import java.awt.event.ItemListener ; 33 import java.util.ArrayList ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.ResourceBundle ; 37 import java.util.Stack ; 38 import javax.swing.BorderFactory ; 39 import javax.swing.Box ; 40 import javax.swing.BoxLayout ; 41 import javax.swing.DefaultComboBoxModel ; 42 import javax.swing.DefaultListCellRenderer ; 43 import javax.swing.JButton ; 44 import javax.swing.JCheckBox ; 45 import javax.swing.JComboBox ; 46 import javax.swing.JComponent ; 47 import javax.swing.JLabel ; 48 import javax.swing.JList ; 49 import javax.swing.JPanel ; 50 import javax.swing.JTextArea ; 51 import javax.swing.ListCellRenderer ; 52 import javax.swing.UIManager ; 53 import javax.swing.border.Border ; 54 import javax.swing.border.CompoundBorder ; 55 import javax.swing.border.TitledBorder ; 56 import javax.swing.event.ChangeEvent ; 57 import javax.swing.event.ChangeListener ; 58 import javax.swing.plaf.UIResource ; 59 import org.netbeans.api.java.classpath.ClassPath; 60 import org.netbeans.api.project.SourceGroup; 61 import org.openide.DialogDescriptor; 62 import org.openide.DialogDisplayer; 63 import org.openide.awt.Mnemonics; 64 import org.openide.filesystems.FileObject; 65 import org.openide.filesystems.FileUtil; 66 import org.openide.loaders.DataFolder; 67 import org.openide.loaders.DataObject; 68 import org.openide.nodes.Node; 69 import org.openide.util.HelpCtx; 70 import org.openide.util.Lookup; 71 import org.openide.util.NbBundle; 72 73 74 79 public final class JUnitCfgOfCreate extends SelfResizingPanel 80 implements ChangeListener { 81 82 83 private static final String TEST_CLASS_SUFFIX = "Test"; 85 88 private final Node[] nodes; 89 90 private final boolean multipleClasses; 91 92 private boolean singlePackage; 93 94 private boolean singleClass; 95 96 private String testClassName; 97 98 private List <ChangeListener > changeListeners; 99 100 private String initialMessage; 101 102 107 private boolean hasTargetFolders = false; 108 109 114 private boolean checkBoxesOK; 115 116 120 private String msgChkBoxesInvalid; 121 122 127 private boolean classNameValid; 128 129 134 private boolean isAcceptable; 135 136 137 private static final int MSG_TYPE_NO_TARGET_FOLDERS = 0; 138 139 private static final int MSG_TYPE_INVALID_CHKBOXES = 1; 140 141 private static final int MSG_TYPE_CLASSNAME_INVALID = 2; 142 143 private static final int MSG_TYPE_CLASSNAME_NOT_DEFAULT = 3; 144 145 private MessageStack msgStack = new MessageStack(4); 146 147 152 JUnitCfgOfCreate(Node[] nodes) { 153 assert (nodes != null) && (nodes.length != 0); 154 155 this.nodes = nodes; 156 multipleClasses = checkMultipleClasses(); 157 158 initBundle(); 159 try { 160 initComponents(); 161 setBorder(BorderFactory.createEmptyBorder(12, 12, 0, 11)); 162 addAccessibleDescriptions(); 163 initializeCheckBoxStates(); 164 fillFormData(); 165 checkAcceptability(); 166 setupUserInteraction(); 167 168 174 175 } finally { 176 unlinkBundle(); 177 } 178 } 179 180 private void addAccessibleDescriptions() { 181 182 this.getAccessibleContext().setAccessibleDescription(bundle.getString("JUnitCfgOfCreate.AD")); 184 185 187 if (this.tfClassName != null) { 188 this.tfClassName.setToolTipText( 189 bundle.getString("JUnitCfgOfCreate.clsName.toolTip")); this.tfClassName.getAccessibleContext().setAccessibleName( 191 bundle.getString("JUnitCfgOfCreate.clsName.AN")); this.tfClassName.getAccessibleContext().setAccessibleDescription( 193 bundle.getString("JUnitCfgOfCreate.clsName.AD")); } 195 196 this.cboxLocation.setToolTipText( 197 bundle.getString("JUnitCfgOfCreate.location.toolTip")); this.cboxLocation.getAccessibleContext().setAccessibleName( 199 bundle.getString("JUnitCfgOfCreate.location.AN")); this.cboxLocation.getAccessibleContext().setAccessibleDescription( 201 bundle.getString("JUnitCfgOfCreate.location.AD")); 203 this.chkPublic.setToolTipText(bundle.getString("JUnitCfgOfCreate.chkPublic.toolTip")); 205 this.chkPublic.getAccessibleContext().setAccessibleDescription(bundle.getString("JUnitCfgOfCreate.chkPublic.AD")); 206 207 this.chkProtected.setToolTipText(bundle.getString("JUnitCfgOfCreate.chkProtected.toolTip")); 208 this.chkProtected.getAccessibleContext().setAccessibleDescription(bundle.getString("JUnitCfgOfCreate.chkProtected.AD")); 209 210 this.chkPackage.setToolTipText(bundle.getString("JUnitCfgOfCreate.chkPackage.toolTip")); 211 this.chkPackage.getAccessibleContext().setAccessibleDescription(bundle.getString("JUnitCfgOfCreate.chkPackage.AD")); 212 213 this.chkComments.setToolTipText(bundle.getString("JUnitCfgOfCreate.chkComments.toolTip")); 214 this.chkComments.getAccessibleContext().setAccessibleDescription(bundle.getString("JUnitCfgOfCreate.chkComments.AD")); 215 216 this.chkContent.setToolTipText(bundle.getString("JUnitCfgOfCreate.chkContent.toolTip")); 217 this.chkContent.getAccessibleContext().setAccessibleDescription(bundle.getString("JUnitCfgOfCreate.chkContent.AD")); 218 219 this.chkJavaDoc.setToolTipText(bundle.getString("JUnitCfgOfCreate.chkJavaDoc.toolTip")); 220 this.chkJavaDoc.getAccessibleContext().setAccessibleDescription(bundle.getString("JUnitCfgOfCreate.chkJavaDoc.AD")); 221 222 if (multipleClasses) { 223 this.chkExceptions.setToolTipText(bundle.getString("JUnitCfgOfCreate.chkExceptions.toolTip")); 224 this.chkExceptions.getAccessibleContext().setAccessibleDescription(bundle.getString("JUnitCfgOfCreate.chkExceptions.AD")); 225 226 this.chkAbstractImpl.setToolTipText(bundle.getString("JUnitCfgOfCreate.chkAbstractImpl.toolTip")); 227 this.chkAbstractImpl.getAccessibleContext().setAccessibleDescription(bundle.getString("JUnitCfgOfCreate.chkAbstractImpl.AD")); 228 229 this.chkPackagePrivateClasses.setToolTipText(bundle.getString("JUnitCfgOfCreate.chkPackagePrivateClasses.toolTip")); 230 this.chkPackagePrivateClasses.getAccessibleContext().setAccessibleDescription(bundle.getString("JUnitCfgOfCreate.chkPackagePrivateClasses.AD")); 231 232 this.chkGenerateSuites.setToolTipText(bundle.getString("JUnitCfgOfCreate.chkGenerateSuites.toolTip")); 233 this.chkGenerateSuites.getAccessibleContext().setAccessibleDescription(bundle.getString("JUnitCfgOfCreate.chkGenerateSuites.AD")); 234 } 235 236 } 237 238 249 private boolean checkMultipleClasses() { 250 if (nodes.length > 1) { 251 return true; 252 } 253 254 Lookup nodeLookup = nodes[0].getLookup(); 255 if (nodeLookup.lookup(DataObject.Container.class) != null) { 256 singlePackage = nodeLookup.lookup(DataFolder.class) 257 != null; 258 return true; 259 } 260 261 singleClass = false; 262 DataObject dataObj = nodeLookup.lookup(DataObject.class); 263 if (dataObj == null) { 264 return true; 265 } 266 267 singleClass = dataObj.getPrimaryFile().isData(); 268 return !singleClass; 269 } 270 271 277 boolean configure() { 278 279 String title = NbBundle.getMessage(JUnitCfgOfCreate.class, 281 "JUnitCfgOfCreate.Title"); ChangeListener changeListener; 283 final JButton btnOK = new JButton ( 284 NbBundle.getMessage(JUnitCfgOfCreate.class, "LBL_OK")); btnOK.setEnabled(isAcceptable()); 286 addChangeListener(changeListener = new ChangeListener () { 287 public void stateChanged(ChangeEvent e) { 288 btnOK.setEnabled(isAcceptable()); 289 } 290 }); 291 292 Object returned = DialogDisplayer.getDefault().notify( 293 new DialogDescriptor ( 294 this, 295 title, 296 true, new Object [] {btnOK, DialogDescriptor.CANCEL_OPTION}, 298 btnOK, DialogDescriptor.DEFAULT_ALIGN, 300 new HelpCtx(JUnitCfgOfCreate.class), 301 (ActionListener ) null 302 )); 303 removeChangeListener(changeListener); 304 305 if (returned == btnOK) { 306 rememberCheckBoxStates(); 307 testClassName = (tfClassName != null) ? tfClassName.getText() 308 : null; 309 return true; 310 } 311 return false; 312 } 313 314 320 boolean isSingleClass() { 321 return singleClass; 322 } 323 324 331 String getTestClassName() { 332 return testClassName; 333 } 334 335 336 public ResourceBundle bundle; 337 338 343 private void initializeCheckBoxStates() { 344 final JUnitSettings settings = JUnitSettings.getDefault(); 345 346 chkPublic.setSelected(settings.isMembersPublic()); 347 chkProtected.setSelected(settings.isMembersProtected()); 348 chkPackage.setSelected(settings.isMembersPackage()); 349 chkComments.setSelected(settings.isBodyComments()); 350 chkContent.setSelected(settings.isBodyContent()); 351 chkJavaDoc.setSelected(settings.isJavaDoc()); 352 if (multipleClasses) { 353 chkGenerateSuites.setSelected(settings.isGenerateSuiteClasses()); 354 chkPackagePrivateClasses.setSelected( 355 settings.isIncludePackagePrivateClasses()); 356 chkAbstractImpl.setSelected(settings.isGenerateAbstractImpl()); 357 chkExceptions.setSelected(settings.isGenerateExceptionClasses()); 358 } 359 chkSetUp.setSelected(settings.isGenerateSetUp()); 360 chkTearDown.setSelected(settings.isGenerateTearDown()); 361 362 checkChkBoxesStates(); 363 } 364 365 370 private void rememberCheckBoxStates() { 371 final JUnitSettings settings = JUnitSettings.getDefault(); 372 373 settings.setMembersPublic(chkPublic.isSelected()); 374 settings.setMembersProtected(chkProtected.isSelected()); 375 settings.setMembersPackage(chkPackage.isSelected()); 376 settings.setBodyComments(chkComments.isSelected()); 377 settings.setBodyContent(chkContent.isSelected()); 378 settings.setJavaDoc(chkJavaDoc.isSelected()); 379 if (multipleClasses) { 380 settings.setGenerateSuiteClasses(chkGenerateSuites.isSelected()); 381 settings.setIncludePackagePrivateClasses( 382 chkPackagePrivateClasses.isSelected()); 383 settings.setGenerateAbstractImpl(chkAbstractImpl.isSelected()); 384 settings.setGenerateExceptionClasses(chkExceptions.isSelected()); 385 } 386 settings.setGenerateSetUp(chkSetUp.isSelected()); 387 settings.setGenerateTearDown(chkTearDown.isSelected()); 388 } 389 390 396 private void initBundle() { 397 bundle = NbBundle.getBundle(JUnitCfgOfCreate.class); 398 } 399 400 406 private void unlinkBundle() { 407 bundle = null; 408 } 409 410 413 private void initComponents() { 414 setLayout(new BorderLayout (0, 12)); 415 416 add(createNameAndLocationPanel(), BorderLayout.NORTH); 417 add(createMessagePanel(), BorderLayout.CENTER); 418 add(createCodeGenPanel(), BorderLayout.SOUTH); 419 } 420 421 423 private void setupUserInteraction() { 424 final ItemListener listener = new CheckBoxListener(); 425 426 chkPublic.addItemListener(listener); 427 chkProtected.addItemListener(listener); 428 chkPackage.addItemListener(listener); 429 } 430 431 433 private void checkChkBoxesStates() { 434 checkBoxesOK = chkPublic.isSelected() 435 || chkProtected.isSelected() 436 || chkPackage.isSelected(); 437 if (checkBoxesOK) { 438 setMessage(null, MSG_TYPE_INVALID_CHKBOXES); 439 } else { 440 if (msgChkBoxesInvalid == null) { 441 msgChkBoxesInvalid = NbBundle.getMessage( 443 JUnitCfgOfCreate.class, 444 "MSG_AllMethodTypesDisabled"); } 446 setMessage(msgChkBoxesInvalid, MSG_TYPE_INVALID_CHKBOXES); 447 } 448 } 449 450 453 private final class CheckBoxListener implements ItemListener { 454 public CheckBoxListener () {} 455 456 public void itemStateChanged(ItemEvent e) { 457 final Object source = e.getSource(); 458 459 assert source == chkPublic 460 || source == chkProtected 461 || source == chkPackage; 462 checkChkBoxesStates(); 463 checkAcceptability(); 464 } 465 466 } 467 468 470 private Component createNameAndLocationPanel() { 471 JPanel panel = new JPanel (); 472 473 final boolean askForClassName = singleClass; 474 475 JLabel lblClassToTest = new JLabel (); 476 JLabel lblClassName = askForClassName ? new JLabel () : null; 477 JLabel lblLocation = new JLabel (); 478 479 String classToTestKey = singlePackage 480 ? "LBL_PackageToTest" : singleClass 482 ? "LBL_ClassToTest" : "LBL_MultipleClassesSelected"; 485 Mnemonics.setLocalizedText( 486 lblClassToTest, 487 NbBundle.getMessage(getClass(), classToTestKey)); 488 if (askForClassName) { 489 Mnemonics.setLocalizedText( 490 lblClassName, 491 NbBundle.getMessage(getClass(), "LBL_ClassName")); } 493 Mnemonics.setLocalizedText( 494 lblLocation, 495 NbBundle.getMessage(getClass(), "LBL_Location")); 497 if (singlePackage || singleClass) { 498 lblClassToTestValue = new JLabel (); 499 } 500 if (askForClassName) { 501 tfClassName = new ClassNameTextField(); 502 tfClassName.setChangeListener(this); 503 } 504 cboxLocation = new JComboBox (); 505 506 if (askForClassName) { 507 lblClassName.setLabelFor(tfClassName); 508 } 509 lblLocation.setLabelFor(cboxLocation); 510 511 if (lblClassToTestValue != null) { 512 Font labelFont = javax.swing.UIManager.getDefaults() 513 .getFont("TextField.font"); if (labelFont != null) { 515 lblClassToTestValue.setFont(labelFont); 516 } 517 } 518 519 panel.setLayout(new GridBagLayout ()); 520 521 GridBagConstraints gbcLeft = new GridBagConstraints (); 522 gbcLeft.anchor = GridBagConstraints.WEST; 523 gbcLeft.insets.bottom = 12; 524 gbcLeft.insets.right = 6; 525 526 GridBagConstraints gbcRight = new GridBagConstraints (); 527 gbcRight.anchor = GridBagConstraints.WEST; 528 gbcRight.insets.bottom = 12; 529 gbcRight.weightx = 1.0f; 530 gbcRight.fill = GridBagConstraints.BOTH; 531 gbcRight.gridwidth = GridBagConstraints.REMAINDER; 532 533 if (lblClassToTestValue != null) { 534 panel.add(lblClassToTest, gbcLeft); 535 panel.add(lblClassToTestValue, gbcRight); 536 } else { 537 panel.add(lblClassToTest, gbcRight); 538 } 539 if (askForClassName) { 540 panel.add(lblClassName, gbcLeft); 541 panel.add(tfClassName, gbcRight); 542 } 543 gbcLeft.insets.bottom = 0; 544 gbcRight.insets.bottom = 0; 545 panel.add(lblLocation, gbcLeft); 546 panel.add(cboxLocation, gbcRight); 547 548 return panel; 549 } 550 551 553 private void checkClassNameValidity() { 554 if (tfClassName == null) { 555 classNameValid = true; 556 return; 557 } 558 559 String key = null; 560 final int state = tfClassName.getStatus(); 561 switch (state) { 562 case ClassNameTextField.STATUS_EMPTY: 563 key = "MSG_ClassnameMustNotBeEmpty"; break; 566 case ClassNameTextField.STATUS_INVALID: 567 key = "MSG_InvalidClassName"; break; 570 case ClassNameTextField.STATUS_VALID_NOT_DEFAULT: 571 key = "MSG_ClassNameNotDefault"; break; 574 } 575 if (state != ClassNameTextField.STATUS_VALID_NOT_DEFAULT) { 576 setMessage(null, MSG_TYPE_CLASSNAME_NOT_DEFAULT); 577 } 578 setMessage((key != null) 579 ? NbBundle.getMessage(getClass(), key) 580 : null, 581 MSG_TYPE_CLASSNAME_INVALID); 582 583 classNameValid = 584 (state == ClassNameTextField.STATUS_VALID) 585 || (state == ClassNameTextField.STATUS_VALID_NOT_DEFAULT); 586 } 587 588 596 public void stateChanged(ChangeEvent e) { 597 checkClassNameValidity(); 598 checkAcceptability(); 599 } 600 601 603 private void checkAcceptability() { 604 final boolean wasAcceptable = isAcceptable; 605 isAcceptable = hasTargetFolders && classNameValid && checkBoxesOK; 606 if (isAcceptable != wasAcceptable) { 607 fireStateChange(); 608 } 609 } 610 611 616 private boolean isAcceptable() { 617 return isAcceptable; 618 } 619 620 626 protected void paintedFirstTime(java.awt.Graphics g) { 627 if (initialMessage != null) { 628 displayMessage(initialMessage); 629 initialMessage = null; 630 } 631 } 632 633 643 private void setMessage(final String message, final int msgType) { 644 String msgToDisplay = msgStack.setMessage(msgType, message); 645 if (msgToDisplay == null) { 646 return; } 648 649 650 if (!isPainted()) { 651 initialMessage = msgToDisplay; 652 } else { 653 displayMessage(msgToDisplay); 654 } 655 } 656 657 666 private void displayMessage(String message) { 667 if (message == null) { 668 message = ""; } 670 671 txtAreaMessage.setText(message); 672 adjustWindowSize(); 673 } 674 675 677 private Component createMessagePanel() { 678 txtAreaMessage = (JTextArea ) GuiUtils.createMultilineLabel(""); 680 Color color = UIManager.getColor("nb.errorForeground"); if (color == null) { 682 color = new Color (89, 79, 191); } 684 txtAreaMessage.setForeground(color); 685 686 return txtAreaMessage; 687 } 688 689 694 private Component createCodeGenPanel() { 695 696 697 String [] chkBoxIDs; 698 JCheckBox [] chkBoxes; 699 if (multipleClasses) { 700 chkBoxIDs = new String [] { 701 GuiUtils.CHK_PUBLIC, 702 GuiUtils.CHK_PROTECTED, 703 GuiUtils.CHK_PACKAGE, 704 GuiUtils.CHK_PACKAGE_PRIVATE_CLASSES, 705 GuiUtils.CHK_ABSTRACT_CLASSES, 706 GuiUtils.CHK_EXCEPTION_CLASSES, 707 GuiUtils.CHK_SUITES, 708 GuiUtils.CHK_SETUP, 709 GuiUtils.CHK_TEARDOWN, 710 GuiUtils.CHK_METHOD_BODIES, 711 GuiUtils.CHK_JAVADOC, 712 GuiUtils.CHK_HINTS 713 }; 714 } else { 715 chkBoxIDs = new String [] { 716 GuiUtils.CHK_PUBLIC, 717 GuiUtils.CHK_PROTECTED, 718 GuiUtils.CHK_PACKAGE, 719 null, null, null, null, GuiUtils.CHK_SETUP, 724 GuiUtils.CHK_TEARDOWN, 725 GuiUtils.CHK_METHOD_BODIES, 726 GuiUtils.CHK_JAVADOC, 727 GuiUtils.CHK_HINTS 728 }; 729 } 730 chkBoxes = GuiUtils.createCheckBoxes(chkBoxIDs); 731 int i = 0; 732 chkPublic = chkBoxes[i++]; 733 chkProtected = chkBoxes[i++]; 734 chkPackage = chkBoxes[i++]; 735 chkPackagePrivateClasses = chkBoxes[i++]; chkAbstractImpl = chkBoxes[i++]; chkExceptions = chkBoxes[i++]; chkGenerateSuites = chkBoxes[i++]; chkSetUp = chkBoxes[i++]; 740 chkTearDown = chkBoxes[i++]; 741 chkContent = chkBoxes[i++]; 742 chkJavaDoc = chkBoxes[i++]; 743 chkComments = chkBoxes[i++]; 744 745 746 JComponent methodAccessLevels = GuiUtils.createChkBoxGroup( 747 bundle.getString("JUnitCfgOfCreate.groupAccessLevels"), new JCheckBox [] {chkPublic, chkProtected, chkPackage}); 749 JComponent classTypes = null; 750 JComponent optionalClasses = null; 751 if (multipleClasses) { 752 classTypes = GuiUtils.createChkBoxGroup( 753 bundle.getString("JUnitCfgOfCreate.groupClassTypes"), new JCheckBox [] {chkPackagePrivateClasses, 755 chkAbstractImpl, chkExceptions}); 756 optionalClasses = GuiUtils.createChkBoxGroup( 757 bundle.getString("JUnitCfgOfCreate.groupOptClasses"), new JCheckBox [] {chkGenerateSuites}); 759 } 760 JComponent optionalCode = GuiUtils.createChkBoxGroup( 761 bundle.getString("JUnitCfgOfCreate.groupOptCode"), new JCheckBox [] {chkSetUp, chkTearDown, chkContent}); 763 JComponent optionalComments = GuiUtils.createChkBoxGroup( 764 bundle.getString("JUnitCfgOfCreate.groupOptComments"), new JCheckBox [] {chkJavaDoc, chkComments}); 766 767 768 Box leftColumn = Box.createVerticalBox(); 769 leftColumn.add(methodAccessLevels); 770 if (multipleClasses) { 771 leftColumn.add(Box.createVerticalStrut(11)); 772 leftColumn.add(classTypes); 773 } else { 774 779 leftColumn.add(Box.createVerticalStrut(0)); 780 } 781 leftColumn.add(Box.createVerticalGlue()); 782 783 784 Box rightColumn = Box.createVerticalBox(); 785 if (multipleClasses) { 786 rightColumn.add(optionalClasses); 787 rightColumn.add(Box.createVerticalStrut(11)); 788 } 789 rightColumn.add(optionalCode); 790 rightColumn.add(Box.createVerticalStrut(11)); 791 rightColumn.add(optionalComments); 792 rightColumn.add(Box.createVerticalGlue()); 793 794 795 JPanel jpCodeGen = new JPanel (); 797 jpCodeGen.setLayout(new BoxLayout (jpCodeGen, BoxLayout.X_AXIS)); 798 jpCodeGen.add(leftColumn); 799 jpCodeGen.add(Box.createHorizontalStrut(24)); 800 jpCodeGen.add(rightColumn); 801 802 803 addTitledBorder(jpCodeGen, 804 new Insets (12, 12, 11, 12), 805 bundle.getString("JUnitCfgOfCreate.jpCodeGen.title")); 807 808 methodAccessLevels.setAlignmentX(0.0f); 809 if (multipleClasses) { 810 classTypes.setAlignmentX(0.0f); 811 optionalClasses.setAlignmentX(0.0f); 812 } 813 optionalCode.setAlignmentX(0.0f); 814 optionalComments.setAlignmentX(0.0f); 815 816 return jpCodeGen; 817 } 818 819 827 private static void addTitledBorder(JComponent component, 828 Insets insets, 829 String title) { 830 Border insideBorder = BorderFactory.createEmptyBorder( 831 insets.top, insets.left, insets.bottom, insets.right); 832 Border outsideBorder = new TitledBorder ( 833 BorderFactory.createEtchedBorder(), title); 834 component.setBorder(new CompoundBorder (outsideBorder, insideBorder)); 835 } 836 837 839 FileObject getTargetFolder() { 840 Object selectedLocation = cboxLocation.getSelectedItem(); 841 842 if (selectedLocation == null) { 843 return null; 844 } 845 846 if (selectedLocation instanceof SourceGroup) { 847 return ((SourceGroup) selectedLocation).getRootFolder(); 848 } 849 assert selectedLocation instanceof FileObject; return (FileObject) selectedLocation; 851 } 852 853 856 private void fillFormData() { 857 final DataObject dataObj = (DataObject) 858 nodes[0].getLookup().lookup(DataObject.class); 859 final FileObject fileObj = dataObj.getPrimaryFile(); 860 861 if (singleClass) { 862 assert nodes.length == 1; 863 864 ClassPath cp = ClassPath.getClassPath(fileObj, ClassPath.SOURCE); 865 String className = cp.getResourceName(fileObj, '.', false); 866 lblClassToTestValue.setText(className); 867 868 if (tfClassName != null) { 869 String prefilledName = className + TEST_CLASS_SUFFIX; 870 tfClassName.setText(prefilledName); 871 tfClassName.setDefaultText(prefilledName); 872 tfClassName.setCaretPosition(prefilledName.length()); 873 } 874 } else if (singlePackage) { 875 assert nodes.length == 1; 876 877 ClassPath cp = ClassPath.getClassPath(fileObj, ClassPath.SOURCE); 878 String packageName = cp.getResourceName(fileObj, '.', true); 879 if (packageName.length() == 0) { 880 packageName = NbBundle.getMessage( 881 getClass(), 882 "DefaultPackageName"); } 884 lblClassToTestValue.setText(packageName); 885 } else { 886 } 888 889 setupLocationChooser(fileObj); 890 891 checkClassNameValidity(); 892 } 893 894 896 private void setupLocationChooser(FileObject refFileObject) { 897 Object [] targetFolders = TestUtil.getTestTargets(refFileObject); 898 if (targetFolders.length != 0) { 899 hasTargetFolders = true; 900 cboxLocation.setModel(new DefaultComboBoxModel (targetFolders)); 901 cboxLocation.setRenderer(new LocationChooserRenderer()); 902 } else { 903 hasTargetFolders = false; 904 String msgNoTargetsFound = NbBundle.getMessage( 906 getClass(), 907 refFileObject.isFolder() 908 ? "MSG_NoTestTarget_Fo" : "MSG_NoTestTarget_Fi", refFileObject.getNameExt()); 911 setMessage(msgNoTargetsFound, MSG_TYPE_NO_TARGET_FOLDERS); 912 disableComponents(); 913 } 914 } 915 916 925 private final class LocationChooserRenderer extends JLabel implements ListCellRenderer , UIResource { 926 927 public LocationChooserRenderer () { 928 setOpaque(true); 929 } 930 931 public Component getListCellRendererComponent( 932 JList list, 933 Object value, 934 int index, 935 boolean isSelected, 936 boolean cellHasFocus) { 937 setName("ComboBox.listRenderer"); 940 String text = value instanceof SourceGroup 941 ? ((SourceGroup) value).getDisplayName() 942 : value instanceof FileObject 943 ? FileUtil.getFileDisplayName((FileObject) value) 944 : value.toString(); 945 setText(text); 946 947 if ( isSelected ) { 948 setBackground(list.getSelectionBackground()); 949 setForeground(list.getSelectionForeground()); 950 } 951 else { 952 setBackground(list.getBackground()); 953 setForeground(list.getForeground()); 954 } 955 956 return this; 957 } 958 959 public String getName() { 961 String name = super.getName(); 962 return name == null ? "ComboBox.renderer" : name; } 964 965 } 966 967 976 private void addChangeListener(ChangeListener l) { 977 if (changeListeners == null) { 978 changeListeners = new ArrayList <ChangeListener >(3); 979 } 980 changeListeners.add(l); 981 } 982 983 991 private void removeChangeListener(ChangeListener l) { 992 if (changeListeners != null 993 && changeListeners.remove(l) 994 && changeListeners.isEmpty()) { 995 changeListeners = null; 996 } 997 } 998 999 1004 private void fireStateChange() { 1005 if (changeListeners != null) { 1006 ChangeEvent e = new ChangeEvent (this); 1007 for (Iterator i = changeListeners.iterator(); i.hasNext(); ) { 1008 ((ChangeListener ) i.next()).stateChanged(e); 1009 } 1010 } 1011 } 1012 1013 1017 private void disableComponents() { 1018 final Stack <Container > stack = new Stack <Container >(); 1019 stack.push(this); 1020 1021 while (!stack.empty()) { 1022 Container container = stack.pop(); 1023 Component comps[] = container.getComponents(); 1024 for (int i = 0; i < comps.length; i++) { 1025 final java.awt.Component comp = comps[i]; 1026 1027 if (comp == txtAreaMessage) { 1028 continue; 1029 } 1030 if (comp instanceof JPanel ) { 1031 JPanel panel = (JPanel ) comp; 1032 stack.push(panel); 1033 1034 final Border border = panel.getBorder(); 1035 if (border != null) { 1036 disableBorderTitles(border); 1037 } 1038 continue; 1039 } 1040 comp.setEnabled(false); 1041 if (comp instanceof java.awt.Container ) { 1042 Container nestedCont = (Container ) comp; 1043 if (nestedCont.getComponentCount() != 0) { 1044 stack.push(nestedCont); 1045 } 1046 } 1047 } 1048 } 1049 } 1050 1051 1053 private static void disableBorderTitles(Border border) { 1054 1055 if (border instanceof TitledBorder ) { 1056 disableBorderTitle((TitledBorder ) border); 1057 return; 1058 } 1059 1060 if (!(border instanceof CompoundBorder )) { 1061 return; 1062 } 1063 1064 Stack <CompoundBorder > stack = new Stack <CompoundBorder >(); 1065 stack.push((CompoundBorder ) border); 1066 while (!stack.empty()) { 1067 CompoundBorder cb = stack.pop(); 1068 1069 Border b; 1070 b = cb.getOutsideBorder(); 1071 if (b instanceof CompoundBorder ) { 1072 stack.push((CompoundBorder ) b); 1073 } else if (b instanceof TitledBorder ) { 1074 disableBorderTitle((TitledBorder ) b); 1075 } 1076 1077 b = cb.getInsideBorder(); 1078 if (b instanceof CompoundBorder ) { 1079 stack.push((CompoundBorder ) b); 1080 } else if (b instanceof TitledBorder ) { 1081 disableBorderTitle((TitledBorder ) b); 1082 } 1083 } 1084 } 1085 1086 1088 private static void disableBorderTitle(TitledBorder border) { 1089 final Color color = UIManager.getColor( 1090 "Label.disabledForeground"); if (color != null) { 1092 border.setTitleColor(color); 1093 } 1094 } 1095 1096 private JLabel lblClassToTestValue; 1097 private ClassNameTextField tfClassName; 1098 private JTextArea txtAreaMessage; 1099 private JComboBox cboxLocation; 1100 private JCheckBox chkAbstractImpl; 1101 private JCheckBox chkComments; 1102 private JCheckBox chkContent; 1103 private JCheckBox chkExceptions; 1104 private JCheckBox chkGenerateSuites; 1105 private JCheckBox chkJavaDoc; 1106 private JCheckBox chkPackage; 1107 private JCheckBox chkPackagePrivateClasses; 1108 private JCheckBox chkProtected; 1109 private JCheckBox chkPublic; 1110 private JCheckBox chkSetUp; 1111 private JCheckBox chkTearDown; 1112 1113} 1114 | Popular Tags |