find usages & validation in project structure reworked
[fedora-idea.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / artifacts / actions / InlineArtifactAction.java
blob7a69521191d579f75d88e5d72c4f2ba4d9550703
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.DumbAwareAction;
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.packaging.artifacts.Artifact;
26 import com.intellij.packaging.elements.ArtifactRootElement;
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.elements.ArtifactPackagingElement;
31 import com.intellij.packaging.ui.ArtifactEditorContext;
33 /**
34 * @author nik
36 public class InlineArtifactAction extends DumbAwareAction {
37 private final ArtifactEditorEx myEditor;
39 public InlineArtifactAction(ArtifactEditorEx editor) {
40 super(ProjectBundle.message("action.name.inline.artifact"));
41 myEditor = editor;
44 @Override
45 public void update(AnActionEvent e) {
46 final LayoutTreeSelection selection = myEditor.getLayoutTreeComponent().getSelection();
47 final PackagingElementNode<?> node = selection.getNodeIfSingle();
48 PackagingElement<?> element = selection.getElementIfSingle();
49 e.getPresentation().setEnabled(element instanceof ArtifactPackagingElement && node != null && node.getParentElement(element) != null);
52 public void actionPerformed(AnActionEvent e) {
53 final LayoutTreeComponent treeComponent = myEditor.getLayoutTreeComponent();
54 final LayoutTreeSelection selection = treeComponent.getSelection();
55 final PackagingElement<?> element = selection.getElementIfSingle();
56 final PackagingElementNode<?> node = selection.getNodeIfSingle();
57 if (node == null || !(element instanceof ArtifactPackagingElement)) return;
59 final CompositePackagingElement<?> parent = node.getParentElement(element);
60 if (parent == null) {
61 return;
63 if (!treeComponent.checkCanRemove(selection.getNodes())) return;
64 if (!treeComponent.checkCanAdd(null, parent, node)) return;
66 treeComponent.editLayout(new Runnable() {
67 public void run() {
68 parent.removeChild(element);
69 final ArtifactEditorContext context = myEditor.getContext();
70 final Artifact artifact = ((ArtifactPackagingElement)element).findArtifact(context);
71 if (artifact != null) {
72 final CompositePackagingElement<?> rootElement = artifact.getRootElement();
73 if (rootElement instanceof ArtifactRootElement<?>) {
74 for (PackagingElement<?> child : rootElement.getChildren()) {
75 parent.addOrFindChild(ArtifactUtil.copyWithChildren(child, context.getProject()));
78 else {
79 parent.addOrFindChild(ArtifactUtil.copyWithChildren(rootElement, context.getProject()));
83 });
84 treeComponent.rebuildTree();