Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 16:11:35 GMT
  2. Package jalview.gui

File SplitFrame.java

 

Coverage histogram

../../img/srcFileCovDistChart4.png
49% of files have more coverage

Code metrics

106
296
51
1
1,110
741
122
0.41
5.8
51
2.39

Classes

Class Line # Actions
SplitFrame 77 296 122
0.357615935.8%
 

Contributing tests

This file is covered by 2 tests. .

Source view

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.AlignViewportI;
24    import jalview.api.AlignViewControllerGuiI;
25    import jalview.api.FeatureSettingsControllerI;
26    import jalview.api.SplitContainerI;
27    import jalview.controller.FeatureSettingsControllerGuiI;
28    import jalview.datamodel.AlignmentI;
29    import jalview.jbgui.GAlignFrame;
30    import jalview.jbgui.GSplitFrame;
31    import jalview.structure.StructureSelectionManager;
32    import jalview.util.MessageManager;
33    import jalview.util.Platform;
34    import jalview.viewmodel.AlignmentViewport;
35    import java.awt.BorderLayout;
36    import java.awt.Component;
37    import java.awt.Dimension;
38    import java.awt.event.ActionEvent;
39    import java.awt.event.ActionListener;
40    import java.awt.event.KeyAdapter;
41    import java.awt.event.KeyEvent;
42    import java.awt.event.KeyListener;
43    import java.beans.PropertyVetoException;
44    import java.util.Arrays;
45    import java.util.List;
46    import java.util.Map.Entry;
47   
48    import javax.swing.AbstractAction;
49    import javax.swing.InputMap;
50    import javax.swing.JButton;
51    import javax.swing.JComponent;
52    import javax.swing.JDesktopPane;
53    import javax.swing.JInternalFrame;
54    import javax.swing.JLayeredPane;
55    import javax.swing.JMenuItem;
56    import javax.swing.JPanel;
57    import javax.swing.JTabbedPane;
58    import javax.swing.KeyStroke;
59    import javax.swing.event.ChangeEvent;
60    import javax.swing.event.ChangeListener;
61    import javax.swing.event.InternalFrameAdapter;
62    import javax.swing.event.InternalFrameEvent;
63   
64   
65    /**
66    * An internal frame on the desktop that hosts a horizontally split view of
67    * linked DNA and Protein alignments. Additional views can be created in linked
68    * pairs, expanded to separate split frames, or regathered into a single frame.
69    * <p>
70    * (Some) operations on each alignment are automatically mirrored on the other.
71    * These include mouseover (highlighting), sequence and column selection,
72    * sequence ordering and sorting, and grouping, colouring and sorting by tree.
73    *
74    * @author gmcarstairs
75    *
76    */
 
