@Test
  public void testEditComment() throws Exception {
    enableSilentOperation(VcsConfiguration.StandardConfirmation.ADD);
    final VirtualFile file = createFileInCommand("a.txt", "old content");
    final ChangeListManager changeListManager = ChangeListManager.getInstance(myProject);
    changeListManager.ensureUpToDate(false);

    final String listName = "test";
    final LocalChangeList list = changeListManager.addChangeList(listName, null);
    changeListManager.moveChangesTo(list, new Change[] {changeListManager.getChange(file)});

    final String finalText = "final text";

    myScheme.doTest(
        new Runnable() {
          public void run() {
            final String intermediate = "intermediate text";
            changeListManager.editComment(list.getName(), intermediate);
            assert changeListManager.findChangeList(listName) != null;
            LocalChangeList list = changeListManager.findChangeList(listName);
            assert intermediate.equals(list.getComment());

            changeListManager.editComment(list.getName(), finalText);
            list = changeListManager.findChangeList(listName);
            assert finalText.equals(list.getComment());
          }
        });

    LocalChangeList changedList = changeListManager.findChangeList(listName);
    assert finalText.equals(changedList.getComment());

    changeListManager.ensureUpToDate(false);
    changedList = changeListManager.findChangeList(listName);
    assert finalText.equals(changedList.getComment());
  }
  @Test
  public void testRemove() throws Exception {
    enableSilentOperation(VcsConfiguration.StandardConfirmation.ADD);
    final VirtualFile file = createFileInCommand("a.txt", "old content");
    final VirtualFile fileB = createFileInCommand("b.txt", "old content");
    final ChangeListManager changeListManager = ChangeListManager.getInstance(myProject);
    changeListManager.ensureUpToDate(false);

    final LocalChangeList list = changeListManager.addChangeList("test", null);
    changeListManager.moveChangesTo(list, new Change[] {changeListManager.getChange(file)});

    myScheme.doTest(
        new Runnable() {
          @Override
          public void run() {
            changeListManager.removeChangeList(list.getName());
            assert changeListManager.findChangeList(list.getName()) == null;
            checkFilesAreInList(
                new VirtualFile[] {file, fileB}, myDefaulListName, changeListManager);
          }
        });

    assert changeListManager.findChangeList(list.getName()) == null;
    checkFilesAreInList(new VirtualFile[] {file, fileB}, myDefaulListName, changeListManager);

    changeListManager.ensureUpToDate(false);
    assert changeListManager.findChangeList(list.getName()) == null;
    checkFilesAreInList(new VirtualFile[] {file, fileB}, myDefaulListName, changeListManager);
  }
  @Nullable
  public LocalChangeList getSelectedList(Project project) {
    ChangeListManager manager = ChangeListManager.getInstance(project);
    if (myRbNew.isSelected()) {
      String newText = myNewListPanel.getChangeListName();
      if (manager.findChangeList(newText) != null) {
        Messages.showErrorDialog(
            project,
            VcsBundle.message("changes.newchangelist.warning.already.exists.text", newText),
            VcsBundle.message("changes.newchangelist.warning.already.exists.title"));
        return null;
      }
    }
    final boolean existingSelected = myRbExisting.isSelected();
    VcsConfiguration.getInstance(myProject).PRESELECT_EXISTING_CHANGELIST = existingSelected;

    if (existingSelected) {
      return (LocalChangeList) myExistingListsCombo.getSelectedItem();
    } else {
      LocalChangeList changeList =
          manager.addChangeList(
              myNewListPanel.getChangeListName(), myNewListPanel.getDescription());
      myNewListPanel.changelistCreatedOrChanged(changeList);
      if (myNewListPanel.getMakeActiveCheckBox().isSelected()) {
        manager.setDefaultChangeList(changeList);
      }
      VcsConfiguration.getInstance(project).MAKE_NEW_CHANGELIST_ACTIVE =
          myNewListPanel.getMakeActiveCheckBox().isSelected();

      return changeList;
    }
  }
  public void actionPerformed(AnActionEvent e) {
    final Project project = e.getData(CommonDataKeys.PROJECT);
    if (project == null) return;
    final ShelvedChangeList[] changeLists =
        e.getData(ShelvedChangesViewManager.SHELVED_CHANGELIST_KEY);
    List<ShelvedChange> changes = e.getData(ShelvedChangesViewManager.SHELVED_CHANGE_KEY);
    List<ShelvedBinaryFile> binaryFiles =
        e.getData(ShelvedChangesViewManager.SHELVED_BINARY_FILE_KEY);
    if (changes != null && binaryFiles != null && changes.size() == 0 && binaryFiles.size() == 0) {
      changes = null;
      binaryFiles = null;
    }
    LOG.assertTrue(changeLists != null);

    final ChangeListManager changeListManager = ChangeListManager.getInstance(project);
    final List<LocalChangeList> allChangeLists = changeListManager.getChangeListsCopy();
    String defaultName = changeLists[0].DESCRIPTION;
    LocalChangeList list = null;
    if (changeLists.length == 1) {
      final LocalChangeList sameNamedList = changeListManager.findChangeList(defaultName);
      if (sameNamedList != null) {
        list = sameNamedList;
      }
    }
    if (list == null) {
      list = changeListManager.getDefaultChangeList();
    }
    final ChangeListChooser chooser =
        new ChangeListChooser(
            project,
            allChangeLists,
            list,
            VcsBundle.message("unshelve.changelist.chooser.title"),
            defaultName);
    chooser.show();
    if (!chooser.isOK()) {
      return;
    }

    FileDocumentManager.getInstance().saveAllDocuments();

    final List<ShelvedBinaryFile> finalBinaryFiles = binaryFiles;
    final List<ShelvedChange> finalChanges = changes;
    ProgressManager.getInstance()
        .run(
            new Task.Backgroundable(
                project, "Unshelve changes", true, BackgroundFromStartOption.getInstance()) {
              @Override
              public void run(@NotNull ProgressIndicator indicator) {
                for (ShelvedChangeList changeList : changeLists) {
                  ShelveChangesManager.getInstance(project)
                      .unshelveChangeList(
                          changeList, finalChanges, finalBinaryFiles, chooser.getSelectedList());
                }
              }
            });
  }
  private void checkFilesAreInList(
      final VirtualFile[] files, final String listName, final ChangeListManager manager) {
    System.out.println("Checking files for list: " + listName);
    assert manager.findChangeList(listName) != null;
    final Collection<Change> changes = manager.findChangeList(listName).getChanges();
    assert changes.size() == files.length;

    for (Change change : changes) {
      final VirtualFile vf = change.getAfterRevision().getFile().getVirtualFile();
      boolean found = false;
      for (VirtualFile file : files) {
        if (file.equals(vf)) {
          found = true;
          break;
        }
      }
      assert found == true;
    }
  }
  public static void checkDeletedFilesAreInList(
      final VirtualFile[] files, final String listName, final ChangeListManager manager) {
    assert manager.findChangeList(listName) != null;
    final LocalChangeList list = manager.findChangeList(listName);
    final Collection<Change> changes = list.getChanges();
    assert changes.size() == files.length : debugRealListContent(list);

    for (Change change : changes) {
      final File vf = change.getBeforeRevision().getFile().getIOFile();
      boolean found = false;
      for (VirtualFile vfile : files) {
        final File file = new File(vfile.getPath());
        if (file.equals(vf)) {
          found = true;
          break;
        }
      }
      assert found == true : debugRealListContent(list);
    }
  }
  public static void checkFilesAreInList(
      final String listName, final ChangeListManager manager, final VirtualFile... files) {
    assert manager.findChangeList(listName) != null;
    final LocalChangeList list = manager.findChangeList(listName);
    final Collection<Change> changes = list.getChanges();
    assert changes.size() == files.length
        : "size: " + changes.size() + " " + debugRealListContent(list);

    for (Change change : changes) {
      final VirtualFile vf = change.getAfterRevision().getFile().getVirtualFile();
      boolean found = false;
      for (VirtualFile file : files) {
        if (file.equals(vf)) {
          found = true;
          break;
        }
      }
      assert found == true : debugRealListContent(list);
    }
  }
  public void projectOpened() {

    TaskProjectConfiguration projectConfiguration = getProjectConfiguration();

    servers:
    for (TaskProjectConfiguration.SharedServer server : projectConfiguration.servers) {
      if (server.type == null || server.url == null) {
        continue;
      }
      for (TaskRepositoryType<?> repositoryType : TaskRepositoryType.getRepositoryTypes()) {
        if (repositoryType.getName().equals(server.type)) {
          for (TaskRepository repository : myRepositories) {
            if (!repositoryType.equals(repository.getRepositoryType())) {
              continue;
            }
            if (server.url.equals(repository.getUrl())) {
              continue servers;
            }
          }
          TaskRepository repository = repositoryType.createRepository();
          repository.setUrl(server.url);
          myRepositories.add(repository);
        }
      }
    }

    myContextManager.pack(200, 50);

    // make sure the task is associated with default changelist
    LocalTask defaultTask = findTask(LocalTaskImpl.DEFAULT_TASK_ID);
    LocalChangeList defaultList = myChangeListManager.findChangeList(LocalChangeList.DEFAULT_NAME);
    if (defaultList != null && defaultTask != null) {
      ChangeListInfo listInfo = new ChangeListInfo(defaultList);
      if (!defaultTask.getChangeLists().contains(listInfo)) {
        defaultTask.addChangelist(listInfo);
      }
    }

    // remove already not existing changelists from tasks changelists
    for (LocalTask localTask : getLocalTasks()) {
      for (Iterator<ChangeListInfo> iterator = localTask.getChangeLists().iterator();
          iterator.hasNext(); ) {
        final ChangeListInfo changeListInfo = iterator.next();
        if (myChangeListManager.getChangeList(changeListInfo.id) == null) {
          iterator.remove();
        }
      }
    }

    myChangeListManager.addChangeListListener(myChangeListListener);
  }
 private void createChangeList(LocalTask task, String name, @Nullable String comment) {
   LocalChangeList changeList = myChangeListManager.findChangeList(name);
   if (changeList == null) {
     changeList = myChangeListManager.addChangeList(name, comment);
   } else {
     final LocalTask associatedTask = getAssociatedTask(changeList);
     if (associatedTask != null) {
       associatedTask.removeChangelist(new ChangeListInfo(changeList));
     }
     changeList.setComment(comment);
   }
   task.addChangelist(new ChangeListInfo(changeList));
   myChangeListManager.setDefaultChangeList(changeList);
 }
 private void createChangelist(final ContinuationPause context) {
   final ChangeListManager listManager = ChangeListManager.getInstance(myProject);
   String name = myTitle;
   int i = 1;
   boolean updateDefaultList = false;
   while (true) {
     final LocalChangeList changeList = listManager.findChangeList(name);
     if (changeList == null) {
       final LocalChangeList newList = listManager.addChangeList(name, null);
       listManager.setDefaultChangeList(newList);
       updateDefaultList = true;
       break;
     }
     if (changeList.getChanges().isEmpty()) {
       if (!changeList.isDefault()) {
         listManager.setDefaultChangeList(changeList);
         updateDefaultList = true;
       }
       break;
     }
     name = myTitle + " (" + i + ")";
     ++i;
   }
   if (updateDefaultList) {
     context.suspend();
     listManager.invokeAfterUpdate(
         new Runnable() {
           public void run() {
             context.ping();
           }
         },
         InvokeAfterUpdateMode.BACKGROUND_NOT_CANCELLABLE_NOT_AWT,
         "",
         ModalityState.NON_MODAL);
   }
 }