/**
  * Check destination directory and set appropriate error text if there are problems
  *
  * @return true if destination components are OK.
  */
 private boolean checkDestination() {
   if (myParentDirectory.getText().length() == 0 || myDirectoryName.getText().length() == 0) {
     setErrorText(null);
     setOKActionEnabled(false);
     return false;
   }
   File file = new File(myParentDirectory.getText(), myDirectoryName.getText());
   if (file.exists()) {
     setErrorText(GitBundle.message("clone.destination.exists.error", file));
     setOKActionEnabled(false);
     return false;
   } else if (!file.getParentFile().exists()) {
     setErrorText(GitBundle.message("clone.parent.missing.error", file.getParent()));
     setOKActionEnabled(false);
     return false;
   }
   return true;
 }
 /** Update state dialog depending on the current state of the fields */
 private void updateDialogState() {
   String branch = myBranchTextField.getText();
   if (branch.length() != 0) {
     setOKButtonText(GitBundle.getString("unstash.button.branch"));
     myPopStashCheckBox.setEnabled(false);
     myPopStashCheckBox.setSelected(true);
     myReinstateIndexCheckBox.setEnabled(false);
     myReinstateIndexCheckBox.setSelected(true);
     if (!GitBranchNameValidator.INSTANCE.checkInput(branch)) {
       setErrorText(GitBundle.getString("unstash.error.invalid.branch.name"));
       setOKActionEnabled(false);
       return;
     }
     if (myBranches.contains(branch)) {
       setErrorText(GitBundle.getString("unstash.error.branch.exists"));
       setOKActionEnabled(false);
       return;
     }
   } else {
     if (!myPopStashCheckBox.isEnabled()) {
       myPopStashCheckBox.setSelected(false);
     }
     myPopStashCheckBox.setEnabled(true);
     setOKButtonText(
         myPopStashCheckBox.isSelected()
             ? GitBundle.getString("unstash.button.pop")
             : GitBundle.getString("unstash.button.apply"));
     if (!myReinstateIndexCheckBox.isEnabled()) {
       myReinstateIndexCheckBox.setSelected(false);
     }
     myReinstateIndexCheckBox.setEnabled(true);
   }
   if (myStashList.getModel().getSize() == 0) {
     myViewButton.setEnabled(false);
     myDropButton.setEnabled(false);
     myClearButton.setEnabled(false);
     setErrorText(null);
     setOKActionEnabled(false);
     return;
   } else {
     myClearButton.setEnabled(true);
   }
   if (myStashList.getSelectedIndex() == -1) {
     myViewButton.setEnabled(false);
     myDropButton.setEnabled(false);
     setErrorText(null);
     setOKActionEnabled(false);
     return;
   } else {
     myViewButton.setEnabled(true);
     myDropButton.setEnabled(true);
   }
   setErrorText(null);
   setOKActionEnabled(true);
 }
  /** Init components */
  private void initListeners() {
    FileChooserDescriptor fcd = FileChooserDescriptorFactory.createSingleFolderDescriptor();
    fcd.setShowFileSystemRoots(true);
    fcd.setTitle(GitBundle.getString("clone.destination.directory.title"));
    fcd.setDescription(GitBundle.getString("clone.destination.directory.description"));
    fcd.setHideIgnored(false);
    myParentDirectory.addActionListener(
        new ComponentWithBrowseButton.BrowseFolderActionListener<JTextField>(
            fcd.getTitle(),
            fcd.getDescription(),
            myParentDirectory,
            myProject,
            fcd,
            TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT) {
          @Override
          protected VirtualFile getInitialFile() {
            // suggest project base directory only if nothing is typed in the component.
            String text = getComponentText();
            if (text.length() == 0) {
              VirtualFile file = myProject.getBaseDir();
              if (file != null) {
                return file;
              }
            }
            return super.getInitialFile();
          }
        });

    final DocumentListener updateOkButtonListener =
        new DocumentAdapter() {
          @Override
          protected void textChanged(DocumentEvent e) {
            updateButtons();
          }
        };
    myParentDirectory.getChildComponent().getDocument().addDocumentListener(updateOkButtonListener);
    String parentDir = GitRememberedInputs.getInstance().getCloneParentDir();
    if (StringUtil.isEmptyOrSpaces(parentDir)) {
      parentDir = ProjectUtil.getBaseDir();
    }
    myParentDirectory.setText(parentDir);

    myDirectoryName.getDocument().addDocumentListener(updateOkButtonListener);

    myTestButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(final ActionEvent e) {
            test();
          }
        });

    setOKActionEnabled(false);
    myTestButton.setEnabled(false);
  }
 /** @return unstash handler */
 private GitLineHandler handler() {
   GitLineHandler h = new GitLineHandler(myProject, getGitRoot(), GitCommand.STASH);
   h.setNoSSH(true);
   String branch = myBranchTextField.getText();
   if (branch.length() == 0) {
     h.addParameters(myPopStashCheckBox.isSelected() ? "pop" : "apply");
     if (myReinstateIndexCheckBox.isSelected()) {
       h.addParameters("--index");
     }
   } else {
     h.addParameters("branch", branch);
   }
   String selectedStash = getSelectedStash().getStash();
   addStashParameter(h, selectedStash);
   return h;
 }
  /**
   * A constructor
   *
   * @param project the project
   * @param roots the list of the roots
   * @param defaultRoot the default root to select
   */
  public GitUnstashDialog(
      final Project project, final List<VirtualFile> roots, final VirtualFile defaultRoot) {
    super(project, true);
    setModal(false);
    myProject = project;
    myVcs = GitVcs.getInstance(project);
    setTitle(GitBundle.getString("unstash.title"));
    setOKButtonText(GitBundle.getString("unstash.button.apply"));
    GitUIUtil.setupRootChooser(project, roots, defaultRoot, myGitRootComboBox, myCurrentBranch);
    myStashList.setModel(new DefaultListModel());
    refreshStashList();
    myGitRootComboBox.addActionListener(
        new ActionListener() {
          public void actionPerformed(final ActionEvent e) {
            refreshStashList();
            updateDialogState();
          }
        });
    myStashList.addListSelectionListener(
        new ListSelectionListener() {
          public void valueChanged(final ListSelectionEvent e) {
            updateDialogState();
          }
        });
    myBranchTextField
        .getDocument()
        .addDocumentListener(
            new DocumentAdapter() {
              protected void textChanged(final DocumentEvent e) {
                updateDialogState();
              }
            });
    myPopStashCheckBox.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            updateDialogState();
          }
        });
    myClearButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(final ActionEvent e) {
            if (Messages.YES
                == Messages.showYesNoDialog(
                    GitUnstashDialog.this.getContentPane(),
                    GitBundle.message("git.unstash.clear.confirmation.message"),
                    GitBundle.message("git.unstash.clear.confirmation.title"),
                    Messages.getWarningIcon())) {
              GitLineHandler h = new GitLineHandler(myProject, getGitRoot(), GitCommand.STASH);
              h.setNoSSH(true);
              h.addParameters("clear");
              GitHandlerUtil.doSynchronously(
                  h, GitBundle.getString("unstash.clearing.stashes"), h.printableCommandLine());
              refreshStashList();
              updateDialogState();
            }
          }
        });
    myDropButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(final ActionEvent e) {
            final StashInfo stash = getSelectedStash();
            if (Messages.YES
                == Messages.showYesNoDialog(
                    GitUnstashDialog.this.getContentPane(),
                    GitBundle.message(
                        "git.unstash.drop.confirmation.message",
                        stash.getStash(),
                        stash.getMessage()),
                    GitBundle.message("git.unstash.drop.confirmation.title", stash.getStash()),
                    Messages.getQuestionIcon())) {
              final ModalityState current = ModalityState.current();
              ProgressManager.getInstance()
                  .run(
                      new Task.Modal(myProject, "Removing stash " + stash.getStash(), false) {
                        @Override
                        public void run(@NotNull ProgressIndicator indicator) {
                          final GitSimpleHandler h = dropHandler(stash.getStash());
                          try {
                            h.run();
                            h.unsilence();
                          } catch (final VcsException ex) {
                            ApplicationManager.getApplication()
                                .invokeLater(
                                    new Runnable() {
                                      @Override
                                      public void run() {
                                        GitUIUtil.showOperationError(
                                            myProject, ex, h.printableCommandLine());
                                      }
                                    },
                                    current);
                          }
                        }
                      });
              refreshStashList();
              updateDialogState();
            }
          }

          private GitSimpleHandler dropHandler(String stash) {
            GitSimpleHandler h = new GitSimpleHandler(myProject, getGitRoot(), GitCommand.STASH);
            h.setNoSSH(true);
            h.addParameters("drop");
            addStashParameter(h, stash);
            return h;
          }
        });
    myViewButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(final ActionEvent e) {
            final VirtualFile root = getGitRoot();
            String resolvedStash;
            String selectedStash = getSelectedStash().getStash();
            try {
              GitSimpleHandler h = new GitSimpleHandler(project, root, GitCommand.REV_LIST);
              h.setNoSSH(true);
              h.setSilent(true);
              h.addParameters("--timestamp", "--max-count=1");
              addStashParameter(h, selectedStash);
              h.endOptions();
              final String output = h.run();
              resolvedStash =
                  GitRevisionNumber.parseRevlistOutputAsRevisionNumber(h, output).asString();
            } catch (VcsException ex) {
              GitUIUtil.showOperationError(myProject, ex, "resolving revision");
              return;
            }
            GitShowAllSubmittedFilesAction.showSubmittedFiles(
                myProject, resolvedStash, root, true, false);
          }
        });
    init();
    updateDialogState();
  }
 public String getDirectoryName() {
   return myDirectoryName.getText();
 }