update artifacts on module rename (IDEADEV-40824)
[fedora-idea.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / artifacts / actions / ExtractArtifactAction.java
blob2cc19955dff323627345b5371ecab1e8f0f105fb
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.roots.ui.configuration.artifacts.actions;
18 import com.intellij.openapi.actionSystem.AnActionEvent;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.project.ProjectBundle;
21 import com.intellij.openapi.roots.ui.configuration.artifacts.ArtifactEditorEx;
22 import com.intellij.openapi.roots.ui.configuration.artifacts.LayoutTreeComponent;
23 import com.intellij.openapi.roots.ui.configuration.artifacts.LayoutTreeSelection;
24 import com.intellij.openapi.ui.Messages;
25 import com.intellij.packaging.artifacts.ArtifactPointerManager;
26 import com.intellij.packaging.artifacts.ModifiableArtifact;
27 import com.intellij.packaging.elements.CompositePackagingElement;
28 import com.intellij.packaging.elements.PackagingElement;
29 import com.intellij.packaging.impl.artifacts.ArtifactUtil;
30 import com.intellij.packaging.impl.artifacts.PlainArtifactType;
31 import com.intellij.packaging.impl.elements.ArtifactPackagingElement;
33 import java.util.Collection;
35 /**
36 * @author nik
38 public class ExtractArtifactAction extends LayoutTreeActionBase {
39 public ExtractArtifactAction(ArtifactEditorEx editor) {
40 super(ProjectBundle.message("action.name.extract.artifact"), editor);
43 @Override
44 protected boolean isEnabled() {
45 return myArtifactEditor.getLayoutTreeComponent().getSelection().getCommonParentElement() != null;
48 public void actionPerformed(AnActionEvent e) {
49 final LayoutTreeComponent treeComponent = myArtifactEditor.getLayoutTreeComponent();
50 final LayoutTreeSelection selection = treeComponent.getSelection();
51 final CompositePackagingElement<?> parent = selection.getCommonParentElement();
52 if (parent == null) return;
54 if (!treeComponent.checkCanRemove(selection.getNodes())) {
55 return;
58 final Collection<? extends PackagingElement> selectedElements = selection.getElements();
59 final String name = Messages.showInputDialog(myArtifactEditor.getMainComponent(), ProjectBundle.message("label.text.specify.artifact.name"),
60 ProjectBundle.message("dialog.title.extract.artifact"), null);
61 if (name != null) {
62 final Project project = myArtifactEditor.getContext().getProject();
63 //todo[nik] select type?
64 final ModifiableArtifact artifact = myArtifactEditor.getContext().getOrCreateModifiableArtifactModel().addArtifact(name, PlainArtifactType.getInstance());
65 treeComponent.editLayout(new Runnable() {
66 public void run() {
67 for (PackagingElement<?> element : selectedElements) {
68 artifact.getRootElement().addOrFindChild(ArtifactUtil.copyWithChildren(element, project));
70 for (PackagingElement element : selectedElements) {
71 parent.removeChild(element);
73 parent.addOrFindChild(new ArtifactPackagingElement(project, ArtifactPointerManager.getInstance(project).createPointer(artifact, myArtifactEditor.getContext().getArtifactModel())));
75 });
76 treeComponent.rebuildTree();