3 import jalview.jbgui.GSplitFrame;
5 import java.awt.Component;
6 import java.awt.MouseInfo;
8 import java.awt.Rectangle;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11 import java.awt.event.KeyAdapter;
12 import java.awt.event.KeyEvent;
13 import java.awt.event.KeyListener;
14 import java.util.Map.Entry;
16 import javax.swing.AbstractAction;
17 import javax.swing.JComponent;
18 import javax.swing.KeyStroke;
19 import javax.swing.event.InternalFrameAdapter;
20 import javax.swing.event.InternalFrameEvent;
22 public class SplitFrame extends GSplitFrame
24 private static final long serialVersionUID = 1L;
26 public SplitFrame(JComponent top, JComponent bottom)
33 * Initialise this frame.
37 setSize(AlignFrame.DEFAULT_WIDTH, Desktop.instance.getHeight() - 20);
39 addCloseFrameListener();
48 * Add a listener to tidy up when the frame is closed.
50 protected void addCloseFrameListener()
52 addInternalFrameListener(new InternalFrameAdapter()
55 public void internalFrameClosed(InternalFrameEvent evt)
57 if (getTopComponent() instanceof AlignFrame)
59 ((AlignFrame) getTopComponent())
60 .closeMenuItem_actionPerformed(true);
62 if (getBottomComponent() instanceof AlignFrame)
64 ((AlignFrame) getBottomComponent())
65 .closeMenuItem_actionPerformed(true);
72 * Add a key listener that delegates to whichever split component the mouse is
73 * in (or does nothing if neither).
75 protected void addKeyListener()
77 // TODO Key Bindings rather than KeyListener are recommended for Swing
78 addKeyListener(new KeyAdapter() {
81 public void keyPressed(KeyEvent e)
83 Component c = getComponentAtMouse();
86 for (KeyListener kl : c.getKeyListeners())
94 public void keyReleased(KeyEvent e)
96 Component c = getComponentAtMouse();
99 for (KeyListener kl : c.getKeyListeners())
110 * Returns the split pane component the mouse is in, or null if neither.
114 protected Component getComponentAtMouse()
116 Point loc = MouseInfo.getPointerInfo().getLocation();
118 if (isIn(loc, getTopComponent())) {
119 return getTopComponent();
121 else if (isIn(loc, getBottomComponent()))
123 return getBottomComponent();
128 private boolean isIn(Point loc, JComponent comp)
130 Point p = comp.getLocationOnScreen();
131 Rectangle r = new Rectangle(p.x, p.y, comp.getWidth(), comp.getHeight());
132 return r.contains(loc);
136 * Set key bindings (recommended for Swing over key accelerators). For now,
137 * delegate to the corresponding key accelerator for the AlignFrame that the
138 * mouse is in. Hopefully can be simplified in future if AlignFrame is changed
139 * to use key bindings rather than accelerators.
141 private void addKeyBindings()
143 if (getTopComponent() instanceof AlignFrame)
145 for (Entry<KeyStroke, ActionListener> acc : ((AlignFrame) getTopComponent())
146 .getAccelerators().entrySet())
149 final KeyStroke ks = acc.getKey();
150 this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ks, ks);
151 this.getActionMap().put(ks, new AbstractAction()
154 public void actionPerformed(ActionEvent e)
156 Component c = getComponentAtMouse();
157 if (c instanceof AlignFrame)
159 ((AlignFrame) c).getAccelerators().get(ks)
160 .actionPerformed(null);
166 * Disable unwanted here
168 // X expand views - wrecks the split pane view
169 this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(
170 KeyStroke.getKeyStroke(KeyEvent.VK_X, 0, false));