From a039ad3f18583c05eca2a9b4ce3fabba8b601dc3 Mon Sep 17 00:00:00 2001 From: nik Date: Tue, 3 Nov 2009 15:30:53 +0300 Subject: [PATCH] IDEADEV-41036: Artifact editor: Remove button is enabled with focus on the output tree root --- .../actions/ExtractArtifactAction.java | 20 ++++---- .../actions/LayoutTreeActionBase.java | 46 +++++++++++++++++++ .../actions/RemovePackagingElementAction.java | 26 ++++++++--- 3 files changed, 73 insertions(+), 19 deletions(-) create mode 100644 java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/artifacts/actions/LayoutTreeActionBase.java diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/artifacts/actions/ExtractArtifactAction.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/artifacts/actions/ExtractArtifactAction.java index 7c701ede4c4b..bc96b49d0686 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/artifacts/actions/ExtractArtifactAction.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/artifacts/actions/ExtractArtifactAction.java @@ -36,22 +36,18 @@ import java.util.Collection; /** * @author nik */ -public class ExtractArtifactAction extends DumbAwareAction { - private ArtifactEditorEx myEditor; - +public class ExtractArtifactAction extends LayoutTreeActionBase { public ExtractArtifactAction(ArtifactEditorEx editor) { - super(ProjectBundle.message("action.name.extract.artifact")); - myEditor = editor; + super(ProjectBundle.message("action.name.extract.artifact"), editor); } @Override - public void update(AnActionEvent e) { - final LayoutTreeSelection selection = myEditor.getLayoutTreeComponent().getSelection(); - e.getPresentation().setEnabled(selection.getCommonParentElement() != null); + protected boolean isEnabled() { + return myArtifactEditor.getLayoutTreeComponent().getSelection().getCommonParentElement() != null; } public void actionPerformed(AnActionEvent e) { - final LayoutTreeComponent treeComponent = myEditor.getLayoutTreeComponent(); + final LayoutTreeComponent treeComponent = myArtifactEditor.getLayoutTreeComponent(); final LayoutTreeSelection selection = treeComponent.getSelection(); final CompositePackagingElement parent = selection.getCommonParentElement(); if (parent == null) return; @@ -61,12 +57,12 @@ public class ExtractArtifactAction extends DumbAwareAction { } final Collection selectedElements = selection.getElements(); - final String name = Messages.showInputDialog(myEditor.getMainComponent(), ProjectBundle.message("label.text.specify.artifact.name"), + final String name = Messages.showInputDialog(myArtifactEditor.getMainComponent(), ProjectBundle.message("label.text.specify.artifact.name"), ProjectBundle.message("dialog.title.extract.artifact"), null); if (name != null) { - final Project project = myEditor.getContext().getProject(); + final Project project = myArtifactEditor.getContext().getProject(); //todo[nik] select type? - final ModifiableArtifact artifact = myEditor.getContext().getModifiableArtifactModel().addArtifact(name, PlainArtifactType.getInstance()); + final ModifiableArtifact artifact = myArtifactEditor.getContext().getModifiableArtifactModel().addArtifact(name, PlainArtifactType.getInstance()); treeComponent.editLayout(new Runnable() { public void run() { for (PackagingElement element : selectedElements) { diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/artifacts/actions/LayoutTreeActionBase.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/artifacts/actions/LayoutTreeActionBase.java new file mode 100644 index 000000000000..944c959e61ee --- /dev/null +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/artifacts/actions/LayoutTreeActionBase.java @@ -0,0 +1,46 @@ +/* + * Copyright 2000-2009 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.roots.ui.configuration.artifacts.actions; + +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.project.DumbAwareAction; +import com.intellij.openapi.roots.ui.configuration.artifacts.ArtifactEditorEx; + +import javax.swing.*; + +/** + * @author nik + */ +public abstract class LayoutTreeActionBase extends DumbAwareAction { + protected final ArtifactEditorEx myArtifactEditor; + + protected LayoutTreeActionBase(String text, String description, Icon icon, ArtifactEditorEx artifactEditor) { + super(text, description, icon); + myArtifactEditor = artifactEditor; + } + + protected LayoutTreeActionBase(String text, ArtifactEditorEx artifactEditor) { + super(text); + myArtifactEditor = artifactEditor; + } + + @Override + public void update(AnActionEvent e) { + e.getPresentation().setEnabled(isEnabled()); + } + + protected abstract boolean isEnabled(); +} diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/artifacts/actions/RemovePackagingElementAction.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/artifacts/actions/RemovePackagingElementAction.java index 4854a551391c..b84c215c51b2 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/artifacts/actions/RemovePackagingElementAction.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/artifacts/actions/RemovePackagingElementAction.java @@ -19,23 +19,35 @@ import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.project.DumbAwareAction; import com.intellij.openapi.project.ProjectBundle; import com.intellij.openapi.roots.ui.configuration.artifacts.ArtifactEditorEx; +import com.intellij.openapi.roots.ui.configuration.artifacts.LayoutTreeSelection; +import com.intellij.openapi.roots.ui.configuration.artifacts.nodes.PackagingElementNode; +import com.intellij.packaging.elements.PackagingElement; import com.intellij.util.Icons; +import java.util.List; + /** * @author nik */ -public class RemovePackagingElementAction extends DumbAwareAction { - private final ArtifactEditorEx myArtifactEditor; +public class RemovePackagingElementAction extends LayoutTreeActionBase { public RemovePackagingElementAction(ArtifactEditorEx artifactEditor) { - super(ProjectBundle.message("action.name.remove.packaging.element"), ProjectBundle.message("action.description.remove.packaging.elements"), Icons.DELETE_ICON); - myArtifactEditor = artifactEditor; + super(ProjectBundle.message("action.name.remove.packaging.element"), ProjectBundle.message("action.description.remove.packaging.elements"), Icons.DELETE_ICON, + artifactEditor); } @Override - public void update(AnActionEvent e) { - e.getPresentation().setEnabled(!myArtifactEditor.getLayoutTreeComponent().getSelection().getElements().isEmpty() - && !myArtifactEditor.getLayoutTreeComponent().isEditing()); + protected boolean isEnabled() { + final LayoutTreeSelection selection = myArtifactEditor.getLayoutTreeComponent().getSelection(); + if (selection.getElements().isEmpty() || myArtifactEditor.getLayoutTreeComponent().isEditing()) { + return false; + } + for (PackagingElementNode node : selection.getNodes()) { + if (node.getParentNode() == null) { + return false; + } + } + return true; } public void actionPerformed(AnActionEvent e) {