2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b2)
3 * Copyright (C) 2015 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.api.ViewStyleI;
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.Toolkit;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.awt.event.KeyAdapter;
37 import java.awt.event.KeyEvent;
38 import java.awt.event.KeyListener;
39 import java.beans.PropertyVetoException;
40 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.UIDefaults;
49 import javax.swing.UIManager;
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 long serialVersionUID = 1L;
69 public SplitFrame(GAlignFrame top, GAlignFrame bottom)
76 * Initialise this frame.
80 getTopFrame().setSplitFrame(this);
81 getBottomFrame().setSplitFrame(this);
82 getTopFrame().setVisible(true);
83 getBottomFrame().setVisible(true);
85 ((AlignFrame) getTopFrame()).getViewport().setCodingComplement(
86 ((AlignFrame) getBottomFrame()).getViewport());
89 * estimate width and height of SplitFrame; this.getInsets() doesn't seem to
90 * give the full additional size (a few pixels short)
92 UIDefaults defaults = UIManager.getDefaults();
93 Set<Object> keySet = defaults.keySet();
94 for (Object key : keySet)
96 System.out.println(key.toString() + " = "
97 + UIManager.get(key).toString());
99 int widthFudge = Platform.isAMac() ? 28 : 28; // Windows tbc
100 int heightFudge = Platform.isAMac() ? 50 : 50; // tbc
101 int width = ((AlignFrame) getTopFrame()).getWidth() + widthFudge;
102 int height = ((AlignFrame) getTopFrame()).getHeight()
103 + ((AlignFrame) getBottomFrame()).getHeight() + DIVIDER_SIZE
105 height = fitHeightToDesktop(height);
106 setSize(width, height);
110 addCloseFrameListener();
116 addCommandListeners();
120 * Reduce the height if too large to fit in the Desktop. Also adjust the
121 * divider location in proportion.
125 * @return original or reduced height
127 public int fitHeightToDesktop(int height)
129 // allow about 65 pixels for Desktop decorators on Windows
131 int newHeight = Math.min(height, Desktop.instance.getHeight() - 65);
132 if (newHeight != height)
134 int oldDividerLocation = getDividerLocation();
135 setDividerLocation(oldDividerLocation * newHeight / height);
141 * Set the top and bottom frames to listen to each others Commands (e.g. Edit,
144 protected void addCommandListeners()
146 // TODO if CommandListener is only ever 1:1 for complementary views,
147 // may change broadcast pattern to direct messaging (more efficient)
148 final StructureSelectionManager ssm = StructureSelectionManager
149 .getStructureSelectionManager(Desktop.instance);
150 ssm.addCommandListener(((AlignFrame) getTopFrame()).getViewport());
151 ssm.addCommandListener(((AlignFrame) getBottomFrame()).getViewport());
155 * Do any tweaking and twerking of the layout wanted.
157 public void adjustLayout()
160 * Ensure sequence ids are the same width so sequences line up
162 int w1 = ((AlignFrame) getTopFrame()).getViewport().getIdWidth();
163 int w2 = ((AlignFrame) getBottomFrame()).getViewport().getIdWidth();
164 int w3 = Math.max(w1, w2);
167 ((AlignFrame) getTopFrame()).getViewport().setIdWidth(w3);
171 ((AlignFrame) getBottomFrame()).getViewport().setIdWidth(w3);
175 * Scale protein to either 1 or 3 times character width of dna
177 final AlignViewport topViewport = ((AlignFrame) getTopFrame()).viewport;
178 final AlignViewport bottomViewport = ((AlignFrame) getBottomFrame()).viewport;
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 ViewStyleI vs = protein.getViewStyle();
188 int scale = vs.isScaleProteinAsCdna() ? 3 : 1;
189 vs.setCharWidth(scale * cdna.getViewStyle().getCharWidth());
190 protein.setViewStyle(vs);
195 * Add a listener to tidy up when the frame is closed.
197 protected void addCloseFrameListener()
199 addInternalFrameListener(new InternalFrameAdapter()
202 public void internalFrameClosed(InternalFrameEvent evt)
210 * Add a key listener that delegates to whichever split component the mouse is
211 * in (or does nothing if neither).
213 protected void addKeyListener()
215 addKeyListener(new KeyAdapter()
219 public void keyPressed(KeyEvent e)
221 AlignFrame af = (AlignFrame) getFrameAtMouse();
224 * Intercept and override any keys here if wanted.
226 if (!overrideKey(e, af))
230 for (KeyListener kl : af.getKeyListeners())
239 public void keyReleased(KeyEvent e)
241 Component c = getFrameAtMouse();
244 for (KeyListener kl : c.getKeyListeners())
255 * Returns true if the key event is overriden and actioned (or ignored) here,
256 * else returns false, indicating it should be delegated to the AlignFrame's
259 * We can't handle Cmd-Key combinations here, instead this is done by
260 * overriding key bindings.
262 * @see addKeyOverrides
267 protected boolean overrideKey(KeyEvent e, AlignFrame af)
269 boolean actioned = false;
270 int keyCode = e.getKeyCode();
273 case KeyEvent.VK_DOWN:
274 if (e.isAltDown() || !af.viewport.cursorMode)
277 * Key down (or Alt-key-down in cursor mode) - move selected sequences
279 ((AlignFrame) getTopFrame()).moveSelectedSequences(false);
280 ((AlignFrame) getBottomFrame()).moveSelectedSequences(false);
286 if (e.isAltDown() || !af.viewport.cursorMode)
289 * Key up (or Alt-key-up in cursor mode) - move selected sequences
291 ((AlignFrame) getTopFrame()).moveSelectedSequences(true);
292 ((AlignFrame) getBottomFrame()).moveSelectedSequences(true);
302 * Set key bindings (recommended for Swing over key accelerators).
304 private void addKeyBindings()
306 overrideDelegatedKeyBindings();
308 overrideImplementedKeyBindings();
312 * Override key bindings with alternative action methods implemented in this
315 protected void overrideImplementedKeyBindings()
320 overrideExpandViews();
321 overrideGatherViews();
325 * Replace Cmd-W close view action with our version.
327 protected void overrideCloseView()
329 AbstractAction action;
331 * Ctrl-W / Cmd-W - close view or window
333 KeyStroke key_cmdW = KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit
334 .getDefaultToolkit().getMenuShortcutKeyMask(), false);
335 action = new AbstractAction()
338 public void actionPerformed(ActionEvent e)
340 closeView_actionPerformed();
343 overrideKeyBinding(key_cmdW, action);
347 * Replace Cmd-T new view action with our version.
349 protected void overrideNewView()
352 * Ctrl-T / Cmd-T open new view
354 KeyStroke key_cmdT = KeyStroke.getKeyStroke(KeyEvent.VK_T, Toolkit
355 .getDefaultToolkit().getMenuShortcutKeyMask(), false);
356 AbstractAction action = new AbstractAction()
359 public void actionPerformed(ActionEvent e)
361 newView_actionPerformed();
364 overrideKeyBinding(key_cmdT, action);
368 * For now, delegates key events to the corresponding key accelerator for the
369 * AlignFrame that the mouse is in. Hopefully can be simplified in future if
370 * AlignFrame is changed to use key bindings rather than accelerators.
372 protected void overrideDelegatedKeyBindings()
374 if (getTopFrame() instanceof AlignFrame)
377 * Get all accelerator keys in the top frame (the bottom should be
378 * identical) and override each one.
380 for (Entry<KeyStroke, JMenuItem> acc : ((AlignFrame) getTopFrame())
381 .getAccelerators().entrySet())
383 overrideKeyBinding(acc);
389 * Overrides an AlignFrame key accelerator with our version which delegates to
390 * the action listener in whichever frame has the mouse (and does nothing if
395 private void overrideKeyBinding(Entry<KeyStroke, JMenuItem> acc)
397 final KeyStroke ks = acc.getKey();
398 InputMap inputMap = this.getInputMap(JComponent.WHEN_FOCUSED);
399 inputMap.put(ks, ks);
400 this.getActionMap().put(ks, new AbstractAction()
403 public void actionPerformed(ActionEvent e)
405 Component c = getFrameAtMouse();
406 if (c != null && c instanceof AlignFrame)
408 for (ActionListener a : ((AlignFrame) c).getAccelerators()
409 .get(ks).getActionListeners())
411 a.actionPerformed(null);
419 * Replace an accelerator key's action with the specified action.
423 protected void overrideKeyBinding(KeyStroke ks, AbstractAction action)
425 this.getActionMap().put(ks, action);
426 overrideMenuItem(ks, action);
430 * Create and link new views (with matching names) in both panes.
432 * Note this is _not_ multiple tabs, each hosting a split pane view, rather it
433 * is a single split pane with each split holding multiple tabs which are
436 * TODO implement instead with a tabbed holder in the SplitView, each tab
437 * holding a single JSplitPane. Would avoid a duplicated tab, at the cost of
438 * some additional coding.
440 protected void newView_actionPerformed()
442 AlignFrame topFrame = (AlignFrame) getTopFrame();
443 AlignFrame bottomFrame = (AlignFrame) getBottomFrame();
444 final boolean scaleProteinAsCdna = topFrame.viewport
445 .isScaleProteinAsCdna();
447 AlignmentPanel newTopPanel = topFrame.newView(null, true);
448 AlignmentPanel newBottomPanel = bottomFrame.newView(null, true);
451 * This currently (for the first new view only) leaves the top pane on tab 0
452 * but the bottom on tab 1. This results from 'setInitialTabVisible' echoing
453 * from the bottom back to the first frame. Next line is a fudge to work
454 * around this. TODO find a better way.
456 if (topFrame.getTabIndex() != bottomFrame.getTabIndex())
458 topFrame.setDisplayedView(newTopPanel);
461 newBottomPanel.av.viewName = newTopPanel.av.viewName;
462 newTopPanel.av.setCodingComplement(newBottomPanel.av);
465 * These lines can be removed once scaleProteinAsCdna is added to element
466 * Viewport in jalview.xsd, as Jalview2XML.copyAlignPanel will then take
469 newTopPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
470 newBottomPanel.av.setScaleProteinAsCdna(scaleProteinAsCdna);
473 * Line up id labels etc
477 final StructureSelectionManager ssm = StructureSelectionManager
478 .getStructureSelectionManager(Desktop.instance);
479 ssm.addCommandListener(newTopPanel.av);
480 ssm.addCommandListener(newBottomPanel.av);
484 * Close the currently selected view in both panes. If there is only one view,
485 * close this split frame.
487 protected void closeView_actionPerformed()
489 int viewCount = ((AlignFrame) getTopFrame()).getAlignPanels().size();
496 AlignmentPanel topPanel = ((AlignFrame) getTopFrame()).alignPanel;
497 AlignmentPanel bottomPanel = ((AlignFrame) getBottomFrame()).alignPanel;
499 ((AlignFrame) getTopFrame()).closeView(topPanel);
500 ((AlignFrame) getBottomFrame()).closeView(bottomPanel);
505 * Close child frames and this split frame.
509 ((AlignFrame) getTopFrame()).closeMenuItem_actionPerformed(true);
510 ((AlignFrame) getBottomFrame()).closeMenuItem_actionPerformed(true);
513 this.setClosed(true);
514 } catch (PropertyVetoException e)
521 * Replace AlignFrame 'expand views' action with SplitFrame version.
523 protected void overrideExpandViews()
525 KeyStroke key_X = KeyStroke.getKeyStroke(KeyEvent.VK_X, 0, false);
526 AbstractAction action = new AbstractAction()
529 public void actionPerformed(ActionEvent e)
531 expandViews_actionPerformed();
534 overrideMenuItem(key_X, action);
538 * Replace AlignFrame 'gather views' action with SplitFrame version.
540 protected void overrideGatherViews()
542 KeyStroke key_G = KeyStroke.getKeyStroke(KeyEvent.VK_G, 0, false);
543 AbstractAction action = new AbstractAction()
546 public void actionPerformed(ActionEvent e)
548 gatherViews_actionPerformed();
551 overrideMenuItem(key_G, action);
555 * Override the menu action associated with the keystroke in the child frames,
556 * replacing it with the given action.
561 private void overrideMenuItem(KeyStroke ks, AbstractAction action)
563 overrideMenuItem(ks, action, getTopFrame());
564 overrideMenuItem(ks, action, getBottomFrame());
568 * Override the menu action associated with the keystroke in one child frame,
569 * replacing it with the given action. Mwahahahaha.
575 private void overrideMenuItem(KeyStroke key, final AbstractAction action,
578 if (comp instanceof AlignFrame)
580 JMenuItem mi = ((AlignFrame) comp).getAccelerators().get(key);
583 for (ActionListener al : mi.getActionListeners())
585 mi.removeActionListener(al);
587 mi.addActionListener(new ActionListener()
590 public void actionPerformed(ActionEvent e)
592 action.actionPerformed(e);
600 * Expand any multiple views (which are always in pairs) into separate split
603 protected void expandViews_actionPerformed()
605 Desktop.instance.explodeViews(this);
609 * Gather any other SplitFrame views of this alignment back in as multiple
610 * (pairs of) views in this SplitFrame.
612 protected void gatherViews_actionPerformed()
614 Desktop.instance.gatherViews(this);
618 * Returns the alignment in the complementary frame to the one given.
621 public AlignmentI getComplement(Object alignFrame)
623 if (alignFrame == this.getTopFrame())
625 return ((AlignFrame) getBottomFrame()).viewport.getAlignment();
627 else if (alignFrame == this.getBottomFrame())
629 return ((AlignFrame) getTopFrame()).viewport.getAlignment();
635 * Returns the title of the complementary frame to the one given.
638 public String getComplementTitle(Object alignFrame)
640 if (alignFrame == this.getTopFrame())
642 return ((AlignFrame) getBottomFrame()).getTitle();
644 else if (alignFrame == this.getBottomFrame())
646 return ((AlignFrame) getTopFrame()).getTitle();
652 * Set the 'other half' to hidden / revealed.
655 public void setComplementVisible(Object alignFrame, boolean show)
658 * Hiding the AlignPanel suppresses unnecessary repaints
660 if (alignFrame == getTopFrame())
662 ((AlignFrame) getBottomFrame()).alignPanel.setVisible(show);
664 else if (alignFrame == getBottomFrame())
666 ((AlignFrame) getTopFrame()).alignPanel.setVisible(show);
668 super.setComplementVisible(alignFrame, show);
672 * Replace Cmd-F Find action with our version. This is necessary because the
673 * 'default' Finder searches in the first AlignFrame it finds. We need it to
674 * search in the half of the SplitFrame that has the mouse.
676 protected void overrideFind()
679 * Ctrl-F / Cmd-F open Finder dialog, 'focused' on the right alignment
681 KeyStroke key_cmdF = KeyStroke.getKeyStroke(KeyEvent.VK_F, Toolkit
682 .getDefaultToolkit().getMenuShortcutKeyMask(), false);
683 AbstractAction action = new AbstractAction()
686 public void actionPerformed(ActionEvent e)
688 Component c = getFrameAtMouse();
689 if (c != null && c instanceof AlignFrame)
691 AlignFrame af = (AlignFrame) c;
692 new Finder(af.viewport, af.alignPanel);
696 overrideKeyBinding(key_cmdF, action);