simplify GTDUCollector a bit more
[idea/community.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / artifacts / actions / SurroundElementWithAction.java
1 /*
2  * Copyright 2000-2010 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.openapi.roots.ui.configuration.artifacts.actions;
17
18 import com.intellij.ide.JavaUiBundle;
19 import com.intellij.openapi.actionSystem.AnActionEvent;
20 import com.intellij.openapi.project.Project;
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.popup.JBPopupFactory;
26 import com.intellij.openapi.ui.popup.PopupStep;
27 import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
28 import com.intellij.packaging.elements.CompositePackagingElement;
29 import com.intellij.packaging.elements.CompositePackagingElementType;
30 import com.intellij.packaging.elements.PackagingElement;
31 import com.intellij.packaging.elements.PackagingElementFactory;
32 import com.intellij.packaging.impl.artifacts.ArtifactUtil;
33 import com.intellij.util.PathUtil;
34 import com.intellij.util.containers.ContainerUtil;
35 import org.jetbrains.annotations.NotNull;
36
37 import javax.swing.*;
38 import java.util.List;
39
40 import static com.intellij.openapi.keymap.KeymapUtil.getActiveKeymapShortcuts;
41
42 public class SurroundElementWithAction extends LayoutTreeActionBase {
43   public SurroundElementWithAction(ArtifactEditorEx artifactEditor) {
44     super("Surround With...", artifactEditor);
45     registerCustomShortcutSet(getActiveKeymapShortcuts("SurroundWith"), artifactEditor.getLayoutTreeComponent().getLayoutTree());
46   }
47
48   @Override
49   protected boolean isEnabled() {
50     return myArtifactEditor.getLayoutTreeComponent().getSelection().getCommonParentElement() != null;
51   }
52
53   @Override
54   public void actionPerformed(@NotNull AnActionEvent e) {
55     final LayoutTreeComponent treeComponent = myArtifactEditor.getLayoutTreeComponent();
56     final LayoutTreeSelection selection = treeComponent.getSelection();
57     final CompositePackagingElement<?> parent = selection.getCommonParentElement();
58     if (parent == null) return;
59     final PackagingElementNode<?> parentNode = selection.getNodes().get(0).getParentNode();
60     if (parentNode == null) return;
61
62     if (!treeComponent.checkCanModifyChildren(parent, parentNode, selection.getNodes())) {
63       return;
64     }
65
66     final CompositePackagingElementType<?>[] types = PackagingElementFactory.getInstance().getCompositeElementTypes();
67     final List<PackagingElement<?>> selected = selection.getElements();
68     if (types.length == 1) {
69       surroundWith(types[0], parent, selected, treeComponent);
70     }
71     else {
72       JBPopupFactory.getInstance().createListPopup(new BaseListPopupStep<CompositePackagingElementType>(
73         JavaUiBundle.message("popup.title.surround.with"), types) {
74         @Override
75         public Icon getIconFor(CompositePackagingElementType aValue) {
76           return aValue.getCreateElementIcon();
77         }
78
79         @NotNull
80         @Override
81         public String getTextFor(CompositePackagingElementType value) {
82           return value.getPresentableName();
83         }
84
85         @Override
86         public PopupStep onChosen(final CompositePackagingElementType selectedValue, boolean finalChoice) {
87           return doFinalStep(() -> surroundWith(selectedValue, parent, selected, treeComponent));
88         }
89       }).showInBestPositionFor(e.getDataContext());
90     }
91   }
92
93   private void surroundWith(final CompositePackagingElementType<?> type, final CompositePackagingElement<?> parent, final List<? extends PackagingElement<?>> selected,
94                             LayoutTreeComponent treeComponent) {
95     if (myArtifactEditor.isDisposed() || selected.isEmpty()) return;
96
97     final Project project = myArtifactEditor.getContext().getProject();
98     final String elementName = ContainerUtil.getFirstItem(selected, null).createPresentation(myArtifactEditor.getContext()).getPresentableName();
99     final String baseName = PathUtil.suggestFileName(elementName);
100     final CompositePackagingElement<?> newParent = type.createComposite(parent, baseName, myArtifactEditor.getContext());
101     if (newParent != null) {
102       treeComponent.editLayout(() -> {
103         for (PackagingElement<?> element : selected) {
104           newParent.addOrFindChild(ArtifactUtil.copyWithChildren(element, project));
105         }
106         for (PackagingElement<?> element : selected) {
107           parent.removeChild(element);
108         }
109         parent.addOrFindChild(newParent);
110       });
111       treeComponent.rebuildTree();
112     }
113   }
114 }