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.
17 package com
.intellij
.ide
.favoritesTreeView
.actions
;
19 import com
.intellij
.ide
.favoritesTreeView
.FavoriteNodeProvider
;
20 import com
.intellij
.ide
.favoritesTreeView
.FavoritesManager
;
21 import com
.intellij
.ide
.projectView
.ProjectView
;
22 import com
.intellij
.ide
.projectView
.ProjectViewNode
;
23 import com
.intellij
.ide
.projectView
.ViewSettings
;
24 import com
.intellij
.ide
.projectView
.impl
.AbstractProjectViewPane
;
25 import com
.intellij
.ide
.projectView
.impl
.ModuleGroup
;
26 import com
.intellij
.ide
.projectView
.impl
.nodes
.*;
27 import com
.intellij
.ide
.util
.treeView
.AbstractTreeNode
;
28 import com
.intellij
.openapi
.actionSystem
.*;
29 import com
.intellij
.openapi
.actionSystem
.ex
.DataConstantsEx
;
30 import com
.intellij
.openapi
.diagnostic
.Logger
;
31 import com
.intellij
.openapi
.extensions
.Extensions
;
32 import com
.intellij
.openapi
.module
.Module
;
33 import com
.intellij
.openapi
.project
.Project
;
34 import com
.intellij
.openapi
.vfs
.VirtualFile
;
35 import com
.intellij
.psi
.PsiDirectory
;
36 import com
.intellij
.psi
.PsiElement
;
37 import com
.intellij
.psi
.PsiFile
;
38 import com
.intellij
.psi
.PsiManager
;
39 import com
.intellij
.psi
.util
.PsiTreeUtil
;
40 import org
.jetbrains
.annotations
.NotNull
;
42 import java
.util
.ArrayList
;
43 import java
.util
.Collection
;
44 import java
.util
.Collections
;
50 public class AddToFavoritesAction
extends AnAction
{
51 private static final Logger LOG
= Logger
.getInstance("com.intellij.ide.favoritesTreeView.actions.AddToFavoritesAction");
53 private final String myFavoritesListName
;
55 public AddToFavoritesAction(String choosenList
) {
56 super(choosenList
.replaceAll("_", "__"));
57 myFavoritesListName
= choosenList
;
60 public void actionPerformed(AnActionEvent e
) {
61 final DataContext dataContext
= e
.getDataContext();
63 Collection
<AbstractTreeNode
> nodesToAdd
= getNodesToAdd(dataContext
, true);
65 if (nodesToAdd
!= null && !nodesToAdd
.isEmpty()) {
66 Project project
= e
.getData(PlatformDataKeys
.PROJECT
);
67 FavoritesManager
.getInstance(project
).addRoots(myFavoritesListName
, nodesToAdd
);
71 public static Collection
<AbstractTreeNode
> getNodesToAdd(final DataContext dataContext
, final boolean inProjectView
) {
72 Project project
= PlatformDataKeys
.PROJECT
.getData(dataContext
);
74 if (project
== null) return null;
76 Module moduleContext
= LangDataKeys
.MODULE_CONTEXT
.getData(dataContext
);
78 Collection
<AbstractTreeNode
> nodesToAdd
= null;
79 for(FavoriteNodeProvider provider
: Extensions
.getExtensions(FavoriteNodeProvider
.EP_NAME
, project
)) {
80 nodesToAdd
= provider
.getFavoriteNodes(dataContext
, ViewSettings
.DEFAULT
);
81 if (nodesToAdd
!= null) {
86 if (nodesToAdd
== null) {
87 Object elements
= collectSelectedElements(dataContext
);
88 if (elements
!= null) {
89 nodesToAdd
= createNodes(project
, moduleContext
, elements
, inProjectView
, ViewSettings
.DEFAULT
);
95 public void update(AnActionEvent e
) {
96 final DataContext dataContext
= e
.getDataContext();
97 Project project
= e
.getData(PlatformDataKeys
.PROJECT
);
98 if (project
== null) {
99 e
.getPresentation().setEnabled(false);
102 e
.getPresentation().setEnabled(canCreateNodes(dataContext
, e
));
106 public static boolean canCreateNodes(final DataContext dataContext
, final AnActionEvent e
) {
107 final boolean inProjectView
= e
.getPlace().equals(ActionPlaces
.J2EE_VIEW_POPUP
) ||
108 e
.getPlace().equals(ActionPlaces
.STRUCTURE_VIEW_POPUP
) ||
109 e
.getPlace().equals(ActionPlaces
.PROJECT_VIEW_POPUP
);
110 return getNodesToAdd(dataContext
, inProjectView
) != null;
113 static Object
retrieveData(Object object
, Object data
) {
114 return object
== null ? data
: object
;
117 private static Object
collectSelectedElements(final DataContext dataContext
) {
118 Object elements
= retrieveData(null, LangDataKeys
.PSI_ELEMENT
.getData(dataContext
));
119 elements
= retrieveData(elements
, LangDataKeys
.PSI_ELEMENT_ARRAY
.getData(dataContext
));
120 elements
= retrieveData(elements
, LangDataKeys
.PSI_FILE
.getData(dataContext
));
121 elements
= retrieveData(elements
, dataContext
.getData(DataConstantsEx
.MODULE_GROUP_ARRAY
));
122 elements
= retrieveData(elements
, LangDataKeys
.MODULE_CONTEXT_ARRAY
.getData(dataContext
));
123 elements
= retrieveData(elements
, dataContext
.getData(DataConstantsEx
.LIBRARY_GROUP_ARRAY
));
124 elements
= retrieveData(elements
, dataContext
.getData(DataConstantsEx
.NAMED_LIBRARY_ARRAY
));
125 elements
= retrieveData(elements
, PlatformDataKeys
.VIRTUAL_FILE
.getData(dataContext
));
126 elements
= retrieveData(elements
, PlatformDataKeys
.VIRTUAL_FILE_ARRAY
.getData(dataContext
));
132 Collection
<AbstractTreeNode
> createNodes(Project project
, Module moduleContext
, Object object
, boolean inProjectView
, ViewSettings favoritesConfig
) {
133 if (project
== null) return Collections
.emptyList();
134 ArrayList
<AbstractTreeNode
> result
= new ArrayList
<AbstractTreeNode
>();
135 for (FavoriteNodeProvider provider
: Extensions
.getExtensions(FavoriteNodeProvider
.EP_NAME
, project
)) {
136 final AbstractTreeNode treeNode
= provider
.createNode(project
, object
, favoritesConfig
);
137 if (treeNode
!= null) {
138 result
.add(treeNode
);
142 final PsiManager psiManager
= PsiManager
.getInstance(project
);
144 final String currentViewId
= ProjectView
.getInstance(project
).getCurrentViewId();
145 AbstractProjectViewPane pane
= ProjectView
.getInstance(project
).getProjectViewPaneById(currentViewId
);
149 if (object
instanceof PsiElement
[]) {
150 for (PsiElement psiElement
: (PsiElement
[])object
) {
151 addPsiElementNode(psiElement
, project
, result
, favoritesConfig
, moduleContext
);
157 if (object
instanceof PsiElement
) {
158 Module containingModule
= null;
159 if (inProjectView
&& ProjectView
.getInstance(project
).isShowModules(currentViewId
)) {
160 if (pane
!= null && pane
.getSelectedDescriptor() != null && pane
.getSelectedDescriptor().getElement() instanceof AbstractTreeNode
) {
161 AbstractTreeNode abstractTreeNode
= ((AbstractTreeNode
)pane
.getSelectedDescriptor().getElement());
162 while (abstractTreeNode
!= null && !(abstractTreeNode
.getParent() instanceof AbstractModuleNode
)) {
163 abstractTreeNode
= abstractTreeNode
.getParent();
165 if (abstractTreeNode
!= null) {
166 containingModule
= ((AbstractModuleNode
)abstractTreeNode
.getParent()).getValue();
170 addPsiElementNode((PsiElement
)object
, project
, result
, favoritesConfig
, containingModule
);
174 if (object
instanceof VirtualFile
[]) {
175 for (VirtualFile vFile
: (VirtualFile
[])object
) {
176 PsiElement element
= psiManager
.findFile(vFile
);
177 if (element
== null) element
= psiManager
.findDirectory(vFile
);
178 addPsiElementNode(element
,
188 if (object
instanceof VirtualFile
) {
189 final VirtualFile vFile
= (VirtualFile
)object
;
190 final PsiFile psiFile
= psiManager
.findFile(vFile
);
191 addPsiElementNode(psiFile
,
200 if (object
instanceof ModuleGroup
[]) {
201 for (ModuleGroup moduleGroup
: (ModuleGroup
[])object
) {
202 result
.add(new ProjectViewModuleGroupNode(project
, moduleGroup
, favoritesConfig
));
208 if (object
instanceof Module
) object
= new Module
[]{(Module
)object
};
209 if (object
instanceof Module
[]) {
210 for (Module module1
: (Module
[])object
) {
211 result
.add(new ProjectViewModuleNode(project
, module1
, favoritesConfig
));
216 //on library group node
217 if (object
instanceof LibraryGroupElement
[]) {
218 for (LibraryGroupElement libraryGroup
: (LibraryGroupElement
[])object
) {
219 result
.add(new LibraryGroupNode(project
, libraryGroup
, favoritesConfig
));
224 //on named library node
225 if (object
instanceof NamedLibraryElement
[]) {
226 for (NamedLibraryElement namedLibrary
: (NamedLibraryElement
[])object
) {
227 result
.add(new NamedLibraryElementNode(project
, namedLibrary
, favoritesConfig
));
234 private static void addPsiElementNode(PsiElement psiElement
,
235 final Project project
,
236 final ArrayList
<AbstractTreeNode
> result
,
237 final ViewSettings favoritesConfig
,
240 Class
<?
extends AbstractTreeNode
> klass
= getPsiElementNodeClass(psiElement
);
242 psiElement
= PsiTreeUtil
.getParentOfType(psiElement
, PsiFile
.class);
243 if (psiElement
!= null) {
244 klass
= PsiFileNode
.class;
248 final Object value
= psiElement
;
250 if (klass
!= null && value
!= null) {
251 result
.add(ProjectViewNode
.createTreeNode(klass
, project
, value
, favoritesConfig
));
254 catch (Exception e
) {
260 private static Class
<?
extends AbstractTreeNode
> getPsiElementNodeClass(PsiElement psiElement
) {
261 Class
<?
extends AbstractTreeNode
> klass
= null;
262 if (psiElement
instanceof PsiFile
) {
263 klass
= PsiFileNode
.class;
265 else if (psiElement
instanceof PsiDirectory
) {
266 klass
= PsiDirectoryNode
.class;