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