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