merge commit
[jalview.git] / src / jalview / jbgui / GSplitFrame.java
1 package jalview.jbgui;
2
3 import java.awt.Component;
4 import java.awt.MouseInfo;
5 import java.awt.Point;
6 import java.awt.Rectangle;
7
8 import javax.swing.JInternalFrame;
9 import javax.swing.JSplitPane;
10 import javax.swing.plaf.basic.BasicInternalFrameUI;
11
12 import jalview.util.Platform;
13
14 public class GSplitFrame extends JInternalFrame
15 {
16   private static final long serialVersionUID = 1L;
17
18   private GAlignFrame topFrame;
19
20   private GAlignFrame bottomFrame;
21
22   private JSplitPane splitPane;
23
24   /**
25    * Constructor
26    * 
27    * @param top
28    * @param bottom
29    */
30   public GSplitFrame(GAlignFrame top, GAlignFrame bottom)
31   {
32     this.topFrame = top;
33     this.bottomFrame = bottom;
34
35     hideTitleBars();
36
37     addSplitPane();
38   }
39
40   /**
41    * Create and add the split pane containing the top and bottom components.
42    */
43   protected void addSplitPane()
44   {
45     splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topFrame,
46             bottomFrame);
47     splitPane.setVisible(true);
48     final double ratio = topFrame.getHeight()
49             / (double) (topFrame.getHeight() + bottomFrame.getHeight());
50     splitPane.setDividerLocation(ratio);
51     splitPane.setResizeWeight(ratio);
52     splitPane.setDividerSize(5);
53     add(splitPane);
54   }
55
56   /**
57    * Try to hide the title bars as a waste of precious space.
58    * 
59    * @see http
60    *      ://stackoverflow.com/questions/7218971/java-method-works-on-windows
61    *      -but-not-macintosh -java
62    */
63   protected void hideTitleBars()
64   {
65     if (new Platform().isAMac())
66     {
67       // this saves some space - but doesn't hide the title bar
68       topFrame.putClientProperty("JInternalFrame.isPalette", true);
69       // topFrame.getRootPane().putClientProperty("Window.style", "small");
70       bottomFrame.putClientProperty("JInternalFrame.isPalette", true);
71     }
72     else
73     {
74       ((BasicInternalFrameUI) topFrame.getUI()).setNorthPane(null);
75       ((BasicInternalFrameUI) bottomFrame.getUI()).setNorthPane(null);
76     }
77   }
78
79   public GAlignFrame getTopFrame()
80   {
81     return topFrame;
82   }
83
84   public GAlignFrame getBottomFrame()
85   {
86     return bottomFrame;
87   }
88
89   /**
90    * Returns the split pane component the mouse is in, or null if neither.
91    * 
92    * @return
93    */
94   protected GAlignFrame getFrameAtMouse()
95   {
96     Point loc = MouseInfo.getPointerInfo().getLocation();
97     
98     if (isIn(loc, splitPane.getTopComponent()))
99     {
100       return getTopFrame();
101     }
102     else if (isIn(loc, splitPane.getBottomComponent()))
103     {
104       return getBottomFrame();
105     }
106     return null;
107   }
108
109   private boolean isIn(Point loc, Component comp)
110   {
111     if (!comp.isVisible())
112     {
113       return false;
114     }
115     Point p = comp.getLocationOnScreen();
116     Rectangle r = new Rectangle(p.x, p.y, comp.getWidth(), comp.getHeight());
117     return r.contains(loc);
118   }
119
120   /**
121    * Make the complement of the specified split component visible or hidden,
122    * adjusting the position of the split divide.
123    */
124   public void setComplementVisible(Object alignFrame, boolean show)
125   {
126     if (alignFrame == this.topFrame)
127     {
128       this.bottomFrame.setVisible(show);
129     }
130     else if (alignFrame == this.bottomFrame)
131     {
132       this.topFrame.setVisible(show);
133     }
134     if (show)
135     {
136       // SplitPane needs nudging to restore 50-50 split
137       // TODO save/restore other ratios
138       splitPane.setDividerLocation(0.5d);
139     }
140     validate();
141   }
142 }