toolwindow resize for docked windows
[fedora-idea.git] / platform / platform-impl / src / com / intellij / ide / actions / ResizeToolWindowAction.java
blobe6ad23eed16740f06d6f0e047b8f2280bf6e63e8
1 /*
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;
29 import javax.swing.*;
30 import java.awt.*;
32 public abstract class ResizeToolWindowAction extends AnAction implements DumbAware {
34 private ToolWindow myLastWindow;
35 private ToolWindowManager myLastManager;
37 protected JLabel myScrollHelper = new JLabel("W");
39 @Override
40 public final void update(AnActionEvent e) {
41 Project project = PlatformDataKeys.PROJECT.getData(e.getDataContext());
42 if (project == null) {
43 setDisabled(e);
44 return;
47 ToolWindowManager mgr = ToolWindowManager.getInstance(project);
49 String active = mgr.getActiveToolWindowId();
50 if (active != null) {
51 ToolWindow window = mgr.getToolWindow(active);
53 if (!window.isAvailable() || !window.isVisible() || window.getType() == ToolWindowType.FLOATING) {
54 setDisabled(e);
55 return;
58 update(e, window, mgr);
59 if (e.getPresentation().isEnabled()) {
60 myLastWindow = window;
61 myLastManager = mgr;
63 else {
64 setDisabled(e);
67 else {
68 setDisabled(e);
72 private void setDisabled(AnActionEvent e) {
73 e.getPresentation().setEnabled(false);
74 myLastWindow = null;
75 myLastManager = null;
78 protected abstract void update(AnActionEvent event, ToolWindow window, ToolWindowManager mgr);
80 @Override
81 public final void actionPerformed(AnActionEvent e) {
82 actionPerformed(e, myLastWindow, myLastManager);
85 @Nullable
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;
99 break;
101 } else {
102 if (eachScrollable.isVerticalScrollingNeeded()) {
103 scrollable = eachScrollable;
104 break;
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;
119 return null;
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 {
145 @Override
146 protected void update(AnActionEvent event, ToolWindow window, ToolWindowManager mgr) {
147 event.getPresentation().setEnabled(!window.getAnchor().isHorizontal());
150 @Override
151 protected void actionPerformed(AnActionEvent e, ToolWindow wnd, ToolWindowManager mgr) {
152 stretch(wnd, true, false);
156 public static class Right extends ResizeToolWindowAction {
157 @Override
158 protected void update(AnActionEvent event, ToolWindow window, ToolWindowManager mgr) {
159 event.getPresentation().setEnabled(!window.getAnchor().isHorizontal());
162 @Override
163 protected void actionPerformed(AnActionEvent e, ToolWindow wnd, ToolWindowManager mgr) {
164 stretch(wnd, true, true);
168 public static class Up extends ResizeToolWindowAction {
169 @Override
170 protected void update(AnActionEvent event, ToolWindow window, ToolWindowManager mgr) {
171 event.getPresentation().setEnabled(window.getAnchor().isHorizontal());
174 @Override
175 protected void actionPerformed(AnActionEvent e, ToolWindow wnd, ToolWindowManager mgr) {
176 stretch(wnd, false, true);
180 public static class Down extends ResizeToolWindowAction {
181 @Override
182 protected void update(AnActionEvent event, ToolWindow window, ToolWindowManager mgr) {
183 event.getPresentation().setEnabled(window.getAnchor().isHorizontal());
186 @Override
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() {
195 return true;
198 public int getNextHorizontalScroll() {
199 return myScrollHelper.getPreferredSize().width * Registry.intValue("ide.windowSystem.hScrollChars");
202 public boolean isVerticalScrollingNeeded() {
203 return true;
206 public int getNextVerticalScroll() {
207 return myScrollHelper.getPreferredSize().height * Registry.intValue("ide.windowSystem.vScrollChars");