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
.DumbAwareAction
;
20 import com
.intellij
.openapi
.project
.Project
;
21 import com
.intellij
.openapi
.project
.ProjectBundle
;
22 import com
.intellij
.openapi
.roots
.ui
.configuration
.artifacts
.ArtifactEditorEx
;
23 import com
.intellij
.openapi
.roots
.ui
.configuration
.artifacts
.LayoutTreeComponent
;
24 import com
.intellij
.openapi
.roots
.ui
.configuration
.artifacts
.LayoutTreeSelection
;
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
;
39 public class ExtractArtifactAction
extends DumbAwareAction
{
40 private ArtifactEditorEx myEditor
;
42 public ExtractArtifactAction(ArtifactEditorEx editor
) {
43 super(ProjectBundle
.message("action.name.extract.artifact"));
48 public void update(AnActionEvent e
) {
49 final LayoutTreeSelection selection
= myEditor
.getLayoutTreeComponent().getSelection();
50 e
.getPresentation().setEnabled(selection
.getCommonParentElement() != null);
53 public void actionPerformed(AnActionEvent e
) {
54 final LayoutTreeComponent treeComponent
= myEditor
.getLayoutTreeComponent();
55 final LayoutTreeSelection selection
= treeComponent
.getSelection();
56 final CompositePackagingElement
<?
> parent
= selection
.getCommonParentElement();
57 if (parent
== null) return;
59 if (!treeComponent
.checkCanRemove(selection
.getNodes())) {
63 final Collection
<?
extends PackagingElement
> selectedElements
= selection
.getElements();
64 final String name
= Messages
.showInputDialog(myEditor
.getMainComponent(), ProjectBundle
.message("label.text.specify.artifact.name"),
65 ProjectBundle
.message("dialog.title.extract.artifact"), null);
67 final Project project
= myEditor
.getContext().getProject();
68 //todo[nik] select type?
69 final ModifiableArtifact artifact
= myEditor
.getContext().getModifiableArtifactModel().addArtifact(name
, PlainArtifactType
.getInstance());
70 treeComponent
.editLayout(new Runnable() {
72 for (PackagingElement
<?
> element
: selectedElements
) {
73 artifact
.getRootElement().addOrFindChild(ArtifactUtil
.copyWithChildren(element
, project
));
75 for (PackagingElement element
: selectedElements
) {
76 parent
.removeChild(element
);
78 parent
.addOrFindChild(new ArtifactPackagingElement(project
, ArtifactPointerManager
.getInstance(project
).create(artifact
)));
81 treeComponent
.rebuildTree();