public boolean editName(@NotNull final String fromName, @NotNull final String toName) {
   if (fromName.equals(toName)) return false;
   final LocalChangeList list = myMap.get(fromName);
   final boolean canEdit = list != null && (!list.isReadOnly());
   if (canEdit) {
     final LocalChangeListImpl listImpl = (LocalChangeListImpl) list;
     listImpl.setNameImpl(toName);
     myMap.remove(fromName);
     myMap.put(toName, list);
     final ChangeListEditHandler editHandler = listImpl.getEditHandler();
     if (editHandler != null) {
       listImpl.setCommentImpl(
           editHandler.changeCommentOnChangeName(toName, listImpl.getComment()));
     }
   }
   return canEdit;
 }
  /**
   * @return if list with name exists, return previous default list name or null of there wasn't
   *     previous
   */
  @Nullable
  public String setDefault(final String name) {
    final LocalChangeList newDefault = myMap.get(name);
    if (newDefault == null) {
      return null;
    }
    String previousName = null;
    if (myDefault != null) {
      ((LocalChangeListImpl) myDefault).setDefault(false);
      previousName = myDefault.getName();
    }

    ((LocalChangeListImpl) newDefault).setDefault(true);
    myDefault = newDefault;

    return previousName;
  }
  public void removeRegisteredChangeFor(FilePath path) {
    myIdx.remove(path);

    for (LocalChangeList list : myMap.values()) {
      for (Change change : list.getChanges()) {
        final ContentRevision afterRevision = change.getAfterRevision();
        if (afterRevision != null && afterRevision.getFile().equals(path)) {
          ((LocalChangeListImpl) list).removeChange(change);
          return;
        }
        final ContentRevision beforeRevision = change.getBeforeRevision();
        if (beforeRevision != null && beforeRevision.getFile().equals(path)) {
          ((LocalChangeListImpl) list).removeChange(change);
          return;
        }
      }
    }
  }
  public void notifyDoneProcessingChanges(final ChangeListListener dispatcher) {
    List<ChangeList> changedLists = new ArrayList<ChangeList>();
    final Map<LocalChangeListImpl, List<Change>> removedChanges =
        new HashMap<LocalChangeListImpl, List<Change>>();
    final Map<LocalChangeListImpl, List<Change>> addedChanges =
        new HashMap<LocalChangeListImpl, List<Change>>();
    for (LocalChangeList list : myMap.values()) {
      final List<Change> removed = new ArrayList<Change>();
      final List<Change> added = new ArrayList<Change>();
      final LocalChangeListImpl listImpl = (LocalChangeListImpl) list;
      if (listImpl.doneProcessingChanges(removed, added)) {
        changedLists.add(list);
      }
      if (!removed.isEmpty()) {
        removedChanges.put(listImpl, removed);
      }
      if (!added.isEmpty()) {
        addedChanges.put(listImpl, added);
      }
    }
    for (Map.Entry<LocalChangeListImpl, List<Change>> entry : removedChanges.entrySet()) {
      dispatcher.changesRemoved(entry.getValue(), entry.getKey());
    }
    for (Map.Entry<LocalChangeListImpl, List<Change>> entry : addedChanges.entrySet()) {
      dispatcher.changesAdded(entry.getValue(), entry.getKey());
    }
    for (ChangeList changeList : changedLists) {
      dispatcher.changeListChanged(changeList);
    }
    mySwitchedHolder.calculateChildren();

    for (String name : myListsToDisappear) {
      final LocalChangeList changeList = myMap.get(name);
      if ((changeList != null)
          && changeList.getChanges().isEmpty()
          && (!changeList.isReadOnly())
          && (!changeList.isDefault())) {
        removeChangeList(name);
      }
    }
    myListsToDisappear.clear();
  }
 @Nullable
 public MultiMap<LocalChangeList, Change> moveChangesTo(
     final String name, final Change[] changes) {
   final LocalChangeListImpl changeList = (LocalChangeListImpl) myMap.get(name);
   if (changeList != null) {
     final MultiMap<LocalChangeList, Change> result = new MultiMap<LocalChangeList, Change>();
     for (LocalChangeList list : myMap.values()) {
       if (list.equals(changeList)) continue;
       for (Change change : changes) {
         final Change removedChange = ((LocalChangeListImpl) list).removeChange(change);
         if (removedChange != null) {
           changeList.addChange(removedChange);
           result.putValue(list, removedChange);
         }
       }
     }
     return result;
   }
   return null;
 }
  private void checkForMultipleCopiesNotMove(boolean somethingChanged) {
    final MultiMap<FilePath, Pair<Change, String>> moves =
        new MultiMap<FilePath, Pair<Change, String>>() {
          @NotNull
          protected Collection<Pair<Change, String>> createCollection() {
            return new LinkedList<Pair<Change, String>>();
          }
        };

    for (LocalChangeList changeList : myMap.values()) {
      final Collection<Change> changes = changeList.getChanges();
      for (Change change : changes) {
        if (change.isMoved() || change.isRenamed()) {
          moves.putValue(
              change.getBeforeRevision().getFile(), Pair.create(change, changeList.getName()));
        }
      }
    }
    for (FilePath filePath : moves.keySet()) {
      final List<Pair<Change, String>> copies = (List<Pair<Change, String>>) moves.get(filePath);
      if (copies.size() == 1) continue;
      Collections.sort(copies, MyChangesAfterRevisionComparator.getInstance());
      for (int i = 0; i < (copies.size() - 1); i++) {
        somethingChanged = true;
        final Pair<Change, String> item = copies.get(i);
        final Change oldChange = item.getFirst();
        final Change newChange = new Change(null, oldChange.getAfterRevision());

        final LocalChangeListImpl list = (LocalChangeListImpl) myMap.get(item.getSecond());
        list.removeChange(oldChange);
        list.addChange(newChange);

        final VcsKey key = myIdx.getVcsFor(oldChange);
        myIdx.changeRemoved(oldChange);
        myIdx.changeAdded(newChange, key);
      }
    }
    if (somethingChanged) {
      FileStatusManager.getInstance(myProject).fileStatusesChanged();
    }
  }
  LocalChangeList addChangeList(
      String id,
      @NotNull final String name,
      @Nullable final String description,
      final boolean inUpdate,
      @Nullable Object data) {
    final boolean contains = myMap.containsKey(name);
    LOG.assertTrue(!contains, "Attempt to create duplicate changelist " + name);
    final LocalChangeListImpl newList =
        (LocalChangeListImpl) LocalChangeList.createEmptyChangeList(myProject, name);
    newList.setData(data);

    if (description != null) {
      newList.setCommentImpl(description);
    }
    if (id != null) {
      newList.setId(id);
    }
    myMap.put(name, newList);
    if (inUpdate) {
      // scope is not important: nothing had been added jet, nothing to move to "old state" members
      newList.startProcessingChanges(
          myProject, null); // this is executed only when use through GATE
    }
    return newList.copy();
  }
 public boolean addChangeToList(
     @NotNull final String name, final Change change, final VcsKey vcsKey) {
   LOG.debug(
       "[addChangeToList] name: "
           + name
           + " change: "
           + ChangesUtil.getFilePath(change).getPath()
           + " vcs: "
           + (vcsKey == null ? null : vcsKey.getName()));
   final LocalChangeList changeList = myMap.get(name);
   if (changeList != null) {
     ((LocalChangeListImpl) changeList).addChange(change);
     myIdx.changeAdded(change, vcsKey);
   }
   return changeList != null;
 }
  public boolean removeChangeList(@NotNull String name) {
    final LocalChangeList list = myMap.get(name);
    if (list == null) {
      return false;
    }
    if (list.isDefault()) {
      throw new RuntimeException(
          new IncorrectOperationException("Cannot remove default changelist"));
    }
    final String listName = list.getName();

    for (Change change : list.getChanges()) {
      ((LocalChangeListImpl) myDefault).addChange(change);
    }

    final LocalChangeList removed = myMap.remove(listName);
    return true;
  }
 @Nullable
 public String editComment(@NotNull final String fromName, final String newComment) {
   final LocalChangeList list = myMap.get(fromName);
   if (list != null) {
     final String oldComment = list.getComment();
     if (!Comparing.equal(oldComment, newComment)) {
       final LocalChangeListImpl listImpl = (LocalChangeListImpl) list;
       listImpl.setCommentImpl(newComment);
       final ChangeListEditHandler editHandler = listImpl.getEditHandler();
       if (editHandler != null) {
         listImpl.setNameImpl(
             editHandler.changeNameOnChangeComment(listImpl.getName(), listImpl.getComment()));
         if (!fromName.equals(listImpl.getName())) {
           myMap.remove(fromName);
           myMap.put(listImpl.getName(), list);
         }
       }
     }
     return oldComment;
   }
   return null;
 }
 public void addChangeToCorrespondingList(final Change change, final VcsKey vcsKey) {
   final String path = ChangesUtil.getFilePath(change).getPath();
   LOG.debug(
       "[addChangeToCorrespondingList] for change "
           + path
           + " type: "
           + change.getType()
           + " have before revision: "
           + (change.getBeforeRevision() != null));
   assert myDefault != null;
   for (LocalChangeList list : myMap.values()) {
     if (list.isDefault()) {
       LOG.debug(
           "[addChangeToCorrespondingList] skip default list: "
               + list.getName()
               + " type: "
               + change.getType()
               + " have before revision: "
               + (change.getBeforeRevision() != null));
       continue;
     }
     if (((LocalChangeListImpl) list).processChange(change)) {
       LOG.debug(
           "[addChangeToCorrespondingList] matched: "
               + list.getName()
               + " type: "
               + change.getType()
               + " have before revision: "
               + (change.getBeforeRevision() != null));
       myIdx.changeAdded(change, vcsKey);
       return;
     }
   }
   ((LocalChangeListImpl) myDefault).processChange(change);
   myIdx.changeAdded(change, vcsKey);
 }