77    public class SplitFrame extends GSplitFrame implements SplitContainerI
78    {
79    private static final int WINDOWS_INSETS_WIDTH = 28; // tbc
80   
81    private static final int MAC_INSETS_WIDTH = 28;
82   
83    private static final int WINDOWS_INSETS_HEIGHT = 50; // tbc
84   
85    private static final int MAC_INSETS_HEIGHT = 50;
86   
87    private static final int DESKTOP_DECORATORS_HEIGHT = 65;
88   
89    private static final long serialVersionUID = 1L;
90   
91    /**
92    * geometry for Feature Settings Holder
93    */
94    private static final int FS_MIN_WIDTH = 400;
95   
96    private static final int FS_MIN_HEIGHT = 400;
97   
 
98  3 toggle public SplitFrame(GAlignFrame top, GAlignFrame bottom)
99    {
100  3 super(top, bottom);
101  3 init();
102    }
103   
104    /**
105    * Initialise this frame.
106    */
 
107  3 toggle protected void init()
108    {
109  3 setFrameIcon(null);
110  3 getTopFrame().setSplitFrame(this);
111  3 getBottomFrame().setSplitFrame(this);
112  3 getTopFrame().setVisible(true);
113  3 getBottomFrame().setVisible(true);
114   
115  3 ((AlignFrame) getTopFrame()).getViewport().setCodingComplement(
116    ((AlignFrame) getBottomFrame()).getViewport());
117   
118    /*
119    * estimate width and height of SplitFrame; this.getInsets() doesn't seem to
120    * give the full additional size (a few pixels short)
121    */
122  3 int widthFudge = Platform.isAMacAndNotJS() ? MAC_INSETS_WIDTH
123    : WINDOWS_INSETS_WIDTH;
124  3 int heightFudge = Platform.isAMacAndNotJS() ? MAC_INSETS_HEIGHT
125    : WINDOWS_INSETS_HEIGHT;
126  3 int width = ((AlignFrame) getTopFrame()).getWidth() + widthFudge;
127  3 int height = ((AlignFrame) getTopFrame()).getHeight()
128    + ((AlignFrame) getBottomFrame()).getHeight() + DIVIDER_SIZE
129    + heightFudge;
130  3 height = fitHeightToDesktop(height);
131  3 setSize(width, height);
132   
133  3 adjustLayout();
134   
135  3 addCloseFrameListener();
136   
137  3 addKeyListener();
138   
139  3 addKeyBindings();
140   
141  3 addCommandListeners();
142    }
143   
144    /**
145    * Reduce the height if too large to fit in the Desktop. Also adjust the
146    * divider location in proportion.
147    *
148    * @param height
149    * in pixels
150    * @return original or reduced height
151    */
 
152  3 toggle public int fitHeightToDesktop(int height)
153    {
154    // allow about 65 pixels for Desktop decorators on Windows
155   
156  3 int newHeight = Math.min(height,
157    Desktop.getInstance().getHeight() - DESKTOP_DECORATORS_HEIGHT);
158  3 if (newHeight != height)
159    {
160  3 int oldDividerLocation = getDividerLocation();
161  3 setDividerLocation(oldDividerLocation * newHeight / height);
162    }
163  3 return newHeight;
164    }
165   
166    /**
167    * Set the top and bottom frames to listen to each others Commands (e.g. Edit,
168    * Order).
169    */
 
170  3 toggle protected void addCommandListeners()
171    {
172    // TODO if CommandListener is only ever 1:1 for complementary views,
173    // may change broadcast pattern to direct messaging (more efficient)
174  3 final StructureSelectionManager ssm = StructureSelectionManager
175    .getStructureSelectionManager(Desktop.getInstance());
176  3 ssm.addCommandListener(((AlignFrame) getTopFrame()).getViewport());
177  3 ssm.addCommandListener(((AlignFrame) getBottomFrame()).getViewport());
178    }
179   
180    /**
181    * Do any tweaking and twerking of the layout wanted.
182    */
 
183  3 toggle public void adjustLayout()
184    {
185  3 final AlignViewport topViewport = ((AlignFrame) getTopFrame()).viewport;
186  3 final AlignViewport bottomViewport = ((AlignFrame) getBottomFrame()).viewport;
187   
188    /*
189    * Ensure sequence ids are the same width so sequences line up
190    */
191  3 int w1 = topViewport.getIdWidth();
192  3 int w2 = bottomViewport.getIdWidth();
193  3 int w3 = Math.max(w1, w2);
194  3 topViewport.setIdWidth(w3);
195  3 bottomViewport.setIdWidth(w3);
196   
197    /*
198    * Scale protein to either 1 or 3 times character width of dna
199    */
200  3 final AlignmentI topAlignment = topViewport.getAlignment();
201  3 final AlignmentI bottomAlignment = bottomViewport.getAlignment();
202  3 AlignmentViewport cdna = topAlignment.isNucleotide() ? topViewport
203  0 : (bottomAlignment.isNucleotide() ? bottomViewport : null);
204  3 AlignmentViewport protein = !topAlignment.isNucleotide() ? topViewport
205  3 : (!bottomAlignment.isNucleotide() ? bottomViewport : null);
206  3 if (protein != null && cdna != null)
207    {
208  3 int scale = protein.isScaleProteinAsCdna() ? 3 : 1;
209  3 protein.setCharWidth(scale * cdna.getViewStyle().getCharWidth());
210    }
211    }
212   
213    /**
214    * Adjusts the divider for a sensible split of the real estate (for example,
215    * when many transcripts are shown with a single protein). This should only be
216    * called after the split pane has been laid out (made visible) so it has a
217    * height. The aim is to avoid unnecessary vertical scroll bars, while
218    * ensuring that at least 2 sequences are visible in each panel.
219    * <p>
220    * Once laid out, the user may choose to customise as they wish, so this
221    * method is not called again after the initial layout.
222    */
 
223  2 toggle protected void adjustInitialLayout()
224    {
225  2 AlignFrame topFrame = (AlignFrame) getTopFrame();
226  2 AlignFrame bottomFrame = (AlignFrame) getBottomFrame();
227   
228    /*
229    * recompute layout of top and bottom panels to reflect their
230    * actual (rather than requested) height
231    */
232  2 topFrame.alignPanel.adjustAnnotationHeight();
233  2 bottomFrame.alignPanel.adjustAnnotationHeight();
234   
235  2 final AlignViewportI topViewport = topFrame.viewport;
236  2 final AlignViewportI bottomViewport = bottomFrame.viewport;
237  2 final AlignmentI topAlignment = topViewport.getAlignment();
238  2 final AlignmentI bottomAlignment = bottomViewport.getAlignment();
239  2 boolean topAnnotations = topViewport.isShowAnnotation();
240  2 boolean bottomAnnotations = bottomViewport.isShowAnnotation();
241    // TODO need number of visible sequences here, not #sequences - how?
242  2 int topCount = topAlignment.getHeight();
243  2 int bottomCount = bottomAlignment.getHeight();
244  2 int topCharHeight = topViewport.getViewStyle().getCharHeight();
245  2 int bottomCharHeight = bottomViewport.getViewStyle().getCharHeight();
246   
247    /*
248    * calculate the minimum ratio that leaves at least the height
249    * of two sequences (after rounding) visible in the top panel
250    */
251  2 int topPanelHeight = topFrame.getHeight();
252  2 int bottomPanelHeight = bottomFrame.getHeight();
253  2 int topSequencesHeight = topFrame.alignPanel.getSeqPanel().seqCanvas
254    .getHeight();
255  2 int topPanelMinHeight = topPanelHeight
256    - Math.max(0, topSequencesHeight - 3 * topCharHeight);
257  2 double totalHeight = (double) topPanelHeight + bottomPanelHeight;
258  2 double minRatio = topPanelMinHeight / totalHeight;
259   
260    /*
261    * calculate the maximum ratio that leaves at least the height
262    * of two sequences (after rounding) visible in the bottom panel
263    */
264  2 int bottomSequencesHeight = bottomFrame.alignPanel.getSeqPanel().seqCanvas
265    .getHeight();
266  2 int bottomPanelMinHeight = bottomPanelHeight
267    - Math.max(0, bottomSequencesHeight - 3 * bottomCharHeight);
268  2 double maxRatio = (totalHeight - bottomPanelMinHeight) / totalHeight;
269   
270    /*
271    * estimate ratio of (topFrameContent / bottomFrameContent)
272    */
273  2 int insets = Platform.isAMacAndNotJS() ? MAC_INSETS_HEIGHT
274    : WINDOWS_INSETS_HEIGHT;
275    // allow 3 'rows' for scale, scrollbar, status bar
276  2 int topHeight = insets + (3 + topCount) * topCharHeight
277  2 + (topAnnotations ? topViewport.calcPanelHeight() : 0);
278  2 int bottomHeight = insets + (3 + bottomCount) * bottomCharHeight
279  2 + (bottomAnnotations ? bottomViewport.calcPanelHeight() : 0);
280  2 double ratio = ((double) topHeight)
281    / (double) (topHeight + bottomHeight);
282   
283    /*
284    * limit ratio to avoid concealing all sequences
285    */
286  2 ratio = Math.min(ratio, maxRatio);
287  2 ratio = Math.max(ratio, minRatio);
288  2 setRelativeDividerLocation(ratio);
289    }
290   
291    /**
292    * Add a listener to tidy up when the frame is closed.
293    */
 
294  3 toggle protected void addCloseFrameListener()
295    {
296  3 addInternalFrameListener(new InternalFrameAdapter()
297    {
 
298  3 toggle @Override
299    public void internalFrameClosed(InternalFrameEvent evt)
300    {
301  3 close();
302    };
303    });
304    }
305   
306    /**
307    * Add a key listener that delegates to whichever split component the mouse is
308    * in (or does nothing if neither).
309    */
 
310  3 toggle protected void addKeyListener()
311    {
312  3 addKeyListener(new KeyAdapter()
313    {
314   
 
315  0 toggle @Override
316    public void keyPressed(KeyEvent e)
317    {
318  0 AlignFrame af = (AlignFrame) getFrameAtMouse();
319   
320    /*
321    * Intercept and override any keys here if wanted.
322    */
323  0 if (!overrideKey(e, af))
324    {
325  0 if (af != null)
326    {
327  0 for (KeyListener kl : af.getKeyListeners())
328    {
329  0 kl.keyPressed(e);
330    }
331    }
332    }
333    }
334   
 
335  0 toggle @Override
336    public void keyReleased(KeyEvent e)
337    {
338  0 Component c = getFrameAtMouse();
339  0 if (c != null)
340    {
341  0 for (KeyListener kl : c.getKeyListeners())
342    {
343  0 kl.keyReleased(e);
344    }
345    }
346    }
347   
348    });
349    }
350   
351    /**
352    * Returns true if the key event is overriden and actioned (or ignored) here,
353    * else returns false, indicating it should be delegated to the AlignFrame's
354    * usual handler.
355    * <p>
356    * We can't handle Cmd-Key combinations here, instead this is done by
357    * overriding key bindings.
358    *
359    * @see addKeyOverrides
360    * @param e
361    * @param af
362    * @return
363    */
 
364  0 toggle protected boolean overrideKey(KeyEvent e, AlignFrame af)
365    {
366  0 boolean actioned = false;
367  0 int keyCode = e.getKeyCode();
368  0 switch (keyCode)
369    {
370  0 case KeyEvent.VK_DOWN:
371  0 if (e.isAltDown() || !af.viewport.cursorMode)
372    {
373    /*
374    * Key down (or Alt-key-down in cursor mode) - move selected sequences
375    */
376  0 ((AlignFrame) getTopFrame()).moveSelectedSequences(false);
377  0 ((AlignFrame) getBottomFrame()).moveSelectedSequences(false);
378  0 actioned = true;
379  0 e.consume();
380    }
381  0 break;
382  0 case KeyEvent.VK_UP:
383  0 if (e.isAltDown() || !af.viewport.cursorMode)
384    {
385    /*
386    * Key up (or Alt-key-up in cursor mode) - move selected sequences
387    */
388  0 ((AlignFrame) getTopFrame()).moveSelectedSequences(true);
389  0 ((AlignFrame) getBottomFrame()).moveSelectedSequences(true);
390  0 actioned = true;
391  0 e.consume();
392    }
393  0 break;
394  0 default:
395    }
396  0 return actioned;
397    }
398   
399    /**
400    * Set key bindings (recommended for Swing over key accelerators).
401    */
 
402  3 toggle private void addKeyBindings()
403    {
404  3 overrideDelegatedKeyBindings();
405   
406  3 overrideImplementedKeyBindings();
407    }
408   
409    /**
410    * Override key bindings with alternative action methods implemented in this
411    * class.
412    */
 
413  3 toggle protected void overrideImplementedKeyBindings()
414    {
415  3 overrideFind();
416  3 overrideNewView();
417  3 overrideCloseView();
418  3 overrideExpandViews();
419  3 overrideGatherViews();
420    }
421   
422    /**
423    * Replace Cmd-W close view action with our version.
424    */
 
425  3 toggle protected void overrideCloseView()
426    {
427  3 AbstractAction action;
428    /*
429    * Ctrl-W / Cmd-W - close view or window
430    */
431  3 KeyStroke key_cmdW = KeyStroke.getKeyStroke(KeyEvent.VK_W,
432    Platform.SHORTCUT_KEY_MASK, false);
433  3 action = new AbstractAction()
434    {
 
435  0 toggle @Override
436    public void actionPerformed(ActionEvent e)
437    {
438  0 closeView_actionPerformed();
439    }
440    };
441  3 overrideKeyBinding(key_cmdW, action);
442    }
443   
444    /**
445    * Replace Cmd-T new view action with our version.
446    */
 
447  3 toggle protected void overrideNewView()
448    {
449    /*
450    * Ctrl-T / Cmd-T open new view
451    */
452  3 KeyStroke key_cmdT = KeyStroke.getKeyStroke(KeyEvent.VK_T,
453    Platform.SHORTCUT_KEY_MASK, false);
454  3 AbstractAction action = new AbstractAction()
455    {
 
456  0 toggle @Override
457    public void actionPerformed(ActionEvent e)
458    {
459  0 newView_actionPerformed();
460    }
461    };
462  3 overrideKeyBinding(key_cmdT, action);
463    }
464   
465    /**
466    * For now, delegates key events to the corresponding key accelerator for the
467    * AlignFrame that the mouse is in. Hopefully can be simplified in future if
468    * AlignFrame is changed to use key bindings rather than accelerators.
469    */
 
470  3 toggle protected void overrideDelegatedKeyBindings()
471    {
472  3 if (getTopFrame() instanceof AlignFrame)
473    {
474    /*
475    * Get all accelerator keys in the top frame (the bottom should be
476    * identical) and override each one.
477    */
478  3 for (Entry<KeyStroke, JMenuItem> acc : ((AlignFrame) getTopFrame())
479    .getAccelerators().entrySet())
480    {
481  81 overrideKeyBinding(acc);
482    }
483    }
484    }
485   
486    /**
487    * Overrides an AlignFrame key accelerator with our version which delegates to
488    * the action listener in whichever frame has the mouse (and does nothing if
489    * neither has).
490    *
491    * @param acc
492    */
 
493  81 toggle private void overrideKeyBinding(Entry<KeyStroke, JMenuItem> acc)
494    {
495  81 final KeyStroke ks = acc.getKey();
496  81 InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
497  81 inputMap.put(ks, ks);
498  81 this.getActionMap().put(ks, new AbstractAction()
499    {
 
500  0 toggle @Override
501    public void actionPerformed(ActionEvent e)
502    {
503  0 Component c = getFrameAtMouse();
504  0 if (c != null && c instanceof AlignFrame)
505    {
506  0 for (ActionListener a : ((AlignFrame) c).getAccelerators().get(ks)
507    .getActionListeners())
508    {
509  0 a.actionPerformed(null);
510    }
511    }
512    }
513    });
514    }
515   
516    /**
517    * Replace an accelerator key's action with the specified action.
518    *
519    * @param ks
520    */
 
521  9 toggle protected void overrideKeyBinding(KeyStroke ks, AbstractAction action)
522    {
523  9 this.getActionMap().put(ks, action);
524  9 overrideMenuItem(ks, action);
525    }
526   
527    /**
528    * Create and link new views (with matching names) in both panes.
529    * <p>
530    * Note this is _not_ multiple tabs, each hosting a split pane view, rather it
531    * is a single split pane with each split holding multiple tabs which are
532    * linked in pairs.
533    * <p>
534    * TODO implement instead with a tabbed holder in the SplitView, each tab
535    * holding a single JSplitPane. Would avoid a duplicated tab, at the cost of
536    * some additional coding.
537    */
 
538  0 toggle protected void newView_actionPerformed()
539    {
540  0 AlignFrame topFrame = (AlignFrame) getTopFrame();
541  0 AlignFrame bottomFrame = (AlignFrame) getBottomFrame();
542  0 final boolean scaleProteinAsCdna = topFrame.viewport
543    .isScaleProteinAsCdna();
544   
545  0 AlignmentPanel newTopPanel = topFrame.newView(null, true);
546  0 AlignmentPanel newBottomPanel = bottomFrame.newView(null, true);
547   
548    /*
549    * This currently (for the first new view only) leaves the top pane on tab 0
550    * but the bottom on tab 1. This results from 'setInitialTabVisible' echoing
551    * from the bottom back to the first frame. Next line is a fudge to work
552    * around this. TODO find a better way.
553    */
554  0 if (topFrame.getTabIndex() != bottomFrame.getTabIndex())
555    {
556  0 topFrame.setDisplayedView(newTopPanel);
557    }
558   
559  0 newBottomPanel.av.setViewName(newTopPanel.av.getViewName());
560  0 newTopPanel.av.setCodingComplement(newBottomPanel.av);
561   
562    /*
563    * These lines can be removed once scaleProteinAsCdna is added to element
564    * Viewport in jalview.xsd, as Jalview2XML.copyAlignPanel will then take
565    * care of it
566    */
567  0 newTopPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
568  0 newBottomPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
569   
570    /*
571    * Line up id labels etc
572    */
573  0 adjustLayout();
574   
575  0 final StructureSelectionManager ssm = StructureSelectionManager
576    .getStructureSelectionManager(Desktop.getInstance());
577  0 ssm.addCommandListener(newTopPanel.av);
578  0 ssm.addCommandListener(newBottomPanel.av);
579    }
580   
581    /**
582    * Close the currently selected view in both panes. If there is only one view,
583    * close this split frame.
584    */
 
585  0 toggle protected void closeView_actionPerformed()
586    {
587  0 int viewCount = ((AlignFrame) getTopFrame()).getAlignPanels().size();
588  0 if (viewCount < 2)
589    {
590  0 close();
591  0 return;
592    }
593   
594  0 AlignmentPanel topPanel = ((AlignFrame) getTopFrame()).alignPanel;
595  0 AlignmentPanel bottomPanel = ((AlignFrame) getBottomFrame()).alignPanel;
596   
597  0 ((AlignFrame) getTopFrame()).closeView(topPanel);
598  0 ((AlignFrame) getBottomFrame()).closeView(bottomPanel);
599   
600    }
601   
602    /**
603    * Close child frames and this split frame.
604    */
 
605  3 toggle public void close()
606    {
607  3 ((AlignFrame) getTopFrame()).closeMenuItem_actionPerformed(true);
608  3 ((AlignFrame) getBottomFrame()).closeMenuItem_actionPerformed(true);
609  3 try
610    {
611  3 this.setClosed(true);
612    } catch (PropertyVetoException e)
613    {
614    // ignore
615    }
616    }
617   
618    /**
619    * Replace AlignFrame 'expand views' action with SplitFrame version.
620    */
 
621  3 toggle protected void overrideExpandViews()
622    {
623  3 KeyStroke key_X = KeyStroke.getKeyStroke(KeyEvent.VK_X, 0, false);
624  3 AbstractAction action = new AbstractAction()
625    {
 
626  0 toggle @Override
627    public void actionPerformed(ActionEvent e)
628    {
629  0 expandViews_actionPerformed();
630    }
631    };
632  3 overrideMenuItem(key_X, action);
633    }
634   
635    /**
636    * Replace AlignFrame 'gather views' action with SplitFrame version.
637    */
 
638  3 toggle protected void overrideGatherViews()
639    {
640  3 KeyStroke key_G = KeyStroke.getKeyStroke(KeyEvent.VK_G, 0, false);
641  3 AbstractAction action = new AbstractAction()
642    {
 
643  0 toggle @Override
644    public void actionPerformed(ActionEvent e)
645    {
646  0 gatherViews_actionPerformed();
647    }
648    };
649  3 overrideMenuItem(key_G, action);
650    }
651   
652    /**
653    * Override the menu action associated with the keystroke in the child frames,
654    * replacing it with the given action.
655    *
656    * @param ks
657    * @param action
658    */
 
659  15 toggle private void overrideMenuItem(KeyStroke ks, AbstractAction action)
660    {
661  15 overrideMenuItem(ks, action, getTopFrame());
662  15 overrideMenuItem(ks, action, getBottomFrame());
663    }
664   
665    /**
666    * Override the menu action associated with the keystroke in one child frame,
667    * replacing it with the given action. Mwahahahaha.
668    *
669    * @param key
670    * @param action
671    * @param comp
672    */
 
673  30 toggle private void overrideMenuItem(KeyStroke key, final AbstractAction action,
674    JComponent comp)
675    {
676  30 if (comp instanceof AlignFrame)
677    {
678  30 JMenuItem mi = ((AlignFrame) comp).getAccelerators().get(key);
679  30 if (mi != null)
680    {
681  30 for (ActionListener al : mi.getActionListeners())
682    {
683  30 mi.removeActionListener(al);
684    }
685  30 mi.addActionListener(new ActionListener()
686    {
 
687  0 toggle @Override
688    public void actionPerformed(ActionEvent e)
689    {
690  0 action.actionPerformed(e);
691    }
692    });
693    }
694    }
695    }
696   
697    /**
698    * Expand any multiple views (which are always in pairs) into separate split
699    * frames.
700    */
 
701  0 toggle protected void expandViews_actionPerformed()
702    {
703  0 Desktop.getInstance().explodeViews(this);
704    }
705   
706    /**
707    * Gather any other SplitFrame views of this alignment back in as multiple
708    * (pairs of) views in this SplitFrame.
709    */
 
710  0 toggle protected void gatherViews_actionPerformed()
711    {
712  0 Desktop.getInstance().gatherViews(this);
713    }
714   
715    /**
716    * Returns the alignment in the complementary frame to the one given.
717    */
 
718  0 toggle @Override
719    public AlignmentI getComplement(Object alignFrame)
720    {
721  0 if (alignFrame == this.getTopFrame())
722    {
723  0 return ((AlignFrame) getBottomFrame()).viewport.getAlignment();
724    }
725  0 else if (alignFrame == this.getBottomFrame())
726    {
727  0 return ((AlignFrame) getTopFrame()).viewport.getAlignment();
728    }
729  0 return null;
730    }
731   
732    /**
733    * Returns the title of the complementary frame to the one given.
734    */
 
735  0 toggle @Override
736    public String getComplementTitle(Object alignFrame)
737    {
738  0 if (alignFrame == this.getTopFrame())
739    {
740  0 return ((AlignFrame) getBottomFrame()).getTitle();
741    }
742  0 else if (alignFrame == this.getBottomFrame())
743    {
744  0 return ((AlignFrame) getTopFrame()).getTitle();
745    }
746  0 return null;
747    }
748   
749    /**
750    * Set the 'other half' to hidden / revealed.
751    */
 
752  0 toggle @Override
753    public void setComplementVisible(Object alignFrame, boolean show)
754    {
755    /*
756    * Hiding the AlignPanel suppresses unnecessary repaints
757    */
758  0 if (alignFrame == getTopFrame())
759    {
760  0 ((AlignFrame) getBottomFrame()).alignPanel.setVisible(show);
761    }
762  0 else if (alignFrame == getBottomFrame())
763    {
764  0 ((AlignFrame) getTopFrame()).alignPanel.setVisible(show);
765    }
766  0 super.setComplementVisible(alignFrame, show);
767    }
768   
769    /**
770    * return the AlignFrames held by this container
771    *
772    * @return { Top alignFrame (Usually CDS), Bottom AlignFrame (Usually
773    * Protein)}
774    */
 
775  0 toggle public List<AlignFrame> getAlignFrames()
776    {
777  0 return Arrays
778    .asList(new AlignFrame[]
779    { (AlignFrame) getTopFrame(), (AlignFrame) getBottomFrame() });
780    }
781   
 
782  0 toggle @Override
783    public AlignFrame getComplementAlignFrame(
784    AlignViewControllerGuiI alignFrame)
785    {
786  0 if (getTopFrame() == alignFrame)
787    {
788  0 return (AlignFrame) getBottomFrame();
789    }
790  0 if (getBottomFrame() == alignFrame)
791    {
792  0 return (AlignFrame) getTopFrame();
793    }
794    // we didn't know anything about this frame...
795  0 return null;
796    }
797   
798    /**
799    * Replace Cmd-F Find action with our version. This is necessary because the
800    * 'default' Finder searches in the first AlignFrame it finds. We need it to
801    * search in the half of the SplitFrame that has the mouse.
802    */
 
803  3 toggle protected void overrideFind()
804    {
805    /*
806    * Ctrl-F / Cmd-F open Finder dialog, 'focused' on the right alignment
807    */
808  3 KeyStroke key_cmdF = KeyStroke.getKeyStroke(KeyEvent.VK_F,
809    Platform.SHORTCUT_KEY_MASK, false);
810  3 AbstractAction action = new AbstractAction()
811    {
 
812  0 toggle @Override
813    public void actionPerformed(ActionEvent e)
814    {
815  0 Component c = getFrameAtMouse();
816  0 if (c != null && c instanceof AlignFrame)
817    {
818  0 AlignFrame af = (AlignFrame) c;
819  0 boolean dna = af.getViewport().getAlignment().isNucleotide();
820  0 String scope = MessageManager.getString("label.in") + " "
821  0 + (dna ? MessageManager.getString("label.nucleotide")
822    : MessageManager.getString("label.protein"));
823  0 new Finder(af.alignPanel, true, scope);
824    }
825    }
826    };
827  3 overrideKeyBinding(key_cmdF, action);
828    }
829   
830    /**
831    * Override to do nothing if triggered from one of the child frames
832    */
 
833  11 toggle @Override
834    public void setSelected(boolean selected) throws PropertyVetoException
835    {
836  11 JDesktopPane desktopPane = getDesktopPane();
837  11 JInternalFrame fr = desktopPane == null ? null
838    : desktopPane.getSelectedFrame();
839  11 if (fr == getTopFrame() || fr == getBottomFrame())
840    {
841    /*
842    * patch for JAL-3288 (deselecting top/bottom frame closes popup menu);
843    * it may be possible to remove this method in future
844    * if the underlying Java behaviour changes
845    */
846  0 if (selected)
847    {
848  0 moveToFront();
849    }
850  0 return;
851    }
852  11 super.setSelected(selected);
853    }
854   
855    /**
856    * holds the frame for feature settings, so Protein and DNA tabs can be managed
857    */
858    JInternalFrame featureSettingsUI;
859   
860    JTabbedPane featureSettingsPanels;
861   
 
862  0 toggle @Override
863    public void addFeatureSettingsUI(
864    FeatureSettingsControllerGuiI featureSettings)
865    {
866  0 boolean showInternalFrame = false;
867  0 if (featureSettingsUI == null || featureSettingsPanels == null)
868    {
869  0 showInternalFrame = true;
870  0 featureSettingsPanels = new JTabbedPane();
871  0 featureSettingsPanels.addChangeListener(new ChangeListener()
872    {
873   
 
874  0 toggle @Override
875    public void stateChanged(ChangeEvent e)
876    {
877  0 if (e.getSource() != featureSettingsPanels
878    || featureSettingsUI == null
879    || featureSettingsUI.isClosed()
880    || !featureSettingsUI.isVisible())
881    {
882    // not our tabbed pane
883  0 return;
884    }
885  0 int tab = featureSettingsPanels.getSelectedIndex();
886  0 if (tab < 0 || featureSettingsPanels
887    .getSelectedComponent() instanceof FeatureSettingsControllerGuiI)
888    {
889    // no tab selected or already showing a feature settings GUI
890  0 return;
891    }
892  0 getAlignFrames().get(tab).showFeatureSettingsUI();
893    }
894    });
895  0 featureSettingsUI = new JInternalFrame(MessageManager.getString(
896    "label.sequence_feature_settings_for_CDS_and_Protein"));
897  0 featureSettingsUI.setFrameIcon(null);
898  0 featureSettingsPanels.setOpaque(true);
899   
900  0 JPanel dialog = new JPanel();
901  0 dialog.setOpaque(true);
902  0 dialog.setLayout(new BorderLayout());
903  0 dialog.add(featureSettingsPanels, BorderLayout.CENTER);
904  0 JPanel buttons = new JPanel();
905  0 JButton ok = new JButton(MessageManager.getString("action.ok"));
906  0 ok.addActionListener(new ActionListener()
907    {
908   
 
909  0 toggle @Override
910    public void actionPerformed(ActionEvent e)
911    {
912  0 try
913    {
914  0 featureSettingsUI.setClosed(true);
915    } catch (PropertyVetoException pv)
916    {
917  0 pv.printStackTrace();
918    }
919    }
920    });
921  0 JButton cancel = new JButton(
922    MessageManager.getString("action.cancel"));
923  0 cancel.addActionListener(new ActionListener()
924    {
925   
 
926  0 toggle @Override
927    public void actionPerformed(ActionEvent e)
928    {
929  0 try
930    {
931  0 for (Component fspanel : featureSettingsPanels.getComponents())
932    {
933  0 if (fspanel instanceof FeatureSettingsControllerGuiI)
934    {
935  0 ((FeatureSettingsControllerGuiI) fspanel).revert();
936    }
937    }
938  0 featureSettingsUI.setClosed(true);
939    } catch (Exception pv)
940    {
941  0 pv.printStackTrace();
942    }
943    }
944    });
945  0 buttons.add(ok);
946  0 buttons.add(cancel);
947  0 dialog.add(buttons, BorderLayout.SOUTH);
948  0 featureSettingsUI.setContentPane(dialog);
949  0 createDummyTabs();
950    }
951  0 if (featureSettingsPanels
952    .indexOfTabComponent((Component) featureSettings) > -1)
953    {
954    // just show the feature settings !
955  0 featureSettingsPanels
956    .setSelectedComponent((Component) featureSettings);
957  0 return;
958    }
959    // otherwise replace the dummy tab with the given feature settings
960  0 int pos = getAlignFrames().indexOf(featureSettings.getAlignframe());
961    // if pos==-1 then alignFrame isn't managed by this splitframe
962  0 if (pos == 0)
963    {
964  0 featureSettingsPanels.removeTabAt(0);
965  0 featureSettingsPanels.insertTab(tabName[0], null,
966    (Component) featureSettings,
967    MessageManager.formatMessage(
968    "label.sequence_feature_settings_for", tabName[0]),
969    0);
970    }
971  0 if (pos == 1)
972    {
973  0 featureSettingsPanels.removeTabAt(1);
974  0 featureSettingsPanels.insertTab(tabName[1], null,
975    (Component) featureSettings,
976    MessageManager.formatMessage(
977    "label.sequence_feature_settings_for", tabName[1]),
978    1);
979    }
980  0 featureSettingsPanels.setSelectedComponent((Component) featureSettings);
981   
982    // TODO: JAL-3535 - construct a feature settings title including names of
983    // currently selected CDS and Protein names
984   
985  0 if (showInternalFrame)
986    {
987  0 if (Platform.isAMacAndNotJS())
988    {
989  0 Desktop.addInternalFrame(featureSettingsUI,
990    MessageManager.getString(
991    "label.sequence_feature_settings_for_CDS_and_Protein"),
992    600, 480);
993    }
994    else
995    {
996  0 Desktop.addInternalFrame(featureSettingsUI,
997    MessageManager.getString(
998    "label.sequence_feature_settings_for_CDS_and_Protein"),
999    600, 450);
1000    }
1001  0 featureSettingsUI
1002    .setMinimumSize(new Dimension(FS_MIN_WIDTH, FS_MIN_HEIGHT));
1003   
1004  0 featureSettingsUI.addInternalFrameListener(
1005    new javax.swing.event.InternalFrameAdapter()
1006    {
 
1007  0 toggle @Override
1008    public void internalFrameClosed(
1009    javax.swing.event.InternalFrameEvent evt)
1010    {
1011  0 for (int tab = 0; tab < featureSettingsPanels
1012    .getTabCount();)
1013    {
1014  0 FeatureSettingsControllerGuiI fsettings = (FeatureSettingsControllerGuiI) featureSettingsPanels
1015    .getTabComponentAt(tab);
1016  0 if (fsettings != null)
1017    {
1018  0 featureSettingsPanels.removeTabAt(tab);
1019  0 fsettings.featureSettings_isClosed();
1020    }
1021    else
1022    {
1023  0 tab++;
1024    }
1025    }
1026  0 featureSettingsPanels = null;
1027  0 featureSettingsUI = null;
1028    };
1029    });
1030  0 featureSettingsUI.setLayer(JLayeredPane.PALETTE_LAYER);
1031    }
1032    }
1033   
1034    /**
1035    * tab names for feature settings
1036    */
1037    private String[] tabName = new String[] {
1038    MessageManager.getString("label.CDS"),
1039    MessageManager.getString("label.protein") };
1040   
1041    /**
1042    * create placeholder tabs which materialise the feature settings for a given
1043    * view. Also reinitialises any tabs containing stale feature settings
1044    */
 
1045  0 toggle private void createDummyTabs()
1046    {
1047  0 for (int tabIndex = 0; tabIndex < 2; tabIndex++)
1048    {
1049  0 JPanel dummyTab = new JPanel();
1050  0 featureSettingsPanels.addTab(tabName[tabIndex], dummyTab);
1051    }
1052    }
1053   
 
1054  0 toggle private void replaceWithDummyTab(FeatureSettingsControllerI toClose)
1055    {
1056  0 Component dummyTab = null;
1057  0 for (int tabIndex = 0; tabIndex < 2; tabIndex++)
1058    {
1059  0 if (featureSettingsPanels.getTabCount() > tabIndex)
1060    {
1061  0 dummyTab = featureSettingsPanels.getTabComponentAt(tabIndex);
1062  0 if (dummyTab instanceof FeatureSettingsControllerGuiI
1063    && !dummyTab.isVisible())
1064    {
1065  0 featureSettingsPanels.removeTabAt(tabIndex);
1066    // close the feature Settings tab
1067  0 ((FeatureSettingsControllerGuiI) dummyTab)
1068    .featureSettings_isClosed();
1069    // create a dummy tab in its place
1070  0 dummyTab = new JPanel();
1071  0 featureSettingsPanels.insertTab(tabName[tabIndex], null, dummyTab,
1072    MessageManager.formatMessage(
1073    "label.sequence_feature_settings_for",
1074    tabName[tabIndex]),
1075    tabIndex);
1076    }
1077    }
1078    }
1079    }
1080   
 
1081  0 toggle @Override
1082    public void closeFeatureSettings(
1083    FeatureSettingsControllerI featureSettings,
1084    boolean closeContainingFrame)
1085    {
1086  0 if (featureSettingsUI != null)
1087    {
1088  0 if (closeContainingFrame)
1089    {
1090  0 try
1091    {
1092  0 featureSettingsUI.setClosed(true);
1093    } catch (Exception x)
1094    {
1095    }
1096  0 featureSettingsUI = null;
1097    }
1098    else
1099    {
1100  0 replaceWithDummyTab(featureSettings);
1101    }
1102    }
1103    }
1104   
 
1105  0 toggle @Override
1106    public boolean isFeatureSettingsOpen()
1107    {
1108  0 return featureSettingsUI != null && !featureSettingsUI.isClosed();
1109    }
1110    }