2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
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;
31 import java.awt.Component;
32 import java.awt.Toolkit;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.KeyAdapter;
36 import java.awt.event.KeyEvent;
37 import java.awt.event.KeyListener;
38 import java.beans.PropertyVetoException;
39 import java.util.Arrays;
40 import java.util.List;
41 import java.util.Map.Entry;
43 import javax.swing.AbstractAction;
44 import javax.swing.InputMap;
45 import javax.swing.JComponent;
46 import javax.swing.JMenuItem;
47 import javax.swing.KeyStroke;
48 import javax.swing.event.InternalFrameAdapter;
49 import javax.swing.event.InternalFrameEvent;
52 * An internal frame on the desktop that hosts a horizontally split view of
53 * linked DNA and Protein alignments. Additional views can be created in linked
54 * pairs, expanded to separate split frames, or regathered into a single frame.
56 * (Some) operations on each alignment are automatically mirrored on the other.
57 * These include mouseover (highlighting), sequence and column selection,
58 * sequence ordering and sorting, and grouping, colouring and sorting by tree.
63 public class SplitFrame extends GSplitFrame implements SplitContainerI
65 private static final int WINDOWS_INSETS_WIDTH = 28; // tbc
67 private static final int MAC_INSETS_WIDTH = 28;
69 private static final int WINDOWS_INSETS_HEIGHT = 50; // tbc
71 private static final int MAC_INSETS_HEIGHT = 50;
73 private static final int DESKTOP_DECORATORS_HEIGHT = 65;
75 private static final long serialVersionUID = 1L;
77 public SplitFrame(GAlignFrame top, GAlignFrame bottom)
84 * Initialise this frame.
88 getTopFrame().setSplitFrame(this);
89 getBottomFrame().setSplitFrame(this);
90 getTopFrame().setVisible(true);
91 getBottomFrame().setVisible(true);
93 ((AlignFrame) getTopFrame()).getViewport().setCodingComplement(
94 ((AlignFrame) getBottomFrame()).getViewport());
97 * estimate width and height of SplitFrame; this.getInsets() doesn't seem to
98 * give the full additional size (a few pixels short)
100 int widthFudge = Platform.isAMac() ? MAC_INSETS_WIDTH
101 : WINDOWS_INSETS_WIDTH;
102 int heightFudge = Platform.isAMac() ? MAC_INSETS_HEIGHT
103 : WINDOWS_INSETS_HEIGHT;
104 int width = ((AlignFrame) getTopFrame()).getWidth() + widthFudge;
105 int height = ((AlignFrame) getTopFrame()).getHeight()
106 + ((AlignFrame) getBottomFrame()).getHeight() + DIVIDER_SIZE
108 height = fitHeightToDesktop(height);
109 setSize(width, height);
113 addCloseFrameListener();
119 addCommandListeners();
123 * Reduce the height if too large to fit in the Desktop. Also adjust the
124 * divider location in proportion.
128 * @return original or reduced height
130 public int fitHeightToDesktop(int height)
132 // allow about 65 pixels for Desktop decorators on Windows
134 int newHeight = Math.min(height,
135 Desktop.instance.getHeight() - DESKTOP_DECORATORS_HEIGHT);
136 if (newHeight != height)
138 int oldDividerLocation = getDividerLocation();
139 setDividerLocation(oldDividerLocation * newHeight / height);
145 * Set the top and bottom frames to listen to each others Commands (e.g. Edit,
148 protected void addCommandListeners()
150 // TODO if CommandListener is only ever 1:1 for complementary views,
151 // may change broadcast pattern to direct messaging (more efficient)
152 final StructureSelectionManager ssm = StructureSelectionManager
153 .getStructureSelectionManager(Desktop.instance);
154 ssm.addCommandListener(((AlignFrame) getTopFrame()).getViewport());
155 ssm.addCommandListener(((AlignFrame) getBottomFrame()).getViewport());
159 * Do any tweaking and twerking of the layout wanted.
161 public void adjustLayout()
164 * Ensure sequence ids are the same width so sequences line up
166 int w1 = ((AlignFrame) getTopFrame()).getViewport().getIdWidth();
167 int w2 = ((AlignFrame) getBottomFrame()).getViewport().getIdWidth();
168 int w3 = Math.max(w1, w2);
171 ((AlignFrame) getTopFrame()).getViewport().setIdWidth(w3);
175 ((AlignFrame) getBottomFrame()).getViewport().setIdWidth(w3);
179 * Scale protein to either 1 or 3 times character width of dna
181 final AlignViewport topViewport = ((AlignFrame) getTopFrame()).viewport;
182 final AlignViewport bottomViewport = ((AlignFrame) getBottomFrame()).viewport;
183 final AlignmentI topAlignment = topViewport.getAlignment();
184 final AlignmentI bottomAlignment = bottomViewport.getAlignment();
185 AlignmentViewport cdna = topAlignment.isNucleotide() ? topViewport
186 : (bottomAlignment.isNucleotide() ? bottomViewport : null);
187 AlignmentViewport protein = !topAlignment.isNucleotide() ? topViewport
188 : (!bottomAlignment.isNucleotide() ? bottomViewport : null);
189 if (protein != null && cdna != null)
191 int scale = protein.isScaleProteinAsCdna() ? 3 : 1;
192 protein.setCharWidth(scale * cdna.getViewStyle().getCharWidth());
197 * Adjusts the divider for a sensible split of the real estate (for example,
198 * when many transcripts are shown with a single protein). This should only be
199 * called after the split pane has been laid out (made visible) so it has a
200 * height. The aim is to avoid unnecessary vertical scroll bars, while
201 * ensuring that at least 2 sequences are visible in each panel.
203 * Once laid out, the user may choose to customise as they wish, so this
204 * method is not called again after the initial layout.
206 protected void adjustInitialLayout()
208 AlignFrame topFrame = (AlignFrame) getTopFrame();
209 AlignFrame bottomFrame = (AlignFrame) getBottomFrame();
212 * recompute layout of top and bottom panels to reflect their
213 * actual (rather than requested) height
215 topFrame.alignPanel.adjustAnnotationHeight();
216 bottomFrame.alignPanel.adjustAnnotationHeight();
218 final AlignViewport topViewport = topFrame.viewport;
219 final AlignViewport bottomViewport = bottomFrame.viewport;
220 final AlignmentI topAlignment = topViewport.getAlignment();
221 final AlignmentI bottomAlignment = bottomViewport.getAlignment();
222 boolean topAnnotations = topViewport.isShowAnnotation();
223 boolean bottomAnnotations = bottomViewport.isShowAnnotation();
224 // TODO need number of visible sequences here, not #sequences - how?
225 int topCount = topAlignment.getHeight();
226 int bottomCount = bottomAlignment.getHeight();
227 int topCharHeight = topViewport.getViewStyle().getCharHeight();
228 int bottomCharHeight = bottomViewport.getViewStyle().getCharHeight();
231 * calculate the minimum ratio that leaves at least the height
232 * of two sequences (after rounding) visible in the top panel
234 int topPanelHeight = topFrame.getHeight();
235 int bottomPanelHeight = bottomFrame.getHeight();
236 int topSequencesHeight = topFrame.alignPanel.getSeqPanel().seqCanvas
238 int topPanelMinHeight = topPanelHeight
239 - Math.max(0, topSequencesHeight - 3 * topCharHeight);
240 double totalHeight = (double) topPanelHeight + bottomPanelHeight;
241 double minRatio = topPanelMinHeight / totalHeight;
244 * calculate the maximum ratio that leaves at least the height
245 * of two sequences (after rounding) visible in the bottom panel
247 int bottomSequencesHeight = bottomFrame.alignPanel.getSeqPanel().seqCanvas
249 int bottomPanelMinHeight = bottomPanelHeight
250 - Math.max(0, bottomSequencesHeight - 3 * bottomCharHeight);
251 double maxRatio = (totalHeight - bottomPanelMinHeight) / totalHeight;
254 * estimate ratio of (topFrameContent / bottomFrameContent)
256 int insets = Platform.isAMac() ? MAC_INSETS_HEIGHT
257 : WINDOWS_INSETS_HEIGHT;
258 // allow 3 'rows' for scale, scrollbar, status bar
259 int topHeight = insets + (3 + topCount) * topCharHeight
260 + (topAnnotations ? topViewport.calcPanelHeight() : 0);
261 int bottomHeight = insets + (3 + bottomCount) * bottomCharHeight
262 + (bottomAnnotations ? bottomViewport.calcPanelHeight() : 0);
263 double ratio = ((double) topHeight)
264 / (double) (topHeight + bottomHeight);
267 * limit ratio to avoid concealing all sequences
269 ratio = Math.min(ratio, maxRatio);
270 ratio = Math.max(ratio, minRatio);
271 setRelativeDividerLocation(ratio);
275 * Add a listener to tidy up when the frame is closed.
277 protected void addCloseFrameListener()
279 addInternalFrameListener(new InternalFrameAdapter()
282 public void internalFrameClosed(InternalFrameEvent evt)
290 * Add a key listener that delegates to whichever split component the mouse is
291 * in (or does nothing if neither).
293 protected void addKeyListener()
295 addKeyListener(new KeyAdapter()
299 public void keyPressed(KeyEvent e)
301 AlignFrame af = (AlignFrame) getFrameAtMouse();
304 * Intercept and override any keys here if wanted.
306 if (!overrideKey(e, af))
310 for (KeyListener kl : af.getKeyListeners())
319 public void keyReleased(KeyEvent e)
321 Component c = getFrameAtMouse();
324 for (KeyListener kl : c.getKeyListeners())
335 * Returns true if the key event is overriden and actioned (or ignored) here,
336 * else returns false, indicating it should be delegated to the AlignFrame's
339 * We can't handle Cmd-Key combinations here, instead this is done by
340 * overriding key bindings.
342 * @see addKeyOverrides
347 protected boolean overrideKey(KeyEvent e, AlignFrame af)
349 boolean actioned = false;
350 int keyCode = e.getKeyCode();
353 case KeyEvent.VK_DOWN:
354 if (e.isAltDown() || !af.viewport.cursorMode)
357 * Key down (or Alt-key-down in cursor mode) - move selected sequences
359 ((AlignFrame) getTopFrame()).moveSelectedSequences(false);
360 ((AlignFrame) getBottomFrame()).moveSelectedSequences(false);
366 if (e.isAltDown() || !af.viewport.cursorMode)
369 * Key up (or Alt-key-up in cursor mode) - move selected sequences
371 ((AlignFrame) getTopFrame()).moveSelectedSequences(true);
372 ((AlignFrame) getBottomFrame()).moveSelectedSequences(true);
383 * Set key bindings (recommended for Swing over key accelerators).
385 private void addKeyBindings()
387 overrideDelegatedKeyBindings();
389 overrideImplementedKeyBindings();
393 * Override key bindings with alternative action methods implemented in this
396 protected void overrideImplementedKeyBindings()
401 overrideExpandViews();
402 overrideGatherViews();
406 * Replace Cmd-W close view action with our version.
408 protected void overrideCloseView()
410 AbstractAction action;
412 * Ctrl-W / Cmd-W - close view or window
414 KeyStroke key_cmdW = KeyStroke.getKeyStroke(KeyEvent.VK_W,
415 Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false);
416 action = new AbstractAction()
419 public void actionPerformed(ActionEvent e)
421 closeView_actionPerformed();
424 overrideKeyBinding(key_cmdW, action);
428 * Replace Cmd-T new view action with our version.
430 protected void overrideNewView()
433 * Ctrl-T / Cmd-T open new view
435 KeyStroke key_cmdT = KeyStroke.getKeyStroke(KeyEvent.VK_T,
436 Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false);
437 AbstractAction action = new AbstractAction()
440 public void actionPerformed(ActionEvent e)
442 newView_actionPerformed();
445 overrideKeyBinding(key_cmdT, action);
449 * For now, delegates key events to the corresponding key accelerator for the
450 * AlignFrame that the mouse is in. Hopefully can be simplified in future if
451 * AlignFrame is changed to use key bindings rather than accelerators.
453 protected void overrideDelegatedKeyBindings()
455 if (getTopFrame() instanceof AlignFrame)
458 * Get all accelerator keys in the top frame (the bottom should be
459 * identical) and override each one.
461 for (Entry<KeyStroke, JMenuItem> acc : ((AlignFrame) getTopFrame())
462 .getAccelerators().entrySet())
464 overrideKeyBinding(acc);
470 * Overrides an AlignFrame key accelerator with our version which delegates to
471 * the action listener in whichever frame has the mouse (and does nothing if
476 private void overrideKeyBinding(Entry<KeyStroke, JMenuItem> acc)
478 final KeyStroke ks = acc.getKey();
479 InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
480 inputMap.put(ks, ks);
481 this.getActionMap().put(ks, new AbstractAction()
484 public void actionPerformed(ActionEvent e)
486 Component c = getFrameAtMouse();
487 if (c != null && c instanceof AlignFrame)
489 for (ActionListener a : ((AlignFrame) c).getAccelerators().get(ks)
490 .getActionListeners())
492 a.actionPerformed(null);
500 * Replace an accelerator key's action with the specified action.
504 protected void overrideKeyBinding(KeyStroke ks, AbstractAction action)
506 this.getActionMap().put(ks, action);
507 overrideMenuItem(ks, action);
511 * Create and link new views (with matching names) in both panes.
513 * Note this is _not_ multiple tabs, each hosting a split pane view, rather it
514 * is a single split pane with each split holding multiple tabs which are
517 * TODO implement instead with a tabbed holder in the SplitView, each tab
518 * holding a single JSplitPane. Would avoid a duplicated tab, at the cost of
519 * some additional coding.
521 protected void newView_actionPerformed()
523 AlignFrame topFrame = (AlignFrame) getTopFrame();
524 AlignFrame bottomFrame = (AlignFrame) getBottomFrame();
525 final boolean scaleProteinAsCdna = topFrame.viewport
526 .isScaleProteinAsCdna();
528 AlignmentPanel newTopPanel = topFrame.newView(null, true);
529 AlignmentPanel newBottomPanel = bottomFrame.newView(null, true);
532 * This currently (for the first new view only) leaves the top pane on tab 0
533 * but the bottom on tab 1. This results from 'setInitialTabVisible' echoing
534 * from the bottom back to the first frame. Next line is a fudge to work
535 * around this. TODO find a better way.
537 if (topFrame.getTabIndex() != bottomFrame.getTabIndex())
539 topFrame.setDisplayedView(newTopPanel);
542 newBottomPanel.av.viewName = newTopPanel.av.viewName;
543 newTopPanel.av.setCodingComplement(newBottomPanel.av);
546 * These lines can be removed once scaleProteinAsCdna is added to element
547 * Viewport in jalview.xsd, as Jalview2XML.copyAlignPanel will then take
550 newTopPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
551 newBottomPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
554 * Line up id labels etc
558 final StructureSelectionManager ssm = StructureSelectionManager
559 .getStructureSelectionManager(Desktop.instance);
560 ssm.addCommandListener(newTopPanel.av);
561 ssm.addCommandListener(newBottomPanel.av);
565 * Close the currently selected view in both panes. If there is only one view,
566 * close this split frame.
568 protected void closeView_actionPerformed()
570 int viewCount = ((AlignFrame) getTopFrame()).getAlignPanels().size();
577 AlignmentPanel topPanel = ((AlignFrame) getTopFrame()).alignPanel;
578 AlignmentPanel bottomPanel = ((AlignFrame) getBottomFrame()).alignPanel;
580 ((AlignFrame) getTopFrame()).closeView(topPanel);
581 ((AlignFrame) getBottomFrame()).closeView(bottomPanel);
586 * Close child frames and this split frame.
590 ((AlignFrame) getTopFrame()).closeMenuItem_actionPerformed(true);
591 ((AlignFrame) getBottomFrame()).closeMenuItem_actionPerformed(true);
594 this.setClosed(true);
595 } catch (PropertyVetoException e)
602 * Replace AlignFrame 'expand views' action with SplitFrame version.
604 protected void overrideExpandViews()
606 KeyStroke key_X = KeyStroke.getKeyStroke(KeyEvent.VK_X, 0, false);
607 AbstractAction action = new AbstractAction()
610 public void actionPerformed(ActionEvent e)
612 expandViews_actionPerformed();
615 overrideMenuItem(key_X, action);
619 * Replace AlignFrame 'gather views' action with SplitFrame version.
621 protected void overrideGatherViews()
623 KeyStroke key_G = KeyStroke.getKeyStroke(KeyEvent.VK_G, 0, false);
624 AbstractAction action = new AbstractAction()
627 public void actionPerformed(ActionEvent e)
629 gatherViews_actionPerformed();
632 overrideMenuItem(key_G, action);
636 * Override the menu action associated with the keystroke in the child frames,
637 * replacing it with the given action.
642 private void overrideMenuItem(KeyStroke ks, AbstractAction action)
644 overrideMenuItem(ks, action, getTopFrame());
645 overrideMenuItem(ks, action, getBottomFrame());
649 * Override the menu action associated with the keystroke in one child frame,
650 * replacing it with the given action. Mwahahahaha.
656 private void overrideMenuItem(KeyStroke key, final AbstractAction action,
659 if (comp instanceof AlignFrame)
661 JMenuItem mi = ((AlignFrame) comp).getAccelerators().get(key);
664 for (ActionListener al : mi.getActionListeners())
666 mi.removeActionListener(al);
668 mi.addActionListener(new ActionListener()
671 public void actionPerformed(ActionEvent e)
673 action.actionPerformed(e);
681 * Expand any multiple views (which are always in pairs) into separate split
684 protected void expandViews_actionPerformed()
686 Desktop.instance.explodeViews(this);
690 * Gather any other SplitFrame views of this alignment back in as multiple
691 * (pairs of) views in this SplitFrame.
693 protected void gatherViews_actionPerformed()
695 Desktop.instance.gatherViews(this);
699 * Returns the alignment in the complementary frame to the one given.
702 public AlignmentI getComplement(Object alignFrame)
704 if (alignFrame == this.getTopFrame())
706 return ((AlignFrame) getBottomFrame()).viewport.getAlignment();
708 else if (alignFrame == this.getBottomFrame())
710 return ((AlignFrame) getTopFrame()).viewport.getAlignment();
716 * Returns the title of the complementary frame to the one given.
719 public String getComplementTitle(Object alignFrame)
721 if (alignFrame == this.getTopFrame())
723 return ((AlignFrame) getBottomFrame()).getTitle();
725 else if (alignFrame == this.getBottomFrame())
727 return ((AlignFrame) getTopFrame()).getTitle();
733 * Set the 'other half' to hidden / revealed.
736 public void setComplementVisible(Object alignFrame, boolean show)
739 * Hiding the AlignPanel suppresses unnecessary repaints
741 if (alignFrame == getTopFrame())
743 ((AlignFrame) getBottomFrame()).alignPanel.setVisible(show);
745 else if (alignFrame == getBottomFrame())
747 ((AlignFrame) getTopFrame()).alignPanel.setVisible(show);
749 super.setComplementVisible(alignFrame, show);
753 * return the AlignFrames held by this container
755 * @return { Top alignFrame (Usually CDS), Bottom AlignFrame (Usually
758 public List<AlignFrame> getAlignFrames()
761 .asList(new AlignFrame[]
762 { (AlignFrame) getTopFrame(), (AlignFrame) getBottomFrame() });
766 * Replace Cmd-F Find action with our version. This is necessary because the
767 * 'default' Finder searches in the first AlignFrame it finds. We need it to
768 * search in the half of the SplitFrame that has the mouse.
770 protected void overrideFind()
773 * Ctrl-F / Cmd-F open Finder dialog, 'focused' on the right alignment
775 KeyStroke key_cmdF = KeyStroke.getKeyStroke(KeyEvent.VK_F,
776 Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false);
777 AbstractAction action = new AbstractAction()
780 public void actionPerformed(ActionEvent e)
782 Component c = getFrameAtMouse();
783 if (c != null && c instanceof AlignFrame)
785 AlignFrame af = (AlignFrame) c;
786 new Finder(af.viewport, af.alignPanel);
790 overrideKeyBinding(key_cmdF, action);