JAL-845 implement alignment of protein to match cDNA alignment
[jalview.git] / src / jalview / jbgui / GSplitFrame.java
index 281a93e..27b3324 100644 (file)
@@ -1,16 +1,25 @@
 package jalview.jbgui;
 
-import javax.swing.JComponent;
+import jalview.util.Platform;
+
+import java.awt.Component;
+import java.awt.MouseInfo;
+import java.awt.Point;
+import java.awt.Rectangle;
+
 import javax.swing.JInternalFrame;
 import javax.swing.JSplitPane;
+import javax.swing.plaf.basic.BasicInternalFrameUI;
 
 public class GSplitFrame extends JInternalFrame
 {
   private static final long serialVersionUID = 1L;
 
-  private JComponent topComponent;
+  private GAlignFrame topFrame;
 
-  private JComponent bottomComponent;
+  private GAlignFrame bottomFrame;
+
+  private JSplitPane splitPane;
 
   /**
    * Constructor
@@ -18,26 +27,113 @@ public class GSplitFrame extends JInternalFrame
    * @param top
    * @param bottom
    */
-  public GSplitFrame(JComponent top, JComponent bottom)
+  public GSplitFrame(GAlignFrame top, GAlignFrame bottom)
+  {
+    this.topFrame = top;
+    this.bottomFrame = bottom;
+
+    hideTitleBars();
+
+    addSplitPane();
+  }
+
+  /**
+   * Create and add the split pane containing the top and bottom components.
+   */
+  protected void addSplitPane()
   {
-    this.topComponent = top;
-    this.bottomComponent = bottom;
-    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, top,
-            bottom);
+    splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topFrame,
+            bottomFrame);
     splitPane.setVisible(true);
-    add(splitPane);
     splitPane.setDividerLocation(0.5d);
     splitPane.setResizeWeight(0.5d);
     splitPane.setDividerSize(0);
+    add(splitPane);
+  }
+
+  /**
+   * Try to hide the title bars as a waste of precious space.
+   * 
+   * @see http
+   *      ://stackoverflow.com/questions/7218971/java-method-works-on-windows
+   *      -but-not-macintosh -java
+   */
+  protected void hideTitleBars()
+  {
+    if (new Platform().isAMac())
+    {
+      // this saves some space - but doesn't hide the title bar
+      topFrame.putClientProperty("JInternalFrame.isPalette", true);
+      // topFrame.getRootPane().putClientProperty("Window.style", "small");
+      bottomFrame.putClientProperty("JInternalFrame.isPalette", true);
+    }
+    else
+    {
+      ((BasicInternalFrameUI) topFrame.getUI()).setNorthPane(null);
+      ((BasicInternalFrameUI) bottomFrame.getUI()).setNorthPane(null);
+    }
+  }
+
+  public GAlignFrame getTopFrame()
+  {
+    return topFrame;
+  }
+
+  public GAlignFrame getBottomFrame()
+  {
+    return bottomFrame;
   }
 
-  public JComponent getTopComponent()
+  /**
+   * Returns the split pane component the mouse is in, or null if neither.
+   * 
+   * @return
+   */
+  protected GAlignFrame getFrameAtMouse()
+  {
+    Point loc = MouseInfo.getPointerInfo().getLocation();
+    
+    if (isIn(loc, splitPane.getTopComponent()))
+    {
+      return getTopFrame();
+    }
+    else if (isIn(loc, splitPane.getBottomComponent()))
+    {
+      return getBottomFrame();
+    }
+    return null;
+  }
+
+  private boolean isIn(Point loc, Component comp)
   {
-    return topComponent;
+    if (!comp.isVisible())
+    {
+      return false;
+    }
+    Point p = comp.getLocationOnScreen();
+    Rectangle r = new Rectangle(p.x, p.y, comp.getWidth(), comp.getHeight());
+    return r.contains(loc);
   }
 
-  public JComponent getBottomComponent()
+  /**
+   * Make the complement of the specified split component visible or hidden,
+   * adjusting the position of the split divide.
+   */
+  public void setComplementVisible(Component alignFrame, boolean show)
   {
-    return bottomComponent;
+    if (alignFrame == this.topFrame)
+    {
+      this.bottomFrame.setVisible(show);
+    }
+    else if (alignFrame == this.bottomFrame)
+    {
+      this.topFrame.setVisible(show);
+    }
+    if (show)
+    {
+      // SplitPane needs nudging to restore 50-50 split
+      splitPane.setDividerLocation(0.5d);
+    }
+    validate();
   }
 }