All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.intellij.openapi.wm.impl.welcomeScreen.EditProjectGroupAction Maven / Gradle / Ivy

Go to download

A packaging of the IntelliJ Community Edition platform-impl library. This is release number 1 of trunk branch 142.

The newest version!
/*
 * 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.AnAction;
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.*;
import java.util.List;

/**
 * @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;
           }
         }
      }
    }
  }

  @Override
  public void update(AnActionEvent e) {
    final List selected = getSelectedElements(e);
    boolean enabled = !selected.isEmpty() && selected.get(0) instanceof ProjectGroupActionGroup;
    e.getPresentation().setEnabledAndVisible(enabled);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy