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()
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
170 int w1 = topViewport.getIdWidth();
171 int w2 = bottomViewport.getIdWidth();
172 int w3 = Math.max(w1, w2);
173 topViewport.setIdWidth(w3);
174 bottomViewport.setIdWidth(w3);
177 * Scale protein to either 1 or 3 times character width of dna
179 final AlignmentI topAlignment = topViewport.getAlignment();
180 final AlignmentI bottomAlignment = bottomViewport.getAlignment();
181 AlignmentViewport cdna = topAlignment.isNucleotide() ? topViewport
182 : (bottomAlignment.isNucleotide() ? bottomViewport : null);
183 AlignmentViewport protein = !topAlignment.isNucleotide() ? topViewport
184 : (!bottomAlignment.isNucleotide() ? bottomViewport : null);
185 if (protein != null && cdna != null)
187 int scale = protein.isScaleProteinAsCdna() ? 3 : 1;
188 protein.setCharWidth(scale * cdna.getViewStyle().getCharWidth());
193 * Adjusts the divider for a sensible split of the real estate (for example,
194 * when many transcripts are shown with a single protein). This should only be
195 * called after the split pane has been laid out (made visible) so it has a
196 * height. The aim is to avoid unnecessary vertical scroll bars, while
197 * ensuring that at least 2 sequences are visible in each panel.
199 * Once laid out, the user may choose to customise as they wish, so this
200 * method is not called again after the initial layout.
202 protected void adjustInitialLayout()
204 AlignFrame topFrame = (AlignFrame) getTopFrame();
205 AlignFrame bottomFrame = (AlignFrame) getBottomFrame();
208 * recompute layout of top and bottom panels to reflect their
209 * actual (rather than requested) height
211 topFrame.alignPanel.adjustAnnotationHeight();
212 bottomFrame.alignPanel.adjustAnnotationHeight();
214 final AlignViewport topViewport = topFrame.viewport;
215 final AlignViewport bottomViewport = bottomFrame.viewport;
216 final AlignmentI topAlignment = topViewport.getAlignment();
217 final AlignmentI bottomAlignment = bottomViewport.getAlignment();
218 boolean topAnnotations = topViewport.isShowAnnotation();
219 boolean bottomAnnotations = bottomViewport.isShowAnnotation();
220 // TODO need number of visible sequences here, not #sequences - how?
221 int topCount = topAlignment.getHeight();
222 int bottomCount = bottomAlignment.getHeight();
223 int topCharHeight = topViewport.getViewStyle().getCharHeight();
224 int bottomCharHeight = bottomViewport.getViewStyle().getCharHeight();
227 * calculate the minimum ratio that leaves at least the height
228 * of two sequences (after rounding) visible in the top panel
230 int topPanelHeight = topFrame.getHeight();
231 int bottomPanelHeight = bottomFrame.getHeight();
232 int topSequencesHeight = topFrame.alignPanel.getSeqPanel().seqCanvas
234 int topPanelMinHeight = topPanelHeight
235 - Math.max(0, topSequencesHeight - 3 * topCharHeight);
236 double totalHeight = (double) topPanelHeight + bottomPanelHeight;
237 double minRatio = topPanelMinHeight / totalHeight;
240 * calculate the maximum ratio that leaves at least the height
241 * of two sequences (after rounding) visible in the bottom panel
243 int bottomSequencesHeight = bottomFrame.alignPanel.getSeqPanel().seqCanvas
245 int bottomPanelMinHeight = bottomPanelHeight
246 - Math.max(0, bottomSequencesHeight - 3 * bottomCharHeight);
247 double maxRatio = (totalHeight - bottomPanelMinHeight) / totalHeight;
250 * estimate ratio of (topFrameContent / bottomFrameContent)
252 int insets = Platform.isAMac() ? MAC_INSETS_HEIGHT
253 : WINDOWS_INSETS_HEIGHT;
254 // allow 3 'rows' for scale, scrollbar, status bar
255 int topHeight = insets + (3 + topCount) * topCharHeight
256 + (topAnnotations ? topViewport.calcPanelHeight() : 0);
257 int bottomHeight = insets + (3 + bottomCount) * bottomCharHeight
258 + (bottomAnnotations ? bottomViewport.calcPanelHeight() : 0);
259 double ratio = ((double) topHeight)
260 / (double) (topHeight + bottomHeight);
263 * limit ratio to avoid concealing all sequences
265 ratio = Math.min(ratio, maxRatio);
266 ratio = Math.max(ratio, minRatio);
267 setRelativeDividerLocation(ratio);
271 * Add a listener to tidy up when the frame is closed.
273 protected void addCloseFrameListener()
275 addInternalFrameListener(new InternalFrameAdapter()
278 public void internalFrameClosed(InternalFrameEvent evt)
286 * Add a key listener that delegates to whichever split component the mouse is
287 * in (or does nothing if neither).
289 protected void addKeyListener()
291 addKeyListener(new KeyAdapter()
295 public void keyPressed(KeyEvent e)
297 AlignFrame af = (AlignFrame) getFrameAtMouse();
300 * Intercept and override any keys here if wanted.
302 if (!overrideKey(e, af))
306 for (KeyListener kl : af.getKeyListeners())
315 public void keyReleased(KeyEvent e)
317 Component c = getFrameAtMouse();
320 for (KeyListener kl : c.getKeyListeners())
331 * Returns true if the key event is overriden and actioned (or ignored) here,
332 * else returns false, indicating it should be delegated to the AlignFrame's
335 * We can't handle Cmd-Key combinations here, instead this is done by
336 * overriding key bindings.
338 * @see addKeyOverrides
343 protected boolean overrideKey(KeyEvent e, AlignFrame af)
345 boolean actioned = false;
346 int keyCode = e.getKeyCode();
349 case KeyEvent.VK_DOWN:
350 if (e.isAltDown() || !af.viewport.cursorMode)
353 * Key down (or Alt-key-down in cursor mode) - move selected sequences
355 ((AlignFrame) getTopFrame()).moveSelectedSequences(false);
356 ((AlignFrame) getBottomFrame()).moveSelectedSequences(false);
362 if (e.isAltDown() || !af.viewport.cursorMode)
365 * Key up (or Alt-key-up in cursor mode) - move selected sequences
367 ((AlignFrame) getTopFrame()).moveSelectedSequences(true);
368 ((AlignFrame) getBottomFrame()).moveSelectedSequences(true);
379 * Set key bindings (recommended for Swing over key accelerators).
381 private void addKeyBindings()
383 overrideDelegatedKeyBindings();
385 overrideImplementedKeyBindings();
389 * Override key bindings with alternative action methods implemented in this
392 protected void overrideImplementedKeyBindings()
397 overrideExpandViews();
398 overrideGatherViews();
402 * Replace Cmd-W close view action with our version.
404 protected void overrideCloseView()
406 AbstractAction action;
408 * Ctrl-W / Cmd-W - close view or window
410 KeyStroke key_cmdW = KeyStroke.getKeyStroke(KeyEvent.VK_W,
411 jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
412 action = new AbstractAction()
415 public void actionPerformed(ActionEvent e)
417 closeView_actionPerformed();
420 overrideKeyBinding(key_cmdW, action);
424 * Replace Cmd-T new view action with our version.
426 protected void overrideNewView()
429 * Ctrl-T / Cmd-T open new view
431 KeyStroke key_cmdT = KeyStroke.getKeyStroke(KeyEvent.VK_T,
432 jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
433 AbstractAction action = new AbstractAction()
436 public void actionPerformed(ActionEvent e)
438 newView_actionPerformed();
441 overrideKeyBinding(key_cmdT, action);
445 * For now, delegates key events to the corresponding key accelerator for the
446 * AlignFrame that the mouse is in. Hopefully can be simplified in future if
447 * AlignFrame is changed to use key bindings rather than accelerators.
449 protected void overrideDelegatedKeyBindings()
451 if (getTopFrame() instanceof AlignFrame)
454 * Get all accelerator keys in the top frame (the bottom should be
455 * identical) and override each one.
457 for (Entry<KeyStroke, JMenuItem> acc : ((AlignFrame) getTopFrame())
458 .getAccelerators().entrySet())
460 overrideKeyBinding(acc);
466 * Overrides an AlignFrame key accelerator with our version which delegates to
467 * the action listener in whichever frame has the mouse (and does nothing if
472 private void overrideKeyBinding(Entry<KeyStroke, JMenuItem> acc)
474 final KeyStroke ks = acc.getKey();
475 InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
476 inputMap.put(ks, ks);
477 this.getActionMap().put(ks, new AbstractAction()
480 public void actionPerformed(ActionEvent e)
482 Component c = getFrameAtMouse();
483 if (c != null && c instanceof AlignFrame)
485 for (ActionListener a : ((AlignFrame) c).getAccelerators().get(ks)
486 .getActionListeners())
488 a.actionPerformed(null);
496 * Replace an accelerator key's action with the specified action.
500 protected void overrideKeyBinding(KeyStroke ks, AbstractAction action)
502 this.getActionMap().put(ks, action);
503 overrideMenuItem(ks, action);
507 * Create and link new views (with matching names) in both panes.
509 * Note this is _not_ multiple tabs, each hosting a split pane view, rather it
510 * is a single split pane with each split holding multiple tabs which are
513 * TODO implement instead with a tabbed holder in the SplitView, each tab
514 * holding a single JSplitPane. Would avoid a duplicated tab, at the cost of
515 * some additional coding.
517 protected void newView_actionPerformed()
519 AlignFrame topFrame = (AlignFrame) getTopFrame();
520 AlignFrame bottomFrame = (AlignFrame) getBottomFrame();
521 final boolean scaleProteinAsCdna = topFrame.viewport
522 .isScaleProteinAsCdna();
524 AlignmentPanel newTopPanel = topFrame.newView(null, true);
525 AlignmentPanel newBottomPanel = bottomFrame.newView(null, true);
528 * This currently (for the first new view only) leaves the top pane on tab 0
529 * but the bottom on tab 1. This results from 'setInitialTabVisible' echoing
530 * from the bottom back to the first frame. Next line is a fudge to work
531 * around this. TODO find a better way.
533 if (topFrame.getTabIndex() != bottomFrame.getTabIndex())
535 topFrame.setDisplayedView(newTopPanel);
538 newBottomPanel.av.setViewName(newTopPanel.av.getViewName());
539 newTopPanel.av.setCodingComplement(newBottomPanel.av);
542 * These lines can be removed once scaleProteinAsCdna is added to element
543 * Viewport in jalview.xsd, as Jalview2XML.copyAlignPanel will then take
546 newTopPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
547 newBottomPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
550 * Line up id labels etc
554 final StructureSelectionManager ssm = StructureSelectionManager
555 .getStructureSelectionManager(Desktop.instance);
556 ssm.addCommandListener(newTopPanel.av);
557 ssm.addCommandListener(newBottomPanel.av);
561 * Close the currently selected view in both panes. If there is only one view,
562 * close this split frame.
564 protected void closeView_actionPerformed()
566 int viewCount = ((AlignFrame) getTopFrame()).getAlignPanels().size();
573 AlignmentPanel topPanel = ((AlignFrame) getTopFrame()).alignPanel;
574 AlignmentPanel bottomPanel = ((AlignFrame) getBottomFrame()).alignPanel;
576 ((AlignFrame) getTopFrame()).closeView(topPanel);
577 ((AlignFrame) getBottomFrame()).closeView(bottomPanel);
582 * Close child frames and this split frame.
586 ((AlignFrame) getTopFrame()).closeMenuItem_actionPerformed(true);
587 ((AlignFrame) getBottomFrame()).closeMenuItem_actionPerformed(true);
590 this.setClosed(true);
591 } catch (PropertyVetoException e)
598 * Replace AlignFrame 'expand views' action with SplitFrame version.
600 protected void overrideExpandViews()
602 KeyStroke key_X = KeyStroke.getKeyStroke(KeyEvent.VK_X, 0, false);
603 AbstractAction action = new AbstractAction()
606 public void actionPerformed(ActionEvent e)
608 expandViews_actionPerformed();
611 overrideMenuItem(key_X, action);
615 * Replace AlignFrame 'gather views' action with SplitFrame version.
617 protected void overrideGatherViews()
619 KeyStroke key_G = KeyStroke.getKeyStroke(KeyEvent.VK_G, 0, false);
620 AbstractAction action = new AbstractAction()
623 public void actionPerformed(ActionEvent e)
625 gatherViews_actionPerformed();
628 overrideMenuItem(key_G, action);
632 * Override the menu action associated with the keystroke in the child frames,
633 * replacing it with the given action.
638 private void overrideMenuItem(KeyStroke ks, AbstractAction action)
640 overrideMenuItem(ks, action, getTopFrame());
641 overrideMenuItem(ks, action, getBottomFrame());
645 * Override the menu action associated with the keystroke in one child frame,
646 * replacing it with the given action. Mwahahahaha.
652 private void overrideMenuItem(KeyStroke key, final AbstractAction action,
655 if (comp instanceof AlignFrame)
657 JMenuItem mi = ((AlignFrame) comp).getAccelerators().get(key);
660 for (ActionListener al : mi.getActionListeners())
662 mi.removeActionListener(al);
664 mi.addActionListener(new ActionListener()
667 public void actionPerformed(ActionEvent e)
669 action.actionPerformed(e);
677 * Expand any multiple views (which are always in pairs) into separate split
680 protected void expandViews_actionPerformed()
682 Desktop.instance.explodeViews(this);
686 * Gather any other SplitFrame views of this alignment back in as multiple
687 * (pairs of) views in this SplitFrame.
689 protected void gatherViews_actionPerformed()
691 Desktop.instance.gatherViews(this);
695 * Returns the alignment in the complementary frame to the one given.
698 public AlignmentI getComplement(Object alignFrame)
700 if (alignFrame == this.getTopFrame())
702 return ((AlignFrame) getBottomFrame()).viewport.getAlignment();
704 else if (alignFrame == this.getBottomFrame())
706 return ((AlignFrame) getTopFrame()).viewport.getAlignment();
712 * Returns the title of the complementary frame to the one given.
715 public String getComplementTitle(Object alignFrame)
717 if (alignFrame == this.getTopFrame())
719 return ((AlignFrame) getBottomFrame()).getTitle();
721 else if (alignFrame == this.getBottomFrame())
723 return ((AlignFrame) getTopFrame()).getTitle();
729 * Set the 'other half' to hidden / revealed.
732 public void setComplementVisible(Object alignFrame, boolean show)
735 * Hiding the AlignPanel suppresses unnecessary repaints
737 if (alignFrame == getTopFrame())
739 ((AlignFrame) getBottomFrame()).alignPanel.setVisible(show);
741 else if (alignFrame == getBottomFrame())
743 ((AlignFrame) getTopFrame()).alignPanel.setVisible(show);
745 super.setComplementVisible(alignFrame, show);
749 * return the AlignFrames held by this container
751 * @return { Top alignFrame (Usually CDS), Bottom AlignFrame (Usually
754 public List<AlignFrame> getAlignFrames()
757 .asList(new AlignFrame[]
758 { (AlignFrame) getTopFrame(), (AlignFrame) getBottomFrame() });
762 * Replace Cmd-F Find action with our version. This is necessary because the
763 * 'default' Finder searches in the first AlignFrame it finds. We need it to
764 * search in the half of the SplitFrame that has the mouse.
766 protected void overrideFind()
769 * Ctrl-F / Cmd-F open Finder dialog, 'focused' on the right alignment
771 KeyStroke key_cmdF = KeyStroke.getKeyStroke(KeyEvent.VK_F,
772 jalview.util.ShortcutKeyMaskExWrapper.getMenuShortcutKeyMaskEx(), false);
773 AbstractAction action = new AbstractAction()
776 public void actionPerformed(ActionEvent e)
778 Component c = getFrameAtMouse();
779 if (c != null && c instanceof AlignFrame)
781 AlignFrame af = (AlignFrame) c;
782 new Finder(af.viewport, af.alignPanel);
786 overrideKeyBinding(key_cmdF, action);
790 * Override to do nothing if triggered from one of the child frames
793 public void setSelected(boolean selected) throws PropertyVetoException
795 JDesktopPane desktopPane = getDesktopPane();
796 JInternalFrame fr = desktopPane == null ? null
797 : desktopPane.getSelectedFrame();
798 if (fr == getTopFrame() || fr == getBottomFrame())
801 * patch for JAL-3288 (deselecting top/bottom frame closes popup menu);
802 * it may be possible to remove this method in future
803 * if the underlying Java behaviour changes
811 super.setSelected(selected);