/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.wm.impl.welcomeScreen;

import com.intellij.ide.ProjectGroup;
import com.intellij.ide.ProjectGroupActionGroup;
import com.intellij.ide.RecentProjectsManager;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.InputValidatorEx;
import com.intellij.openapi.ui.Messages;
import com.intellij.ui.ListScrollingUtil;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @author Konstantin Bulenkov
 */
public class EditProjectGroupAction extends RecentProjectsWelcomeScreenActionBase {
  @Override
  public void actionPerformed(AnActionEvent e) {
    final ProjectGroup group = ((ProjectGroupActionGroup)getSelectedElements(e).get(0)).getGroup();
    JList list = getList(e);
    assert list != null;
    DefaultListModel model = getDataModel(e);
    String name = Messages.showInputDialog(list, "Enter group name: ", "Change Group Name", null, group.getName(),
                                           new InputValidatorEx() {
                                             @Nullable
                                             @Override
                                             public String getErrorText(String inputString) {
                                               inputString = inputString.trim();
                                               if (inputString.length() == 0) {
                                                 return "Name cannot be empty.";
                                               }
                                               if (!checkInput(inputString)) {
                                                 return "Group '" + inputString + "' already exists.";
                                               }
                                               return null;
                                             }

                                             @Override
                                             public boolean checkInput(String inputString) {
                                               inputString = inputString.trim();
                                               if (inputString.equals(group.getName())) return true;
                                               for (ProjectGroup projectGroup : RecentProjectsManager.getInstance().getGroups()) {
                                                 if (projectGroup.getName().equals(inputString)) {
                                                   return false;
                                                 }
                                               }
                                               return true;
                                             }

                                             @Override
                                             public boolean canClose(String inputString) {
                                               return true;
                                             }
                                           });
    if (name != null && model != null) {
      group.setName(name);
      rebuildRecentProjectDataModel(model);
      for (int i = 0; i < model.getSize(); i++) {
        Object element = model.get(i);
        if (element instanceof ProjectGroupActionGroup) {
           if (((ProjectGroupActionGroup)element).getGroup().equals(group)) {
             ListScrollingUtil.selectItem(list, i);
             break;
           }
         }
      }
    }
  }
}

@@ -781,16 +781,67 @@
 anager;%0A
+import com.intellij.openapi.actionSystem.AnAction;%0A
 import c
@@ -1085,16 +1085,39 @@
 swing.*;
+%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -4094,11 +4094,280 @@
    %7D%0A  %7D
+%0A%0A  @Override%0A  public void update(AnActionEvent e) %7B%0A    final List%3CAnAction%3E selected = getSelectedElements(e);%0A    boolean enabled = !selected.isEmpty() && selected.get(0) instanceof ProjectGroupActionGroup;%0A    e.getPresentation().setEnabledAndVisible(enabled);%0A  %7D
 %0A%7D%0A

fix CCE in Edit Project Group action