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);
90 getBottomFrame().setSplitFrame(this);
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()
165 * Ensure sequence ids are the same width so sequences line up
167 int w1 = ((AlignFrame) getTopFrame()).getViewport().getIdWidth();
168 int w2 = ((AlignFrame) getBottomFrame()).getViewport().getIdWidth();
169 int w3 = Math.max(w1, w2);
172 ((AlignFrame) getTopFrame()).getViewport().setIdWidth(w3);
176 ((AlignFrame) getBottomFrame()).getViewport().setIdWidth(w3);
180 * Scale protein to either 1 or 3 times character width of dna
182 final AlignViewport topViewport = ((AlignFrame) getTopFrame()).viewport;
183 final AlignViewport bottomViewport = ((AlignFrame) getBottomFrame()).viewport;
184 final AlignmentI topAlignment = topViewport.getAlignment();
185 final AlignmentI bottomAlignment = bottomViewport.getAlignment();
186 AlignmentViewport cdna = topAlignment.isNucleotide() ? topViewport
187 : (bottomAlignment.isNucleotide() ? bottomViewport : null);
188 AlignmentViewport protein = !topAlignment.isNucleotide() ? topViewport
189 : (!bottomAlignment.isNucleotide() ? bottomViewport : null);
190 if (protein != null && cdna != null)
192 int scale = protein.isScaleProteinAsCdna() ? 3 : 1;
193 protein.setCharWidth(scale * cdna.getViewStyle().getCharWidth());
198 * Adjusts the divider for a sensible split of the real estate (for example,
199 * when many transcripts are shown with a single protein). This should only be
200 * called after the split pane has been laid out (made visible) so it has a
201 * height. The aim is to avoid unnecessary vertical scroll bars, while
202 * ensuring that at least 2 sequences are visible in each panel.
204 * Once laid out, the user may choose to customise as they wish, so this
205 * method is not called again after the initial layout.
207 protected void adjustInitialLayout()
209 AlignFrame topFrame = (AlignFrame) getTopFrame();
210 AlignFrame bottomFrame = (AlignFrame) getBottomFrame();
213 * recompute layout of top and bottom panels to reflect their
214 * actual (rather than requested) height
216 topFrame.alignPanel.adjustAnnotationHeight();
217 bottomFrame.alignPanel.adjustAnnotationHeight();
219 final AlignViewport topViewport = topFrame.viewport;
220 final AlignViewport bottomViewport = bottomFrame.viewport;
221 final AlignmentI topAlignment = topViewport.getAlignment();
222 final AlignmentI bottomAlignment = bottomViewport.getAlignment();
223 boolean topAnnotations = topViewport.isShowAnnotation();
224 boolean bottomAnnotations = bottomViewport.isShowAnnotation();
225 // TODO need number of visible sequences here, not #sequences - how?
226 int topCount = topAlignment.getHeight();
227 int bottomCount = bottomAlignment.getHeight();
228 int topCharHeight = topViewport.getViewStyle().getCharHeight();
229 int bottomCharHeight = bottomViewport.getViewStyle().getCharHeight();
232 * calculate the minimum ratio that leaves at least the height
233 * of two sequences (after rounding) visible in the top panel
235 int topPanelHeight = topFrame.getHeight();
236 int bottomPanelHeight = bottomFrame.getHeight();
237 int topSequencesHeight = topFrame.alignPanel.getSeqPanel().seqCanvas
239 int topPanelMinHeight = topPanelHeight
240 - Math.max(0, topSequencesHeight - 3 * topCharHeight);
241 double totalHeight = (double) topPanelHeight + bottomPanelHeight;
242 double minRatio = topPanelMinHeight / totalHeight;
245 * calculate the maximum ratio that leaves at least the height
246 * of two sequences (after rounding) visible in the bottom panel
248 int bottomSequencesHeight = bottomFrame.alignPanel.getSeqPanel().seqCanvas
250 int bottomPanelMinHeight = bottomPanelHeight
251 - Math.max(0, bottomSequencesHeight - 3 * bottomCharHeight);
252 double maxRatio = (totalHeight - bottomPanelMinHeight) / totalHeight;
255 * estimate ratio of (topFrameContent / bottomFrameContent)
257 int insets = Platform.isAMac() ? MAC_INSETS_HEIGHT
258 : WINDOWS_INSETS_HEIGHT;
259 // allow 3 'rows' for scale, scrollbar, status bar
260 int topHeight = insets + (3 + topCount) * topCharHeight
261 + (topAnnotations ? topViewport.calcPanelHeight() : 0);
262 int bottomHeight = insets + (3 + bottomCount) * bottomCharHeight
263 + (bottomAnnotations ? bottomViewport.calcPanelHeight() : 0);
264 double ratio = ((double) topHeight)
265 / (double) (topHeight + bottomHeight);
268 * limit ratio to avoid concealing all sequences
270 ratio = Math.min(ratio, maxRatio);
271 ratio = Math.max(ratio, minRatio);
272 setRelativeDividerLocation(ratio);
276 * Add a listener to tidy up when the frame is closed.
278 protected void addCloseFrameListener()
280 addInternalFrameListener(new InternalFrameAdapter()
283 public void internalFrameClosed(InternalFrameEvent evt)
291 * Add a key listener that delegates to whichever split component the mouse is
292 * in (or does nothing if neither).
294 protected void addKeyListener()
296 addKeyListener(new KeyAdapter()
300 public void keyPressed(KeyEvent e)
302 AlignFrame af = (AlignFrame) getFrameAtMouse();
305 * Intercept and override any keys here if wanted.
307 if (!overrideKey(e, af))
311 for (KeyListener kl : af.getKeyListeners())
320 public void keyReleased(KeyEvent e)
322 Component c = getFrameAtMouse();
325 for (KeyListener kl : c.getKeyListeners())
336 * Returns true if the key event is overriden and actioned (or ignored) here,
337 * else returns false, indicating it should be delegated to the AlignFrame's
340 * We can't handle Cmd-Key combinations here, instead this is done by
341 * overriding key bindings.
343 * @see addKeyOverrides
348 protected boolean overrideKey(KeyEvent e, AlignFrame af)
350 boolean actioned = false;
351 int keyCode = e.getKeyCode();
354 case KeyEvent.VK_DOWN:
355 if (e.isAltDown() || !af.viewport.cursorMode)
358 * Key down (or Alt-key-down in cursor mode) - move selected sequences
360 ((AlignFrame) getTopFrame()).moveSelectedSequences(false);
361 ((AlignFrame) getBottomFrame()).moveSelectedSequences(false);
367 if (e.isAltDown() || !af.viewport.cursorMode)
370 * Key up (or Alt-key-up in cursor mode) - move selected sequences
372 ((AlignFrame) getTopFrame()).moveSelectedSequences(true);
373 ((AlignFrame) getBottomFrame()).moveSelectedSequences(true);
384 * Set key bindings (recommended for Swing over key accelerators).
386 private void addKeyBindings()
388 overrideDelegatedKeyBindings();
390 overrideImplementedKeyBindings();
394 * Override key bindings with alternative action methods implemented in this
397 protected void overrideImplementedKeyBindings()
402 overrideExpandViews();
403 overrideGatherViews();
407 * Replace Cmd-W close view action with our version.
409 protected void overrideCloseView()
411 AbstractAction action;
413 * Ctrl-W / Cmd-W - close view or window
415 KeyStroke key_cmdW = KeyStroke.getKeyStroke(KeyEvent.VK_W,
416 jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
417 action = new AbstractAction()
420 public void actionPerformed(ActionEvent e)
422 closeView_actionPerformed();
425 overrideKeyBinding(key_cmdW, action);
429 * Replace Cmd-T new view action with our version.
431 protected void overrideNewView()
434 * Ctrl-T / Cmd-T open new view
436 KeyStroke key_cmdT = KeyStroke.getKeyStroke(KeyEvent.VK_T,
437 jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
438 AbstractAction action = new AbstractAction()
441 public void actionPerformed(ActionEvent e)
443 newView_actionPerformed();
446 overrideKeyBinding(key_cmdT, action);
450 * For now, delegates key events to the corresponding key accelerator for the
451 * AlignFrame that the mouse is in. Hopefully can be simplified in future if
452 * AlignFrame is changed to use key bindings rather than accelerators.
454 protected void overrideDelegatedKeyBindings()
456 if (getTopFrame() instanceof AlignFrame)
459 * Get all accelerator keys in the top frame (the bottom should be
460 * identical) and override each one.
462 for (Entry<KeyStroke, JMenuItem> acc : ((AlignFrame) getTopFrame())
463 .getAccelerators().entrySet())
465 overrideKeyBinding(acc);
471 * Overrides an AlignFrame key accelerator with our version which delegates to
472 * the action listener in whichever frame has the mouse (and does nothing if
477 private void overrideKeyBinding(Entry<KeyStroke, JMenuItem> acc)
479 final KeyStroke ks = acc.getKey();
480 InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
481 inputMap.put(ks, ks);
482 this.getActionMap().put(ks, new AbstractAction()
485 public void actionPerformed(ActionEvent e)
487 Component c = getFrameAtMouse();
488 if (c != null && c instanceof AlignFrame)
490 for (ActionListener a : ((AlignFrame) c).getAccelerators().get(ks)
491 .getActionListeners())
493 a.actionPerformed(null);
501 * Replace an accelerator key's action with the specified action.
505 protected void overrideKeyBinding(KeyStroke ks, AbstractAction action)
507 this.getActionMap().put(ks, action);
508 overrideMenuItem(ks, action);
512 * Create and link new views (with matching names) in both panes.
514 * Note this is _not_ multiple tabs, each hosting a split pane view, rather it
515 * is a single split pane with each split holding multiple tabs which are
518 * TODO implement instead with a tabbed holder in the SplitView, each tab
519 * holding a single JSplitPane. Would avoid a duplicated tab, at the cost of
520 * some additional coding.
522 protected void newView_actionPerformed()
524 AlignFrame topFrame = (AlignFrame) getTopFrame();
525 AlignFrame bottomFrame = (AlignFrame) getBottomFrame();
526 final boolean scaleProteinAsCdna = topFrame.viewport
527 .isScaleProteinAsCdna();
529 AlignmentPanel newTopPanel = topFrame.newView(null, true);
530 AlignmentPanel newBottomPanel = bottomFrame.newView(null, true);
533 * This currently (for the first new view only) leaves the top pane on tab 0
534 * but the bottom on tab 1. This results from 'setInitialTabVisible' echoing
535 * from the bottom back to the first frame. Next line is a fudge to work
536 * around this. TODO find a better way.
538 if (topFrame.getTabIndex() != bottomFrame.getTabIndex())
540 topFrame.setDisplayedView(newTopPanel);
543 newBottomPanel.av.setViewName(newTopPanel.av.getViewName());
544 newTopPanel.av.setCodingComplement(newBottomPanel.av);
547 * These lines can be removed once scaleProteinAsCdna is added to element
548 * Viewport in jalview.xsd, as Jalview2XML.copyAlignPanel will then take
551 newTopPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
552 newBottomPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
555 * Line up id labels etc
559 final StructureSelectionManager ssm = StructureSelectionManager
560 .getStructureSelectionManager(Desktop.instance);
561 ssm.addCommandListener(newTopPanel.av);
562 ssm.addCommandListener(newBottomPanel.av);
566 * Close the currently selected view in both panes. If there is only one view,
567 * close this split frame.
569 protected void closeView_actionPerformed()
571 int viewCount = ((AlignFrame) getTopFrame()).getAlignPanels().size();
578 AlignmentPanel topPanel = ((AlignFrame) getTopFrame()).alignPanel;
579 AlignmentPanel bottomPanel = ((AlignFrame) getBottomFrame()).alignPanel;
581 ((AlignFrame) getTopFrame()).closeView(topPanel);
582 ((AlignFrame) getBottomFrame()).closeView(bottomPanel);
587 * Close child frames and this split frame.
591 ((AlignFrame) getTopFrame()).closeMenuItem_actionPerformed(true);
592 ((AlignFrame) getBottomFrame()).closeMenuItem_actionPerformed(true);
595 this.setClosed(true);
596 } catch (PropertyVetoException e)
603 * Replace AlignFrame 'expand views' action with SplitFrame version.
605 protected void overrideExpandViews()
607 KeyStroke key_X = KeyStroke.getKeyStroke(KeyEvent.VK_X, 0, false);
608 AbstractAction action = new AbstractAction()
611 public void actionPerformed(ActionEvent e)
613 expandViews_actionPerformed();
616 overrideMenuItem(key_X, action);
620 * Replace AlignFrame 'gather views' action with SplitFrame version.
622 protected void overrideGatherViews()
624 KeyStroke key_G = KeyStroke.getKeyStroke(KeyEvent.VK_G, 0, false);
625 AbstractAction action = new AbstractAction()
628 public void actionPerformed(ActionEvent e)
630 gatherViews_actionPerformed();
633 overrideMenuItem(key_G, action);
637 * Override the menu action associated with the keystroke in the child frames,
638 * replacing it with the given action.
643 private void overrideMenuItem(KeyStroke ks, AbstractAction action)
645 overrideMenuItem(ks, action, getTopFrame());
646 overrideMenuItem(ks, action, getBottomFrame());
650 * Override the menu action associated with the keystroke in one child frame,
651 * replacing it with the given action. Mwahahahaha.
657 private void overrideMenuItem(KeyStroke key, final AbstractAction action,
660 if (comp instanceof AlignFrame)
662 JMenuItem mi = ((AlignFrame) comp).getAccelerators().get(key);
665 for (ActionListener al : mi.getActionListeners())
667 mi.removeActionListener(al);
669 mi.addActionListener(new ActionListener()
672 public void actionPerformed(ActionEvent e)
674 action.actionPerformed(e);
682 * Expand any multiple views (which are always in pairs) into separate split
685 protected void expandViews_actionPerformed()
687 Desktop.instance.explodeViews(this);
691 * Gather any other SplitFrame views of this alignment back in as multiple
692 * (pairs of) views in this SplitFrame.
694 protected void gatherViews_actionPerformed()
696 Desktop.instance.gatherViews(this);
700 * Returns the alignment in the complementary frame to the one given.
703 public AlignmentI getComplement(Object alignFrame)
705 if (alignFrame == this.getTopFrame())
707 return ((AlignFrame) getBottomFrame()).viewport.getAlignment();
709 else if (alignFrame == this.getBottomFrame())
711 return ((AlignFrame) getTopFrame()).viewport.getAlignment();
717 * Returns the title of the complementary frame to the one given.
720 public String getComplementTitle(Object alignFrame)
722 if (alignFrame == this.getTopFrame())
724 return ((AlignFrame) getBottomFrame()).getTitle();
726 else if (alignFrame == this.getBottomFrame())
728 return ((AlignFrame) getTopFrame()).getTitle();
734 * Set the 'other half' to hidden / revealed.
737 public void setComplementVisible(Object alignFrame, boolean show)
740 * Hiding the AlignPanel suppresses unnecessary repaints
742 if (alignFrame == getTopFrame())
744 ((AlignFrame) getBottomFrame()).alignPanel.setVisible(show);
746 else if (alignFrame == getBottomFrame())
748 ((AlignFrame) getTopFrame()).alignPanel.setVisible(show);
750 super.setComplementVisible(alignFrame, show);
754 * return the AlignFrames held by this container
756 * @return { Top alignFrame (Usually CDS), Bottom AlignFrame (Usually
759 public List<AlignFrame> getAlignFrames()
762 .asList(new AlignFrame[]
763 { (AlignFrame) getTopFrame(), (AlignFrame) getBottomFrame() });
767 * Replace Cmd-F Find action with our version. This is necessary because the
768 * 'default' Finder searches in the first AlignFrame it finds. We need it to
769 * search in the half of the SplitFrame that has the mouse.
771 protected void overrideFind()
774 * Ctrl-F / Cmd-F open Finder dialog, 'focused' on the right alignment
776 KeyStroke key_cmdF = KeyStroke.getKeyStroke(KeyEvent.VK_F,
777 jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
778 AbstractAction action = new AbstractAction()
781 public void actionPerformed(ActionEvent e)
783 Component c = getFrameAtMouse();
784 if (c != null && c instanceof AlignFrame)
786 AlignFrame af = (AlignFrame) c;
787 new Finder(af.viewport, af.alignPanel);
791 overrideKeyBinding(key_cmdF, action);
795 * Override to do nothing if triggered from one of the child frames
798 public void setSelected(boolean selected) throws PropertyVetoException
800 JDesktopPane desktopPane = getDesktopPane();
801 JInternalFrame fr = desktopPane == null ? null
802 : desktopPane.getSelectedFrame();
803 if (fr == getTopFrame() || fr == getBottomFrame())
806 * patch for JAL-3288 (deselecting top/bottom frame closes popup menu);
807 * it may be possible to remove this method in future
808 * if the underlying Java behaviour changes
816 super.setSelected(selected);