ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / platform / platform-impl / src / com / intellij / ide / actions / ResizeToolWindowAction.java
blob6062ff1b4b51e69fbafe803ab717b791024c6715
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;
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 Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
67 if (owner == null || !(SwingUtilities.getWindowAncestor(owner) instanceof IdeFrame)) {
68 setDisabled(e);
69 return;
73 ToolWindowManager mgr = ToolWindowManager.getInstance(project);
75 ToolWindow window = myToolWindow;
77 if (window != null || mgr.getActiveToolWindowId() != null) {
78 if (window == null) {
79 window = mgr.getToolWindow(mgr.getActiveToolWindowId());
82 if (window == null || !window.isAvailable() || !window.isVisible() || window.getType() == ToolWindowType.FLOATING) {
83 setDisabled(e);
84 return;
87 update(e, window, mgr);
88 if (e.getPresentation().isEnabled()) {
89 myLastWindow = window;
90 myLastManager = mgr;
92 else {
93 setDisabled(e);
96 else {
97 setDisabled(e);
101 private void setDisabled(AnActionEvent e) {
102 e.getPresentation().setEnabled(false);
103 myLastWindow = null;
104 myLastManager = null;
107 protected abstract void update(AnActionEvent event, ToolWindow window, ToolWindowManager mgr);
109 @Override
110 public final void actionPerformed(AnActionEvent e) {
111 actionPerformed(e, myLastWindow, myLastManager);
114 @Nullable
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;
128 break;
130 } else {
131 if (eachScrollable.isVerticalScrollingNeeded()) {
132 scrollable = eachScrollable;
133 break;
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;
148 return null;
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 {
175 public Left() {
178 public Left(String text) {
179 super(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);
190 @Override
191 protected void update(AnActionEvent event, ToolWindow window, ToolWindowManager mgr) {
192 event.getPresentation().setEnabled(!window.getAnchor().isHorizontal());
195 @Override
196 protected void actionPerformed(AnActionEvent e, ToolWindow wnd, ToolWindowManager mgr) {
197 stretch(wnd, true, false);
201 public static class Right extends ResizeToolWindowAction {
203 public Right() {
206 public Right(String text) {
207 super(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);
218 @Override
219 protected void update(AnActionEvent event, ToolWindow window, ToolWindowManager mgr) {
220 event.getPresentation().setEnabled(!window.getAnchor().isHorizontal());
223 @Override
224 protected void actionPerformed(AnActionEvent e, ToolWindow wnd, ToolWindowManager mgr) {
225 stretch(wnd, true, true);
229 public static class Up extends ResizeToolWindowAction {
231 public Up() {
234 public Up(String text) {
235 super(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);
246 @Override
247 protected void update(AnActionEvent event, ToolWindow window, ToolWindowManager mgr) {
248 event.getPresentation().setEnabled(window.getAnchor().isHorizontal());
251 @Override
252 protected void actionPerformed(AnActionEvent e, ToolWindow wnd, ToolWindowManager mgr) {
253 stretch(wnd, false, true);
257 public static class Down extends ResizeToolWindowAction {
259 public Down() {
262 public Down(String text) {
263 super(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);
274 @Override
275 protected void update(AnActionEvent event, ToolWindow window, ToolWindowManager mgr) {
276 event.getPresentation().setEnabled(window.getAnchor().isHorizontal());
279 @Override
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() {
288 return true;
291 public int getNextHorizontalScroll() {
292 return getReferenceSize().width * Registry.intValue("ide.windowSystem.hScrollChars");
295 public boolean isVerticalScrollingNeeded() {
296 return true;
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");
308 } else {
309 return new Dimension(1, 1);
313 return myScrollHelper.getPreferredSize();