void saveRemainingPatches(
      final ShelvedChangeList changeList,
      final List<FilePatch> remainingPatches,
      final List<ShelvedBinaryFile> remainingBinaries,
      CommitContext commitContext) {
    final File newPath = getPatchPath(changeList.DESCRIPTION);
    try {
      FileUtil.copy(new File(changeList.PATH), newPath);
    } catch (IOException e) {
      // do not delete if cannot recycle
      return;
    }
    final ShelvedChangeList listCopy =
        new ShelvedChangeList(
            newPath.getAbsolutePath(),
            changeList.DESCRIPTION,
            new ArrayList<ShelvedBinaryFile>(changeList.getBinaryFiles()));
    listCopy.DATE = (changeList.DATE == null) ? null : new Date(changeList.DATE.getTime());

    writePatchesToFile(myProject, changeList.PATH, remainingPatches, commitContext);

    changeList.getBinaryFiles().retainAll(remainingBinaries);
    changeList.clearLoadedChanges();
    recycleChangeList(listCopy, changeList);
    notifyStateChanged();
  }
  private void recycleChangeList(
      final ShelvedChangeList listCopy, final ShelvedChangeList newList) {
    if (newList != null) {
      for (Iterator<ShelvedBinaryFile> shelvedChangeListIterator =
              listCopy.getBinaryFiles().iterator();
          shelvedChangeListIterator.hasNext(); ) {
        final ShelvedBinaryFile binaryFile = shelvedChangeListIterator.next();
        for (ShelvedBinaryFile newBinary : newList.getBinaryFiles()) {
          if (Comparing.equal(newBinary.BEFORE_PATH, binaryFile.BEFORE_PATH)
              && Comparing.equal(newBinary.AFTER_PATH, binaryFile.AFTER_PATH)) {
            shelvedChangeListIterator.remove();
          }
        }
      }
      for (Iterator<ShelvedChange> iterator = listCopy.getChanges(myProject).iterator();
          iterator.hasNext(); ) {
        final ShelvedChange change = iterator.next();
        for (ShelvedChange newChange : newList.getChanges(myProject)) {
          if (Comparing.equal(change.getBeforePath(), newChange.getBeforePath())
              && Comparing.equal(change.getAfterPath(), newChange.getAfterPath())) {
            iterator.remove();
          }
        }
      }

      // needed only if partial unshelve
      try {
        final CommitContext commitContext = new CommitContext();
        final List<FilePatch> patches = new ArrayList<FilePatch>();
        for (ShelvedChange change : listCopy.getChanges(myProject)) {
          patches.add(change.loadFilePatch(myProject, commitContext));
        }
        writePatchesToFile(myProject, listCopy.PATH, patches, commitContext);
      } catch (IOException e) {
        LOG.info(e);
        // left file as is
      } catch (PatchSyntaxException e) {
        LOG.info(e);
        // left file as is
      }
    }

    if ((!listCopy.getBinaryFiles().isEmpty()) || (!listCopy.getChanges(myProject).isEmpty())) {
      listCopy.setRecycled(true);
      myRecycledShelvedChangeLists.add(listCopy);
      notifyStateChanged();
    }
  }
 private static List<ShelvedBinaryFile> getBinaryFilesToUnshelve(
     final ShelvedChangeList changeList,
     final List<ShelvedBinaryFile> binaryFiles,
     final List<ShelvedBinaryFile> remainingBinaries) {
   if (binaryFiles == null) {
     return new ArrayList<ShelvedBinaryFile>(changeList.getBinaryFiles());
   }
   ArrayList<ShelvedBinaryFile> result = new ArrayList<ShelvedBinaryFile>();
   for (ShelvedBinaryFile file : changeList.getBinaryFiles()) {
     if (binaryFiles.contains(file)) {
       result.add(file);
     } else {
       remainingBinaries.add(file);
     }
   }
   return result;
 }
  private void deleteListImpl(final ShelvedChangeList changeList) {
    File file = new File(changeList.PATH);
    myFileProcessor.delete(file.getName());

    for (ShelvedBinaryFile binaryFile : changeList.getBinaryFiles()) {
      final String path = binaryFile.SHELVED_PATH;
      if (path != null) {
        File binFile = new File(path);
        myFileProcessor.delete(binFile.getName());
      }
    }
  }