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
= new JLabel("W");
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 ToolWindowManager mgr
= ToolWindowManager
.getInstance(project
);
68 ToolWindow window
= myToolWindow
;
70 if (window
!= null || mgr
.getActiveToolWindowId() != null) {
72 window
= mgr
.getToolWindow(mgr
.getActiveToolWindowId());
75 if (window
== null || !window
.isAvailable() || !window
.isVisible() || window
.getType() == ToolWindowType
.FLOATING
) {
80 update(e
, window
, mgr
);
81 if (e
.getPresentation().isEnabled()) {
82 myLastWindow
= window
;
94 private void setDisabled(AnActionEvent e
) {
95 e
.getPresentation().setEnabled(false);
100 protected abstract void update(AnActionEvent event
, ToolWindow window
, ToolWindowManager mgr
);
103 public final void actionPerformed(AnActionEvent e
) {
104 actionPerformed(e
, myLastWindow
, myLastManager
);
108 private ToolWindowScrollable
getScrollable(ToolWindow wnd
, boolean isHorizontalStretchingOffered
) {
109 KeyboardFocusManager mgr
= KeyboardFocusManager
.getCurrentKeyboardFocusManager();
111 Component eachComponent
= mgr
.getFocusOwner();
112 ToolWindowScrollable scrollable
= null;
113 while (eachComponent
!= null) {
114 if (!SwingUtilities
.isDescendingFrom(eachComponent
, wnd
.getComponent())) break;
116 if (eachComponent
instanceof ToolWindowScrollable
) {
117 ToolWindowScrollable eachScrollable
= (ToolWindowScrollable
)eachComponent
;
118 if (isHorizontalStretchingOffered
) {
119 if (eachScrollable
.isHorizontalScrollingNeeded()) {
120 scrollable
= eachScrollable
;
124 if (eachScrollable
.isVerticalScrollingNeeded()) {
125 scrollable
= eachScrollable
;
131 eachComponent
= eachComponent
.getParent();
134 if (scrollable
== null) {
135 scrollable
= new DefaultToolWindowScrollable();
138 if (isHorizontalStretchingOffered
&& scrollable
.isHorizontalScrollingNeeded()) return scrollable
;
139 if (!isHorizontalStretchingOffered
&& scrollable
.isVerticalScrollingNeeded()) return scrollable
;
144 protected abstract void actionPerformed(AnActionEvent e
, ToolWindow wnd
, ToolWindowManager mgr
);
146 protected void stretch(ToolWindow wnd
, boolean isHorizontalStretching
, boolean isIncrementAction
) {
147 ToolWindowScrollable scrollable
= getScrollable(wnd
, isHorizontalStretching
);
148 if (scrollable
== null) return;
150 ToolWindowAnchor anchor
= wnd
.getAnchor();
151 if (isHorizontalStretching
&& !anchor
.isHorizontal()) {
152 incWidth(wnd
, scrollable
.getNextHorizontalScroll(), (anchor
== ToolWindowAnchor
.LEFT
) == isIncrementAction
);
153 } else if (!isHorizontalStretching
&& anchor
.isHorizontal()) {
154 incHeight(wnd
, scrollable
.getNextVerticalScroll(), (anchor
== ToolWindowAnchor
.TOP
) != isIncrementAction
);
158 private void incWidth(ToolWindow wnd
, int value
, boolean isPositive
) {
159 ((ToolWindowEx
)wnd
).stretchWidth(isPositive ? value
: -value
);
162 private void incHeight(ToolWindow wnd
, int value
, boolean isPositive
) {
163 ((ToolWindowEx
)wnd
).stretchHeight(isPositive ? value
: -value
);
166 public static class Left
extends ResizeToolWindowAction
{
171 public Left(String text
) {
175 public Left(String text
, String description
, Icon icon
) {
176 super(text
, description
, icon
);
179 public Left(ToolWindow toolWindow
, JComponent c
) {
180 super(toolWindow
, "ResizeToolWindowLeft", c
);
184 protected void update(AnActionEvent event
, ToolWindow window
, ToolWindowManager mgr
) {
185 event
.getPresentation().setEnabled(!window
.getAnchor().isHorizontal());
189 protected void actionPerformed(AnActionEvent e
, ToolWindow wnd
, ToolWindowManager mgr
) {
190 stretch(wnd
, true, false);
194 public static class Right
extends ResizeToolWindowAction
{
199 public Right(String text
) {
203 public Right(String text
, String description
, Icon icon
) {
204 super(text
, description
, icon
);
207 public Right(ToolWindow toolWindow
, JComponent c
) {
208 super(toolWindow
, "ResizeToolWindowRight", c
);
212 protected void update(AnActionEvent event
, ToolWindow window
, ToolWindowManager mgr
) {
213 event
.getPresentation().setEnabled(!window
.getAnchor().isHorizontal());
217 protected void actionPerformed(AnActionEvent e
, ToolWindow wnd
, ToolWindowManager mgr
) {
218 stretch(wnd
, true, true);
222 public static class Up
extends ResizeToolWindowAction
{
227 public Up(String text
) {
231 public Up(String text
, String description
, Icon icon
) {
232 super(text
, description
, icon
);
235 public Up(ToolWindow toolWindow
, JComponent c
) {
236 super(toolWindow
, "ResizeToolWindowUp", c
);
240 protected void update(AnActionEvent event
, ToolWindow window
, ToolWindowManager mgr
) {
241 event
.getPresentation().setEnabled(window
.getAnchor().isHorizontal());
245 protected void actionPerformed(AnActionEvent e
, ToolWindow wnd
, ToolWindowManager mgr
) {
246 stretch(wnd
, false, true);
250 public static class Down
extends ResizeToolWindowAction
{
255 public Down(String text
) {
259 public Down(String text
, String description
, Icon icon
) {
260 super(text
, description
, icon
);
263 public Down(ToolWindow toolWindow
, JComponent c
) {
264 super(toolWindow
, "ResizeToolWindowDown", c
);
268 protected void update(AnActionEvent event
, ToolWindow window
, ToolWindowManager mgr
) {
269 event
.getPresentation().setEnabled(window
.getAnchor().isHorizontal());
273 protected void actionPerformed(AnActionEvent e
, ToolWindow wnd
, ToolWindowManager mgr
) {
274 stretch(wnd
, false, false);
278 private class DefaultToolWindowScrollable
implements ToolWindowScrollable
{
280 public boolean isHorizontalScrollingNeeded() {
284 public int getNextHorizontalScroll() {
285 return myScrollHelper
.getPreferredSize().width
* Registry
.intValue("ide.windowSystem.hScrollChars");
288 public boolean isVerticalScrollingNeeded() {
292 public int getNextVerticalScroll() {
293 return myScrollHelper
.getPreferredSize().height
* Registry
.intValue("ide.windowSystem.vScrollChars");