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
.palette
.impl
;
18 import com
.intellij
.ide
.dnd
.DnDEvent
;
19 import com
.intellij
.ide
.dnd
.DnDManager
;
20 import com
.intellij
.ide
.dnd
.DnDTarget
;
21 import com
.intellij
.ide
.palette
.PaletteGroup
;
22 import com
.intellij
.ide
.palette
.PaletteItem
;
23 import com
.intellij
.openapi
.actionSystem
.*;
24 import com
.intellij
.openapi
.project
.Project
;
25 import com
.intellij
.ui
.PopupHandler
;
26 import com
.intellij
.util
.ui
.UIUtil
;
27 import org
.jetbrains
.annotations
.NonNls
;
28 import org
.jetbrains
.annotations
.Nullable
;
31 import javax
.swing
.border
.CompoundBorder
;
33 import java
.awt
.event
.*;
38 public class PaletteGroupHeader
extends JCheckBox
implements DataProvider
{
39 private final PaletteWindow myPaletteWindow
;
40 private PaletteComponentList myComponentList
;
41 private final PaletteGroup myGroup
;
43 public PaletteGroupHeader(PaletteWindow paletteWindow
, PaletteGroup group
) {
44 myPaletteWindow
= paletteWindow
;
46 if (group
.getName() == null) {
50 setText(group
.getName());
53 addActionListener(new ActionListener() {
54 public void actionPerformed(ActionEvent e
) {
55 if (myComponentList
!= null) {
56 myComponentList
.setVisible(isSelected());
61 addMouseListener(new PopupHandler() {
62 public void invokePopup(Component comp
, int x
, int y
) {
63 myPaletteWindow
.setLastFocusedGroup(PaletteGroupHeader
.this);
64 showGroupPopupMenu(comp
, x
, y
);
68 setIcon(UIUtil
.getTreeCollapsedIcon());
69 setSelectedIcon(UIUtil
.getTreeExpandedIcon());
70 setFont(getFont().deriveFont(Font
.BOLD
));
71 setFocusPainted(false);
72 setMargin(new Insets(0, 3, 0, 3));
73 if (getBorder() instanceof CompoundBorder
) { // from BasicLookAndFeel
74 Dimension pref
= getPreferredSize();
76 setPreferredSize(pref
);
79 DnDManager
.getInstance().registerTarget(new DnDTarget() {
80 public boolean update(DnDEvent aEvent
) {
81 setBorderPainted(true);
82 aEvent
.setDropPossible(aEvent
.getAttachedObject() instanceof PaletteItem
, null);
86 public void drop(DnDEvent aEvent
) {
87 setBorderPainted(false);
88 if (aEvent
.getAttachedObject() instanceof PaletteItem
) {
89 myGroup
.handleDrop(myPaletteWindow
.getProject(), (PaletteItem
) aEvent
.getAttachedObject(), -1);
93 public void cleanUpOnLeave() {
94 setBorderPainted(false);
97 public void updateDraggedImage(Image image
, Point dropPoint
, Point imageOffset
) {
101 addFocusListener(new FocusAdapter() {
102 @Override public void focusGained(FocusEvent e
) {
103 myPaletteWindow
.setLastFocusedGroup(PaletteGroupHeader
.this);
110 public void showGroupPopupMenu(final Component comp
, final int x
, final int y
) {
111 ActionGroup group
= myGroup
.getPopupActionGroup();
113 ActionPopupMenu popupMenu
= ActionManager
.getInstance().createActionPopupMenu(ActionPlaces
.UNKNOWN
, group
);
114 popupMenu
.getComponent().show(comp
, x
, y
);
118 private void initActions() {
119 @NonNls InputMap inputMap
= getInputMap(WHEN_FOCUSED
);
120 inputMap
.put(KeyStroke
.getKeyStroke(KeyEvent
.VK_DOWN
, 0, false), "moveFocusDown");
121 inputMap
.put(KeyStroke
.getKeyStroke(KeyEvent
.VK_UP
, 0, false), "moveFocusUp");
122 inputMap
.put(KeyStroke
.getKeyStroke(KeyEvent
.VK_LEFT
, 0, false), "collapse");
123 inputMap
.put(KeyStroke
.getKeyStroke(KeyEvent
.VK_RIGHT
, 0, false), "expand");
125 @NonNls ActionMap actionMap
= getActionMap();
126 actionMap
.put("moveFocusDown", new MoveFocusAction(true));
127 actionMap
.put("moveFocusUp", new MoveFocusAction(false));
128 actionMap
.put("collapse", new ExpandAction(false));
129 actionMap
.put("expand", new ExpandAction(true));
132 @Override public Color
getBackground() {
133 if (isFocusOwner()) {
134 return UIUtil
.getListSelectionBackground();
136 return super.getBackground();
139 @Override public Color
getForeground() {
140 if (isFocusOwner()) {
141 return UIUtil
.getListSelectionForeground();
143 return super.getForeground();
146 public void setComponentList(final PaletteComponentList componentList
) {
147 myComponentList
= componentList
;
150 public PaletteComponentList
getComponentList() {
151 return myComponentList
;
154 public PaletteGroup
getGroup() {
158 @Nullable public Object
getData(String dataId
) {
159 Object data
= myPaletteWindow
.getData(dataId
);
160 if (data
!= null) return data
;
161 Project project
= (Project
)myPaletteWindow
.getData(DataConstants
.PROJECT
);
162 return myGroup
.getData(project
, dataId
);
165 private class MoveFocusAction
extends AbstractAction
{
166 private final boolean moveDown
;
168 public MoveFocusAction(boolean moveDown
) {
169 this.moveDown
= moveDown
;
172 public void actionPerformed(ActionEvent e
) {
173 KeyboardFocusManager kfm
= KeyboardFocusManager
.getCurrentKeyboardFocusManager();
174 Container container
= kfm
.getCurrentFocusCycleRoot();
175 FocusTraversalPolicy policy
= container
.getFocusTraversalPolicy();
176 if (null == policy
) policy
= kfm
.getDefaultFocusTraversalPolicy();
178 moveDown ? policy
.getComponentAfter(container
, PaletteGroupHeader
.this) : policy
.getComponentBefore(container
, PaletteGroupHeader
.this);
179 if (null != next
&& next
instanceof PaletteComponentList
) {
180 final PaletteComponentList list
= (PaletteComponentList
)next
;
181 if (list
.getModel().getSize() != 0) {
182 list
.takeFocusFrom(PaletteGroupHeader
.this, list
== myComponentList ?
0 : -1);
186 next
= moveDown ? policy
.getComponentAfter(container
, next
) : policy
.getComponentBefore(container
, next
);
189 if (null != next
&& next
instanceof PaletteGroupHeader
) {
195 private class ExpandAction
extends AbstractAction
{
196 private final boolean expand
;
198 public ExpandAction(boolean expand
) {
199 this.expand
= expand
;
202 public void actionPerformed(ActionEvent e
) {
203 if (expand
== isSelected()) return;
205 if (myComponentList
!= null) {
206 myComponentList
.setVisible(isSelected());