package jetbrains.mps.lang.generator.pluginSolution.plugin;
/*Generated by MPS */
import com.intellij.openapi.ui.DialogWrapper;
import jetbrains.mps.project.MPSProject;
import org.jetbrains.mps.openapi.model.SModelReference;
import jetbrains.mps.workbench.goTo.ui.ChooseByNamePanel;
import org.jetbrains.annotations.Nullable;
import com.intellij.openapi.ui.ValidationInfo;
import javax.swing.JComponent;
import javax.swing.JPanel;
import java.awt.GridLayout;
import org.jetbrains.mps.util.Condition;
import org.jetbrains.mps.openapi.model.SModel;
import jetbrains.mps.smodel.SModelStereotype;
import jetbrains.mps.util.AndCondition;
import jetbrains.mps.util.NotCondition;
import jetbrains.mps.scope.ConditionalScope;
import jetbrains.mps.FilteredGlobalScope;
import org.jetbrains.mps.openapi.module.SRepository;
import jetbrains.mps.workbench.choose.ChooseByNameData;
import jetbrains.mps.workbench.choose.ModelsPresentation;
import jetbrains.mps.workbench.choose.ModelScopeIterable;
import jetbrains.mps.workbench.goTo.ui.MpsPopupFactory;
import com.intellij.ide.util.gotoByName.ChooseByNamePopupComponent;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.util.Disposer;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.BoxLayout;
import javax.swing.BorderFactory;
import javax.swing.JRadioButton;
import java.awt.event.ActionEvent;
/*package*/ class DeriveGenPlanDialog extends DialogWrapper {
private final MPSProject myProject;
private DeriveGenPlanDialog.ExclusiveAlternativesPanel<Integer> myStepModuleKind;
private DeriveGenPlanDialog.ExclusiveAlternativesPanel<Integer> myStepGranularityKind;
private SModelReference mySampleModel;
private ChooseByNamePanel myChoosePanel;
/*package*/ DeriveGenPlanDialog(MPSProject mpsProject) {
super(mpsProject.getProject());
myProject = mpsProject;
}
@Override
public void show() {
init();
super.show();
}
@Nullable
@Override
protected ValidationInfo doValidate() {
if (myStepModuleKind.getSelectedValue() == null) {
return new ValidationInfo("Please select kind of a module to reference from the plan", myStepModuleKind.getComponent());
}
if (myStepGranularityKind.getSelectedValue() == null) {
return new ValidationInfo("", myStepGranularityKind.getComponent());
}
if (mySampleModel == null) {
return new ValidationInfo("Missing model to derive plan from", myChoosePanel.getPanel());
}
return super.doValidate();
}
@Override
protected void doOKAction() {
super.doOKAction();
System.out.println("Module:" + myStepModuleKind.getSelectedValue());
System.out.println("Granularity:" + myStepGranularityKind.getSelectedValue());
System.out.println("Model:" + mySampleModel);
}
@Nullable
public SModelReference getSelectedModel() {
return mySampleModel;
}
public boolean isDistinctStep() {
return myStepGranularityKind.getSelectedValue() == 4;
}
public boolean useLanguageInSteps() {
return myStepModuleKind.getSelectedValue() == 1;
}
@Nullable
protected JComponent createCenterPanel() {
myStepModuleKind = new DeriveGenPlanDialog.ExclusiveAlternativesPanel("Specify transformation with");
myStepModuleKind.addOption("Language", 1);
myStepModuleKind.addOption("Generator", 2);
myStepGranularityKind = new DeriveGenPlanDialog.ExclusiveAlternativesPanel("Step granularity");
myStepGranularityKind.addOption("All transformation in a single step", 3);
myStepGranularityKind.addOption("Single language/generator per step", 4);
JPanel p = new JPanel(new GridLayout(1, 2));
p.add(myStepModuleKind.getComponent());
p.add(myStepGranularityKind.getComponent());
Condition<SModel> isStub = new Condition<SModel>() {
public boolean met(SModel m) {
return SModelStereotype.isStubModel(m);
}
};
Condition<SModel> isDescriptor = new Condition<SModel>() {
public boolean met(SModel m) {
return SModelStereotype.isDescriptorModel(m);
}
};
Condition<SModel> filter = new AndCondition<SModel>(NotCondition.negate(isStub), NotCondition.negate(isDescriptor));
ConditionalScope localScope = new ConditionalScope(myProject.getScope(), null, filter);
ConditionalScope globalScope = new ConditionalScope(new FilteredGlobalScope(), null, filter);
SRepository repo = myProject.getRepository();
ChooseByNameData<SModelReference> gotoData = new ChooseByNameData<SModelReference>(new ModelsPresentation(repo));
gotoData.derivePrompts("model").setScope(new ModelScopeIterable(localScope, repo), new ModelScopeIterable(globalScope, repo));
myChoosePanel = MpsPopupFactory.createPanelForPackage(myProject.getProject(), gotoData, false);
myChoosePanel.invoke(new ChooseByNamePopupComponent.Callback() {
public void elementChosen(Object o) {
if (o instanceof SModelReference) {
setSampleModel(((SModelReference) o));
}
}
}, ModalityState.current(), false);
Disposer.register(getDisposable(), myChoosePanel);
JPanel rv = new JPanel(new BorderLayout());
rv.add(myChoosePanel.getPanel(), BorderLayout.NORTH);
rv.add(p, BorderLayout.CENTER);
return rv;
}
@Nullable
@Override
public JComponent getPreferredFocusedComponent() {
return myChoosePanel.getPreferredFocusedComponent();
}
/*package*/ void setSampleModel(SModelReference modelRef) {
mySampleModel = modelRef;
}
private static class ExclusiveAlternativesPanel<AssociatedValue> implements ActionListener {
private final Box myComponent;
private final ButtonGroup myGroup;
private AssociatedValue mySelectedValue;
public ExclusiveAlternativesPanel(String title) {
myComponent = new Box(BoxLayout.Y_AXIS);
myGroup = new ButtonGroup();
myComponent.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), title));
}
public JComponent getComponent() {
return myComponent;
}
public void addOption(String title, AssociatedValue associatedValue) {
JRadioButton b = new JRadioButton(title);
b.addActionListener(this);
b.putClientProperty(getClass().getSimpleName(), associatedValue);
myComponent.add(b);
myGroup.add(b);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() instanceof JRadioButton) {
Object value = ((JRadioButton) event.getSource()).getClientProperty(getClass().getSimpleName());
mySelectedValue = (AssociatedValue) value;
}
}
@Nullable
public AssociatedValue getSelectedValue() {
return mySelectedValue;
}
}
}