Hierarchy providers refactored
[fedora-idea.git] / lang-impl / src / com / intellij / ide / favoritesTreeView / FavoritesProjectViewPane.java
blob1b5c7d7f10517408d7eb7840bcb43c35b179a53d
1 package com.intellij.ide.favoritesTreeView;
3 import com.intellij.ide.IdeBundle;
4 import com.intellij.ide.SelectInTarget;
5 import com.intellij.ide.projectView.ProjectView;
6 import com.intellij.ide.projectView.impl.AbstractProjectViewPane;
7 import com.intellij.openapi.actionSystem.ActionManager;
8 import com.intellij.openapi.actionSystem.DefaultActionGroup;
9 import com.intellij.openapi.actionSystem.IdeActions;
10 import com.intellij.openapi.project.Project;
11 import com.intellij.openapi.util.ActionCallback;
12 import com.intellij.openapi.util.IconLoader;
13 import com.intellij.openapi.vfs.VirtualFile;
14 import com.intellij.psi.PsiElement;
15 import com.intellij.psi.util.PsiUtilBase;
16 import com.intellij.util.ArrayUtil;
17 import org.jetbrains.annotations.NonNls;
18 import org.jetbrains.annotations.NotNull;
20 import javax.swing.*;
22 /**
23 * @author cdr
25 public class FavoritesProjectViewPane extends AbstractProjectViewPane {
26 @NonNls public static final String ID = "Favorites";
27 private FavoritesTreeViewPanel myViewPanel;
28 private final ProjectView myProjectView;
29 private final FavoritesManager myFavoritesManager;
30 private final FavoritesManager.FavoritesListener myFavoritesListener;
32 protected FavoritesProjectViewPane(final Project project, ProjectView projectView, FavoritesManager favoritesManager) {
33 super(project);
34 myProjectView = projectView;
35 myFavoritesManager = favoritesManager;
36 myFavoritesListener = new FavoritesManager.FavoritesListener() {
37 public void rootsChanged(String listName) {
39 public void listAdded(String listName) {
40 refreshMySubIdsAndSelect(listName);
43 public void listRemoved(String listName) {
44 String selectedSubId = getSubId();
45 refreshMySubIdsAndSelect(selectedSubId);
48 private void refreshMySubIdsAndSelect(String listName) {
49 myFavoritesManager.removeFavoritesListener(myFavoritesListener);
50 myProjectView.removeProjectPane(FavoritesProjectViewPane.this);
51 myProjectView.addProjectPane(FavoritesProjectViewPane.this);
52 myFavoritesManager.addFavoritesListener(myFavoritesListener);
54 if (ArrayUtil.find(myFavoritesManager.getAvailableFavoritesLists(), listName) == -1) {
55 listName = null;
57 myProjectView.changeView(ID, listName);
62 public String getTitle() {
63 return IdeBundle.message("action.toolwindow.favorites");
66 public Icon getIcon() {
67 return IconLoader.getIcon("/general/toolWindowFavorites.png");
70 @NotNull
71 public String getId() {
72 return ID;
75 public void installComparator() {
76 final ProjectView projectView = ProjectView.getInstance(myProject);
77 getTreeBuilder().setNodeDescriptorComparator(new FavoritesComparator(projectView.isSortByType(ID), myProject));
80 public JComponent createComponent() {
81 //if (myViewPanel != null) return myViewPanel;
83 myViewPanel = new FavoritesTreeViewPanel(myProject, null, getSubId());
84 myTree = myViewPanel.getTree();
85 setTreeBuilder(myViewPanel.getBuilder());
86 myTreeStructure = myViewPanel.getFavoritesTreeStructure();
87 installComparator();
88 return myViewPanel;
91 public void dispose() {
92 myViewPanel = null;
93 super.dispose();
96 @NotNull
97 public String[] getSubIds() {
98 return myFavoritesManager.getAvailableFavoritesLists();
101 @NotNull
102 public String getPresentableSubIdName(@NotNull final String subId) {
103 return subId;
106 public ActionCallback updateFromRoot(boolean restoreExpandedPaths) {
107 return ((FavoritesViewTreeBuilder) getTreeBuilder()).updateFromRootCB();
110 public void select(Object object, VirtualFile file, boolean requestFocus) {
111 if (!(object instanceof PsiElement)) return;
112 /*PsiElement element = (PsiElement)object;
113 PsiFile psiFile = element.getContainingFile();
114 if (psiFile != null) {
115 element = psiFile;
118 if (element instanceof PsiJavaFile) {
119 final PsiClass[] classes = ((PsiJavaFile)element).getClasses();
120 if (classes.length > 0) {
121 element = classes[0];
125 final PsiElement originalElement = element.getOriginalElement();*/
126 final VirtualFile virtualFile = PsiUtilBase.getVirtualFile((PsiElement)object);
127 final String list = FavoritesViewSelectInTarget.findSuitableFavoritesList(virtualFile, myProject, getSubId());
128 if (list == null) return;
129 if (!list.equals(getSubId())) {
130 ProjectView.getInstance(myProject).changeView(ID, list);
132 myViewPanel.selectElement(object, virtualFile, requestFocus);
135 public int getWeight() {
136 return 4;
139 public SelectInTarget createSelectInTarget() {
140 return new FavoritesViewSelectInTarget(myProject);
143 public void addToolbarActions(final DefaultActionGroup group) {
144 group.add(ActionManager.getInstance().getAction(IdeActions.RENAME_FAVORITES_LIST));
147 //project component related
148 public void projectOpened() {
149 myProjectView.addProjectPane(this);
150 myFavoritesManager.addFavoritesListener(myFavoritesListener);
153 public void projectClosed() {
156 @NonNls
157 public String getComponentName() {
158 return "FavoritesProjectViewPane";
161 public void initComponent() {
165 public void disposeComponent() {
166 myFavoritesManager.removeFavoritesListener(myFavoritesListener);