JAL-845 implement alignment of protein to match cDNA alignment
[jalview.git] / src / jalview / jbgui / GSplitFrame.java
index 40cce0d..27b3324 100644 (file)
@@ -1,7 +1,15 @@
 package jalview.jbgui;
 
+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
 {
@@ -11,6 +19,8 @@ public class GSplitFrame extends JInternalFrame
 
   private GAlignFrame bottomFrame;
 
+  private JSplitPane splitPane;
+
   /**
    * Constructor
    * 
@@ -21,13 +31,47 @@ public class GSplitFrame extends JInternalFrame
   {
     this.topFrame = top;
     this.bottomFrame = bottom;
-    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, top,
-            bottom);
+
+    hideTitleBars();
+
+    addSplitPane();
+  }
+
+  /**
+   * Create and add the split pane containing the top and bottom components.
+   */
+  protected void addSplitPane()
+  {
+    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()
@@ -39,4 +83,57 @@ public class GSplitFrame extends JInternalFrame
   {
     return bottomFrame;
   }
+
+  /**
+   * 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)
+  {
+    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);
+  }
+
+  /**
+   * 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)
+  {
+    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();
+  }
 }