package com.intellij.ide.favoritesTreeView;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataConstants;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.InputValidator;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.wm.ToolWindowId;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.util.ArrayUtil;

/**
 * User: anna
 * Date: Feb 24, 2005
 */
public class AddNewFavoritesListAction extends AnAction {
  public void actionPerformed(AnActionEvent e) {
    final Project project = (Project)e.getDataContext().getData(DataConstants.PROJECT);
    if (project != null) {
      final FavoritesTreeViewPanel favoritesTreeViewPanel = doAddNewFavoritesList(project);
      if (favoritesTreeViewPanel != null) {
        final ToolWindowManager windowManager = ToolWindowManager.getInstance(project);
        windowManager.getToolWindow(ToolWindowId.FAVORITES_VIEW).activate(null);
        final FavoritesViewImpl favoritesView = FavoritesViewImpl.getInstance(project);
        favoritesView.setSelectedContent(favoritesView.getContent(favoritesTreeViewPanel));
      }
    }
  }

  public static FavoritesTreeViewPanel doAddNewFavoritesList(final Project project) {
    final String s =
      Messages.showInputDialog(project, "Input new favorites list name", "Add New Favorites List", Messages.getInformationIcon(), "new",
                               new InputValidator() {
                                 public boolean checkInput(String inputString) {
                                   return inputString != null && inputString.trim().length() > 0;
                                 }

                                 public boolean canClose(String inputString) {
                                   final boolean isNew = ArrayUtil.find(FavoritesViewImpl.getInstance(project).getAvailableFavoritesLists(), inputString.trim()) == -1;
                                   if (!isNew) {
                                     Messages.showErrorDialog(project, "Favorites list with name \'" + inputString.trim() + "\' already exists", "Unable To Add Favorites List");
                                     return false;
                                   }
                                   return inputString.trim().length() > 0;
                                 }
                               });
    if (s == null || s.length() == 0) return null;
    final FavoritesViewImpl favoritesView = FavoritesViewImpl.getInstance(project);
    return favoritesView.addNewFavoritesList(s);
  }


}

@@ -466,16 +466,64 @@
 yUtil;%0A%0A
+import java.util.Set;%0Aimport java.util.Arrays;%0A%0A
 /**%0A * U
@@ -1525,13 +1525,30 @@
 (), 
-%22new%22
+getUniqueName(project)
 ,%0A  
@@ -2710,11 +2710,342 @@
 );%0A  %7D%0A%0A
+  private static String getUniqueName(Project project) %7B%0A      String%5B%5D names = FavoritesViewImpl.getInstance(project).getAvailableFavoritesLists();%0A      for (int i = 0; ; i++) %7B%0A        String newName = %22Unnamed%22 + (i %3E 0 ? i : %22%22);%0A        if (ArrayUtil.find(names, newName) %3E -1) continue;%0A        return newName;%0A      %7D%0A    %7D
 %0A%7D%0A

change predefined favorites list name