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
.AnAction
;
19 import com
.intellij
.openapi
.actionSystem
.AnActionEvent
;
20 import com
.intellij
.openapi
.actionSystem
.PlatformDataKeys
;
21 import com
.intellij
.openapi
.project
.DumbAware
;
22 import com
.intellij
.openapi
.project
.Project
;
23 import com
.intellij
.openapi
.util
.registry
.Registry
;
24 import com
.intellij
.openapi
.wm
.*;
25 import com
.intellij
.openapi
.wm
.ex
.ToolWindowEx
;
26 import org
.jetbrains
.annotations
.Nullable
;
27 import sun
.swing
.SwingUtilities2
;
32 public abstract class ResizeToolWindowAction
extends AnAction
implements DumbAware
{
34 private ToolWindow myLastWindow
;
35 private ToolWindowManager myLastManager
;
37 protected JLabel myScrollHelper
= new JLabel("W");
40 public final void update(AnActionEvent e
) {
41 Project project
= PlatformDataKeys
.PROJECT
.getData(e
.getDataContext());
42 if (project
== null) {
47 ToolWindowManager mgr
= ToolWindowManager
.getInstance(project
);
49 String active
= mgr
.getActiveToolWindowId();
51 ToolWindow window
= mgr
.getToolWindow(active
);
53 if (!window
.isAvailable() || !window
.isVisible() || window
.getType() == ToolWindowType
.FLOATING
) {
58 update(e
, window
, mgr
);
59 if (e
.getPresentation().isEnabled()) {
60 myLastWindow
= window
;
72 private void setDisabled(AnActionEvent e
) {
73 e
.getPresentation().setEnabled(false);
78 protected abstract void update(AnActionEvent event
, ToolWindow window
, ToolWindowManager mgr
);
81 public final void actionPerformed(AnActionEvent e
) {
82 actionPerformed(e
, myLastWindow
, myLastManager
);
86 private ToolWindowScrollable
getScrollable(ToolWindow wnd
, boolean isHorizontalStretchingOffered
) {
87 KeyboardFocusManager mgr
= KeyboardFocusManager
.getCurrentKeyboardFocusManager();
89 Component eachComponent
= mgr
.getFocusOwner();
90 ToolWindowScrollable scrollable
= null;
91 while (eachComponent
!= null) {
92 if (!SwingUtilities
.isDescendingFrom(eachComponent
, wnd
.getComponent())) break;
94 if (eachComponent
instanceof ToolWindowScrollable
) {
95 ToolWindowScrollable eachScrollable
= (ToolWindowScrollable
)eachComponent
;
96 if (isHorizontalStretchingOffered
) {
97 if (eachScrollable
.isHorizontalScrollingNeeded()) {
98 scrollable
= eachScrollable
;
102 if (eachScrollable
.isVerticalScrollingNeeded()) {
103 scrollable
= eachScrollable
;
109 eachComponent
= eachComponent
.getParent();
112 if (scrollable
== null) {
113 scrollable
= new DefaultToolWindowScrollable();
116 if (isHorizontalStretchingOffered
&& scrollable
.isHorizontalScrollingNeeded()) return scrollable
;
117 if (!isHorizontalStretchingOffered
&& scrollable
.isVerticalScrollingNeeded()) return scrollable
;
122 protected abstract void actionPerformed(AnActionEvent e
, ToolWindow wnd
, ToolWindowManager mgr
);
124 protected void stretch(ToolWindow wnd
, boolean isHorizontalStretching
, boolean isIncrementAction
) {
125 ToolWindowScrollable scrollable
= getScrollable(wnd
, isHorizontalStretching
);
126 if (scrollable
== null) return;
128 ToolWindowAnchor anchor
= wnd
.getAnchor();
129 if (isHorizontalStretching
&& !anchor
.isHorizontal()) {
130 incWidth(wnd
, scrollable
.getNextHorizontalScroll(), (anchor
== ToolWindowAnchor
.LEFT
) == isIncrementAction
);
131 } else if (!isHorizontalStretching
&& anchor
.isHorizontal()) {
132 incHeight(wnd
, scrollable
.getNextVerticalScroll(), (anchor
== ToolWindowAnchor
.TOP
) != isIncrementAction
);
136 private void incWidth(ToolWindow wnd
, int value
, boolean isPositive
) {
137 ((ToolWindowEx
)wnd
).stretchWidth(isPositive ? value
: -value
);
140 private void incHeight(ToolWindow wnd
, int value
, boolean isPositive
) {
141 ((ToolWindowEx
)wnd
).stretchHeight(isPositive ? value
: -value
);
144 public static class Left
extends ResizeToolWindowAction
{
146 protected void update(AnActionEvent event
, ToolWindow window
, ToolWindowManager mgr
) {
147 event
.getPresentation().setEnabled(!window
.getAnchor().isHorizontal());
151 protected void actionPerformed(AnActionEvent e
, ToolWindow wnd
, ToolWindowManager mgr
) {
152 stretch(wnd
, true, false);
156 public static class Right
extends ResizeToolWindowAction
{
158 protected void update(AnActionEvent event
, ToolWindow window
, ToolWindowManager mgr
) {
159 event
.getPresentation().setEnabled(!window
.getAnchor().isHorizontal());
163 protected void actionPerformed(AnActionEvent e
, ToolWindow wnd
, ToolWindowManager mgr
) {
164 stretch(wnd
, true, true);
168 public static class Up
extends ResizeToolWindowAction
{
170 protected void update(AnActionEvent event
, ToolWindow window
, ToolWindowManager mgr
) {
171 event
.getPresentation().setEnabled(window
.getAnchor().isHorizontal());
175 protected void actionPerformed(AnActionEvent e
, ToolWindow wnd
, ToolWindowManager mgr
) {
176 stretch(wnd
, false, true);
180 public static class Down
extends ResizeToolWindowAction
{
182 protected void update(AnActionEvent event
, ToolWindow window
, ToolWindowManager mgr
) {
183 event
.getPresentation().setEnabled(window
.getAnchor().isHorizontal());
187 protected void actionPerformed(AnActionEvent e
, ToolWindow wnd
, ToolWindowManager mgr
) {
188 stretch(wnd
, false, false);
192 private class DefaultToolWindowScrollable
implements ToolWindowScrollable
{
194 public boolean isHorizontalScrollingNeeded() {
198 public int getNextHorizontalScroll() {
199 return myScrollHelper
.getPreferredSize().width
* Registry
.intValue("ide.windowSystem.hScrollChars");
202 public boolean isVerticalScrollingNeeded() {
206 public int getNextVerticalScroll() {
207 return myScrollHelper
.getPreferredSize().height
* Registry
.intValue("ide.windowSystem.vScrollChars");