2 * Copyright 2000-2010 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
.openapi
.actionSystem
.ActionManager
;
19 import com
.intellij
.openapi
.actionSystem
.AnAction
;
20 import com
.intellij
.openapi
.actionSystem
.AnActionEvent
;
21 import com
.intellij
.openapi
.actionSystem
.PlatformDataKeys
;
22 import com
.intellij
.openapi
.project
.DumbAware
;
23 import com
.intellij
.openapi
.project
.Project
;
24 import com
.intellij
.openapi
.ui
.ShadowAction
;
25 import com
.intellij
.openapi
.util
.registry
.Registry
;
26 import com
.intellij
.openapi
.wm
.*;
27 import com
.intellij
.openapi
.wm
.ex
.ToolWindowEx
;
28 import org
.jetbrains
.annotations
.Nullable
;
33 public abstract class ResizeToolWindowAction
extends AnAction
implements DumbAware
{
35 private ToolWindow myLastWindow
;
36 private ToolWindowManager myLastManager
;
38 protected JLabel myScrollHelper
;
40 private ToolWindow myToolWindow
;
42 protected ResizeToolWindowAction() {
45 protected ResizeToolWindowAction(String text
) {
49 protected ResizeToolWindowAction(String text
, String description
, Icon icon
) {
50 super(text
, description
, icon
);
53 protected ResizeToolWindowAction(ToolWindow toolWindow
, String originalAction
, JComponent c
) {
54 myToolWindow
= toolWindow
;
55 new ShadowAction(this, ActionManager
.getInstance().getAction(originalAction
), c
);
59 public final void update(AnActionEvent e
) {
60 Project project
= PlatformDataKeys
.PROJECT
.getData(e
.getDataContext());
61 if (project
== null) {
66 Component owner
= KeyboardFocusManager
.getCurrentKeyboardFocusManager().getFocusOwner();
67 if (owner
== null || !(SwingUtilities
.getWindowAncestor(owner
) instanceof IdeFrame
)) {
73 ToolWindowManager mgr
= ToolWindowManager
.getInstance(project
);
75 ToolWindow window
= myToolWindow
;
77 if (window
!= null || mgr
.getActiveToolWindowId() != null) {
79 window
= mgr
.getToolWindow(mgr
.getActiveToolWindowId());
82 if (window
== null || !window
.isAvailable() || !window
.isVisible() || window
.getType() == ToolWindowType
.FLOATING
) {
87 update(e
, window
, mgr
);
88 if (e
.getPresentation().isEnabled()) {
89 myLastWindow
= window
;
101 private void setDisabled(AnActionEvent e
) {
102 e
.getPresentation().setEnabled(false);
104 myLastManager
= null;
107 protected abstract void update(AnActionEvent event
, ToolWindow window
, ToolWindowManager mgr
);
110 public final void actionPerformed(AnActionEvent e
) {
111 actionPerformed(e
, myLastWindow
, myLastManager
);
115 private ToolWindowScrollable
getScrollable(ToolWindow wnd
, boolean isHorizontalStretchingOffered
) {
116 KeyboardFocusManager mgr
= KeyboardFocusManager
.getCurrentKeyboardFocusManager();
118 Component eachComponent
= mgr
.getFocusOwner();
119 ToolWindowScrollable scrollable
= null;
120 while (eachComponent
!= null) {
121 if (!SwingUtilities
.isDescendingFrom(eachComponent
, wnd
.getComponent())) break;
123 if (eachComponent
instanceof ToolWindowScrollable
) {
124 ToolWindowScrollable eachScrollable
= (ToolWindowScrollable
)eachComponent
;
125 if (isHorizontalStretchingOffered
) {
126 if (eachScrollable
.isHorizontalScrollingNeeded()) {
127 scrollable
= eachScrollable
;
131 if (eachScrollable
.isVerticalScrollingNeeded()) {
132 scrollable
= eachScrollable
;
138 eachComponent
= eachComponent
.getParent();
141 if (scrollable
== null) {
142 scrollable
= new DefaultToolWindowScrollable();
145 if (isHorizontalStretchingOffered
&& scrollable
.isHorizontalScrollingNeeded()) return scrollable
;
146 if (!isHorizontalStretchingOffered
&& scrollable
.isVerticalScrollingNeeded()) return scrollable
;
151 protected abstract void actionPerformed(AnActionEvent e
, ToolWindow wnd
, ToolWindowManager mgr
);
153 protected void stretch(ToolWindow wnd
, boolean isHorizontalStretching
, boolean isIncrementAction
) {
154 ToolWindowScrollable scrollable
= getScrollable(wnd
, isHorizontalStretching
);
155 if (scrollable
== null) return;
157 ToolWindowAnchor anchor
= wnd
.getAnchor();
158 if (isHorizontalStretching
&& !anchor
.isHorizontal()) {
159 incWidth(wnd
, scrollable
.getNextHorizontalScroll(), (anchor
== ToolWindowAnchor
.LEFT
) == isIncrementAction
);
160 } else if (!isHorizontalStretching
&& anchor
.isHorizontal()) {
161 incHeight(wnd
, scrollable
.getNextVerticalScroll(), (anchor
== ToolWindowAnchor
.TOP
) != isIncrementAction
);
165 private void incWidth(ToolWindow wnd
, int value
, boolean isPositive
) {
166 ((ToolWindowEx
)wnd
).stretchWidth(isPositive ? value
: -value
);
169 private void incHeight(ToolWindow wnd
, int value
, boolean isPositive
) {
170 ((ToolWindowEx
)wnd
).stretchHeight(isPositive ? value
: -value
);
173 public static class Left
extends ResizeToolWindowAction
{
178 public Left(String text
) {
182 public Left(String text
, String description
, Icon icon
) {
183 super(text
, description
, icon
);
186 public Left(ToolWindow toolWindow
, JComponent c
) {
187 super(toolWindow
, "ResizeToolWindowLeft", c
);
191 protected void update(AnActionEvent event
, ToolWindow window
, ToolWindowManager mgr
) {
192 event
.getPresentation().setEnabled(!window
.getAnchor().isHorizontal());
196 protected void actionPerformed(AnActionEvent e
, ToolWindow wnd
, ToolWindowManager mgr
) {
197 stretch(wnd
, true, false);
201 public static class Right
extends ResizeToolWindowAction
{
206 public Right(String text
) {
210 public Right(String text
, String description
, Icon icon
) {
211 super(text
, description
, icon
);
214 public Right(ToolWindow toolWindow
, JComponent c
) {
215 super(toolWindow
, "ResizeToolWindowRight", c
);
219 protected void update(AnActionEvent event
, ToolWindow window
, ToolWindowManager mgr
) {
220 event
.getPresentation().setEnabled(!window
.getAnchor().isHorizontal());
224 protected void actionPerformed(AnActionEvent e
, ToolWindow wnd
, ToolWindowManager mgr
) {
225 stretch(wnd
, true, true);
229 public static class Up
extends ResizeToolWindowAction
{
234 public Up(String text
) {
238 public Up(String text
, String description
, Icon icon
) {
239 super(text
, description
, icon
);
242 public Up(ToolWindow toolWindow
, JComponent c
) {
243 super(toolWindow
, "ResizeToolWindowUp", c
);
247 protected void update(AnActionEvent event
, ToolWindow window
, ToolWindowManager mgr
) {
248 event
.getPresentation().setEnabled(window
.getAnchor().isHorizontal());
252 protected void actionPerformed(AnActionEvent e
, ToolWindow wnd
, ToolWindowManager mgr
) {
253 stretch(wnd
, false, true);
257 public static class Down
extends ResizeToolWindowAction
{
262 public Down(String text
) {
266 public Down(String text
, String description
, Icon icon
) {
267 super(text
, description
, icon
);
270 public Down(ToolWindow toolWindow
, JComponent c
) {
271 super(toolWindow
, "ResizeToolWindowDown", c
);
275 protected void update(AnActionEvent event
, ToolWindow window
, ToolWindowManager mgr
) {
276 event
.getPresentation().setEnabled(window
.getAnchor().isHorizontal());
280 protected void actionPerformed(AnActionEvent e
, ToolWindow wnd
, ToolWindowManager mgr
) {
281 stretch(wnd
, false, false);
285 private class DefaultToolWindowScrollable
implements ToolWindowScrollable
{
287 public boolean isHorizontalScrollingNeeded() {
291 public int getNextHorizontalScroll() {
292 return getReferenceSize().width
* Registry
.intValue("ide.windowSystem.hScrollChars");
295 public boolean isVerticalScrollingNeeded() {
299 public int getNextVerticalScroll() {
300 return getReferenceSize().height
* Registry
.intValue("ide.windowSystem.vScrollChars");
304 private Dimension
getReferenceSize() {
305 if (myScrollHelper
== null) {
306 if (SwingUtilities
.isEventDispatchThread()) {
307 myScrollHelper
= new JLabel("W");
309 return new Dimension(1, 1);
313 return myScrollHelper
.getPreferredSize();