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
.ide
.actions
;
18 import com
.intellij
.ide
.IdeBundle
;
19 import com
.intellij
.openapi
.actionSystem
.ActionGroup
;
20 import com
.intellij
.openapi
.actionSystem
.ActionManager
;
21 import com
.intellij
.openapi
.actionSystem
.AnAction
;
22 import com
.intellij
.openapi
.actionSystem
.AnActionEvent
;
23 import com
.intellij
.openapi
.project
.Project
;
24 import com
.intellij
.openapi
.project
.ProjectManager
;
25 import com
.intellij
.openapi
.project
.ProjectManagerAdapter
;
26 import com
.intellij
.openapi
.util
.IconLoader
;
27 import com
.intellij
.openapi
.wm
.ToolWindowId
;
28 import com
.intellij
.openapi
.wm
.ex
.ToolWindowManagerAdapter
;
29 import com
.intellij
.openapi
.wm
.ex
.ToolWindowManagerEx
;
30 import com
.intellij
.util
.containers
.HashMap
;
31 import org
.jetbrains
.annotations
.Nullable
;
34 import java
.util
.ArrayList
;
35 import java
.util
.Collections
;
36 import java
.util
.Comparator
;
39 * @author Vladimir Kondratyev
41 public final class ToolWindowsGroup
extends ActionGroup
{
42 private static final HashMap
<String
, MyDescriptor
> ourId2Text
;
44 ourId2Text
= new HashMap
<String
, MyDescriptor
>();
45 ourId2Text
.put(ToolWindowId
.COMMANDER
, new MyDescriptor(IdeBundle
.message("action.toolwindow.commander"), IconLoader
.getIcon("/general/toolWindowCommander.png")));
46 ourId2Text
.put(ToolWindowId
.MESSAGES_WINDOW
, new MyDescriptor(IdeBundle
.message("action.toolwindow.messages"), IconLoader
.getIcon("/general/toolWindowMessages.png")));
47 ourId2Text
.put(ToolWindowId
.PROJECT_VIEW
, new MyDescriptor(IdeBundle
.message("action.toolwindow.project"), IconLoader
.getIcon("/general/toolWindowProject.png")));
48 ourId2Text
.put(ToolWindowId
.STRUCTURE_VIEW
, new MyDescriptor(IdeBundle
.message("action.toolwindow.structure"), IconLoader
.getIcon("/general/toolWindowStructure.png")));
49 ourId2Text
.put(ToolWindowId
.ANT_BUILD
, new MyDescriptor(IdeBundle
.message("action.toolwindow.ant.build"), IconLoader
.getIcon("/general/toolWindowAnt.png")));
50 ourId2Text
.put(ToolWindowId
.DEBUG
, new MyDescriptor(IdeBundle
.message("action.toolwindow.debug"), IconLoader
.getIcon("/general/toolWindowDebugger.png")));
51 ourId2Text
.put(ToolWindowId
.RUN
, new MyDescriptor(IdeBundle
.message("action.toolwindow.run"), IconLoader
.getIcon("/general/toolWindowRun.png")));
52 ourId2Text
.put(ToolWindowId
.FIND
, new MyDescriptor(IdeBundle
.message("action.toolwindow.find"), IconLoader
.getIcon("/general/toolWindowFind.png")));
53 ourId2Text
.put(ToolWindowId
.CVS
, new MyDescriptor(IdeBundle
.message("action.toolwindow.cvs"), IconLoader
.getIcon("/general/toolWindowCvs.png")));
54 ourId2Text
.put(ToolWindowId
.HIERARCHY
, new MyDescriptor(IdeBundle
.message("action.toolwindow.hierarchy"), IconLoader
.getIcon("/general/toolWindowHierarchy.png")));
55 ourId2Text
.put(ToolWindowId
.TODO_VIEW
, new MyDescriptor(IdeBundle
.message("action.toolwindow.todo"), IconLoader
.getIcon("/general/toolWindowTodo.png")));
56 ourId2Text
.put(ToolWindowId
.INSPECTION
, new MyDescriptor(IdeBundle
.message("action.toolwindow.inspection"), IconLoader
.getIcon("/general/toolWindowInspection.png")));
57 ourId2Text
.put(ToolWindowId
.FAVORITES_VIEW
, new MyDescriptor(IdeBundle
.message("action.toolwindow.favorites"), IconLoader
.getIcon("/general/toolWindowFavorites.png")));
60 private final ArrayList
<ActivateToolWindowAction
> myChildren
;
61 private final MyToolWindowManagerListener myToolWindowManagerListener
;
63 public ToolWindowsGroup(ProjectManager projectManager
){
64 myChildren
= new ArrayList
<ActivateToolWindowAction
>();
65 myToolWindowManagerListener
=new MyToolWindowManagerListener();
66 projectManager
.addProjectManagerListener(new MyProjectManagerListener());
69 public AnAction
[] getChildren(@Nullable AnActionEvent e
){
70 return myChildren
.toArray(new AnAction
[myChildren
.size()]);
74 * Registers action that activates tool window with specified <code>id</code>.
76 private void addActionForToolWindow(final String id
){
77 // Check that tool window with the same ID isn't already registered
78 for (final ActivateToolWindowAction action
: myChildren
) {
79 if (action
.getToolWindowId().equals(id
)) {
83 // Register an action for activating this tool window
84 final MyDescriptor descriptor
= ourId2Text
.get(id
);
86 final String text
= descriptor
!= null ? descriptor
.myText
: id
;
87 final Icon icon
= descriptor
!= null ? descriptor
.myIcon
: null;
89 ActivateToolWindowAction action
= new ActivateToolWindowAction(id
, text
, icon
);
90 ActionManager
.getInstance().registerAction(ActivateToolWindowAction
.getActionIdForToolWindow(id
),action
);
91 myChildren
.add(action
);
92 Collections
.sort(myChildren
,MyActionComparator
.ourInstance
);
95 private final class MyProjectManagerListener
extends ProjectManagerAdapter
{
96 public void projectClosed(Project project
){
97 final ToolWindowManagerEx windowManagerEx
= ToolWindowManagerEx
.getInstanceEx(project
);
98 if (windowManagerEx
== null) return;
99 windowManagerEx
.removeToolWindowManagerListener(myToolWindowManagerListener
);
102 public void projectOpened(Project project
){
103 final ToolWindowManagerEx toolWindowManager
=ToolWindowManagerEx
.getInstanceEx(project
);
104 if (toolWindowManager
== null) return; //headless environment
105 final String
[] ids
=toolWindowManager
.getToolWindowIds();
106 for (String id
: ids
) {
107 addActionForToolWindow(id
);
109 toolWindowManager
.addToolWindowManagerListener(myToolWindowManagerListener
);
113 private static final class MyActionComparator
implements Comparator
<ActivateToolWindowAction
> {
114 public static final MyActionComparator ourInstance
=new MyActionComparator();
116 private MyActionComparator(){}
118 public int compare(ActivateToolWindowAction action1
, ActivateToolWindowAction action2
){
119 int mnemonic1
=ActivateToolWindowAction
.getMnemonicForToolWindow(action1
.getToolWindowId());
120 int mnemonic2
=ActivateToolWindowAction
.getMnemonicForToolWindow(action2
.getToolWindowId());
122 if(mnemonic1
!=-1&&mnemonic2
==-1){
124 }else if(mnemonic1
==-1&&mnemonic2
!=-1){
126 }else if(mnemonic1
!=-1){
127 return mnemonic1
-mnemonic2
;
128 }else{ // Both actions have no mnemonic, therefore they are sorted alphabetically
129 return action1
.getToolWindowId().compareToIgnoreCase(action2
.getToolWindowId());
134 private final class MyToolWindowManagerListener
extends ToolWindowManagerAdapter
{
135 public void toolWindowRegistered(String id
){
136 addActionForToolWindow(id
);
140 private static final class MyDescriptor
{
141 public final String myText
;
142 public final Icon myIcon
;
144 public MyDescriptor(String text
, Icon icon
) {