IDEA-117380 Can't remove multiple Maven projects at the same time

This commit is contained in:
Sergey Evdokimov
2014-02-28 17:47:03 +04:00
parent ff6b7d9538
commit a77644f2a1
@@ -20,34 +20,20 @@ import com.intellij.notification.NotificationType;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.maven.project.MavenProject;
import org.jetbrains.idea.maven.project.MavenProjectsManager;
import org.jetbrains.idea.maven.utils.MavenUtil;
import org.jetbrains.idea.maven.utils.actions.MavenAction;
import org.jetbrains.idea.maven.utils.actions.MavenActionUtil;
import java.util.ArrayList;
import java.util.List;
public class RemoveManagedFilesAction extends MavenAction {
@Override
protected boolean isAvailable(AnActionEvent e) {
if (!super.isAvailable(e)) return false;
List<VirtualFile> files = MavenActionUtil.getMavenProjectsFiles(e.getDataContext());
if (files.isEmpty()) return false;
return files.size() == 1 || isAllFilesAreManaged(MavenActionUtil.getProjectsManager(e.getDataContext()), files);
}
private static boolean isAllFilesAreManaged(@NotNull MavenProjectsManager projectsManager, List<VirtualFile> files) {
for (VirtualFile file : files) {
if (!projectsManager.isManagedFile(file)) {
return false;
}
}
return true;
return MavenActionUtil.getMavenProjectsFiles(e.getDataContext()).size() > 0;
}
@Override
@@ -57,34 +43,47 @@ public class RemoveManagedFilesAction extends MavenAction {
MavenProjectsManager projectsManager = MavenActionUtil.getProjectsManager(context);
List<VirtualFile> selectedFiles = MavenActionUtil.getMavenProjectsFiles(context);
if (selectedFiles.size() != 1) return;
List<VirtualFile> removableFiles = new ArrayList<VirtualFile>();
VirtualFile pomXml = selectedFiles.get(0);
if (!projectsManager.isManagedFile(pomXml)) {
MavenProject mavenProject = projectsManager.findProject(pomXml);
assert mavenProject != null;
String aggregatorDescription = "";
MavenProject aggregator = projectsManager.findAggregator(mavenProject);
if (aggregator != null) {
aggregatorDescription = " (" + aggregator.getMavenId().getDisplayString() + ')';
for (VirtualFile pomXml : selectedFiles) {
if (projectsManager.isManagedFile(pomXml)) {
removableFiles.add(pomXml);
}
else {
notifyUserIfNeeded(context, projectsManager, selectedFiles, pomXml);
}
}
projectsManager.removeManagedFiles(removableFiles);
}
Notification notification = new Notification(MavenUtil.MAVEN_NOTIFICATION_GROUP, "Failed to remove project",
"You can not remove selected project because it's " +
"imported as a module of another project" +
aggregatorDescription
+". You can use Ignore action. Only root project can be removed.",
NotificationType.ERROR);
private static void notifyUserIfNeeded(DataContext context,
MavenProjectsManager projectsManager,
List<VirtualFile> selectedFiles,
VirtualFile pomXml) {
MavenProject mavenProject = projectsManager.findProject(pomXml);
assert mavenProject != null;
notification.setImportant(true);
notification.notify(MavenActionUtil.getProject(context));
return;
MavenProject aggregator = projectsManager.findAggregator(mavenProject);
while (aggregator != null && !projectsManager.isManagedFile(aggregator.getFile())) {
aggregator = projectsManager.findAggregator(aggregator);
}
projectsManager.removeManagedFiles(selectedFiles);
if (aggregator != null && !selectedFiles.contains(aggregator.getFile())) {
notifyUser(context, mavenProject, aggregator);
}
}
}
private static void notifyUser(DataContext context, MavenProject mavenProject, MavenProject aggregator) {
String aggregatorDescription = " (" + aggregator.getMavenId().getDisplayString() + ')';
Notification notification = new Notification(MavenUtil.MAVEN_NOTIFICATION_GROUP, "Failed to remove project",
"You can not remove " + mavenProject.getName() + " because it's " +
"imported as a module of another project" +
aggregatorDescription
+ ". You can use Ignore action. Only root project can be removed.",
NotificationType.ERROR
);
notification.setImportant(true);
notification.notify(MavenActionUtil.getProject(context));
}
}