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
.sourceItems
;
18 import com
.intellij
.openapi
.actionSystem
.AnAction
;
19 import com
.intellij
.openapi
.actionSystem
.AnActionEvent
;
20 import com
.intellij
.openapi
.actionSystem
.Presentation
;
21 import com
.intellij
.openapi
.roots
.ui
.configuration
.artifacts
.ArtifactEditorEx
;
22 import com
.intellij
.openapi
.roots
.ui
.configuration
.artifacts
.ArtifactEditorImpl
;
23 import com
.intellij
.openapi
.util
.Pair
;
24 import com
.intellij
.openapi
.util
.Ref
;
25 import com
.intellij
.packaging
.artifacts
.Artifact
;
26 import com
.intellij
.packaging
.elements
.CompositePackagingElement
;
27 import com
.intellij
.packaging
.elements
.PackagingElement
;
28 import com
.intellij
.packaging
.impl
.artifacts
.ArtifactUtil
;
29 import com
.intellij
.packaging
.impl
.artifacts
.ParentElementProcessor
;
30 import com
.intellij
.packaging
.impl
.elements
.ManifestFileUtil
;
31 import com
.intellij
.packaging
.ui
.ArtifactEditor
;
32 import com
.intellij
.packaging
.ui
.ArtifactEditorContext
;
33 import com
.intellij
.packaging
.ui
.PackagingSourceItem
;
34 import org
.jetbrains
.annotations
.NotNull
;
35 import org
.jetbrains
.annotations
.Nullable
;
37 import java
.util
.ArrayList
;
38 import java
.util
.List
;
43 public class PutSourceItemIntoParentAndLinkViaManifestAction
extends AnAction
{
44 private final SourceItemsTree mySourceItemsTree
;
45 private final ArtifactEditorEx myArtifactEditor
;
47 public PutSourceItemIntoParentAndLinkViaManifestAction(SourceItemsTree sourceItemsTree
, ArtifactEditorEx artifactEditor
) {
48 mySourceItemsTree
= sourceItemsTree
;
49 myArtifactEditor
= artifactEditor
;
53 public void update(AnActionEvent e
) {
54 final Presentation presentation
= e
.getPresentation();
55 final Artifact artifact
= myArtifactEditor
.getArtifact();
57 final ParentElementsInfo parentInfo
= findParentAndGrandParent(artifact
);
58 if (parentInfo
!= null) {
59 presentation
.setText("Put Into '" + parentInfo
.getGrandparentArtifact().getName() + "' and link via manifest");
62 boolean enable
= parentInfo
!= null;
63 for (PackagingSourceItem item
: mySourceItemsTree
.getSelectedItems()) {
64 if (!item
.getKindOfProducedElements().containsJarFiles()) {
69 presentation
.setVisible(enable
);
70 presentation
.setEnabled(enable
);
74 private ParentElementsInfo
findParentAndGrandParent(Artifact artifact
) {
75 final Ref
<ParentElementsInfo
> result
= Ref
.create(null);
76 ArtifactUtil
.processParents(artifact
, myArtifactEditor
.getContext(), new ParentElementProcessor() {
78 public boolean process(@NotNull CompositePackagingElement
<?
> element
,
79 @NotNull List
<Pair
<Artifact
,CompositePackagingElement
<?
>>> parents
,
80 @NotNull Artifact artifact
) {
81 if (parents
.size() == 1) {
82 final Pair
<Artifact
, CompositePackagingElement
<?
>> parent
= parents
.get(0);
83 result
.set(new ParentElementsInfo(parent
.getFirst(), parent
.getSecond(), artifact
, element
));
94 public void actionPerformed(AnActionEvent e
) {
95 final List
<PackagingSourceItem
> items
= mySourceItemsTree
.getSelectedItems();
96 ParentElementsInfo parentsInfo
= findParentAndGrandParent(myArtifactEditor
.getArtifact());
97 if (parentsInfo
== null) {
101 final Artifact artifact
= parentsInfo
.getGrandparentArtifact();
102 final ArtifactEditorContext context
= myArtifactEditor
.getContext();
104 final Runnable emptyRunnable
= new Runnable() {
108 context
.editLayout(artifact
, emptyRunnable
);
109 context
.editLayout(parentsInfo
.getParentArtifact(), emptyRunnable
);
110 parentsInfo
= findParentAndGrandParent(myArtifactEditor
.getArtifact());//find elements under modifiable root
111 if (parentsInfo
== null) {
115 final CompositePackagingElement
<?
> grandParent
= parentsInfo
.getGrandparentElement();
116 final List
<String
> classpath
= new ArrayList
<String
>();
117 context
.editLayout(artifact
, new Runnable() {
119 for (PackagingSourceItem item
: items
) {
120 final List
<?
extends PackagingElement
<?
>> elements
= item
.createElements(context
);
121 grandParent
.addOrFindChildren(elements
);
122 classpath
.addAll(ManifestFileUtil
.getClasspathForElements(elements
, context
, artifact
.getArtifactType()));
127 final ArtifactEditor parentArtifactEditor
= context
.getOrCreateEditor(parentsInfo
.getParentArtifact());
128 final ParentElementsInfo finalParentsInfo
= parentsInfo
;
129 context
.editLayout(parentsInfo
.getParentArtifact(), new Runnable() {
131 parentArtifactEditor
.addToClasspath(finalParentsInfo
.getParentElement(), classpath
);
134 ((ArtifactEditorImpl
)context
.getOrCreateEditor(parentsInfo
.getGrandparentArtifact())).rebuildTries();
137 private static class ParentElementsInfo
{
138 private Artifact myParentArtifact
;
139 private CompositePackagingElement
<?
> myParentElement
;
140 private Artifact myGrandparentArtifact
;
141 private CompositePackagingElement
<?
> myGrandparentElement
;
143 private ParentElementsInfo(Artifact parentArtifact
,
144 CompositePackagingElement
<?
> parentElement
,
145 Artifact grandparentArtifact
,
146 CompositePackagingElement
<?
> grandparentElement
) {
147 myParentArtifact
= parentArtifact
;
148 myParentElement
= parentElement
;
149 myGrandparentArtifact
= grandparentArtifact
;
150 myGrandparentElement
= grandparentElement
;
153 public Artifact
getParentArtifact() {
154 return myParentArtifact
;
157 public CompositePackagingElement
<?
> getParentElement() {
158 return myParentElement
;
161 public Artifact
getGrandparentArtifact() {
162 return myGrandparentArtifact
;
165 public CompositePackagingElement
<?
> getGrandparentElement() {
166 return myGrandparentElement
;