/*
 * Copyright (c) 2000-2006 JetBrains s.r.o. All Rights Reserved.
 */

/*
 * Created by IntelliJ IDEA.
 * User: yole
 * Date: 23.11.2006
 * Time: 13:40:27
 */
package com.intellij.openapi.vcs.changes.shelf;

import com.intellij.CommonBundle;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vcs.VcsBundle;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.changes.ChangesUtil;
import com.intellij.openapi.vcs.changes.CommitExecutor;
import com.intellij.openapi.vcs.changes.CommitSession;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.Icons;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Collection;

public class ShelveChangesCommitExecutor implements CommitExecutor {
  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.vcs.changes.shelf.ShelveChangesCommitExecutor");

  private final Project myProject;

  public ShelveChangesCommitExecutor(final Project project) {
    myProject = project;
  }

  @NotNull
  public Icon getActionIcon() {
    return Icons.TASK_ICON;
  }

  @Nls
  public String getActionText() {
    return VcsBundle.message("shelve.changes.action");
  }

  @Nls
  public String getActionDescription() {
    return VcsBundle.message("shelve.changes.action");
  }

  @NotNull
  public CommitSession createCommitSession() {
    return new ShelveChangesCommitSession();
  }

  private class ShelveChangesCommitSession implements CommitSession {

    @Nullable
    public JComponent getAdditionalConfigurationUI() {
      return null;
    }

    @Nullable
    public JComponent getAdditionalConfigurationUI(final Collection<Change> changes, final String commitMessage) {
      return null;
    }

    public boolean canExecute(Collection<Change> changes, String commitMessage) {
      return changes.size() > 0;
    }

    public void execute(Collection<Change> changes, String commitMessage) {
      if (changes.size() > 0 && !ChangesUtil.hasFileChanges(changes)) {
        ApplicationManager.getApplication().invokeLater(new Runnable() {
          public void run() {
            Messages.showErrorDialog(myProject, VcsBundle.message("shelve.changes.only.directories"), VcsBundle.message("shelve.changes.action"));
          }
        });
        return;
      }
      try {
        final ShelvedChangeList list = ShelveChangesManager.getInstance(myProject).shelveChanges(changes, commitMessage);
        ShelvedChangesViewManager.getInstance(myProject).activateView(list);
      }
      catch (final Exception ex) {
        LOG.info(ex);
        ApplicationManager.getApplication().invokeLater(new Runnable() {
          public void run() {
            Messages.showErrorDialog(myProject, VcsBundle.message("create.patch.error.title", ex.getMessage()), CommonBundle.getErrorTitle());
          }
        }, ModalityState.NON_MODAL);
      }
    }

    public void executionCanceled() {
    }
  }
}
@@ -352,16 +352,63 @@
 yState;%0A
+import com.intellij.openapi.diagnostic.Logger;%0A
 import c
@@ -444,16 +444,16 @@
 roject;%0A
-
 import c
@@ -572,225 +572,9 @@
 ges.
-Change;%0Aimport com.intellij.openapi.vcs.changes.ChangesUtil;%0Aimport com.intellij.openapi.vcs.changes.CommitExecutor;%0Aimport com.intellij.openapi.vcs.changes.CommitSession;%0Aimport com.intellij.openapi.diagnostic.Logger
+*
 ;%0Aim
@@ -2596,16 +2596,527 @@
 w(list);
+%0A%0A        Change%5B%5D changesArray = changes.toArray(new Change%5Bchanges.size()%5D);%0A        ChangeList changeList = ChangesUtil.getChangeListIfOnlyOne(myProject, changesArray);%0A        if (changeList instanceof LocalChangeList) %7B%0A          LocalChangeList localChangeList = (LocalChangeList) changeList;%0A          if (localChangeList.getChanges().size() == changes.size() && !localChangeList.isDefault()) %7B%0A            ChangeListManager.getInstance(myProject).removeChangeList(localChangeList);%0A          %7D%0A        %7D
 %0A      %7D

delete non-default changelist on shelving all changes (IDEADEV-21449)