71380c7923c2f97b2b16164eff5769f8372e5202
[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);
90     getBottomFrame().setSplitFrame(this);
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      */
170     int w1 = topViewport.getIdWidth();
171     int w2 = bottomViewport.getIdWidth();
172     int w3 = Math.max(w1, w2);
173     topViewport.setIdWidth(w3);
174     bottomViewport.setIdWidth(w3);
175
176     /*
177      * Scale protein to either 1 or 3 times character width of dna
178      */
179     final AlignmentI topAlignment = topViewport.getAlignment();
180     final AlignmentI bottomAlignment = bottomViewport.getAlignment();
181     AlignmentViewport cdna = topAlignment.isNucleotide() ? topViewport
182             : (bottomAlignment.isNucleotide() ? bottomViewport : null);
183     AlignmentViewport protein = !topAlignment.isNucleotide() ? topViewport
184             : (!bottomAlignment.isNucleotide() ? bottomViewport : null);
185     if (protein != null && cdna != null)
186     {
187       int scale = protein.isScaleProteinAsCdna() ? 3 : 1;
188       protein.setCharWidth(scale * cdna.getViewStyle().getCharWidth());
189     }
190   }
191
192   /**
193    * Adjusts the divider for a sensible split of the real estate (for example,
194    * when many transcripts are shown with a single protein). This should only be
195    * called after the split pane has been laid out (made visible) so it has a
196    * height. The aim is to avoid unnecessary vertical scroll bars, while
197    * ensuring that at least 2 sequences are visible in each panel.
198    * <p>
199    * Once laid out, the user may choose to customise as they wish, so this
200    * method is not called again after the initial layout.
201    */
202   protected void adjustInitialLayout()
203   {
204     AlignFrame topFrame = (AlignFrame) getTopFrame();
205     AlignFrame bottomFrame = (AlignFrame) getBottomFrame();
206
207     /*
208      * recompute layout of top and bottom panels to reflect their
209      * actual (rather than requested) height
210      */
211     topFrame.alignPanel.adjustAnnotationHeight();
212     bottomFrame.alignPanel.adjustAnnotationHeight();
213
214     final AlignViewport topViewport = topFrame.viewport;
215     final AlignViewport bottomViewport = bottomFrame.viewport;
216     final AlignmentI topAlignment = topViewport.getAlignment();
217     final AlignmentI bottomAlignment = bottomViewport.getAlignment();
218     boolean topAnnotations = topViewport.isShowAnnotation();
219     boolean bottomAnnotations = bottomViewport.isShowAnnotation();
220     // TODO need number of visible sequences here, not #sequences - how?
221     int topCount = topAlignment.getHeight();
222     int bottomCount = bottomAlignment.getHeight();
223     int topCharHeight = topViewport.getViewStyle().getCharHeight();
224     int bottomCharHeight = bottomViewport.getViewStyle().getCharHeight();
225
226     /*
227      * calculate the minimum ratio that leaves at least the height 
228      * of two sequences (after rounding) visible in the top panel
229      */
230     int topPanelHeight = topFrame.getHeight();
231     int bottomPanelHeight = bottomFrame.getHeight();
232     int topSequencesHeight = topFrame.alignPanel.getSeqPanel().seqCanvas
233             .getHeight();
234     int topPanelMinHeight = topPanelHeight
235             - Math.max(0, topSequencesHeight - 3 * topCharHeight);
236     double totalHeight = (double) topPanelHeight + bottomPanelHeight;
237     double minRatio = topPanelMinHeight / totalHeight;
238
239     /*
240      * calculate the maximum ratio that leaves at least the height 
241      * of two sequences (after rounding) visible in the bottom panel
242      */
243     int bottomSequencesHeight = bottomFrame.alignPanel.getSeqPanel().seqCanvas
244             .getHeight();
245     int bottomPanelMinHeight = bottomPanelHeight
246             - Math.max(0, bottomSequencesHeight - 3 * bottomCharHeight);
247     double maxRatio = (totalHeight - bottomPanelMinHeight) / totalHeight;
248
249     /*
250      * estimate ratio of (topFrameContent / bottomFrameContent)
251      */
252     int insets = Platform.isAMac() ? MAC_INSETS_HEIGHT
253             : WINDOWS_INSETS_HEIGHT;
254     // allow 3 'rows' for scale, scrollbar, status bar
255     int topHeight = insets + (3 + topCount) * topCharHeight
256             + (topAnnotations ? topViewport.calcPanelHeight() : 0);
257     int bottomHeight = insets + (3 + bottomCount) * bottomCharHeight
258             + (bottomAnnotations ? bottomViewport.calcPanelHeight() : 0);
259     double ratio = ((double) topHeight)
260             / (double) (topHeight + bottomHeight);
261
262     /*
263      * limit ratio to avoid concealing all sequences
264      */
265     ratio = Math.min(ratio, maxRatio);
266     ratio = Math.max(ratio, minRatio);
267     setRelativeDividerLocation(ratio);
268   }
269
270   /**
271    * Add a listener to tidy up when the frame is closed.
272    */
273   protected void addCloseFrameListener()
274   {
275     addInternalFrameListener(new InternalFrameAdapter()
276     {
277       @Override
278       public void internalFrameClosed(InternalFrameEvent evt)
279       {
280         close();
281       };
282     });
283   }
284
285   /**
286    * Add a key listener that delegates to whichever split component the mouse is
287    * in (or does nothing if neither).
288    */
289   protected void addKeyListener()
290   {
291     addKeyListener(new KeyAdapter()
292     {
293
294       @Override
295       public void keyPressed(KeyEvent e)
296       {
297         AlignFrame af = (AlignFrame) getFrameAtMouse();
298
299         /*
300          * Intercept and override any keys here if wanted.
301          */
302         if (!overrideKey(e, af))
303         {
304           if (af != null)
305           {
306             for (KeyListener kl : af.getKeyListeners())
307             {
308               kl.keyPressed(e);
309             }
310           }
311         }
312       }
313
314       @Override
315       public void keyReleased(KeyEvent e)
316       {
317         Component c = getFrameAtMouse();
318         if (c != null)
319         {
320           for (KeyListener kl : c.getKeyListeners())
321           {
322             kl.keyReleased(e);
323           }
324         }
325       }
326
327     });
328   }
329
330   /**
331    * Returns true if the key event is overriden and actioned (or ignored) here,
332    * else returns false, indicating it should be delegated to the AlignFrame's
333    * usual handler.
334    * <p>
335    * We can't handle Cmd-Key combinations here, instead this is done by
336    * overriding key bindings.
337    * 
338    * @see addKeyOverrides
339    * @param e
340    * @param af
341    * @return
342    */
343   protected boolean overrideKey(KeyEvent e, AlignFrame af)
344   {
345     boolean actioned = false;
346     int keyCode = e.getKeyCode();
347     switch (keyCode)
348     {
349     case KeyEvent.VK_DOWN:
350       if (e.isAltDown() || !af.viewport.cursorMode)
351       {
352         /*
353          * Key down (or Alt-key-down in cursor mode) - move selected sequences
354          */
355         ((AlignFrame) getTopFrame()).moveSelectedSequences(false);
356         ((AlignFrame) getBottomFrame()).moveSelectedSequences(false);
357         actioned = true;
358         e.consume();
359       }
360       break;
361     case KeyEvent.VK_UP:
362       if (e.isAltDown() || !af.viewport.cursorMode)
363       {
364         /*
365          * Key up (or Alt-key-up in cursor mode) - move selected sequences
366          */
367         ((AlignFrame) getTopFrame()).moveSelectedSequences(true);
368         ((AlignFrame) getBottomFrame()).moveSelectedSequences(true);
369         actioned = true;
370         e.consume();
371       }
372       break;
373     default:
374     }
375     return actioned;
376   }
377
378   /**
379    * Set key bindings (recommended for Swing over key accelerators).
380    */
381   private void addKeyBindings()
382   {
383     overrideDelegatedKeyBindings();
384
385     overrideImplementedKeyBindings();
386   }
387
388   /**
389    * Override key bindings with alternative action methods implemented in this
390    * class.
391    */
392   protected void overrideImplementedKeyBindings()
393   {
394     overrideFind();
395     overrideNewView();
396     overrideCloseView();
397     overrideExpandViews();
398     overrideGatherViews();
399   }
400
401   /**
402    * Replace Cmd-W close view action with our version.
403    */
404   protected void overrideCloseView()
405   {
406     AbstractAction action;
407     /*
408      * Ctrl-W / Cmd-W - close view or window
409      */
410     KeyStroke key_cmdW = KeyStroke.getKeyStroke(KeyEvent.VK_W,
411             jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
412     action = new AbstractAction()
413     {
414       @Override
415       public void actionPerformed(ActionEvent e)
416       {
417         closeView_actionPerformed();
418       }
419     };
420     overrideKeyBinding(key_cmdW, action);
421   }
422
423   /**
424    * Replace Cmd-T new view action with our version.
425    */
426   protected void overrideNewView()
427   {
428     /*
429      * Ctrl-T / Cmd-T open new view
430      */
431     KeyStroke key_cmdT = KeyStroke.getKeyStroke(KeyEvent.VK_T,
432             jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
433     AbstractAction action = new AbstractAction()
434     {
435       @Override
436       public void actionPerformed(ActionEvent e)
437       {
438         newView_actionPerformed();
439       }
440     };
441     overrideKeyBinding(key_cmdT, action);
442   }
443
444   /**
445    * For now, delegates key events to the corresponding key accelerator for the
446    * AlignFrame that the mouse is in. Hopefully can be simplified in future if
447    * AlignFrame is changed to use key bindings rather than accelerators.
448    */
449   protected void overrideDelegatedKeyBindings()
450   {
451     if (getTopFrame() instanceof AlignFrame)
452     {
453       /*
454        * Get all accelerator keys in the top frame (the bottom should be
455        * identical) and override each one.
456        */
457       for (Entry<KeyStroke, JMenuItem> acc : ((AlignFrame) getTopFrame())
458               .getAccelerators().entrySet())
459       {
460         overrideKeyBinding(acc);
461       }
462     }
463   }
464
465   /**
466    * Overrides an AlignFrame key accelerator with our version which delegates to
467    * the action listener in whichever frame has the mouse (and does nothing if
468    * neither has).
469    * 
470    * @param acc
471    */
472   private void overrideKeyBinding(Entry<KeyStroke, JMenuItem> acc)
473   {
474     final KeyStroke ks = acc.getKey();
475     InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
476     inputMap.put(ks, ks);
477     this.getActionMap().put(ks, new AbstractAction()
478     {
479       @Override
480       public void actionPerformed(ActionEvent e)
481       {
482         Component c = getFrameAtMouse();
483         if (c != null && c instanceof AlignFrame)
484         {
485           for (ActionListener a : ((AlignFrame) c).getAccelerators().get(ks)
486                   .getActionListeners())
487           {
488             a.actionPerformed(null);
489           }
490         }
491       }
492     });
493   }
494
495   /**
496    * Replace an accelerator key's action with the specified action.
497    * 
498    * @param ks
499    */
500   protected void overrideKeyBinding(KeyStroke ks, AbstractAction action)
501   {
502     this.getActionMap().put(ks, action);
503     overrideMenuItem(ks, action);
504   }
505
506   /**
507    * Create and link new views (with matching names) in both panes.
508    * <p>
509    * Note this is _not_ multiple tabs, each hosting a split pane view, rather it
510    * is a single split pane with each split holding multiple tabs which are
511    * linked in pairs.
512    * <p>
513    * TODO implement instead with a tabbed holder in the SplitView, each tab
514    * holding a single JSplitPane. Would avoid a duplicated tab, at the cost of
515    * some additional coding.
516    */
517   protected void newView_actionPerformed()
518   {
519     AlignFrame topFrame = (AlignFrame) getTopFrame();
520     AlignFrame bottomFrame = (AlignFrame) getBottomFrame();
521     final boolean scaleProteinAsCdna = topFrame.viewport
522             .isScaleProteinAsCdna();
523
524     AlignmentPanel newTopPanel = topFrame.newView(null, true);
525     AlignmentPanel newBottomPanel = bottomFrame.newView(null, true);
526
527     /*
528      * This currently (for the first new view only) leaves the top pane on tab 0
529      * but the bottom on tab 1. This results from 'setInitialTabVisible' echoing
530      * from the bottom back to the first frame. Next line is a fudge to work
531      * around this. TODO find a better way.
532      */
533     if (topFrame.getTabIndex() != bottomFrame.getTabIndex())
534     {
535       topFrame.setDisplayedView(newTopPanel);
536     }
537
538     newBottomPanel.av.setViewName(newTopPanel.av.getViewName());
539     newTopPanel.av.setCodingComplement(newBottomPanel.av);
540
541     /*
542      * These lines can be removed once scaleProteinAsCdna is added to element
543      * Viewport in jalview.xsd, as Jalview2XML.copyAlignPanel will then take
544      * care of it
545      */
546     newTopPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
547     newBottomPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
548
549     /*
550      * Line up id labels etc
551      */
552     adjustLayout();
553
554     final StructureSelectionManager ssm = StructureSelectionManager
555             .getStructureSelectionManager(Desktop.instance);
556     ssm.addCommandListener(newTopPanel.av);
557     ssm.addCommandListener(newBottomPanel.av);
558   }
559
560   /**
561    * Close the currently selected view in both panes. If there is only one view,
562    * close this split frame.
563    */
564   protected void closeView_actionPerformed()
565   {
566     int viewCount = ((AlignFrame) getTopFrame()).getAlignPanels().size();
567     if (viewCount < 2)
568     {
569       close();
570       return;
571     }
572
573     AlignmentPanel topPanel = ((AlignFrame) getTopFrame()).alignPanel;
574     AlignmentPanel bottomPanel = ((AlignFrame) getBottomFrame()).alignPanel;
575
576     ((AlignFrame) getTopFrame()).closeView(topPanel);
577     ((AlignFrame) getBottomFrame()).closeView(bottomPanel);
578
579   }
580
581   /**
582    * Close child frames and this split frame.
583    */
584   public void close()
585   {
586     ((AlignFrame) getTopFrame()).closeMenuItem_actionPerformed(true);
587     ((AlignFrame) getBottomFrame()).closeMenuItem_actionPerformed(true);
588     try
589     {
590       this.setClosed(true);
591     } catch (PropertyVetoException e)
592     {
593       // ignore
594     }
595   }
596
597   /**
598    * Replace AlignFrame 'expand views' action with SplitFrame version.
599    */
600   protected void overrideExpandViews()
601   {
602     KeyStroke key_X = KeyStroke.getKeyStroke(KeyEvent.VK_X, 0, false);
603     AbstractAction action = new AbstractAction()
604     {
605       @Override
606       public void actionPerformed(ActionEvent e)
607       {
608         expandViews_actionPerformed();
609       }
610     };
611     overrideMenuItem(key_X, action);
612   }
613
614   /**
615    * Replace AlignFrame 'gather views' action with SplitFrame version.
616    */
617   protected void overrideGatherViews()
618   {
619     KeyStroke key_G = KeyStroke.getKeyStroke(KeyEvent.VK_G, 0, false);
620     AbstractAction action = new AbstractAction()
621     {
622       @Override
623       public void actionPerformed(ActionEvent e)
624       {
625         gatherViews_actionPerformed();
626       }
627     };
628     overrideMenuItem(key_G, action);
629   }
630
631   /**
632    * Override the menu action associated with the keystroke in the child frames,
633    * replacing it with the given action.
634    * 
635    * @param ks
636    * @param action
637    */
638   private void overrideMenuItem(KeyStroke ks, AbstractAction action)
639   {
640     overrideMenuItem(ks, action, getTopFrame());
641     overrideMenuItem(ks, action, getBottomFrame());
642   }
643
644   /**
645    * Override the menu action associated with the keystroke in one child frame,
646    * replacing it with the given action. Mwahahahaha.
647    * 
648    * @param key
649    * @param action
650    * @param comp
651    */
652   private void overrideMenuItem(KeyStroke key, final AbstractAction action,
653           JComponent comp)
654   {
655     if (comp instanceof AlignFrame)
656     {
657       JMenuItem mi = ((AlignFrame) comp).getAccelerators().get(key);
658       if (mi != null)
659       {
660         for (ActionListener al : mi.getActionListeners())
661         {
662           mi.removeActionListener(al);
663         }
664         mi.addActionListener(new ActionListener()
665         {
666           @Override
667           public void actionPerformed(ActionEvent e)
668           {
669             action.actionPerformed(e);
670           }
671         });
672       }
673     }
674   }
675
676   /**
677    * Expand any multiple views (which are always in pairs) into separate split
678    * frames.
679    */
680   protected void expandViews_actionPerformed()
681   {
682     Desktop.instance.explodeViews(this);
683   }
684
685   /**
686    * Gather any other SplitFrame views of this alignment back in as multiple
687    * (pairs of) views in this SplitFrame.
688    */
689   protected void gatherViews_actionPerformed()
690   {
691     Desktop.instance.gatherViews(this);
692   }
693
694   /**
695    * Returns the alignment in the complementary frame to the one given.
696    */
697   @Override
698   public AlignmentI getComplement(Object alignFrame)
699   {
700     if (alignFrame == this.getTopFrame())
701     {
702       return ((AlignFrame) getBottomFrame()).viewport.getAlignment();
703     }
704     else if (alignFrame == this.getBottomFrame())
705     {
706       return ((AlignFrame) getTopFrame()).viewport.getAlignment();
707     }
708     return null;
709   }
710
711   /**
712    * Returns the title of the complementary frame to the one given.
713    */
714   @Override
715   public String getComplementTitle(Object alignFrame)
716   {
717     if (alignFrame == this.getTopFrame())
718     {
719       return ((AlignFrame) getBottomFrame()).getTitle();
720     }
721     else if (alignFrame == this.getBottomFrame())
722     {
723       return ((AlignFrame) getTopFrame()).getTitle();
724     }
725     return null;
726   }
727
728   /**
729    * Set the 'other half' to hidden / revealed.
730    */
731   @Override
732   public void setComplementVisible(Object alignFrame, boolean show)
733   {
734     /*
735      * Hiding the AlignPanel suppresses unnecessary repaints
736      */
737     if (alignFrame == getTopFrame())
738     {
739       ((AlignFrame) getBottomFrame()).alignPanel.setVisible(show);
740     }
741     else if (alignFrame == getBottomFrame())
742     {
743       ((AlignFrame) getTopFrame()).alignPanel.setVisible(show);
744     }
745     super.setComplementVisible(alignFrame, show);
746   }
747
748   /**
749    * return the AlignFrames held by this container
750    * 
751    * @return { Top alignFrame (Usually CDS), Bottom AlignFrame (Usually
752    *         Protein)}
753    */
754   public List<AlignFrame> getAlignFrames()
755   {
756     return Arrays
757             .asList(new AlignFrame[]
758             { (AlignFrame) getTopFrame(), (AlignFrame) getBottomFrame() });
759   }
760
761   /**
762    * Replace Cmd-F Find action with our version. This is necessary because the
763    * 'default' Finder searches in the first AlignFrame it finds. We need it to
764    * search in the half of the SplitFrame that has the mouse.
765    */
766   protected void overrideFind()
767   {
768     /*
769      * Ctrl-F / Cmd-F open Finder dialog, 'focused' on the right alignment
770      */
771     KeyStroke key_cmdF = KeyStroke.getKeyStroke(KeyEvent.VK_F,
772             jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
773     AbstractAction action = new AbstractAction()
774     {
775       @Override
776       public void actionPerformed(ActionEvent e)
777       {
778         Component c = getFrameAtMouse();
779         if (c != null && c instanceof AlignFrame)
780         {
781           AlignFrame af = (AlignFrame) c;
782           new Finder(af.viewport, af.alignPanel);
783         }
784       }
785     };
786     overrideKeyBinding(key_cmdF, action);
787   }
788
789   /**
790    * Override to do nothing if triggered from one of the child frames
791    */
792   @Override
793   public void setSelected(boolean selected) throws PropertyVetoException
794   {
795     JDesktopPane desktopPane = getDesktopPane();
796     JInternalFrame fr = desktopPane == null ? null
797             : desktopPane.getSelectedFrame();
798     if (fr == getTopFrame() || fr == getBottomFrame())
799     {
800       /* 
801        * patch for JAL-3288 (deselecting top/bottom frame closes popup menu); 
802        * it may be possible to remove this method in future
803        * if the underlying Java behaviour changes
804        */
805       if (selected)
806       {
807         moveToFront();
808       }
809       return;
810     }
811     super.setSelected(selected);
812   }
813 }