editing included content in artifacts: IDEADEV-41358, IDEADEV-40826, IDEADEV-40733
[fedora-idea.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / artifacts / actions / ExtractArtifactAction.java
blob1bbe35c178493900f5b5cb50551f98a1677dbc26
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.roots.ui.configuration.artifacts.nodes.PackagingElementNode;
25 import com.intellij.openapi.ui.Messages;
26 import com.intellij.packaging.artifacts.ArtifactPointerManager;
27 import com.intellij.packaging.artifacts.ModifiableArtifact;
28 import com.intellij.packaging.elements.CompositePackagingElement;
29 import com.intellij.packaging.elements.PackagingElement;
30 import com.intellij.packaging.impl.artifacts.ArtifactUtil;
31 import com.intellij.packaging.impl.artifacts.PlainArtifactType;
32 import com.intellij.packaging.impl.elements.ArtifactPackagingElement;
34 import java.util.Collection;
36 /**
37 * @author nik
39 public class ExtractArtifactAction extends LayoutTreeActionBase {
40 public ExtractArtifactAction(ArtifactEditorEx editor) {
41 super(ProjectBundle.message("action.name.extract.artifact"), editor);
44 @Override
45 protected boolean isEnabled() {
46 return myArtifactEditor.getLayoutTreeComponent().getSelection().getCommonParentElement() != null;
49 public void actionPerformed(AnActionEvent e) {
50 final LayoutTreeComponent treeComponent = myArtifactEditor.getLayoutTreeComponent();
51 final LayoutTreeSelection selection = treeComponent.getSelection();
52 final CompositePackagingElement<?> parent = selection.getCommonParentElement();
53 if (parent == null) return;
54 final PackagingElementNode<?> parentNode = selection.getNodes().get(0).getParentNode();
55 if (parentNode == null) return;
57 if (!treeComponent.checkCanModifyChildren(parent, parentNode, selection.getNodes())) {
58 return;
61 final Collection<? extends PackagingElement> selectedElements = selection.getElements();
62 final String name = Messages.showInputDialog(myArtifactEditor.getMainComponent(), ProjectBundle.message("label.text.specify.artifact.name"),
63 ProjectBundle.message("dialog.title.extract.artifact"), null);
64 if (name != null) {
65 final Project project = myArtifactEditor.getContext().getProject();
66 //todo[nik] select type?
67 final ModifiableArtifact artifact = myArtifactEditor.getContext().getOrCreateModifiableArtifactModel().addArtifact(name, PlainArtifactType.getInstance());
68 treeComponent.editLayout(new Runnable() {
69 public void run() {
70 for (PackagingElement<?> element : selectedElements) {
71 artifact.getRootElement().addOrFindChild(ArtifactUtil.copyWithChildren(element, project));
73 for (PackagingElement element : selectedElements) {
74 parent.removeChild(element);
76 parent.addOrFindChild(new ArtifactPackagingElement(project, ArtifactPointerManager.getInstance(project).createPointer(artifact, myArtifactEditor.getContext().getArtifactModel())));
78 });
79 treeComponent.rebuildTree();