From 2d4dd67114fe22e6692ceaafa3a6e28e2275e45a Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Mon, 8 Jun 2015 17:21:22 +0300 Subject: [PATCH] create test dialog: replace radiobuttons with combo --- .../createTest/CreateTestDialog.java | 226 ++++++++---------- .../src/messages/CodeInsightBundle.properties | 2 +- 2 files changed, 100 insertions(+), 128 deletions(-) diff --git a/java/java-impl/src/com/intellij/testIntegration/createTest/CreateTestDialog.java b/java/java-impl/src/com/intellij/testIntegration/createTest/CreateTestDialog.java index efdb2e01825f..8019dfa7698a 100644 --- a/java/java-impl/src/com/intellij/testIntegration/createTest/CreateTestDialog.java +++ b/java/java-impl/src/com/intellij/testIntegration/createTest/CreateTestDialog.java @@ -37,9 +37,10 @@ import com.intellij.openapi.help.HelpManager; import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ModuleRootManager; +import com.intellij.openapi.ui.ComboBox; import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.ui.Messages; -import com.intellij.openapi.util.Pair; +import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.*; @@ -53,10 +54,7 @@ import com.intellij.refactoring.util.classMembers.MemberInfo; import com.intellij.testIntegration.JavaTestFramework; import com.intellij.testIntegration.TestFramework; import com.intellij.testIntegration.TestIntegrationUtils; -import com.intellij.ui.EditorTextField; -import com.intellij.ui.RecentsManager; -import com.intellij.ui.ReferenceEditorComboWithBrowseButton; -import com.intellij.ui.ScrollPaneFactory; +import com.intellij.ui.*; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -80,25 +78,24 @@ public class CreateTestDialog extends DialogWrapper { private final Project myProject; private final PsiClass myTargetClass; + private final PsiPackage myTargetPackage; private final Module myTargetModule; private PsiDirectory myTargetDirectory; private TestFramework mySelectedFramework; - private final List myLibraryButtons = new ArrayList(); + private final ComboBox myLibrariesCombo = new ComboBox(new DefaultComboBoxModel()); private EditorTextField myTargetClassNameField; private ReferenceEditorComboWithBrowseButton mySuperClassField; private ReferenceEditorComboWithBrowseButton myTargetPackageField; - private JCheckBox myGenerateBeforeBox; - private JCheckBox myGenerateAfterBox; - private JCheckBox myShowInheritedMethodsBox; - private MemberSelectionTable myMethodsTable; - private JButton myFixLibraryButton; + private JCheckBox myGenerateBeforeBox = new JCheckBox(CodeInsightBundle.message("intention.create.test.dialog.setUp")); + private JCheckBox myGenerateAfterBox = new JCheckBox(CodeInsightBundle.message("intention.create.test.dialog.tearDown")); + private JCheckBox myShowInheritedMethodsBox = new JCheckBox(CodeInsightBundle.message("intention.create.test.dialog.show.inherited")); + private MemberSelectionTable myMethodsTable = new MemberSelectionTable(Collections.emptyList(), null); + private JButton myFixLibraryButton = new JButton(CodeInsightBundle.message("intention.create.test.dialog.fix.library")); private JPanel myFixLibraryPanel; private JLabel myFixLibraryLabel; - private JRadioButton myDefaultLibraryButton; - public CreateTestDialog(@NotNull Project project, @NotNull String title, PsiClass targetClass, @@ -108,113 +105,11 @@ public class CreateTestDialog extends DialogWrapper { myProject = project; myTargetClass = targetClass; + myTargetPackage = targetPackage; myTargetModule = targetModule; - initControls(targetClass, targetPackage); setTitle(title); init(); - - myDefaultLibraryButton.doClick(); - } - - private void initControls(PsiClass targetClass, PsiPackage targetPackage) { - ButtonGroup group = new ButtonGroup(); - - Map nameToButtonMap = new HashMap(); - List> attachedLibraries = new ArrayList>(); - - for (final TestFramework descriptor : Extensions.getExtensions(TestFramework.EXTENSION_NAME)) { - final JRadioButton b = new JRadioButton(descriptor.getName()); - if (descriptor instanceof JavaTestFramework) { - b.setMnemonic(((JavaTestFramework)descriptor).getMnemonic()); - } - myLibraryButtons.add(b); - group.add(b); - - nameToButtonMap.put(descriptor.getName(), b); - if (descriptor.isLibraryAttached(myTargetModule)) { - attachedLibraries.add(Pair.create(descriptor.getName(), b)); - } - - b.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - if (b.isSelected()) onLibrarySelected(descriptor); - } - }); - } - - String defaultLibrary = getDefaultLibraryName(); - if (attachedLibraries.isEmpty()) { - if (defaultLibrary != null) { - myDefaultLibraryButton = nameToButtonMap.get(defaultLibrary); - } - } - else { - if (defaultLibrary != null) { - for (Pair each : attachedLibraries) { - if (each.first.equals(defaultLibrary)) { - myDefaultLibraryButton = each.second; - } - } - } - if (myDefaultLibraryButton == null) { - myDefaultLibraryButton = attachedLibraries.get(0).second; - } - } - if (myDefaultLibraryButton == null) { - myDefaultLibraryButton = myLibraryButtons.get(0); - } - - myFixLibraryButton = new JButton(CodeInsightBundle.message("intention.create.test.dialog.fix.library")); - myFixLibraryButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - ApplicationManager.getApplication().runWriteAction(new Runnable() { - public void run() { - if (mySelectedFramework instanceof JavaTestFramework) { - ((JavaTestFramework)mySelectedFramework).setupLibrary(myTargetModule); - } else { - OrderEntryFix.addJarToRoots(mySelectedFramework.getLibraryPath(), myTargetModule, null); - } - } - }); - myFixLibraryPanel.setVisible(false); - } - }); - - myTargetClassNameField = new EditorTextField(suggestTestClassName(targetClass)); - myTargetClassNameField.getDocument().addDocumentListener(new DocumentAdapter() { - @Override - public void documentChanged(DocumentEvent e) { - getOKAction().setEnabled(PsiNameHelper.getInstance(myProject).isIdentifier(getClassName())); - } - }); - - mySuperClassField = new ReferenceEditorComboWithBrowseButton(new MyChooseSuperClassAction(), null, myProject, true, - JavaCodeFragment.VisibilityChecker.EVERYTHING_VISIBLE, RECENT_SUPERS_KEY); - mySuperClassField.setMinimumSize(mySuperClassField.getPreferredSize()); - - String targetPackageName = targetPackage != null ? targetPackage.getQualifiedName() : ""; - myTargetPackageField = new PackageNameReferenceEditorCombo(targetPackageName, myProject, RECENTS_KEY, CodeInsightBundle.message("dialog.create.class.package.chooser.title")); - - new AnAction() { - public void actionPerformed(AnActionEvent e) { - myTargetPackageField.getButton().doClick(); - } - }.registerCustomShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_DOWN_MASK)), - myTargetPackageField.getChildComponent()); - - myGenerateBeforeBox = new JCheckBox(CodeInsightBundle.message("intention.create.test.dialog.setUp")); - myGenerateAfterBox = new JCheckBox(CodeInsightBundle.message("intention.create.test.dialog.tearDown")); - - myShowInheritedMethodsBox = new JCheckBox(CodeInsightBundle.message("intention.create.test.dialog.show.inherited")); - myShowInheritedMethodsBox.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - updateMethodsTable(); - } - }); - restoreShowInheritedMembersStatus(); - myMethodsTable = new MemberSelectionTable(Collections.emptyList(), null); - updateMethodsTable(); } protected String suggestTestClassName(PsiClass targetClass) { @@ -323,15 +218,6 @@ public class CreateTestDialog extends DialogWrapper { constr.fill = GridBagConstraints.HORIZONTAL; constr.anchor = GridBagConstraints.WEST; - - JPanel librariesPanel = new JPanel(); - BoxLayout l = new BoxLayout(librariesPanel, BoxLayout.X_AXIS); - librariesPanel.setLayout(l); - - for (JRadioButton b : myLibraryButtons) { - librariesPanel.add(b); - } - int gridy = 1; @@ -339,12 +225,14 @@ public class CreateTestDialog extends DialogWrapper { constr.gridy = gridy++; constr.gridx = 0; constr.weightx = 0; - panel.add(new JLabel(CodeInsightBundle.message("intention.create.test.dialog.testing.library")), constr); + final JLabel libLabel = new JLabel(CodeInsightBundle.message("intention.create.test.dialog.testing.library")); + libLabel.setLabelFor(myLibrariesCombo); + panel.add(libLabel, constr); constr.gridx = 1; constr.weightx = 1; constr.gridwidth = GridBagConstraints.REMAINDER; - panel.add(librariesPanel, constr); + panel.add(myLibrariesCombo, constr); myFixLibraryPanel = new JPanel(new BorderLayout()); myFixLibraryLabel = new JLabel(); @@ -366,6 +254,14 @@ public class CreateTestDialog extends DialogWrapper { constr.gridwidth = 1; panel.add(new JLabel(CodeInsightBundle.message("intention.create.test.dialog.class.name")), constr); + myTargetClassNameField = new EditorTextField(suggestTestClassName(myTargetClass)); + myTargetClassNameField.getDocument().addDocumentListener(new DocumentAdapter() { + @Override + public void documentChanged(DocumentEvent e) { + getOKAction().setEnabled(PsiNameHelper.getInstance(myProject).isIdentifier(getClassName())); + } + }); + constr.gridx = 1; constr.weightx = 1; panel.add(myTargetClassNameField, constr); @@ -376,6 +272,9 @@ public class CreateTestDialog extends DialogWrapper { constr.weightx = 0; panel.add(new JLabel(CodeInsightBundle.message("intention.create.test.dialog.super.class")), constr); + mySuperClassField = new ReferenceEditorComboWithBrowseButton(new MyChooseSuperClassAction(), null, myProject, true, + JavaCodeFragment.VisibilityChecker.EVERYTHING_VISIBLE, RECENT_SUPERS_KEY); + mySuperClassField.setMinimumSize(mySuperClassField.getPreferredSize()); constr.gridx = 1; constr.weightx = 1; panel.add(mySuperClassField, constr); @@ -389,6 +288,16 @@ public class CreateTestDialog extends DialogWrapper { constr.gridx = 1; constr.weightx = 1; + + String targetPackageName = myTargetPackage != null ? myTargetPackage.getQualifiedName() : ""; + myTargetPackageField = new PackageNameReferenceEditorCombo(targetPackageName, myProject, RECENTS_KEY, CodeInsightBundle.message("dialog.create.class.package.chooser.title")); + + new AnAction() { + public void actionPerformed(AnActionEvent e) { + myTargetPackageField.getButton().doClick(); + } + }.registerCustomShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_DOWN_MASK)), + myTargetPackageField.getChildComponent()); JPanel targetPackagePanel = new JPanel(new BorderLayout()); targetPackagePanel.add(myTargetPackageField, BorderLayout.CENTER); panel.add(targetPackagePanel, constr); @@ -427,6 +336,69 @@ public class CreateTestDialog extends DialogWrapper { constr.weighty = 1; panel.add(ScrollPaneFactory.createScrollPane(myMethodsTable), constr); + myLibrariesCombo.setRenderer(new ListCellRendererWrapper() { + @Override + public void customize(JList list, TestFramework value, int index, boolean selected, boolean hasFocus) { + if (value != null) { + setText(value.getName()); + } + } + }); + final List attachedLibraries = new ArrayList(); + final String defaultLibrary = getDefaultLibraryName(); + TestFramework defaultDescriptor = null; + final DefaultComboBoxModel model = (DefaultComboBoxModel)myLibrariesCombo.getModel(); + for (final TestFramework descriptor : Extensions.getExtensions(TestFramework.EXTENSION_NAME)) { + model.addElement(descriptor); + if (descriptor.isLibraryAttached(myTargetModule)) { + attachedLibraries.add(descriptor); + } + + if (Comparing.equal(defaultLibrary, descriptor.getName())) { + defaultDescriptor = descriptor; + } + } + + myLibrariesCombo.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + final Object selectedItem = myLibrariesCombo.getSelectedItem(); + if (selectedItem != null) { + onLibrarySelected((TestFramework)selectedItem); + } + } + }); + + if (defaultDescriptor != null && (attachedLibraries.contains(defaultDescriptor) || attachedLibraries.isEmpty())) { + myLibrariesCombo.setSelectedItem(defaultDescriptor); + } + else { + myLibrariesCombo.setSelectedIndex(0); + } + + myFixLibraryButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + ApplicationManager.getApplication().runWriteAction(new Runnable() { + public void run() { + if (mySelectedFramework instanceof JavaTestFramework) { + ((JavaTestFramework)mySelectedFramework).setupLibrary(myTargetModule); + } else { + OrderEntryFix.addJarToRoots(mySelectedFramework.getLibraryPath(), myTargetModule, null); + } + } + }); + myFixLibraryPanel.setVisible(false); + } + }); + + + + myShowInheritedMethodsBox.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + updateMethodsTable(); + } + }); + restoreShowInheritedMembersStatus(); + updateMethodsTable(); return panel; } diff --git a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties index 70bf259b5090..b6cd77b9cf88 100644 --- a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties +++ b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties @@ -230,7 +230,7 @@ intention.break.string.on.line.breaks.text=Break string on '\\n' intention.create.test=Create Test -intention.create.test.dialog.testing.library=Testing library: +intention.create.test.dialog.testing.library=Testing &library: intention.create.test.dialog.language=Language: intention.create.test.dialog.class.name=Class name: intention.create.test.dialog.super.class=Superclass: