Merge remote-tracking branch 'origin/merge/JAL-845_JAL-1640' into
[jalview.git] / src / jalview / jbgui / GSplitFrame.java
1 package jalview.jbgui;
2
3 import jalview.util.Platform;
4
5 import java.awt.Component;
6 import java.awt.MouseInfo;
7 import java.awt.Point;
8 import java.awt.Rectangle;
9
10 import javax.swing.JInternalFrame;
11 import javax.swing.JSplitPane;
12 import javax.swing.plaf.basic.BasicInternalFrameUI;
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     splitPane.setDividerLocation(0.5d);
49     splitPane.setResizeWeight(0.5d);
50     splitPane.setDividerSize(0);
51     add(splitPane);
52   }
53
54   /**
55    * Try to hide the title bars as a waste of precious space.
56    * 
57    * @see http
58    *      ://stackoverflow.com/questions/7218971/java-method-works-on-windows
59    *      -but-not-macintosh -java
60    */
61   protected void hideTitleBars()
62   {
63     if (new Platform().isAMac())
64     {
65       // this saves some space - but doesn't hide the title bar
66       topFrame.putClientProperty("JInternalFrame.isPalette", true);
67       // topFrame.getRootPane().putClientProperty("Window.style", "small");
68       bottomFrame.putClientProperty("JInternalFrame.isPalette", true);
69     }
70     else
71     {
72       ((BasicInternalFrameUI) topFrame.getUI()).setNorthPane(null);
73       ((BasicInternalFrameUI) bottomFrame.getUI()).setNorthPane(null);
74     }
75   }
76
77   public GAlignFrame getTopFrame()
78   {
79     return topFrame;
80   }
81
82   public GAlignFrame getBottomFrame()
83   {
84     return bottomFrame;
85   }
86
87   /**
88    * Returns the split pane component the mouse is in, or null if neither.
89    * 
90    * @return
91    */
92   protected GAlignFrame getFrameAtMouse()
93   {
94     Point loc = MouseInfo.getPointerInfo().getLocation();
95     
96     if (isIn(loc, splitPane.getTopComponent()))
97     {
98       return getTopFrame();
99     }
100     else if (isIn(loc, splitPane.getBottomComponent()))
101     {
102       return getBottomFrame();
103     }
104     return null;
105   }
106
107   private boolean isIn(Point loc, Component comp)
108   {
109     Point p = comp.getLocationOnScreen();
110     Rectangle r = new Rectangle(p.x, p.y, comp.getWidth(), comp.getHeight());
111     return r.contains(loc);
112   }
113
114 }