package jetbrains.mps.ide.platform.refactoring;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import com.intellij.ui.components.JBTextField;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.Nullable;
import javax.swing.JComponent;
import com.intellij.ui.components.JBPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import com.intellij.util.ui.JBUI;
import com.intellij.ui.DocumentAdapter;
import javax.swing.event.DocumentEvent;

@GeneratedClass(node = "r:986938bb-bdb1-4307-b062-e4647a4db0f9(jetbrains.mps.ide.platform.refactoring)/1685972956014329975", model = "r:986938bb-bdb1-4307-b062-e4647a4db0f9(jetbrains.mps.ide.platform.refactoring)")
public class StringChooserDialog extends RefactoringDialog {
  protected JPanel myPanel;
  protected JLabel myLabel = new JLabel();
  protected JTextField myTextField = new JBTextField();
  private String myResultString;
  private String myInitValue;

  public StringChooserDialog(@NotNull Project project, String title, String labelText, @Nullable String initialValue) {
    super(project, true);
    setTitle(title);
    myInitValue = initialValue;
    init();
    if (myInitValue != null) {
      myTextField.setText(myInitValue);
      myTextField.setSelectionStart(0);
      myTextField.setSelectionEnd(myInitValue.length());
    }
    myLabel.setText(labelText);
  }

  @Nullable
  @Override
  protected JComponent createCenterPanel() {
    return null;
  }

  @Override
  public JComponent getPreferredFocusedComponent() {
    return myTextField;
  }

  @Override
  protected JComponent createNorthPanel() {
    this.myPanel = new JBPanel(new GridBagLayout());
    GridBagConstraints gbConstraints = new GridBagConstraints();

    gbConstraints.insets = JBUI.insetsBottom(4);
    gbConstraints.weighty = 0;
    gbConstraints.weightx = 1;
    gbConstraints.gridwidth = GridBagConstraints.REMAINDER;
    gbConstraints.fill = GridBagConstraints.BOTH;
    myPanel.add(myLabel, gbConstraints);

    gbConstraints = new GridBagConstraints();

    gbConstraints.insets = JBUI.insetsBottom(8);
    gbConstraints.gridwidth = 2;
    gbConstraints.fill = GridBagConstraints.BOTH;
    gbConstraints.weightx = 1;
    gbConstraints.gridx = 0;
    gbConstraints.weighty = 1;
    myPanel.add(myTextField, gbConstraints);

    myTextField.getDocument().addDocumentListener(new DocumentAdapter() {
      protected void textChanged(DocumentEvent e) {
        validateValue();
      }
    });

    return myPanel;
  }

  private void validateValue() {
    if (!(allowUnchangedValue()) && myInitValue != null && myTextField.getText().equals(myInitValue)) {
      getRefactorAction().setEnabled(false);
      // This is not error, we just disable refactoring action in that case
      setErrorText(null);
      return;
    }

    if (!(allowEmptyValue()) && isEmptyString(trim_bn0r89_a0a0c0o(myTextField.getText()))) {
      setErrorText("Empty input is not allowed");
      getRefactorAction().setEnabled(false);
      return;
    }

    String error = checkValue();
    if (error != null) {
      setErrorText(error);
      getRefactorAction().setEnabled(false);
      return;
    }

    getRefactorAction().setEnabled(true);
    setErrorText(null);
  }

  /**
   * Switch On/Off check for unchanged value
   * By default unchanged value is forbidden.
   */
  protected boolean allowUnchangedValue() {
    return false;
  }

  /**
   * Switch On/Off check for empty value
   * By default empty value is forbidden.
   */
  protected boolean allowEmptyValue() {
    return false;
  }

  /**
   * <p>
   * Allows to check value before refactoring.<br />
   * To do this, add validation code to this method and return text with error.<br />
   * If no errors are found return {@code null} 
   * </p>
   * <p>
   * This method will be called on every text change event.
   * </p>
   * 
   * @return Error text to display in refactoring dialog or {@code null} if new value is valid.
   */
  @Nullable
  protected String checkValue() {
    return null;
  }

  public String getResultValue() {
    return myResultString;
  }

  @NotNull
  protected String getCurrentValue() {
    return (this.myTextField.getText() != null ? this.myTextField.getText() : "");
  }

  @Override
  protected void doRefactoringAction() {
    myResultString = this.myTextField.getText();
    super.doRefactoringAction();
  }

  public static String getString(Project project, String title, String labelText, String initialValue) {
    return getString(project, title, labelText, initialValue, false);
  }
  public static String getString(Project project, String title, String labelText, String initialValue, final boolean allowUnchanged) {
    StringChooserDialog dialog = new StringChooserDialog(project, title, labelText, initialValue) {
      @Override
      protected boolean allowUnchangedValue() {
        return allowUnchanged;
      }
    };
    dialog.show();
    return dialog.getResultValue();
  }
  private static boolean isEmptyString(String str) {
    return str == null || str.isEmpty();
  }
  public static String trim_bn0r89_a0a0c0o(String str) {
    return (str == null ? null : str.trim());
  }
}
