Add new action: Maximize/Restore Active Dialog
[idea/community.git] / platform / platform-impl / src / com / intellij / ide / actions / MaximizeActiveDialog.java
1 package com.intellij.ide.actions;
2
3 import com.intellij.openapi.actionSystem.AnActionEvent;
4 import com.intellij.openapi.project.DumbAwareAction;
5 import com.intellij.ui.ScreenUtil;
6
7 import javax.swing.FocusManager;
8 import javax.swing.*;
9 import java.awt.*;
10
11 public class MaximizeActiveDialog extends DumbAwareAction {
12   {
13     setEnabledInModalContext(true);
14   }
15
16   @Override
17   public void actionPerformed(AnActionEvent e) {
18     Window window = FocusManager.getCurrentManager().getActiveWindow();
19     if (window instanceof JDialog && ((JDialog)window).isResizable()) {
20       JDialog d = (JDialog)window;
21       JRootPane rootPane = d.getRootPane();
22       if (rootPane == null) return;
23       Rectangle screenRectangle = ScreenUtil.getScreenRectangle(d);
24
25
26       if (d.getBounds().equals(screenRectangle)) {
27         //We have to restore normal state
28         Object value = rootPane.getClientProperty("NORMAL_BOUNDS");
29         if (value instanceof Rectangle) {
30           Rectangle bounds = (Rectangle)value;
31           ScreenUtil.fitToScreen(bounds);
32           d.setBounds(bounds);
33           rootPane.putClientProperty("NORMAL_BOUNDS", null);
34         }
35       }
36       else {
37         rootPane.putClientProperty("NORMAL_BOUNDS", d.getBounds());
38         d.setBounds(screenRectangle);
39       }
40     }
41   }
42 }