toolwindows: stretching for undocked mode
[fedora-idea.git] / platform / platform-impl / src / com / intellij / ide / actions / ResizeToolWindowAction.java
blob7c3a0a87f1976f4925196e1777ab48c4be5e708b
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.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;
30 import javax.swing.*;
31 import java.awt.*;
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) {
46 super(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);
58 @Override
59 public final void update(AnActionEvent e) {
60 Project project = PlatformDataKeys.PROJECT.getData(e.getDataContext());
61 if (project == null) {
62 setDisabled(e);
63 return;
66 ToolWindowManager mgr = ToolWindowManager.getInstance(project);
68 ToolWindow window = myToolWindow;
70 if (window != null || mgr.getActiveToolWindowId() != null) {
71 if (window == null) {
72 window = mgr.getToolWindow(mgr.getActiveToolWindowId());
75 if (window == null || !window.isAvailable() || !window.isVisible() || window.getType() == ToolWindowType.FLOATING) {
76 setDisabled(e);
77 return;
80 update(e, window, mgr);
81 if (e.getPresentation().isEnabled()) {
82 myLastWindow = window;
83 myLastManager = mgr;
85 else {
86 setDisabled(e);
89 else {
90 setDisabled(e);
94 private void setDisabled(AnActionEvent e) {
95 e.getPresentation().setEnabled(false);
96 myLastWindow = null;
97 myLastManager = null;
100 protected abstract void update(AnActionEvent event, ToolWindow window, ToolWindowManager mgr);
102 @Override
103 public final void actionPerformed(AnActionEvent e) {
104 actionPerformed(e, myLastWindow, myLastManager);
107 @Nullable
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;
121 break;
123 } else {
124 if (eachScrollable.isVerticalScrollingNeeded()) {
125 scrollable = eachScrollable;
126 break;
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;
141 return null;
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 {
168 public Left() {
171 public Left(String text) {
172 super(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);
183 @Override
184 protected void update(AnActionEvent event, ToolWindow window, ToolWindowManager mgr) {
185 event.getPresentation().setEnabled(!window.getAnchor().isHorizontal());
188 @Override
189 protected void actionPerformed(AnActionEvent e, ToolWindow wnd, ToolWindowManager mgr) {
190 stretch(wnd, true, false);
194 public static class Right extends ResizeToolWindowAction {
196 public Right() {
199 public Right(String text) {
200 super(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);
211 @Override
212 protected void update(AnActionEvent event, ToolWindow window, ToolWindowManager mgr) {
213 event.getPresentation().setEnabled(!window.getAnchor().isHorizontal());
216 @Override
217 protected void actionPerformed(AnActionEvent e, ToolWindow wnd, ToolWindowManager mgr) {
218 stretch(wnd, true, true);
222 public static class Up extends ResizeToolWindowAction {
224 public Up() {
227 public Up(String text) {
228 super(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);
239 @Override
240 protected void update(AnActionEvent event, ToolWindow window, ToolWindowManager mgr) {
241 event.getPresentation().setEnabled(window.getAnchor().isHorizontal());
244 @Override
245 protected void actionPerformed(AnActionEvent e, ToolWindow wnd, ToolWindowManager mgr) {
246 stretch(wnd, false, true);
250 public static class Down extends ResizeToolWindowAction {
252 public Down() {
255 public Down(String text) {
256 super(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);
267 @Override
268 protected void update(AnActionEvent event, ToolWindow window, ToolWindowManager mgr) {
269 event.getPresentation().setEnabled(window.getAnchor().isHorizontal());
272 @Override
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() {
281 return true;
284 public int getNextHorizontalScroll() {
285 return myScrollHelper.getPreferredSize().width * Registry.intValue("ide.windowSystem.hScrollChars");
288 public boolean isVerticalScrollingNeeded() {
289 return true;
292 public int getNextVerticalScroll() {
293 return myScrollHelper.getPreferredSize().height * Registry.intValue("ide.windowSystem.vScrollChars");