JAL-3364 align split frame sequence panels with wrapped view scale left
[jalview.git] / src / jalview / gui / SplitFrame.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.api.SplitContainerI;
24 import jalview.datamodel.AlignmentI;
25 import jalview.jbgui.GAlignFrame;
26 import jalview.jbgui.GSplitFrame;
27 import jalview.structure.StructureSelectionManager;
28 import jalview.util.Platform;
29 import jalview.viewmodel.AlignmentViewport;
30
31 import java.awt.Component;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.event.KeyAdapter;
35 import java.awt.event.KeyEvent;
36 import java.awt.event.KeyListener;
37 import java.beans.PropertyVetoException;
38 import java.util.Arrays;
39 import java.util.List;
40 import java.util.Map.Entry;
41
42 import javax.swing.AbstractAction;
43 import javax.swing.InputMap;
44 import javax.swing.JComponent;
45 import javax.swing.JDesktopPane;
46 import javax.swing.JInternalFrame;
47 import javax.swing.JMenuItem;
48 import javax.swing.KeyStroke;
49 import javax.swing.event.InternalFrameAdapter;
50 import javax.swing.event.InternalFrameEvent;
51
52 /**
53  * An internal frame on the desktop that hosts a horizontally split view of
54  * linked DNA and Protein alignments. Additional views can be created in linked
55  * pairs, expanded to separate split frames, or regathered into a single frame.
56  * <p>
57  * (Some) operations on each alignment are automatically mirrored on the other.
58  * These include mouseover (highlighting), sequence and column selection,
59  * sequence ordering and sorting, and grouping, colouring and sorting by tree.
60  * 
61  * @author gmcarstairs
62  *
63  */
64 public class SplitFrame extends GSplitFrame implements SplitContainerI
65 {
66   private static final int WINDOWS_INSETS_WIDTH = 28; // tbc
67
68   private static final int MAC_INSETS_WIDTH = 28;
69
70   private static final int WINDOWS_INSETS_HEIGHT = 50; // tbc
71
72   private static final int MAC_INSETS_HEIGHT = 50;
73
74   private static final int DESKTOP_DECORATORS_HEIGHT = 65;
75
76   private static final long serialVersionUID = 1L;
77
78   public SplitFrame(GAlignFrame top, GAlignFrame bottom)
79   {
80     super(top, bottom);
81     init();
82   }
83
84   /**
85    * Initialise this frame.
86    */
87   protected void init()
88   {
89     getTopFrame().setSplitFrame(this, true);
90     getBottomFrame().setSplitFrame(this, false);
91     getTopFrame().setVisible(true);
92     getBottomFrame().setVisible(true);
93
94     ((AlignFrame) getTopFrame()).getViewport().setCodingComplement(
95             ((AlignFrame) getBottomFrame()).getViewport());
96
97     /*
98      * estimate width and height of SplitFrame; this.getInsets() doesn't seem to
99      * give the full additional size (a few pixels short)
100      */
101     int widthFudge = Platform.isAMac() ? MAC_INSETS_WIDTH
102             : WINDOWS_INSETS_WIDTH;
103     int heightFudge = Platform.isAMac() ? MAC_INSETS_HEIGHT
104             : WINDOWS_INSETS_HEIGHT;
105     int width = ((AlignFrame) getTopFrame()).getWidth() + widthFudge;
106     int height = ((AlignFrame) getTopFrame()).getHeight()
107             + ((AlignFrame) getBottomFrame()).getHeight() + DIVIDER_SIZE
108             + heightFudge;
109     height = fitHeightToDesktop(height);
110     setSize(width, height);
111
112     adjustLayout();
113
114     addCloseFrameListener();
115
116     addKeyListener();
117
118     addKeyBindings();
119
120     addCommandListeners();
121   }
122
123   /**
124    * Reduce the height if too large to fit in the Desktop. Also adjust the
125    * divider location in proportion.
126    * 
127    * @param height
128    *          in pixels
129    * @return original or reduced height
130    */
131   public int fitHeightToDesktop(int height)
132   {
133     // allow about 65 pixels for Desktop decorators on Windows
134
135     int newHeight = Math.min(height,
136             Desktop.instance.getHeight() - DESKTOP_DECORATORS_HEIGHT);
137     if (newHeight != height)
138     {
139       int oldDividerLocation = getDividerLocation();
140       setDividerLocation(oldDividerLocation * newHeight / height);
141     }
142     return newHeight;
143   }
144
145   /**
146    * Set the top and bottom frames to listen to each others Commands (e.g. Edit,
147    * Order).
148    */
149   protected void addCommandListeners()
150   {
151     // TODO if CommandListener is only ever 1:1 for complementary views,
152     // may change broadcast pattern to direct messaging (more efficient)
153     final StructureSelectionManager ssm = StructureSelectionManager
154             .getStructureSelectionManager(Desktop.instance);
155     ssm.addCommandListener(((AlignFrame) getTopFrame()).getViewport());
156     ssm.addCommandListener(((AlignFrame) getBottomFrame()).getViewport());
157   }
158
159   /**
160    * Do any tweaking and twerking of the layout wanted.
161    */
162   public void adjustLayout()
163   {
164     final AlignViewport topViewport = ((AlignFrame) getTopFrame()).viewport;
165     final AlignViewport bottomViewport = ((AlignFrame) getBottomFrame()).viewport;
166
167     /*
168      * Ensure sequence ids are the same width so sequences line up;
169      * reduce the idwidth of a panel with a longer wrapped left scale
170      */
171     int w1 = topViewport.getIdWidth();
172     int w2 = bottomViewport.getIdWidth();
173     int topScaleLeft = topViewport.getWrapAlignment()
174             ? ((AlignFrame) getTopFrame()).alignPanel
175                     .getSeqPanel().seqCanvas.getLabelWidthWest()
176             : 0;
177     int bottomScaleLeft = bottomViewport.getWrapAlignment()
178             ? ((AlignFrame) getBottomFrame()).alignPanel
179                     .getSeqPanel().seqCanvas.getLabelWidthWest()
180             : 0;
181     int w3 = Math.max(w1, w2);
182     topViewport
183             .setIdWidth(w3 - Math.max(0, topScaleLeft - bottomScaleLeft));
184     bottomViewport
185             .setIdWidth(w3 - Math.max(0, bottomScaleLeft - topScaleLeft));
186
187     /*
188      * Scale protein to either 1 or 3 times character width of dna
189      */
190     final AlignmentI topAlignment = topViewport.getAlignment();
191     final AlignmentI bottomAlignment = bottomViewport.getAlignment();
192     AlignmentViewport cdna = topAlignment.isNucleotide() ? topViewport
193             : (bottomAlignment.isNucleotide() ? bottomViewport : null);
194     AlignmentViewport protein = !topAlignment.isNucleotide() ? topViewport
195             : (!bottomAlignment.isNucleotide() ? bottomViewport : null);
196     if (protein != null && cdna != null)
197     {
198       int scale = protein.isScaleProteinAsCdna() ? 3 : 1;
199       protein.setCharWidth(scale * cdna.getViewStyle().getCharWidth());
200     }
201
202     repaint();
203   }
204
205   /**
206    * Adjusts the divider for a sensible split of the real estate (for example,
207    * when many transcripts are shown with a single protein). This should only be
208    * called after the split pane has been laid out (made visible) so it has a
209    * height. The aim is to avoid unnecessary vertical scroll bars, while
210    * ensuring that at least 2 sequences are visible in each panel.
211    * <p>
212    * Once laid out, the user may choose to customise as they wish, so this
213    * method is not called again after the initial layout.
214    */
215   protected void adjustInitialLayout()
216   {
217     AlignFrame topFrame = (AlignFrame) getTopFrame();
218     AlignFrame bottomFrame = (AlignFrame) getBottomFrame();
219
220     /*
221      * recompute layout of top and bottom panels to reflect their
222      * actual (rather than requested) height
223      */
224     topFrame.alignPanel.adjustAnnotationHeight();
225     bottomFrame.alignPanel.adjustAnnotationHeight();
226
227     final AlignViewport topViewport = topFrame.viewport;
228     final AlignViewport bottomViewport = bottomFrame.viewport;
229     final AlignmentI topAlignment = topViewport.getAlignment();
230     final AlignmentI bottomAlignment = bottomViewport.getAlignment();
231     boolean topAnnotations = topViewport.isShowAnnotation();
232     boolean bottomAnnotations = bottomViewport.isShowAnnotation();
233     // TODO need number of visible sequences here, not #sequences - how?
234     int topCount = topAlignment.getHeight();
235     int bottomCount = bottomAlignment.getHeight();
236     int topCharHeight = topViewport.getViewStyle().getCharHeight();
237     int bottomCharHeight = bottomViewport.getViewStyle().getCharHeight();
238
239     /*
240      * calculate the minimum ratio that leaves at least the height 
241      * of two sequences (after rounding) visible in the top panel
242      */
243     int topPanelHeight = topFrame.getHeight();
244     int bottomPanelHeight = bottomFrame.getHeight();
245     int topSequencesHeight = topFrame.alignPanel.getSeqPanel().seqCanvas
246             .getHeight();
247     int topPanelMinHeight = topPanelHeight
248             - Math.max(0, topSequencesHeight - 3 * topCharHeight);
249     double totalHeight = (double) topPanelHeight + bottomPanelHeight;
250     double minRatio = topPanelMinHeight / totalHeight;
251
252     /*
253      * calculate the maximum ratio that leaves at least the height 
254      * of two sequences (after rounding) visible in the bottom panel
255      */
256     int bottomSequencesHeight = bottomFrame.alignPanel.getSeqPanel().seqCanvas
257             .getHeight();
258     int bottomPanelMinHeight = bottomPanelHeight
259             - Math.max(0, bottomSequencesHeight - 3 * bottomCharHeight);
260     double maxRatio = (totalHeight - bottomPanelMinHeight) / totalHeight;
261
262     /*
263      * estimate ratio of (topFrameContent / bottomFrameContent)
264      */
265     int insets = Platform.isAMac() ? MAC_INSETS_HEIGHT
266             : WINDOWS_INSETS_HEIGHT;
267     // allow 3 'rows' for scale, scrollbar, status bar
268     int topHeight = insets + (3 + topCount) * topCharHeight
269             + (topAnnotations ? topViewport.calcPanelHeight() : 0);
270     int bottomHeight = insets + (3 + bottomCount) * bottomCharHeight
271             + (bottomAnnotations ? bottomViewport.calcPanelHeight() : 0);
272     double ratio = ((double) topHeight)
273             / (double) (topHeight + bottomHeight);
274
275     /*
276      * limit ratio to avoid concealing all sequences
277      */
278     ratio = Math.min(ratio, maxRatio);
279     ratio = Math.max(ratio, minRatio);
280     setRelativeDividerLocation(ratio);
281   }
282
283   /**
284    * Add a listener to tidy up when the frame is closed.
285    */
286   protected void addCloseFrameListener()
287   {
288     addInternalFrameListener(new InternalFrameAdapter()
289     {
290       @Override
291       public void internalFrameClosed(InternalFrameEvent evt)
292       {
293         close();
294       };
295     });
296   }
297
298   /**
299    * Add a key listener that delegates to whichever split component the mouse is
300    * in (or does nothing if neither).
301    */
302   protected void addKeyListener()
303   {
304     addKeyListener(new KeyAdapter()
305     {
306
307       @Override
308       public void keyPressed(KeyEvent e)
309       {
310         AlignFrame af = (AlignFrame) getFrameAtMouse();
311
312         /*
313          * Intercept and override any keys here if wanted.
314          */
315         if (!overrideKey(e, af))
316         {
317           if (af != null)
318           {
319             for (KeyListener kl : af.getKeyListeners())
320             {
321               kl.keyPressed(e);
322             }
323           }
324         }
325       }
326
327       @Override
328       public void keyReleased(KeyEvent e)
329       {
330         Component c = getFrameAtMouse();
331         if (c != null)
332         {
333           for (KeyListener kl : c.getKeyListeners())
334           {
335             kl.keyReleased(e);
336           }
337         }
338       }
339
340     });
341   }
342
343   /**
344    * Returns true if the key event is overriden and actioned (or ignored) here,
345    * else returns false, indicating it should be delegated to the AlignFrame's
346    * usual handler.
347    * <p>
348    * We can't handle Cmd-Key combinations here, instead this is done by
349    * overriding key bindings.
350    * 
351    * @see addKeyOverrides
352    * @param e
353    * @param af
354    * @return
355    */
356   protected boolean overrideKey(KeyEvent e, AlignFrame af)
357   {
358     boolean actioned = false;
359     int keyCode = e.getKeyCode();
360     switch (keyCode)
361     {
362     case KeyEvent.VK_DOWN:
363       if (e.isAltDown() || !af.viewport.cursorMode)
364       {
365         /*
366          * Key down (or Alt-key-down in cursor mode) - move selected sequences
367          */
368         ((AlignFrame) getTopFrame()).moveSelectedSequences(false);
369         ((AlignFrame) getBottomFrame()).moveSelectedSequences(false);
370         actioned = true;
371         e.consume();
372       }
373       break;
374     case KeyEvent.VK_UP:
375       if (e.isAltDown() || !af.viewport.cursorMode)
376       {
377         /*
378          * Key up (or Alt-key-up in cursor mode) - move selected sequences
379          */
380         ((AlignFrame) getTopFrame()).moveSelectedSequences(true);
381         ((AlignFrame) getBottomFrame()).moveSelectedSequences(true);
382         actioned = true;
383         e.consume();
384       }
385       break;
386     default:
387     }
388     return actioned;
389   }
390
391   /**
392    * Set key bindings (recommended for Swing over key accelerators).
393    */
394   private void addKeyBindings()
395   {
396     overrideDelegatedKeyBindings();
397
398     overrideImplementedKeyBindings();
399   }
400
401   /**
402    * Override key bindings with alternative action methods implemented in this
403    * class.
404    */
405   protected void overrideImplementedKeyBindings()
406   {
407     overrideFind();
408     overrideNewView();
409     overrideCloseView();
410     overrideExpandViews();
411     overrideGatherViews();
412   }
413
414   /**
415    * Replace Cmd-W close view action with our version.
416    */
417   protected void overrideCloseView()
418   {
419     AbstractAction action;
420     /*
421      * Ctrl-W / Cmd-W - close view or window
422      */
423     KeyStroke key_cmdW = KeyStroke.getKeyStroke(KeyEvent.VK_W,
424             jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
425     action = new AbstractAction()
426     {
427       @Override
428       public void actionPerformed(ActionEvent e)
429       {
430         closeView_actionPerformed();
431       }
432     };
433     overrideKeyBinding(key_cmdW, action);
434   }
435
436   /**
437    * Replace Cmd-T new view action with our version.
438    */
439   protected void overrideNewView()
440   {
441     /*
442      * Ctrl-T / Cmd-T open new view
443      */
444     KeyStroke key_cmdT = KeyStroke.getKeyStroke(KeyEvent.VK_T,
445             jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
446     AbstractAction action = new AbstractAction()
447     {
448       @Override
449       public void actionPerformed(ActionEvent e)
450       {
451         newView_actionPerformed();
452       }
453     };
454     overrideKeyBinding(key_cmdT, action);
455   }
456
457   /**
458    * For now, delegates key events to the corresponding key accelerator for the
459    * AlignFrame that the mouse is in. Hopefully can be simplified in future if
460    * AlignFrame is changed to use key bindings rather than accelerators.
461    */
462   protected void overrideDelegatedKeyBindings()
463   {
464     if (getTopFrame() instanceof AlignFrame)
465     {
466       /*
467        * Get all accelerator keys in the top frame (the bottom should be
468        * identical) and override each one.
469        */
470       for (Entry<KeyStroke, JMenuItem> acc : ((AlignFrame) getTopFrame())
471               .getAccelerators().entrySet())
472       {
473         overrideKeyBinding(acc);
474       }
475     }
476   }
477
478   /**
479    * Overrides an AlignFrame key accelerator with our version which delegates to
480    * the action listener in whichever frame has the mouse (and does nothing if
481    * neither has).
482    * 
483    * @param acc
484    */
485   private void overrideKeyBinding(Entry<KeyStroke, JMenuItem> acc)
486   {
487     final KeyStroke ks = acc.getKey();
488     InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
489     inputMap.put(ks, ks);
490     this.getActionMap().put(ks, new AbstractAction()
491     {
492       @Override
493       public void actionPerformed(ActionEvent e)
494       {
495         Component c = getFrameAtMouse();
496         if (c != null && c instanceof AlignFrame)
497         {
498           for (ActionListener a : ((AlignFrame) c).getAccelerators().get(ks)
499                   .getActionListeners())
500           {
501             a.actionPerformed(null);
502           }
503         }
504       }
505     });
506   }
507
508   /**
509    * Replace an accelerator key's action with the specified action.
510    * 
511    * @param ks
512    */
513   protected void overrideKeyBinding(KeyStroke ks, AbstractAction action)
514   {
515     this.getActionMap().put(ks, action);
516     overrideMenuItem(ks, action);
517   }
518
519   /**
520    * Create and link new views (with matching names) in both panes.
521    * <p>
522    * Note this is _not_ multiple tabs, each hosting a split pane view, rather it
523    * is a single split pane with each split holding multiple tabs which are
524    * linked in pairs.
525    * <p>
526    * TODO implement instead with a tabbed holder in the SplitView, each tab
527    * holding a single JSplitPane. Would avoid a duplicated tab, at the cost of
528    * some additional coding.
529    */
530   protected void newView_actionPerformed()
531   {
532     AlignFrame topFrame = (AlignFrame) getTopFrame();
533     AlignFrame bottomFrame = (AlignFrame) getBottomFrame();
534     final boolean scaleProteinAsCdna = topFrame.viewport
535             .isScaleProteinAsCdna();
536
537     AlignmentPanel newTopPanel = topFrame.newView(null, true);
538     AlignmentPanel newBottomPanel = bottomFrame.newView(null, true);
539
540     /*
541      * This currently (for the first new view only) leaves the top pane on tab 0
542      * but the bottom on tab 1. This results from 'setInitialTabVisible' echoing
543      * from the bottom back to the first frame. Next line is a fudge to work
544      * around this. TODO find a better way.
545      */
546     if (topFrame.getTabIndex() != bottomFrame.getTabIndex())
547     {
548       topFrame.setDisplayedView(newTopPanel);
549     }
550
551     newBottomPanel.av.setViewName(newTopPanel.av.getViewName());
552     newTopPanel.av.setCodingComplement(newBottomPanel.av);
553
554     /*
555      * These lines can be removed once scaleProteinAsCdna is added to element
556      * Viewport in jalview.xsd, as Jalview2XML.copyAlignPanel will then take
557      * care of it
558      */
559     newTopPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
560     newBottomPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
561
562     /*
563      * Line up id labels etc
564      */
565     adjustLayout();
566
567     final StructureSelectionManager ssm = StructureSelectionManager
568             .getStructureSelectionManager(Desktop.instance);
569     ssm.addCommandListener(newTopPanel.av);
570     ssm.addCommandListener(newBottomPanel.av);
571   }
572
573   /**
574    * Close the currently selected view in both panes. If there is only one view,
575    * close this split frame.
576    */
577   protected void closeView_actionPerformed()
578   {
579     int viewCount = ((AlignFrame) getTopFrame()).getAlignPanels().size();
580     if (viewCount < 2)
581     {
582       close();
583       return;
584     }
585
586     AlignmentPanel topPanel = ((AlignFrame) getTopFrame()).alignPanel;
587     AlignmentPanel bottomPanel = ((AlignFrame) getBottomFrame()).alignPanel;
588
589     ((AlignFrame) getTopFrame()).closeView(topPanel);
590     ((AlignFrame) getBottomFrame()).closeView(bottomPanel);
591
592   }
593
594   /**
595    * Close child frames and this split frame.
596    */
597   public void close()
598   {
599     ((AlignFrame) getTopFrame()).closeMenuItem_actionPerformed(true);
600     ((AlignFrame) getBottomFrame()).closeMenuItem_actionPerformed(true);
601     try
602     {
603       this.setClosed(true);
604     } catch (PropertyVetoException e)
605     {
606       // ignore
607     }
608   }
609
610   /**
611    * Replace AlignFrame 'expand views' action with SplitFrame version.
612    */
613   protected void overrideExpandViews()
614   {
615     KeyStroke key_X = KeyStroke.getKeyStroke(KeyEvent.VK_X, 0, false);
616     AbstractAction action = new AbstractAction()
617     {
618       @Override
619       public void actionPerformed(ActionEvent e)
620       {
621         expandViews_actionPerformed();
622       }
623     };
624     overrideMenuItem(key_X, action);
625   }
626
627   /**
628    * Replace AlignFrame 'gather views' action with SplitFrame version.
629    */
630   protected void overrideGatherViews()
631   {
632     KeyStroke key_G = KeyStroke.getKeyStroke(KeyEvent.VK_G, 0, false);
633     AbstractAction action = new AbstractAction()
634     {
635       @Override
636       public void actionPerformed(ActionEvent e)
637       {
638         gatherViews_actionPerformed();
639       }
640     };
641     overrideMenuItem(key_G, action);
642   }
643
644   /**
645    * Override the menu action associated with the keystroke in the child frames,
646    * replacing it with the given action.
647    * 
648    * @param ks
649    * @param action
650    */
651   private void overrideMenuItem(KeyStroke ks, AbstractAction action)
652   {
653     overrideMenuItem(ks, action, getTopFrame());
654     overrideMenuItem(ks, action, getBottomFrame());
655   }
656
657   /**
658    * Override the menu action associated with the keystroke in one child frame,
659    * replacing it with the given action. Mwahahahaha.
660    * 
661    * @param key
662    * @param action
663    * @param comp
664    */
665   private void overrideMenuItem(KeyStroke key, final AbstractAction action,
666           JComponent comp)
667   {
668     if (comp instanceof AlignFrame)
669     {
670       JMenuItem mi = ((AlignFrame) comp).getAccelerators().get(key);
671       if (mi != null)
672       {
673         for (ActionListener al : mi.getActionListeners())
674         {
675           mi.removeActionListener(al);
676         }
677         mi.addActionListener(new ActionListener()
678         {
679           @Override
680           public void actionPerformed(ActionEvent e)
681           {
682             action.actionPerformed(e);
683           }
684         });
685       }
686     }
687   }
688
689   /**
690    * Expand any multiple views (which are always in pairs) into separate split
691    * frames.
692    */
693   protected void expandViews_actionPerformed()
694   {
695     Desktop.instance.explodeViews(this);
696   }
697
698   /**
699    * Gather any other SplitFrame views of this alignment back in as multiple
700    * (pairs of) views in this SplitFrame.
701    */
702   protected void gatherViews_actionPerformed()
703   {
704     Desktop.instance.gatherViews(this);
705   }
706
707   /**
708    * Returns the alignment in the complementary frame to the one given.
709    */
710   @Override
711   public AlignmentI getComplement(Object alignFrame)
712   {
713     if (alignFrame == this.getTopFrame())
714     {
715       return ((AlignFrame) getBottomFrame()).viewport.getAlignment();
716     }
717     else if (alignFrame == this.getBottomFrame())
718     {
719       return ((AlignFrame) getTopFrame()).viewport.getAlignment();
720     }
721     return null;
722   }
723
724   /**
725    * Returns the title of the complementary frame to the one given.
726    */
727   @Override
728   public String getComplementTitle(Object alignFrame)
729   {
730     if (alignFrame == this.getTopFrame())
731     {
732       return ((AlignFrame) getBottomFrame()).getTitle();
733     }
734     else if (alignFrame == this.getBottomFrame())
735     {
736       return ((AlignFrame) getTopFrame()).getTitle();
737     }
738     return null;
739   }
740
741   /**
742    * Set the 'other half' to hidden / revealed.
743    */
744   @Override
745   public void setComplementVisible(Object alignFrame, boolean show)
746   {
747     /*
748      * Hiding the AlignPanel suppresses unnecessary repaints
749      */
750     if (alignFrame == getTopFrame())
751     {
752       ((AlignFrame) getBottomFrame()).alignPanel.setVisible(show);
753     }
754     else if (alignFrame == getBottomFrame())
755     {
756       ((AlignFrame) getTopFrame()).alignPanel.setVisible(show);
757     }
758     super.setComplementVisible(alignFrame, show);
759   }
760
761   /**
762    * return the AlignFrames held by this container
763    * 
764    * @return { Top alignFrame (Usually CDS), Bottom AlignFrame (Usually
765    *         Protein)}
766    */
767   public List<AlignFrame> getAlignFrames()
768   {
769     return Arrays
770             .asList(new AlignFrame[]
771             { (AlignFrame) getTopFrame(), (AlignFrame) getBottomFrame() });
772   }
773
774   /**
775    * Replace Cmd-F Find action with our version. This is necessary because the
776    * 'default' Finder searches in the first AlignFrame it finds. We need it to
777    * search in the half of the SplitFrame that has the mouse.
778    */
779   protected void overrideFind()
780   {
781     /*
782      * Ctrl-F / Cmd-F open Finder dialog, 'focused' on the right alignment
783      */
784     KeyStroke key_cmdF = KeyStroke.getKeyStroke(KeyEvent.VK_F,
785             jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
786     AbstractAction action = new AbstractAction()
787     {
788       @Override
789       public void actionPerformed(ActionEvent e)
790       {
791         Component c = getFrameAtMouse();
792         if (c != null && c instanceof AlignFrame)
793         {
794           AlignFrame af = (AlignFrame) c;
795           new Finder(af.viewport, af.alignPanel);
796         }
797       }
798     };
799     overrideKeyBinding(key_cmdF, action);
800   }
801
802   /**
803    * Override to do nothing if triggered from one of the child frames
804    */
805   @Override
806   public void setSelected(boolean selected) throws PropertyVetoException
807   {
808     JDesktopPane desktopPane = getDesktopPane();
809     JInternalFrame fr = desktopPane == null ? null
810             : desktopPane.getSelectedFrame();
811     if (fr == getTopFrame() || fr == getBottomFrame())
812     {
813       /* 
814        * patch for JAL-3288 (deselecting top/bottom frame closes popup menu); 
815        * it may be possible to remove this method in future
816        * if the underlying Java behaviour changes
817        */
818       if (selected)
819       {
820         moveToFront();
821       }
822       return;
823     }
824     super.setSelected(selected);
825   }
826 }