package jetbrains.mps.baseLanguage.unitTest.execution.settings;
/*Generated by MPS */
import com.intellij.ui.components.JBPanel;
import jetbrains.mps.execution.lib.ui.FieldWithPathChooseDialog;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import jetbrains.mps.project.Project;
import javax.swing.JComponent;
import com.intellij.ui.components.JBRadioButton;
import org.jetbrains.annotations.NotNull;
import java.awt.GridBagLayout;
import jetbrains.mps.ide.project.ProjectHelper;
import javax.swing.BoxLayout;
import com.intellij.ui.components.JBLabel;
import javax.swing.ButtonGroup;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.internal.collections.runtime.IVisitor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import jetbrains.mps.ide.common.LayoutUtil;
import com.intellij.ui.components.JBTextField;
import javax.swing.JLabel;
import jetbrains.mps.baseLanguage.execution.api.JavaConfigurationEditorComponent;
import java.util.List;
import jetbrains.mps.baseLanguage.unitTest.execution.client.ITestNodeWrapper;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.ArrayList;
import jetbrains.mps.execution.lib.ClonableList;
import jetbrains.mps.baseLanguage.closures.runtime.Wrappers;
import jetbrains.mps.execution.lib.PointerUtils;
import org.jetbrains.mps.openapi.model.SModelReference;
import org.jetbrains.mps.openapi.module.SModuleReference;
public class JUnitConfigurationEditorComponent extends JBPanel {
private final InProcessJBCheckBox myInProcessCheckBox = new InProcessJBCheckBox("Execute in the same process ", true);
private JBReuseCachesCheckBox myReuseCachesCheckBox = new JBReuseCachesCheckBox("Reuse caches", true);
private FieldWithPathChooseDialog myCachesDir = new FieldWithPathChooseDialog(new FileChooserDescriptor(false, true, false, false, false, false));
private final Project myProject;
private final ModuleChooser myModuleChooser;
private final ModelChooser myModelChooser;
private final TestListPanel myClassesList;
private final TestListPanel myMethodsList;
private final JComponent[] myPanels = new JComponent[JUnitRunTypes.values().length];
private final JBRadioButton[] myButtons = new JBRadioButton[JUnitRunTypes.values().length];
private JUnitRunTypes myRunKind = JUnitRunTypes.PROJECT;
public JUnitConfigurationEditorComponent(@NotNull com.intellij.openapi.project.Project project) {
super(new GridBagLayout());
myProject = ProjectHelper.fromIdeaProject(project);
final JBPanel kindPanel = new JBPanel();
kindPanel.setLayout(new BoxLayout(kindPanel, BoxLayout.X_AXIS));
kindPanel.add(new JBLabel("Test scope:"));
final JBRadioButton projectKind = new JBRadioButton("Project", true);
final JBRadioButton moduleKind = new JBRadioButton("Module");
final JBRadioButton modelKind = new JBRadioButton("Model");
final JBRadioButton classKind = new JBRadioButton("Class");
final JBRadioButton methodKind = new JBRadioButton("Method");
myButtons[JUnitRunTypes.PROJECT.ordinal()] = projectKind;
myButtons[JUnitRunTypes.MODULE.ordinal()] = moduleKind;
myButtons[JUnitRunTypes.MODEL.ordinal()] = modelKind;
myButtons[JUnitRunTypes.NODE.ordinal()] = classKind;
myButtons[JUnitRunTypes.METHOD.ordinal()] = methodKind;
final ButtonGroup kindaRadioGroup = new ButtonGroup();
Sequence.fromIterable(Sequence.fromArray(myButtons)).visitAll(new IVisitor<JBRadioButton>() {
public void visit(JBRadioButton it) {
kindaRadioGroup.add(it);
kindPanel.add(it);
}
});
projectKind.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (projectKind.isSelected()) {
myRunKind = JUnitRunTypes.PROJECT;
}
updatePanels();
}
});
moduleKind.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (moduleKind.isSelected()) {
myRunKind = JUnitRunTypes.MODULE;
}
updatePanels();
}
});
modelKind.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (modelKind.isSelected()) {
myRunKind = JUnitRunTypes.MODEL;
}
updatePanels();
}
});
classKind.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (classKind.isSelected()) {
myRunKind = JUnitRunTypes.NODE;
}
updatePanels();
}
});
methodKind.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (methodKind.isSelected()) {
myRunKind = JUnitRunTypes.METHOD;
}
updatePanels();
}
});
JBPanel projectPanel = new JBPanel(new GridBagLayout());
projectPanel.add(new JBLabel("Project:"), LayoutUtil.createLabelConstraints(0));
JBTextField projectNameField = new JBTextField(project.getName());
projectNameField.setEditable(false);
projectPanel.add(projectNameField, LayoutUtil.createPanelConstraints(1));
JBPanel modulePanel = new JBPanel(new GridBagLayout());
modulePanel.add(new JBLabel("Module:"), LayoutUtil.createLabelConstraints(0));
myModuleChooser = new ModuleChooser(myProject);
modulePanel.add(myModuleChooser, LayoutUtil.createPanelConstraints(1));
JBPanel modelPanel = new JBPanel(new GridBagLayout());
modelPanel.add(new JBLabel("Model:"), LayoutUtil.createLabelConstraints(0));
myModelChooser = new ModelChooser(myProject);
modelPanel.add(myModelChooser, LayoutUtil.createPanelConstraints(1));
myClassesList = new TestListPanel(project, false);
myMethodsList = new TestListPanel(project, true);
myPanels[JUnitRunTypes.PROJECT.ordinal()] = projectPanel;
myPanels[JUnitRunTypes.MODULE.ordinal()] = modulePanel;
myPanels[JUnitRunTypes.MODEL.ordinal()] = modelPanel;
myPanels[JUnitRunTypes.NODE.ordinal()] = myClassesList;
myPanels[JUnitRunTypes.METHOD.ordinal()] = myMethodsList;
JBPanel saveCachesPanel = new JBPanel(new GridBagLayout());
saveCachesPanel.add(new JLabel("Save caches in: "), LayoutUtil.createLabelConstraints(0));
saveCachesPanel.add(myCachesDir, LayoutUtil.createFieldConstraints(1));
add(kindPanel, LayoutUtil.createPanelConstraints(0));
add(projectPanel, LayoutUtil.createPanelConstraints(1));
add(modulePanel, LayoutUtil.createPanelConstraints(1));
add(modelPanel, LayoutUtil.createPanelConstraints(1));
add(myClassesList, LayoutUtil.createPanelConstraints(1));
add(myMethodsList, LayoutUtil.createPanelConstraints(1));
add(myInProcessCheckBox, LayoutUtil.createFieldConstraints(2));
add(myReuseCachesCheckBox, LayoutUtil.createFieldConstraints(3));
add(saveCachesPanel, LayoutUtil.createPanelConstraints(4));
}
public void attachJavaComponent(final JavaConfigurationEditorComponent javaEditorComponent) {
myInProcessCheckBox.registerComponents(Sequence.fromArray(javaEditorComponent.getComponents()));
myInProcessCheckBox.registerComponent(myCachesDir);
myInProcessCheckBox.registerComponent(myReuseCachesCheckBox);
myInProcessCheckBox.update();
}
private void updatePanels() {
Sequence.fromIterable(Sequence.fromArray(myPanels)).visitAll(new IVisitor<JComponent>() {
public void visit(JComponent it) {
it.setVisible(false);
}
});
myPanels[myRunKind.ordinal()].setVisible(true);
}
public void apply(final JUnitSettings_Configuration configuration) {
final List<ITestNodeWrapper> classes = ListSequence.fromList(new ArrayList<ITestNodeWrapper>());
ListSequence.fromList(classes).addSequence(ListSequence.fromList(myClassesList.getItems()));
final List<ITestNodeWrapper> methods = ListSequence.fromList(new ArrayList<ITestNodeWrapper>());
ListSequence.fromList(methods).addSequence(ListSequence.fromList(myMethodsList.getItems()));
final ClonableList<String> testMethods = new ClonableList<String>();
final ClonableList<String> testCases = new ClonableList<String>();
final Wrappers._T<String> model = new Wrappers._T<String>();
final Wrappers._T<String> module = new Wrappers._T<String>();
myProject.getModelAccess().runReadAction(new Runnable() {
public void run() {
for (ITestNodeWrapper testMethod : methods) {
testMethods.add(PointerUtils.pointerToString(testMethod.getNodePointer()));
}
for (ITestNodeWrapper testCase : classes) {
testCases.add(PointerUtils.pointerToString(testCase.getNodePointer()));
}
SModelReference modelRef = myModelChooser.getReference();
if (modelRef != null) {
model.value = modelRef.getName().getValue();
}
SModuleReference moduleRef = myModuleChooser.getReference();
if (moduleRef != null) {
module.value = moduleRef.getModuleName();
}
}
});
configuration.setRunType(myRunKind.ordinal());
configuration.setTestMethods(testMethods);
configuration.setTestCases(testCases);
configuration.setModel(model.value);
configuration.setModule(module.value);
configuration.setInProcess(myInProcessCheckBox.isSelected());
configuration.setReuseCaches(myReuseCachesCheckBox.isSelected());
configuration.setCachesPath(myCachesDir.getText());
}
public void reset(final JUnitSettings_Configuration settings) {
if (settings.getRunType() >= 0 && settings.getRunType() < JUnitRunTypes.values().length) {
myRunKind = settings.getJUnitRunType();
} else {
myRunKind = JUnitRunTypes.PROJECT;
}
myButtons[myRunKind.ordinal()].setSelected(true);
// nodes
final List<ITestNodeWrapper> classes = ListSequence.fromList(new ArrayList<ITestNodeWrapper>());
myProject.getModelAccess().runReadAction(new Runnable() {
public void run() {
ListSequence.fromList(TestUtils.wrapPointerStrings(myProject, settings.getTestCases())).visitAll(new IVisitor<ITestNodeWrapper>() {
public void visit(ITestNodeWrapper it) {
ListSequence.fromList(classes).addElement(it);
}
});
}
});
myClassesList.setData(classes);
// methods
final List<ITestNodeWrapper> methods = ListSequence.fromList(new ArrayList<ITestNodeWrapper>());
myProject.getModelAccess().runReadAction(new Runnable() {
public void run() {
ListSequence.fromList(TestUtils.wrapPointerStrings(myProject, settings.getTestMethods())).visitAll(new IVisitor<ITestNodeWrapper>() {
public void visit(ITestNodeWrapper it) {
ListSequence.fromList(methods).addElement(it);
}
});
}
});
myMethodsList.setData(methods);
if (settings.getModel() != null) {
String modelName = settings.getModel();
myModelChooser.setModel(modelName);
}
if (settings.getModule() != null) {
myModuleChooser.setModule(settings.getModule());
}
updateCheckBoxes(settings);
updatePanels();
myCachesDir.setText(settings.getCachesPath());
}
private void updateCheckBoxes(JUnitSettings_Configuration configuration) {
myInProcessCheckBox.setSelected(configuration.getInProcess());
myReuseCachesCheckBox.setSelected(configuration.getReuseCaches());
myInProcessCheckBox.update();
}
public void dispose() {
myModuleChooser.dispose();
myModelChooser.dispose();
}
}