create test dialog: replace radiobuttons with combo

This commit is contained in:
Anna Kozlova
2015-06-08 17:50:26 +03:00
parent 04397f4110
commit 2d4dd67114
2 changed files with 100 additions and 128 deletions
@@ -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<JRadioButton> myLibraryButtons = new ArrayList<JRadioButton>();
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.<MemberInfo>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<String, JRadioButton> nameToButtonMap = new HashMap<String, JRadioButton>();
List<Pair<String, JRadioButton>> attachedLibraries = new ArrayList<Pair<String, JRadioButton>>();
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<String, JRadioButton> 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.<MemberInfo>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<TestFramework>() {
@Override
public void customize(JList list, TestFramework value, int index, boolean selected, boolean hasFocus) {
if (value != null) {
setText(value.getName());
}
}
});
final List<TestFramework> attachedLibraries = new ArrayList<TestFramework>();
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;
}