package jetbrains.mps.ide.modelchecker.platform.actions;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import com.intellij.openapi.options.SearchableConfigurable;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JCheckBox;
import javax.swing.JTextArea;
import java.util.Hashtable;
import javax.swing.JLabel;
import java.awt.Dimension;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.BorderFactory;
import java.awt.BorderLayout;
import javax.swing.border.EtchedBorder;
import java.awt.GridBagLayout;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagConstraints;
import javax.swing.Icon;
import com.intellij.openapi.options.ConfigurationException;
import java.util.Arrays;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Nls;
import javax.swing.JComponent;

@GeneratedClass(node = "r:5754bb7d-f802-4a0f-bd3d-0764f0d71413(jetbrains.mps.ide.modelchecker.platform.actions)/3719390199793466714", model = "r:5754bb7d-f802-4a0f-bd3d-0764f0d71413(jetbrains.mps.ide.modelchecker.platform.actions)")
public class ModelCheckerPreferencesPage implements SearchableConfigurable {
  private final JPanel myPage;
  private final JSlider myCheckingLevelSlider = new JSlider(JSlider.VERTICAL, 0, ModelCheckerSettings.CheckingLevel.values().length - 1, 0);
  private final JCheckBox myCheckStubsCheckBox = new JCheckBox("Check stub models");
  private final JCheckBox myCheckSpecificCheckBox = new JCheckBox("Perform additional checks");
  private final JCheckBox myRunInParallelCheckBox = new JCheckBox("Run in parallel");
  private final ModelCheckerSettings myModelCheckerSettings;
  private final JTextArea myDescriptionText = new JTextArea();

  public ModelCheckerPreferencesPage() {
    myModelCheckerSettings = ModelCheckerSettings.getInstance();

    Hashtable t = new Hashtable();
    for (ModelCheckerSettings.CheckingLevel level : ModelCheckerSettings.CheckingLevel.values()) {
      t.put(level.ordinal(), new JLabel(level.getPresentation()));
    }
    myCheckingLevelSlider.setInverted(true);
    myCheckingLevelSlider.setLabelTable(t);
    myCheckingLevelSlider.setPaintLabels(true);
    myCheckingLevelSlider.setPaintTicks(true);
    myCheckingLevelSlider.setMajorTickSpacing(1);
    myCheckingLevelSlider.setSnapToTicks(true);
    myCheckingLevelSlider.setPreferredSize(new Dimension(140, 180));
    myCheckingLevelSlider.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent p0) {
        myDescriptionText.setText(ModelCheckerSettings.CheckingLevel.values()[myCheckingLevelSlider.getValue()].getLongDescription());
      }
    });

    myDescriptionText.setEditable(false);
    myDescriptionText.setOpaque(false);
    myDescriptionText.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));

    JPanel levelSelectionPanel = new JPanel(new BorderLayout());
    levelSelectionPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Model checking level"));
    levelSelectionPanel.add(myCheckingLevelSlider, BorderLayout.WEST);
    levelSelectionPanel.add(myDescriptionText, BorderLayout.CENTER);

    myPage = new JPanel(new GridBagLayout());
    myPage.setBorder(new EmptyBorder(10, 10, 10, 10));
    GridBagConstraints c = new GridBagConstraints();

    c.gridx = 0;
    c.gridy = 0;
    c.anchor = GridBagConstraints.NORTHWEST;
    c.fill = GridBagConstraints.HORIZONTAL;
    myPage.add(levelSelectionPanel, c);

    c.fill = GridBagConstraints.NONE;

    c.gridy++;
    myPage.add(myCheckStubsCheckBox, c);

    c.gridy++;
    myPage.add(myCheckSpecificCheckBox, c);

    c.gridy++;
    myPage.add(myRunInParallelCheckBox, c);

    c.gridy++;
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.fill = GridBagConstraints.BOTH;
    myPage.add(new JPanel(), c);
  }

  public String getName() {
    return "Model Checker";
  }

  public Icon getIcon() {
    return null;
  }

  public boolean validate() {
    return true;
  }

  public void apply() throws ConfigurationException {
    myModelCheckerSettings.setCheckingLevel(ModelCheckerSettings.CheckingLevel.values()[myCheckingLevelSlider.getValue()]);
    myModelCheckerSettings.setCheckStubs(myCheckStubsCheckBox.isSelected());
    myModelCheckerSettings.setIncludeAdditionalChecks(myCheckSpecificCheckBox.isSelected());
    myModelCheckerSettings.setRunInParallel(myRunInParallelCheckBox.isSelected());
  }
  public void reset() {
    myCheckingLevelSlider.setValue(Arrays.binarySearch(ModelCheckerSettings.CheckingLevel.values(), myModelCheckerSettings.getCheckingLevel()));
    myCheckStubsCheckBox.setSelected(myModelCheckerSettings.isCheckStubs());
    myCheckSpecificCheckBox.setSelected(myModelCheckerSettings.isIncludeAdditionalChecks());
    myRunInParallelCheckBox.setSelected(myModelCheckerSettings.isRunInParallel());
  }
  @Override
  public boolean isModified() {
    if (myCheckingLevelSlider.getValue() != Arrays.binarySearch(ModelCheckerSettings.CheckingLevel.values(), myModelCheckerSettings.getCheckingLevel())) {
      return true;
    }
    if (myModelCheckerSettings.isCheckStubs() != myCheckStubsCheckBox.isSelected()) {
      return true;
    }
    if (myModelCheckerSettings.isIncludeAdditionalChecks() != myCheckSpecificCheckBox.isSelected()) {
      return true;
    }
    if (myModelCheckerSettings.isRunInParallel() != myRunInParallelCheckBox.isSelected()) {
      return true;
    }
    return false;
  }
  @NotNull
  @NonNls
  public String getId() {
    // have to match one in plugin.xml
    return "preferences.modelCheckerSettings";
  }
  @Nullable
  public Runnable enableSearch(String string) {
    return null;
  }
  @Nls
  public String getDisplayName() {
    // have to match one in plugin.xml
    return "Model Checker";
  }
  @Nullable
  @NonNls
  public String getHelpTopic() {
    return "Model_Checker";
  }
  @Nullable
  public JComponent createComponent() {
    return myPage;
  }
  public void disposeUIResources() {
  }
}
