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
.palette
.impl
;
19 import com
.intellij
.ide
.IdeBundle
;
20 import com
.intellij
.ide
.palette
.PaletteDragEventListener
;
21 import com
.intellij
.ide
.palette
.PaletteItem
;
22 import com
.intellij
.openapi
.components
.ProjectComponent
;
23 import com
.intellij
.openapi
.fileEditor
.FileEditorManager
;
24 import com
.intellij
.openapi
.fileEditor
.FileEditorManagerEvent
;
25 import com
.intellij
.openapi
.fileEditor
.FileEditorManagerListener
;
26 import com
.intellij
.openapi
.project
.Project
;
27 import com
.intellij
.openapi
.startup
.StartupManager
;
28 import com
.intellij
.openapi
.util
.IconLoader
;
29 import com
.intellij
.openapi
.vfs
.VirtualFile
;
30 import com
.intellij
.openapi
.wm
.ToolWindow
;
31 import com
.intellij
.openapi
.wm
.ToolWindowAnchor
;
32 import com
.intellij
.openapi
.wm
.ToolWindowManager
;
33 import com
.intellij
.util
.containers
.ContainerUtil
;
34 import com
.intellij
.util
.ui
.update
.MergingUpdateQueue
;
35 import com
.intellij
.util
.ui
.update
.Update
;
36 import org
.jetbrains
.annotations
.NotNull
;
37 import org
.jetbrains
.annotations
.Nullable
;
39 import javax
.swing
.event
.ListSelectionEvent
;
40 import javax
.swing
.event
.ListSelectionListener
;
41 import java
.awt
.event
.KeyEvent
;
42 import java
.awt
.event
.KeyListener
;
43 import java
.util
.List
;
48 public class PaletteManager
implements ProjectComponent
{
49 private final Project myProject
;
50 private final FileEditorManager myFileEditorManager
;
51 private PaletteWindow myPaletteWindow
;
52 private ToolWindow myPaletteToolWindow
;
53 private final List
<KeyListener
> myKeyListeners
= ContainerUtil
.createEmptyCOWList();
54 private final List
<PaletteDragEventListener
> myDragEventListeners
= ContainerUtil
.createEmptyCOWList();
55 private final List
<ListSelectionListener
> mySelectionListeners
= ContainerUtil
.createEmptyCOWList();
57 public PaletteManager(Project project
, FileEditorManager fileEditorManager
) {
59 myFileEditorManager
= fileEditorManager
;
62 public void projectOpened() {
63 StartupManager
.getInstance(myProject
).registerPostStartupActivity(new Runnable() {
65 myPaletteWindow
= new PaletteWindow(myProject
);
66 myPaletteToolWindow
= ToolWindowManager
.getInstance(myProject
).registerToolWindow(IdeBundle
.message("toolwindow.palette"),
68 ToolWindowAnchor
.RIGHT
);
69 myPaletteToolWindow
.setIcon(IconLoader
.getIcon("/general/toolWindowPalette.png"));
70 myPaletteToolWindow
.setAvailable(false, null);
71 final MyFileEditorManagerListener myListener
= new MyFileEditorManagerListener();
72 myFileEditorManager
.addFileEditorManagerListener(myListener
, myProject
);
77 public void projectClosed() {
78 if (myPaletteWindow
!= null) {
79 ToolWindowManager
.getInstance(myProject
).unregisterToolWindow(IdeBundle
.message("toolwindow.palette"));
80 myPaletteWindow
= null;
85 public String
getComponentName() {
86 return "PaletteManager";
89 public void initComponent() {
92 public void disposeComponent() {
95 public static PaletteManager
getInstance(final Project project
) {
96 return project
.getComponent(PaletteManager
.class);
99 public void clearActiveItem() {
100 if (myPaletteWindow
!= null) {
101 myPaletteWindow
.clearActiveItem();
106 public PaletteItem
getActiveItem() {
107 if (myPaletteWindow
!= null) {
108 return myPaletteWindow
.getActiveItem();
114 public <T
extends PaletteItem
> T
getActiveItem(Class
<T
> cls
) {
115 PaletteItem item
= getActiveItem();
116 if (item
!= null && item
.getClass().isInstance(item
)) {
117 //noinspection unchecked
123 public void addKeyListener(KeyListener l
) {
124 myKeyListeners
.add(l
);
127 public void removeKeyListener(KeyListener l
) {
128 myKeyListeners
.remove(l
);
131 public void addDragEventListener(PaletteDragEventListener l
) {
132 myDragEventListeners
.add(l
);
135 public void removeDragEventListener(PaletteDragEventListener l
) {
136 myDragEventListeners
.remove(l
);
139 public void addSelectionListener(ListSelectionListener l
) {
140 mySelectionListeners
.add(l
);
143 public void removeSelectionListener(ListSelectionListener l
) {
144 mySelectionListeners
.remove(l
);
147 private MergingUpdateQueue myQueue
= new MergingUpdateQueue("palette", 200, true, null);
149 private void processFileEditorChange(@Nullable final VirtualFile selectedFile
) {
151 myQueue
.cancelAllUpdates();
152 myQueue
.queue(new Update("update") {
154 if (myPaletteWindow
== null) return;
155 myPaletteWindow
.refreshPaletteIfChanged(selectedFile
);
156 if (myPaletteWindow
.getActiveGroupCount() == 0) {
157 myPaletteToolWindow
.setAvailable(false, null);
160 myPaletteToolWindow
.setAvailable(true, null);
161 myPaletteToolWindow
.show(null);
167 void notifyKeyEvent(final KeyEvent e
) {
168 for(KeyListener l
: myKeyListeners
) {
169 if (e
.getID() == KeyEvent
.KEY_PRESSED
) {
172 else if (e
.getID() == KeyEvent
.KEY_RELEASED
) {
175 else if (e
.getID() == KeyEvent
.KEY_TYPED
) {
181 void notifyDropActionChanged(int gestureModifiers
) {
182 for(PaletteDragEventListener l
: myDragEventListeners
) {
183 l
.dropActionChanged(gestureModifiers
);
187 void notifySelectionChanged(final ListSelectionEvent event
) {
188 for(ListSelectionListener l
: mySelectionListeners
) {
189 l
.valueChanged(event
);
193 private class MyFileEditorManagerListener
implements FileEditorManagerListener
{
194 public void fileOpened(FileEditorManager source
, VirtualFile file
) {
195 processFileEditorChange(file
);
198 public void fileClosed(FileEditorManager source
, VirtualFile file
) {
199 processFileEditorChange(null);
202 public void selectionChanged(FileEditorManagerEvent event
) {
203 processFileEditorChange(event
.getNewFile());