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.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.event.KeyAdapter;
35 import java.awt.event.KeyEvent;
36 import java.awt.event.KeyListener;
37 import java.beans.PropertyVetoException;
38 import java.util.Arrays;
39 import java.util.List;
40 import java.util.Map.Entry;
42 import javax.swing.AbstractAction;
43 import javax.swing.InputMap;
44 import javax.swing.JComponent;
45 import javax.swing.JDesktopPane;
46 import javax.swing.JInternalFrame;
47 import javax.swing.JMenuItem;
48 import javax.swing.KeyStroke;
49 import javax.swing.event.InternalFrameAdapter;
50 import javax.swing.event.InternalFrameEvent;
53 * An internal frame on the desktop that hosts a horizontally split view of
54 * linked DNA and Protein alignments. Additional views can be created in linked
55 * pairs, expanded to separate split frames, or regathered into a single frame.
57 * (Some) operations on each alignment are automatically mirrored on the other.
58 * These include mouseover (highlighting), sequence and column selection,
59 * sequence ordering and sorting, and grouping, colouring and sorting by tree.
64 public class SplitFrame extends GSplitFrame implements SplitContainerI
66 private static final int WINDOWS_INSETS_WIDTH = 28; // tbc
68 private static final int MAC_INSETS_WIDTH = 28;
70 private static final int WINDOWS_INSETS_HEIGHT = 50; // tbc
72 private static final int MAC_INSETS_HEIGHT = 50;
74 private static final int DESKTOP_DECORATORS_HEIGHT = 65;
76 private static final long serialVersionUID = 1L;
78 public SplitFrame(GAlignFrame top, GAlignFrame bottom)
85 * Initialise this frame.
89 getTopFrame().setSplitFrame(this, true);
90 getBottomFrame().setSplitFrame(this, false);
91 getTopFrame().setVisible(true);
92 getBottomFrame().setVisible(true);
94 ((AlignFrame) getTopFrame()).getViewport().setCodingComplement(
95 ((AlignFrame) getBottomFrame()).getViewport());
98 * estimate width and height of SplitFrame; this.getInsets() doesn't seem to
99 * give the full additional size (a few pixels short)
101 int widthFudge = Platform.isAMac() ? MAC_INSETS_WIDTH
102 : WINDOWS_INSETS_WIDTH;
103 int heightFudge = Platform.isAMac() ? MAC_INSETS_HEIGHT
104 : WINDOWS_INSETS_HEIGHT;
105 int width = ((AlignFrame) getTopFrame()).getWidth() + widthFudge;
106 int height = ((AlignFrame) getTopFrame()).getHeight()
107 + ((AlignFrame) getBottomFrame()).getHeight() + DIVIDER_SIZE
109 height = fitHeightToDesktop(height);
110 setSize(width, height);
114 addCloseFrameListener();
120 addCommandListeners();
124 * Reduce the height if too large to fit in the Desktop. Also adjust the
125 * divider location in proportion.
129 * @return original or reduced height
131 public int fitHeightToDesktop(int height)
133 // allow about 65 pixels for Desktop decorators on Windows
135 int newHeight = Math.min(height,
136 Desktop.instance.getHeight() - DESKTOP_DECORATORS_HEIGHT);
137 if (newHeight != height)
139 int oldDividerLocation = getDividerLocation();
140 setDividerLocation(oldDividerLocation * newHeight / height);
146 * Set the top and bottom frames to listen to each others Commands (e.g. Edit,
149 protected void addCommandListeners()
151 // TODO if CommandListener is only ever 1:1 for complementary views,
152 // may change broadcast pattern to direct messaging (more efficient)
153 final StructureSelectionManager ssm = StructureSelectionManager
154 .getStructureSelectionManager(Desktop.instance);
155 ssm.addCommandListener(((AlignFrame) getTopFrame()).getViewport());
156 ssm.addCommandListener(((AlignFrame) getBottomFrame()).getViewport());
160 * Do any tweaking and twerking of the layout wanted.
162 public void adjustLayout()
164 final AlignViewport topViewport = ((AlignFrame) getTopFrame()).viewport;
165 final AlignViewport bottomViewport = ((AlignFrame) getBottomFrame()).viewport;
168 * Ensure sequence ids are the same width so sequences line up;
169 * reduce the idwidth of a panel with a longer wrapped left scale
171 int w1 = topViewport.getIdWidth();
172 int w2 = bottomViewport.getIdWidth();
173 int topScaleLeft = topViewport.getWrapAlignment()
174 ? ((AlignFrame) getTopFrame()).alignPanel
175 .getSeqPanel().seqCanvas.getLabelWidthWest()
177 int bottomScaleLeft = bottomViewport.getWrapAlignment()
178 ? ((AlignFrame) getBottomFrame()).alignPanel
179 .getSeqPanel().seqCanvas.getLabelWidthWest()
181 int w3 = Math.max(w1, w2);
183 .setIdWidth(w3 - Math.max(0, topScaleLeft - bottomScaleLeft));
185 .setIdWidth(w3 - Math.max(0, bottomScaleLeft - topScaleLeft));
188 * Scale protein to either 1 or 3 times character width of dna
190 final AlignmentI topAlignment = topViewport.getAlignment();
191 final AlignmentI bottomAlignment = bottomViewport.getAlignment();
192 AlignmentViewport cdna = topAlignment.isNucleotide() ? topViewport
193 : (bottomAlignment.isNucleotide() ? bottomViewport : null);
194 AlignmentViewport protein = !topAlignment.isNucleotide() ? topViewport
195 : (!bottomAlignment.isNucleotide() ? bottomViewport : null);
196 if (protein != null && cdna != null)
198 int scale = protein.isScaleProteinAsCdna() ? 3 : 1;
199 protein.setCharWidth(scale * cdna.getViewStyle().getCharWidth());
206 * Adjusts the divider for a sensible split of the real estate (for example,
207 * when many transcripts are shown with a single protein). This should only be
208 * called after the split pane has been laid out (made visible) so it has a
209 * height. The aim is to avoid unnecessary vertical scroll bars, while
210 * ensuring that at least 2 sequences are visible in each panel.
212 * Once laid out, the user may choose to customise as they wish, so this
213 * method is not called again after the initial layout.
215 protected void adjustInitialLayout()
217 AlignFrame topFrame = (AlignFrame) getTopFrame();
218 AlignFrame bottomFrame = (AlignFrame) getBottomFrame();
221 * recompute layout of top and bottom panels to reflect their
222 * actual (rather than requested) height
224 topFrame.alignPanel.adjustAnnotationHeight();
225 bottomFrame.alignPanel.adjustAnnotationHeight();
227 final AlignViewport topViewport = topFrame.viewport;
228 final AlignViewport bottomViewport = bottomFrame.viewport;
229 final AlignmentI topAlignment = topViewport.getAlignment();
230 final AlignmentI bottomAlignment = bottomViewport.getAlignment();
231 boolean topAnnotations = topViewport.isShowAnnotation();
232 boolean bottomAnnotations = bottomViewport.isShowAnnotation();
233 // TODO need number of visible sequences here, not #sequences - how?
234 int topCount = topAlignment.getHeight();
235 int bottomCount = bottomAlignment.getHeight();
236 int topCharHeight = topViewport.getViewStyle().getCharHeight();
237 int bottomCharHeight = bottomViewport.getViewStyle().getCharHeight();
240 * calculate the minimum ratio that leaves at least the height
241 * of two sequences (after rounding) visible in the top panel
243 int topPanelHeight = topFrame.getHeight();
244 int bottomPanelHeight = bottomFrame.getHeight();
245 int topSequencesHeight = topFrame.alignPanel.getSeqPanel().seqCanvas
247 int topPanelMinHeight = topPanelHeight
248 - Math.max(0, topSequencesHeight - 3 * topCharHeight);
249 double totalHeight = (double) topPanelHeight + bottomPanelHeight;
250 double minRatio = topPanelMinHeight / totalHeight;
253 * calculate the maximum ratio that leaves at least the height
254 * of two sequences (after rounding) visible in the bottom panel
256 int bottomSequencesHeight = bottomFrame.alignPanel.getSeqPanel().seqCanvas
258 int bottomPanelMinHeight = bottomPanelHeight
259 - Math.max(0, bottomSequencesHeight - 3 * bottomCharHeight);
260 double maxRatio = (totalHeight - bottomPanelMinHeight) / totalHeight;
263 * estimate ratio of (topFrameContent / bottomFrameContent)
265 int insets = Platform.isAMac() ? MAC_INSETS_HEIGHT
266 : WINDOWS_INSETS_HEIGHT;
267 // allow 3 'rows' for scale, scrollbar, status bar
268 int topHeight = insets + (3 + topCount) * topCharHeight
269 + (topAnnotations ? topViewport.calcPanelHeight() : 0);
270 int bottomHeight = insets + (3 + bottomCount) * bottomCharHeight
271 + (bottomAnnotations ? bottomViewport.calcPanelHeight() : 0);
272 double ratio = ((double) topHeight)
273 / (double) (topHeight + bottomHeight);
276 * limit ratio to avoid concealing all sequences
278 ratio = Math.min(ratio, maxRatio);
279 ratio = Math.max(ratio, minRatio);
280 setRelativeDividerLocation(ratio);
284 * Add a listener to tidy up when the frame is closed.
286 protected void addCloseFrameListener()
288 addInternalFrameListener(new InternalFrameAdapter()
291 public void internalFrameClosed(InternalFrameEvent evt)
299 * Add a key listener that delegates to whichever split component the mouse is
300 * in (or does nothing if neither).
302 protected void addKeyListener()
304 addKeyListener(new KeyAdapter()
308 public void keyPressed(KeyEvent e)
310 AlignFrame af = (AlignFrame) getFrameAtMouse();
313 * Intercept and override any keys here if wanted.
315 if (!overrideKey(e, af))
319 for (KeyListener kl : af.getKeyListeners())
328 public void keyReleased(KeyEvent e)
330 Component c = getFrameAtMouse();
333 for (KeyListener kl : c.getKeyListeners())
344 * Returns true if the key event is overriden and actioned (or ignored) here,
345 * else returns false, indicating it should be delegated to the AlignFrame's
348 * We can't handle Cmd-Key combinations here, instead this is done by
349 * overriding key bindings.
351 * @see addKeyOverrides
356 protected boolean overrideKey(KeyEvent e, AlignFrame af)
358 boolean actioned = false;
359 int keyCode = e.getKeyCode();
362 case KeyEvent.VK_DOWN:
363 if (e.isAltDown() || !af.viewport.cursorMode)
366 * Key down (or Alt-key-down in cursor mode) - move selected sequences
368 ((AlignFrame) getTopFrame()).moveSelectedSequences(false);
369 ((AlignFrame) getBottomFrame()).moveSelectedSequences(false);
375 if (e.isAltDown() || !af.viewport.cursorMode)
378 * Key up (or Alt-key-up in cursor mode) - move selected sequences
380 ((AlignFrame) getTopFrame()).moveSelectedSequences(true);
381 ((AlignFrame) getBottomFrame()).moveSelectedSequences(true);
392 * Set key bindings (recommended for Swing over key accelerators).
394 private void addKeyBindings()
396 overrideDelegatedKeyBindings();
398 overrideImplementedKeyBindings();
402 * Override key bindings with alternative action methods implemented in this
405 protected void overrideImplementedKeyBindings()
410 overrideExpandViews();
411 overrideGatherViews();
415 * Replace Cmd-W close view action with our version.
417 protected void overrideCloseView()
419 AbstractAction action;
421 * Ctrl-W / Cmd-W - close view or window
423 KeyStroke key_cmdW = KeyStroke.getKeyStroke(KeyEvent.VK_W,
424 jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
425 action = new AbstractAction()
428 public void actionPerformed(ActionEvent e)
430 closeView_actionPerformed();
433 overrideKeyBinding(key_cmdW, action);
437 * Replace Cmd-T new view action with our version.
439 protected void overrideNewView()
442 * Ctrl-T / Cmd-T open new view
444 KeyStroke key_cmdT = KeyStroke.getKeyStroke(KeyEvent.VK_T,
445 jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
446 AbstractAction action = new AbstractAction()
449 public void actionPerformed(ActionEvent e)
451 newView_actionPerformed();
454 overrideKeyBinding(key_cmdT, action);
458 * For now, delegates key events to the corresponding key accelerator for the
459 * AlignFrame that the mouse is in. Hopefully can be simplified in future if
460 * AlignFrame is changed to use key bindings rather than accelerators.
462 protected void overrideDelegatedKeyBindings()
464 if (getTopFrame() instanceof AlignFrame)
467 * Get all accelerator keys in the top frame (the bottom should be
468 * identical) and override each one.
470 for (Entry<KeyStroke, JMenuItem> acc : ((AlignFrame) getTopFrame())
471 .getAccelerators().entrySet())
473 overrideKeyBinding(acc);
479 * Overrides an AlignFrame key accelerator with our version which delegates to
480 * the action listener in whichever frame has the mouse (and does nothing if
485 private void overrideKeyBinding(Entry<KeyStroke, JMenuItem> acc)
487 final KeyStroke ks = acc.getKey();
488 InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
489 inputMap.put(ks, ks);
490 this.getActionMap().put(ks, new AbstractAction()
493 public void actionPerformed(ActionEvent e)
495 Component c = getFrameAtMouse();
496 if (c != null && c instanceof AlignFrame)
498 for (ActionListener a : ((AlignFrame) c).getAccelerators().get(ks)
499 .getActionListeners())
501 a.actionPerformed(null);
509 * Replace an accelerator key's action with the specified action.
513 protected void overrideKeyBinding(KeyStroke ks, AbstractAction action)
515 this.getActionMap().put(ks, action);
516 overrideMenuItem(ks, action);
520 * Create and link new views (with matching names) in both panes.
522 * Note this is _not_ multiple tabs, each hosting a split pane view, rather it
523 * is a single split pane with each split holding multiple tabs which are
526 * TODO implement instead with a tabbed holder in the SplitView, each tab
527 * holding a single JSplitPane. Would avoid a duplicated tab, at the cost of
528 * some additional coding.
530 protected void newView_actionPerformed()
532 AlignFrame topFrame = (AlignFrame) getTopFrame();
533 AlignFrame bottomFrame = (AlignFrame) getBottomFrame();
534 final boolean scaleProteinAsCdna = topFrame.viewport
535 .isScaleProteinAsCdna();
537 AlignmentPanel newTopPanel = topFrame.newView(null, true);
538 AlignmentPanel newBottomPanel = bottomFrame.newView(null, true);
541 * This currently (for the first new view only) leaves the top pane on tab 0
542 * but the bottom on tab 1. This results from 'setInitialTabVisible' echoing
543 * from the bottom back to the first frame. Next line is a fudge to work
544 * around this. TODO find a better way.
546 if (topFrame.getTabIndex() != bottomFrame.getTabIndex())
548 topFrame.setDisplayedView(newTopPanel);
551 newBottomPanel.av.setViewName(newTopPanel.av.getViewName());
552 newTopPanel.av.setCodingComplement(newBottomPanel.av);
555 * These lines can be removed once scaleProteinAsCdna is added to element
556 * Viewport in jalview.xsd, as Jalview2XML.copyAlignPanel will then take
559 newTopPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
560 newBottomPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
563 * Line up id labels etc
567 final StructureSelectionManager ssm = StructureSelectionManager
568 .getStructureSelectionManager(Desktop.instance);
569 ssm.addCommandListener(newTopPanel.av);
570 ssm.addCommandListener(newBottomPanel.av);
574 * Close the currently selected view in both panes. If there is only one view,
575 * close this split frame.
577 protected void closeView_actionPerformed()
579 int viewCount = ((AlignFrame) getTopFrame()).getAlignPanels().size();
586 AlignmentPanel topPanel = ((AlignFrame) getTopFrame()).alignPanel;
587 AlignmentPanel bottomPanel = ((AlignFrame) getBottomFrame()).alignPanel;
589 ((AlignFrame) getTopFrame()).closeView(topPanel);
590 ((AlignFrame) getBottomFrame()).closeView(bottomPanel);
595 * Close child frames and this split frame.
599 ((AlignFrame) getTopFrame()).closeMenuItem_actionPerformed(true);
600 ((AlignFrame) getBottomFrame()).closeMenuItem_actionPerformed(true);
603 this.setClosed(true);
604 } catch (PropertyVetoException e)
611 * Replace AlignFrame 'expand views' action with SplitFrame version.
613 protected void overrideExpandViews()
615 KeyStroke key_X = KeyStroke.getKeyStroke(KeyEvent.VK_X, 0, false);
616 AbstractAction action = new AbstractAction()
619 public void actionPerformed(ActionEvent e)
621 expandViews_actionPerformed();
624 overrideMenuItem(key_X, action);
628 * Replace AlignFrame 'gather views' action with SplitFrame version.
630 protected void overrideGatherViews()
632 KeyStroke key_G = KeyStroke.getKeyStroke(KeyEvent.VK_G, 0, false);
633 AbstractAction action = new AbstractAction()
636 public void actionPerformed(ActionEvent e)
638 gatherViews_actionPerformed();
641 overrideMenuItem(key_G, action);
645 * Override the menu action associated with the keystroke in the child frames,
646 * replacing it with the given action.
651 private void overrideMenuItem(KeyStroke ks, AbstractAction action)
653 overrideMenuItem(ks, action, getTopFrame());
654 overrideMenuItem(ks, action, getBottomFrame());
658 * Override the menu action associated with the keystroke in one child frame,
659 * replacing it with the given action. Mwahahahaha.
665 private void overrideMenuItem(KeyStroke key, final AbstractAction action,
668 if (comp instanceof AlignFrame)
670 JMenuItem mi = ((AlignFrame) comp).getAccelerators().get(key);
673 for (ActionListener al : mi.getActionListeners())
675 mi.removeActionListener(al);
677 mi.addActionListener(new ActionListener()
680 public void actionPerformed(ActionEvent e)
682 action.actionPerformed(e);
690 * Expand any multiple views (which are always in pairs) into separate split
693 protected void expandViews_actionPerformed()
695 Desktop.instance.explodeViews(this);
699 * Gather any other SplitFrame views of this alignment back in as multiple
700 * (pairs of) views in this SplitFrame.
702 protected void gatherViews_actionPerformed()
704 Desktop.instance.gatherViews(this);
708 * Returns the alignment in the complementary frame to the one given.
711 public AlignmentI getComplement(Object alignFrame)
713 if (alignFrame == this.getTopFrame())
715 return ((AlignFrame) getBottomFrame()).viewport.getAlignment();
717 else if (alignFrame == this.getBottomFrame())
719 return ((AlignFrame) getTopFrame()).viewport.getAlignment();
725 * Returns the title of the complementary frame to the one given.
728 public String getComplementTitle(Object alignFrame)
730 if (alignFrame == this.getTopFrame())
732 return ((AlignFrame) getBottomFrame()).getTitle();
734 else if (alignFrame == this.getBottomFrame())
736 return ((AlignFrame) getTopFrame()).getTitle();
742 * Set the 'other half' to hidden / revealed.
745 public void setComplementVisible(Object alignFrame, boolean show)
748 * Hiding the AlignPanel suppresses unnecessary repaints
750 if (alignFrame == getTopFrame())
752 ((AlignFrame) getBottomFrame()).alignPanel.setVisible(show);
754 else if (alignFrame == getBottomFrame())
756 ((AlignFrame) getTopFrame()).alignPanel.setVisible(show);
758 super.setComplementVisible(alignFrame, show);
762 * return the AlignFrames held by this container
764 * @return { Top alignFrame (Usually CDS), Bottom AlignFrame (Usually
767 public List<AlignFrame> getAlignFrames()
770 .asList(new AlignFrame[]
771 { (AlignFrame) getTopFrame(), (AlignFrame) getBottomFrame() });
775 * Replace Cmd-F Find action with our version. This is necessary because the
776 * 'default' Finder searches in the first AlignFrame it finds. We need it to
777 * search in the half of the SplitFrame that has the mouse.
779 protected void overrideFind()
782 * Ctrl-F / Cmd-F open Finder dialog, 'focused' on the right alignment
784 KeyStroke key_cmdF = KeyStroke.getKeyStroke(KeyEvent.VK_F,
785 jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
786 AbstractAction action = new AbstractAction()
789 public void actionPerformed(ActionEvent e)
791 Component c = getFrameAtMouse();
792 if (c != null && c instanceof AlignFrame)
794 AlignFrame af = (AlignFrame) c;
795 new Finder(af.viewport, af.alignPanel);
799 overrideKeyBinding(key_cmdF, action);
803 * Override to do nothing if triggered from one of the child frames
806 public void setSelected(boolean selected) throws PropertyVetoException
808 JDesktopPane desktopPane = getDesktopPane();
809 JInternalFrame fr = desktopPane == null ? null
810 : desktopPane.getSelectedFrame();
811 if (fr == getTopFrame() || fr == getBottomFrame())
814 * patch for JAL-3288 (deselecting top/bottom frame closes popup menu);
815 * it may be possible to remove this method in future
816 * if the underlying Java behaviour changes
824 super.setSelected(selected);