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.AlignViewportI;
24 import jalview.api.SplitContainerI;
25 import jalview.datamodel.AlignmentI;
26 import jalview.jbgui.GAlignFrame;
27 import jalview.jbgui.GSplitFrame;
28 import jalview.structure.StructureSelectionManager;
29 import jalview.util.Platform;
30 import jalview.viewmodel.AlignmentViewport;
32 import java.awt.Component;
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.JDesktopPane;
47 import javax.swing.JInternalFrame;
48 import javax.swing.JMenuItem;
49 import javax.swing.KeyStroke;
50 import javax.swing.event.InternalFrameAdapter;
51 import javax.swing.event.InternalFrameEvent;
54 * An internal frame on the desktop that hosts a horizontally split view of
55 * linked DNA and Protein alignments. Additional views can be created in linked
56 * pairs, expanded to separate split frames, or regathered into a single frame.
58 * (Some) operations on each alignment are automatically mirrored on the other.
59 * These include mouseover (highlighting), sequence and column selection,
60 * sequence ordering and sorting, and grouping, colouring and sorting by tree.
65 public class SplitFrame extends GSplitFrame implements SplitContainerI
67 private static final int WINDOWS_INSETS_WIDTH = 28; // tbc
69 private static final int MAC_INSETS_WIDTH = 28;
71 private static final int WINDOWS_INSETS_HEIGHT = 50; // tbc
73 private static final int MAC_INSETS_HEIGHT = 50;
75 private static final int DESKTOP_DECORATORS_HEIGHT = 65;
77 private static final long serialVersionUID = 1L;
79 public SplitFrame(GAlignFrame top, GAlignFrame bottom)
86 * Initialise this frame.
90 getTopFrame().setSplitFrame(this);
91 getBottomFrame().setSplitFrame(this);
92 getTopFrame().setVisible(true);
93 getBottomFrame().setVisible(true);
95 ((AlignFrame) getTopFrame()).getViewport().setCodingComplement(
96 ((AlignFrame) getBottomFrame()).getViewport());
99 * estimate width and height of SplitFrame; this.getInsets() doesn't seem to
100 * give the full additional size (a few pixels short)
102 int widthFudge = Platform.isAMac() ? MAC_INSETS_WIDTH
103 : WINDOWS_INSETS_WIDTH;
104 int heightFudge = Platform.isAMac() ? MAC_INSETS_HEIGHT
105 : WINDOWS_INSETS_HEIGHT;
106 int width = ((AlignFrame) getTopFrame()).getWidth() + widthFudge;
107 int height = ((AlignFrame) getTopFrame()).getHeight()
108 + ((AlignFrame) getBottomFrame()).getHeight() + DIVIDER_SIZE
110 height = fitHeightToDesktop(height);
111 setSize(width, height);
115 addCloseFrameListener();
121 addCommandListeners();
125 * Reduce the height if too large to fit in the Desktop. Also adjust the
126 * divider location in proportion.
130 * @return original or reduced height
132 public int fitHeightToDesktop(int height)
134 // allow about 65 pixels for Desktop decorators on Windows
136 int newHeight = Math.min(height,
137 Desktop.instance.getHeight() - DESKTOP_DECORATORS_HEIGHT);
138 if (newHeight != height)
140 int oldDividerLocation = getDividerLocation();
141 setDividerLocation(oldDividerLocation * newHeight / height);
147 * Set the top and bottom frames to listen to each others Commands (e.g. Edit,
150 protected void addCommandListeners()
152 // TODO if CommandListener is only ever 1:1 for complementary views,
153 // may change broadcast pattern to direct messaging (more efficient)
154 final StructureSelectionManager ssm = StructureSelectionManager
155 .getStructureSelectionManager(Desktop.instance);
156 ssm.addCommandListener(((AlignFrame) getTopFrame()).getViewport());
157 ssm.addCommandListener(((AlignFrame) getBottomFrame()).getViewport());
161 * Do any tweaking and twerking of the layout wanted.
163 public void adjustLayout()
166 * Ensure sequence ids are the same width so sequences line up
168 int w1 = ((AlignFrame) getTopFrame()).getViewport().getIdWidth();
169 int w2 = ((AlignFrame) getBottomFrame()).getViewport().getIdWidth();
170 int w3 = Math.max(w1, w2);
173 ((AlignFrame) getTopFrame()).getViewport().setIdWidth(w3);
177 ((AlignFrame) getBottomFrame()).getViewport().setIdWidth(w3);
181 * Scale protein to either 1 or 3 times character width of dna
183 final AlignViewport topViewport = ((AlignFrame) getTopFrame()).viewport;
184 final AlignViewport bottomViewport = ((AlignFrame) getBottomFrame()).viewport;
185 final AlignmentI topAlignment = topViewport.getAlignment();
186 final AlignmentI bottomAlignment = bottomViewport.getAlignment();
187 AlignmentViewport cdna = topAlignment.isNucleotide() ? topViewport
188 : (bottomAlignment.isNucleotide() ? bottomViewport : null);
189 AlignmentViewport protein = !topAlignment.isNucleotide() ? topViewport
190 : (!bottomAlignment.isNucleotide() ? bottomViewport : null);
191 if (protein != null && cdna != null)
193 int scale = protein.isScaleProteinAsCdna() ? 3 : 1;
194 protein.setCharWidth(scale * cdna.getViewStyle().getCharWidth());
199 * Adjusts the divider for a sensible split of the real estate (for example,
200 * when many transcripts are shown with a single protein). This should only be
201 * called after the split pane has been laid out (made visible) so it has a
202 * height. The aim is to avoid unnecessary vertical scroll bars, while
203 * ensuring that at least 2 sequences are visible in each panel.
205 * Once laid out, the user may choose to customise as they wish, so this
206 * method is not called again after the initial layout.
208 protected void adjustInitialLayout()
210 AlignFrame topFrame = (AlignFrame) getTopFrame();
211 AlignFrame bottomFrame = (AlignFrame) getBottomFrame();
214 * recompute layout of top and bottom panels to reflect their
215 * actual (rather than requested) height
217 topFrame.alignPanel.adjustAnnotationHeight();
218 bottomFrame.alignPanel.adjustAnnotationHeight();
220 final AlignViewportI topViewport = topFrame.viewport;
221 final AlignViewportI bottomViewport = bottomFrame.viewport;
222 final AlignmentI topAlignment = topViewport.getAlignment();
223 final AlignmentI bottomAlignment = bottomViewport.getAlignment();
224 boolean topAnnotations = topViewport.isShowAnnotation();
225 boolean bottomAnnotations = bottomViewport.isShowAnnotation();
226 // TODO need number of visible sequences here, not #sequences - how?
227 int topCount = topAlignment.getHeight();
228 int bottomCount = bottomAlignment.getHeight();
229 int topCharHeight = topViewport.getViewStyle().getCharHeight();
230 int bottomCharHeight = bottomViewport.getViewStyle().getCharHeight();
233 * calculate the minimum ratio that leaves at least the height
234 * of two sequences (after rounding) visible in the top panel
236 int topPanelHeight = topFrame.getHeight();
237 int bottomPanelHeight = bottomFrame.getHeight();
238 int topSequencesHeight = topFrame.alignPanel.getSeqPanel().seqCanvas
240 int topPanelMinHeight = topPanelHeight
241 - Math.max(0, topSequencesHeight - 3 * topCharHeight);
242 double totalHeight = (double) topPanelHeight + bottomPanelHeight;
243 double minRatio = topPanelMinHeight / totalHeight;
246 * calculate the maximum ratio that leaves at least the height
247 * of two sequences (after rounding) visible in the bottom panel
249 int bottomSequencesHeight = bottomFrame.alignPanel.getSeqPanel().seqCanvas
251 int bottomPanelMinHeight = bottomPanelHeight
252 - Math.max(0, bottomSequencesHeight - 3 * bottomCharHeight);
253 double maxRatio = (totalHeight - bottomPanelMinHeight) / totalHeight;
256 * estimate ratio of (topFrameContent / bottomFrameContent)
258 int insets = Platform.isAMac() ? MAC_INSETS_HEIGHT
259 : WINDOWS_INSETS_HEIGHT;
260 // allow 3 'rows' for scale, scrollbar, status bar
261 int topHeight = insets + (3 + topCount) * topCharHeight
262 + (topAnnotations ? topViewport.calcPanelHeight() : 0);
263 int bottomHeight = insets + (3 + bottomCount) * bottomCharHeight
264 + (bottomAnnotations ? bottomViewport.calcPanelHeight() : 0);
265 double ratio = ((double) topHeight)
266 / (double) (topHeight + bottomHeight);
269 * limit ratio to avoid concealing all sequences
271 ratio = Math.min(ratio, maxRatio);
272 ratio = Math.max(ratio, minRatio);
273 setRelativeDividerLocation(ratio);
277 * Add a listener to tidy up when the frame is closed.
279 protected void addCloseFrameListener()
281 addInternalFrameListener(new InternalFrameAdapter()
284 public void internalFrameClosed(InternalFrameEvent evt)
292 * Add a key listener that delegates to whichever split component the mouse is
293 * in (or does nothing if neither).
295 protected void addKeyListener()
297 addKeyListener(new KeyAdapter()
301 public void keyPressed(KeyEvent e)
303 AlignFrame af = (AlignFrame) getFrameAtMouse();
306 * Intercept and override any keys here if wanted.
308 if (!overrideKey(e, af))
312 for (KeyListener kl : af.getKeyListeners())
321 public void keyReleased(KeyEvent e)
323 Component c = getFrameAtMouse();
326 for (KeyListener kl : c.getKeyListeners())
337 * Returns true if the key event is overriden and actioned (or ignored) here,
338 * else returns false, indicating it should be delegated to the AlignFrame's
341 * We can't handle Cmd-Key combinations here, instead this is done by
342 * overriding key bindings.
344 * @see addKeyOverrides
349 protected boolean overrideKey(KeyEvent e, AlignFrame af)
351 boolean actioned = false;
352 int keyCode = e.getKeyCode();
355 case KeyEvent.VK_DOWN:
356 if (e.isAltDown() || !af.viewport.cursorMode)
359 * Key down (or Alt-key-down in cursor mode) - move selected sequences
361 ((AlignFrame) getTopFrame()).moveSelectedSequences(false);
362 ((AlignFrame) getBottomFrame()).moveSelectedSequences(false);
368 if (e.isAltDown() || !af.viewport.cursorMode)
371 * Key up (or Alt-key-up in cursor mode) - move selected sequences
373 ((AlignFrame) getTopFrame()).moveSelectedSequences(true);
374 ((AlignFrame) getBottomFrame()).moveSelectedSequences(true);
385 * Set key bindings (recommended for Swing over key accelerators).
387 private void addKeyBindings()
389 overrideDelegatedKeyBindings();
391 overrideImplementedKeyBindings();
395 * Override key bindings with alternative action methods implemented in this
398 protected void overrideImplementedKeyBindings()
403 overrideExpandViews();
404 overrideGatherViews();
408 * Replace Cmd-W close view action with our version.
410 protected void overrideCloseView()
412 AbstractAction action;
414 * Ctrl-W / Cmd-W - close view or window
416 KeyStroke key_cmdW = KeyStroke.getKeyStroke(KeyEvent.VK_W,
417 jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
418 action = new AbstractAction()
421 public void actionPerformed(ActionEvent e)
423 closeView_actionPerformed();
426 overrideKeyBinding(key_cmdW, action);
430 * Replace Cmd-T new view action with our version.
432 protected void overrideNewView()
435 * Ctrl-T / Cmd-T open new view
437 KeyStroke key_cmdT = KeyStroke.getKeyStroke(KeyEvent.VK_T,
438 jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
439 AbstractAction action = new AbstractAction()
442 public void actionPerformed(ActionEvent e)
444 newView_actionPerformed();
447 overrideKeyBinding(key_cmdT, action);
451 * For now, delegates key events to the corresponding key accelerator for the
452 * AlignFrame that the mouse is in. Hopefully can be simplified in future if
453 * AlignFrame is changed to use key bindings rather than accelerators.
455 protected void overrideDelegatedKeyBindings()
457 if (getTopFrame() instanceof AlignFrame)
460 * Get all accelerator keys in the top frame (the bottom should be
461 * identical) and override each one.
463 for (Entry<KeyStroke, JMenuItem> acc : ((AlignFrame) getTopFrame())
464 .getAccelerators().entrySet())
466 overrideKeyBinding(acc);
472 * Overrides an AlignFrame key accelerator with our version which delegates to
473 * the action listener in whichever frame has the mouse (and does nothing if
478 private void overrideKeyBinding(Entry<KeyStroke, JMenuItem> acc)
480 final KeyStroke ks = acc.getKey();
481 InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
482 inputMap.put(ks, ks);
483 this.getActionMap().put(ks, new AbstractAction()
486 public void actionPerformed(ActionEvent e)
488 Component c = getFrameAtMouse();
489 if (c != null && c instanceof AlignFrame)
491 for (ActionListener a : ((AlignFrame) c).getAccelerators().get(ks)
492 .getActionListeners())
494 a.actionPerformed(null);
502 * Replace an accelerator key's action with the specified action.
506 protected void overrideKeyBinding(KeyStroke ks, AbstractAction action)
508 this.getActionMap().put(ks, action);
509 overrideMenuItem(ks, action);
513 * Create and link new views (with matching names) in both panes.
515 * Note this is _not_ multiple tabs, each hosting a split pane view, rather it
516 * is a single split pane with each split holding multiple tabs which are
519 * TODO implement instead with a tabbed holder in the SplitView, each tab
520 * holding a single JSplitPane. Would avoid a duplicated tab, at the cost of
521 * some additional coding.
523 protected void newView_actionPerformed()
525 AlignFrame topFrame = (AlignFrame) getTopFrame();
526 AlignFrame bottomFrame = (AlignFrame) getBottomFrame();
527 final boolean scaleProteinAsCdna = topFrame.viewport
528 .isScaleProteinAsCdna();
530 AlignmentPanel newTopPanel = topFrame.newView(null, true);
531 AlignmentPanel newBottomPanel = bottomFrame.newView(null, true);
534 * This currently (for the first new view only) leaves the top pane on tab 0
535 * but the bottom on tab 1. This results from 'setInitialTabVisible' echoing
536 * from the bottom back to the first frame. Next line is a fudge to work
537 * around this. TODO find a better way.
539 if (topFrame.getTabIndex() != bottomFrame.getTabIndex())
541 topFrame.setDisplayedView(newTopPanel);
544 newBottomPanel.av.setViewName(newTopPanel.av.getViewName());
545 newTopPanel.av.setCodingComplement(newBottomPanel.av);
548 * These lines can be removed once scaleProteinAsCdna is added to element
549 * Viewport in jalview.xsd, as Jalview2XML.copyAlignPanel will then take
552 newTopPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
553 newBottomPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
556 * Line up id labels etc
560 final StructureSelectionManager ssm = StructureSelectionManager
561 .getStructureSelectionManager(Desktop.instance);
562 ssm.addCommandListener(newTopPanel.av);
563 ssm.addCommandListener(newBottomPanel.av);
567 * Close the currently selected view in both panes. If there is only one view,
568 * close this split frame.
570 protected void closeView_actionPerformed()
572 int viewCount = ((AlignFrame) getTopFrame()).getAlignPanels().size();
579 AlignmentPanel topPanel = ((AlignFrame) getTopFrame()).alignPanel;
580 AlignmentPanel bottomPanel = ((AlignFrame) getBottomFrame()).alignPanel;
582 ((AlignFrame) getTopFrame()).closeView(topPanel);
583 ((AlignFrame) getBottomFrame()).closeView(bottomPanel);
588 * Close child frames and this split frame.
592 ((AlignFrame) getTopFrame()).closeMenuItem_actionPerformed(true);
593 ((AlignFrame) getBottomFrame()).closeMenuItem_actionPerformed(true);
596 this.setClosed(true);
597 } catch (PropertyVetoException e)
604 * Replace AlignFrame 'expand views' action with SplitFrame version.
606 protected void overrideExpandViews()
608 KeyStroke key_X = KeyStroke.getKeyStroke(KeyEvent.VK_X, 0, false);
609 AbstractAction action = new AbstractAction()
612 public void actionPerformed(ActionEvent e)
614 expandViews_actionPerformed();
617 overrideMenuItem(key_X, action);
621 * Replace AlignFrame 'gather views' action with SplitFrame version.
623 protected void overrideGatherViews()
625 KeyStroke key_G = KeyStroke.getKeyStroke(KeyEvent.VK_G, 0, false);
626 AbstractAction action = new AbstractAction()
629 public void actionPerformed(ActionEvent e)
631 gatherViews_actionPerformed();
634 overrideMenuItem(key_G, action);
638 * Override the menu action associated with the keystroke in the child frames,
639 * replacing it with the given action.
644 private void overrideMenuItem(KeyStroke ks, AbstractAction action)
646 overrideMenuItem(ks, action, getTopFrame());
647 overrideMenuItem(ks, action, getBottomFrame());
651 * Override the menu action associated with the keystroke in one child frame,
652 * replacing it with the given action. Mwahahahaha.
658 private void overrideMenuItem(KeyStroke key, final AbstractAction action,
661 if (comp instanceof AlignFrame)
663 JMenuItem mi = ((AlignFrame) comp).getAccelerators().get(key);
666 for (ActionListener al : mi.getActionListeners())
668 mi.removeActionListener(al);
670 mi.addActionListener(new ActionListener()
673 public void actionPerformed(ActionEvent e)
675 action.actionPerformed(e);
683 * Expand any multiple views (which are always in pairs) into separate split
686 protected void expandViews_actionPerformed()
688 Desktop.instance.explodeViews(this);
692 * Gather any other SplitFrame views of this alignment back in as multiple
693 * (pairs of) views in this SplitFrame.
695 protected void gatherViews_actionPerformed()
697 Desktop.instance.gatherViews(this);
701 * Returns the alignment in the complementary frame to the one given.
704 public AlignmentI getComplement(Object alignFrame)
706 if (alignFrame == this.getTopFrame())
708 return ((AlignFrame) getBottomFrame()).viewport.getAlignment();
710 else if (alignFrame == this.getBottomFrame())
712 return ((AlignFrame) getTopFrame()).viewport.getAlignment();
718 * Returns the title of the complementary frame to the one given.
721 public String getComplementTitle(Object alignFrame)
723 if (alignFrame == this.getTopFrame())
725 return ((AlignFrame) getBottomFrame()).getTitle();
727 else if (alignFrame == this.getBottomFrame())
729 return ((AlignFrame) getTopFrame()).getTitle();
735 * Set the 'other half' to hidden / revealed.
738 public void setComplementVisible(Object alignFrame, boolean show)
741 * Hiding the AlignPanel suppresses unnecessary repaints
743 if (alignFrame == getTopFrame())
745 ((AlignFrame) getBottomFrame()).alignPanel.setVisible(show);
747 else if (alignFrame == getBottomFrame())
749 ((AlignFrame) getTopFrame()).alignPanel.setVisible(show);
751 super.setComplementVisible(alignFrame, show);
755 * return the AlignFrames held by this container
757 * @return { Top alignFrame (Usually CDS), Bottom AlignFrame (Usually
760 public List<AlignFrame> getAlignFrames()
763 .asList(new AlignFrame[]
764 { (AlignFrame) getTopFrame(), (AlignFrame) getBottomFrame() });
768 * Replace Cmd-F Find action with our version. This is necessary because the
769 * 'default' Finder searches in the first AlignFrame it finds. We need it to
770 * search in the half of the SplitFrame that has the mouse.
772 protected void overrideFind()
775 * Ctrl-F / Cmd-F open Finder dialog, 'focused' on the right alignment
777 KeyStroke key_cmdF = KeyStroke.getKeyStroke(KeyEvent.VK_F,
778 jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
779 AbstractAction action = new AbstractAction()
782 public void actionPerformed(ActionEvent e)
784 Component c = getFrameAtMouse();
785 if (c != null && c instanceof AlignFrame)
787 AlignFrame af = (AlignFrame) c;
788 new Finder(af.viewport, af.alignPanel);
792 overrideKeyBinding(key_cmdF, action);
796 * Override to do nothing if triggered from one of the child frames
799 public void setSelected(boolean selected) throws PropertyVetoException
801 JDesktopPane desktopPane = getDesktopPane();
802 JInternalFrame fr = desktopPane == null ? null
803 : desktopPane.getSelectedFrame();
804 if (fr == getTopFrame() || fr == getBottomFrame())
807 * patch for JAL-3288 (deselecting top/bottom frame closes popup menu);
808 * it may be possible to remove this method in future
809 * if the underlying Java behaviour changes
817 super.setSelected(selected);