Clover icon

Coverage Report

  1. Project Clover database Wed Jan 7 2026 02:49:01 GMT
  2. Package jalview.gui

File SplitFrame.java

 

Coverage histogram

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

Code metrics

106
298
51
1
1,114
743
122
0.41
5.84
51
2.39

Classes

Class Line # Actions
SplitFrame 77 298 122
0.3604395736%
 

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