83366267eccbcdc84d4a95184145f89497ee581c
[jalview.git] / src / jalview / gui / AlignFrame.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.gui;
22
23 import java.awt.BorderLayout;
24 import java.awt.Color;
25 import java.awt.Component;
26 import java.awt.Rectangle;
27 import java.awt.Toolkit;
28 import java.awt.datatransfer.Clipboard;
29 import java.awt.datatransfer.DataFlavor;
30 import java.awt.datatransfer.StringSelection;
31 import java.awt.datatransfer.Transferable;
32 import java.awt.dnd.DnDConstants;
33 import java.awt.dnd.DropTargetDragEvent;
34 import java.awt.dnd.DropTargetDropEvent;
35 import java.awt.dnd.DropTargetEvent;
36 import java.awt.dnd.DropTargetListener;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.awt.event.FocusAdapter;
40 import java.awt.event.FocusEvent;
41 import java.awt.event.ItemEvent;
42 import java.awt.event.ItemListener;
43 import java.awt.event.KeyAdapter;
44 import java.awt.event.KeyEvent;
45 import java.awt.event.MouseEvent;
46 import java.awt.print.PageFormat;
47 import java.awt.print.PrinterJob;
48 import java.beans.PropertyChangeEvent;
49 import java.io.File;
50 import java.io.FileWriter;
51 import java.io.IOException;
52 import java.io.OutputStreamWriter;
53 import java.io.PrintWriter;
54 import java.net.URL;
55 import java.util.ArrayList;
56 import java.util.Arrays;
57 import java.util.Deque;
58 import java.util.Enumeration;
59 import java.util.Hashtable;
60 import java.util.List;
61 import java.util.Locale;
62 import java.util.Vector;
63
64 import javax.swing.AbstractButton;
65 import javax.swing.ButtonGroup;
66 import javax.swing.JCheckBoxMenuItem;
67 import javax.swing.JComponent;
68 import javax.swing.JEditorPane;
69 import javax.swing.JInternalFrame;
70 import javax.swing.JLabel;
71 import javax.swing.JLayeredPane;
72 import javax.swing.JMenu;
73 import javax.swing.JMenuItem;
74 import javax.swing.JPanel;
75 import javax.swing.JProgressBar;
76 import javax.swing.JRadioButtonMenuItem;
77 import javax.swing.JScrollPane;
78 import javax.swing.SwingUtilities;
79
80 import ext.vamsas.ServiceHandle;
81 import jalview.analysis.AlignmentSorter;
82 import jalview.analysis.AlignmentUtils;
83 import jalview.analysis.CrossRef;
84 import jalview.analysis.Dna;
85 import jalview.analysis.GeneticCodeI;
86 import jalview.analysis.ParseProperties;
87 import jalview.analysis.SequenceIdMatcher;
88 import jalview.api.AlignCalcWorkerI;
89 import jalview.api.AlignExportSettingsI;
90 import jalview.api.AlignViewControllerGuiI;
91 import jalview.api.AlignViewControllerI;
92 import jalview.api.AlignViewportI;
93 import jalview.api.AlignmentViewPanel;
94 import jalview.api.FeatureSettingsControllerI;
95 import jalview.api.FeatureSettingsModelI;
96 import jalview.api.SplitContainerI;
97 import jalview.api.ViewStyleI;
98 import jalview.api.analysis.SimilarityParamsI;
99 import jalview.bin.Cache;
100 import jalview.bin.Console;
101 import jalview.bin.Jalview;
102 import jalview.bin.groovy.JalviewObjectI;
103 import jalview.commands.CommandI;
104 import jalview.commands.EditCommand;
105 import jalview.commands.EditCommand.Action;
106 import jalview.commands.OrderCommand;
107 import jalview.commands.RemoveGapColCommand;
108 import jalview.commands.RemoveGapsCommand;
109 import jalview.commands.SlideSequencesCommand;
110 import jalview.commands.TrimRegionCommand;
111 import jalview.datamodel.AlignExportSettingsAdapter;
112 import jalview.datamodel.AlignedCodonFrame;
113 import jalview.datamodel.Alignment;
114 import jalview.datamodel.AlignmentAnnotation;
115 import jalview.datamodel.AlignmentExportData;
116 import jalview.datamodel.AlignmentI;
117 import jalview.datamodel.AlignmentOrder;
118 import jalview.datamodel.AlignmentView;
119 import jalview.datamodel.ColumnSelection;
120 import jalview.datamodel.ContactMatrixI;
121 import jalview.datamodel.HiddenColumns;
122 import jalview.datamodel.PDBEntry;
123 import jalview.datamodel.SeqCigar;
124 import jalview.datamodel.Sequence;
125 import jalview.datamodel.SequenceGroup;
126 import jalview.datamodel.SequenceI;
127 import jalview.gui.ColourMenuHelper.ColourChangeListener;
128 import jalview.gui.ViewSelectionMenu.ViewSetProvider;
129 import jalview.io.AlignmentProperties;
130 import jalview.io.AnnotationFile;
131 import jalview.io.BackupFiles;
132 import jalview.io.BioJsHTMLOutput;
133 import jalview.io.DataSourceType;
134 import jalview.io.FileFormat;
135 import jalview.io.FileFormatI;
136 import jalview.io.FileFormats;
137 import jalview.io.FileLoader;
138 import jalview.io.FileParse;
139 import jalview.io.FormatAdapter;
140 import jalview.io.HtmlSvgOutput;
141 import jalview.io.IdentifyFile;
142 import jalview.io.JPredFile;
143 import jalview.io.JalviewFileChooser;
144 import jalview.io.JalviewFileView;
145 import jalview.io.JnetAnnotationMaker;
146 import jalview.io.NewickFile;
147 import jalview.io.ScoreMatrixFile;
148 import jalview.io.TCoffeeScoreFile;
149 import jalview.io.exceptions.ImageOutputException;
150 import jalview.io.vcf.VCFLoader;
151 import jalview.jbgui.GAlignFrame;
152 import jalview.project.Jalview2XML;
153 import jalview.schemes.ColourSchemeI;
154 import jalview.schemes.ColourSchemes;
155 import jalview.schemes.ResidueColourScheme;
156 import jalview.schemes.TCoffeeColourScheme;
157 import jalview.util.Constants;
158 import jalview.util.HttpUtils;
159 import jalview.util.ImageMaker.TYPE;
160 import jalview.util.MessageManager;
161 import jalview.util.Platform;
162 import jalview.util.imagemaker.BitmapImageSizing;
163 import jalview.viewmodel.AlignmentViewport;
164 import jalview.viewmodel.ViewportRanges;
165 import jalview.workers.SecondaryStructureConsensusThread;
166 import jalview.ws.DBRefFetcher;
167 import jalview.ws.DBRefFetcher.FetchFinishedListenerI;
168 import jalview.ws.jws1.Discoverer;
169 import jalview.ws.jws2.Jws2Discoverer;
170 import jalview.ws.jws2.jabaws2.Jws2Instance;
171 import jalview.ws.seqfetcher.DbSourceProxy;
172
173 /**
174  * DOCUMENT ME!
175  * 
176  * @author $author$
177  * @version $Revision$
178  */
179 @SuppressWarnings("serial")
180 public class AlignFrame extends GAlignFrame implements DropTargetListener,
181         IProgressIndicator, AlignViewControllerGuiI, ColourChangeListener
182 {
183
184   public static final int DEFAULT_WIDTH = 700;
185
186   public static final int DEFAULT_HEIGHT = 500;
187
188   /*
189    * The currently displayed panel (selected tabbed view if more than one)
190    */
191   public AlignmentPanel alignPanel;
192
193   AlignViewport viewport;
194
195   public AlignViewControllerI avc;
196
197   List<AlignmentPanel> alignPanels = new ArrayList<>();
198
199   /**
200    * Last format used to load or save alignments in this window
201    */
202   FileFormatI currentFileFormat = null;
203
204   /**
205    * Current filename for this alignment
206    */
207   String fileName = null;
208
209   File fileObject;
210
211   /**
212    * Creates a new AlignFrame object with specific width and height.
213    * 
214    * @param al
215    * @param width
216    * @param height
217    */
218   public AlignFrame(AlignmentI al, int width, int height)
219   {
220     this(al, null, width, height);
221   }
222
223   /**
224    * Creates a new AlignFrame object with specific width, height and
225    * sequenceSetId
226    * 
227    * @param al
228    * @param width
229    * @param height
230    * @param sequenceSetId
231    */
232   public AlignFrame(AlignmentI al, int width, int height,
233           String sequenceSetId)
234   {
235     this(al, null, width, height, sequenceSetId);
236   }
237
238   /**
239    * Creates a new AlignFrame object with specific width, height and
240    * sequenceSetId
241    * 
242    * @param al
243    * @param width
244    * @param height
245    * @param sequenceSetId
246    * @param viewId
247    */
248   public AlignFrame(AlignmentI al, int width, int height,
249           String sequenceSetId, String viewId)
250   {
251     this(al, null, width, height, sequenceSetId, viewId);
252   }
253
254   /**
255    * new alignment window with hidden columns
256    * 
257    * @param al
258    *          AlignmentI
259    * @param hiddenColumns
260    *          ColumnSelection or null
261    * @param width
262    *          Width of alignment frame
263    * @param height
264    *          height of frame.
265    */
266   public AlignFrame(AlignmentI al, HiddenColumns hiddenColumns, int width,
267           int height)
268   {
269     this(al, hiddenColumns, width, height, null);
270   }
271
272   /**
273    * Create alignment frame for al with hiddenColumns, a specific width and
274    * height, and specific sequenceId
275    * 
276    * @param al
277    * @param hiddenColumns
278    * @param width
279    * @param height
280    * @param sequenceSetId
281    *          (may be null)
282    */
283   public AlignFrame(AlignmentI al, HiddenColumns hiddenColumns, int width,
284           int height, String sequenceSetId)
285   {
286     this(al, hiddenColumns, width, height, sequenceSetId, null);
287   }
288
289   /**
290    * Create alignment frame for al with hiddenColumns, a specific width and
291    * height, and specific sequenceId
292    * 
293    * @param al
294    * @param hiddenColumns
295    * @param width
296    * @param height
297    * @param sequenceSetId
298    *          (may be null)
299    * @param viewId
300    *          (may be null)
301    */
302   public AlignFrame(AlignmentI al, HiddenColumns hiddenColumns, int width,
303           int height, String sequenceSetId, String viewId)
304   {
305     setSize(width, height);
306
307     if (al.getDataset() == null)
308     {
309       al.setDataset(null);
310     }
311
312     viewport = new AlignViewport(al, hiddenColumns, sequenceSetId, viewId);
313
314     alignPanel = new AlignmentPanel(this, viewport);
315
316     addAlignmentPanel(alignPanel, true);
317     init();
318   }
319
320   public AlignFrame(AlignmentI al, SequenceI[] hiddenSeqs,
321           HiddenColumns hiddenColumns, int width, int height)
322   {
323     setSize(width, height);
324
325     if (al.getDataset() == null)
326     {
327       al.setDataset(null);
328     }
329
330     viewport = new AlignViewport(al, hiddenColumns);
331
332     if (hiddenSeqs != null && hiddenSeqs.length > 0)
333     {
334       viewport.hideSequence(hiddenSeqs);
335     }
336     alignPanel = new AlignmentPanel(this, viewport);
337     addAlignmentPanel(alignPanel, true);
338     init();
339   }
340
341   /**
342    * Make a new AlignFrame from existing alignmentPanels
343    * 
344    * @param ap
345    *          AlignmentPanel
346    * @param av
347    *          AlignViewport
348    */
349   public AlignFrame(AlignmentPanel ap)
350   {
351     viewport = ap.av;
352     alignPanel = ap;
353     addAlignmentPanel(ap, false);
354     init();
355   }
356
357   /**
358    * initalise the alignframe from the underlying viewport data and the
359    * configurations
360    */
361   void init()
362   {
363     setFrameIcon(null);
364
365     // setBackground(Color.white); // BH 2019
366
367     if (!Jalview.isHeadlessMode())
368     {
369       progressBar = new ProgressBar(this.statusPanel, this.statusBar);
370     }
371
372     avc = new jalview.controller.AlignViewController(this, viewport,
373             alignPanel);
374     if (viewport.getAlignmentConservationAnnotation() == null)
375     {
376       // BLOSUM62Colour.setEnabled(false);
377       conservationMenuItem.setEnabled(false);
378       modifyConservation.setEnabled(false);
379       // PIDColour.setEnabled(false);
380       // abovePIDThreshold.setEnabled(false);
381       // modifyPID.setEnabled(false);
382     }
383
384     String sortby = Cache.getDefault("SORT_ALIGNMENT", "No sort");
385
386     if (sortby.equals("Id"))
387     {
388       sortIDMenuItem_actionPerformed(null);
389     }
390     else if (sortby.equals("Pairwise Identity"))
391     {
392       sortPairwiseMenuItem_actionPerformed(null);
393     }
394
395     this.alignPanel.av
396             .setShowAutocalculatedAbove(isShowAutoCalculatedAbove());
397
398     setMenusFromViewport(viewport);
399     buildSortByAnnotationScoresMenu();
400     calculateTree.addActionListener(new ActionListener()
401     {
402
403       @Override
404       public void actionPerformed(ActionEvent e)
405       {
406         openTreePcaDialog();
407       }
408     });
409     buildColourMenu();
410
411     if (Desktop.desktop != null)
412     {
413       this.setDropTarget(new java.awt.dnd.DropTarget(this, this));
414       if (!Platform.isJS())
415       {
416         addServiceListeners();
417       }
418       setGUINucleotide();
419     }
420
421     if (viewport.getWrapAlignment())
422     {
423       wrapMenuItem_actionPerformed(null);
424     }
425
426     if (Cache.getDefault("SHOW_OVERVIEW", false))
427     {
428       this.overviewMenuItem_actionPerformed(null);
429     }
430
431     addKeyListener();
432
433     final List<AlignmentViewPanel> selviews = new ArrayList<>();
434     final List<AlignmentPanel> origview = new ArrayList<>();
435     final String menuLabel = MessageManager
436             .getString("label.copy_format_from");
437     ViewSelectionMenu vsel = new ViewSelectionMenu(menuLabel,
438             new ViewSetProvider()
439             {
440
441               @Override
442               public AlignmentPanel[] getAllAlignmentPanels()
443               {
444                 origview.clear();
445                 origview.add(alignPanel);
446                 // make an array of all alignment panels except for this one
447                 List<AlignmentPanel> aps = new ArrayList<>(
448                         Arrays.asList(Desktop.getAlignmentPanels(null)));
449                 aps.remove(AlignFrame.this.alignPanel);
450                 return aps.toArray(new AlignmentPanel[aps.size()]);
451               }
452             }, selviews, new ItemListener()
453             {
454
455               @Override
456               public void itemStateChanged(ItemEvent e)
457               {
458                 if (origview.size() > 0)
459                 {
460                   final AlignmentPanel ap = origview.get(0);
461
462                   /*
463                    * Copy the ViewStyle of the selected panel to 'this one'.
464                    * Don't change value of 'scaleProteinAsCdna' unless copying
465                    * from a SplitFrame.
466                    */
467                   ViewStyleI vs = selviews.get(0).getAlignViewport()
468                           .getViewStyle();
469                   boolean fromSplitFrame = selviews.get(0)
470                           .getAlignViewport().getCodingComplement() != null;
471                   if (!fromSplitFrame)
472                   {
473                     vs.setScaleProteinAsCdna(ap.getAlignViewport()
474                             .getViewStyle().isScaleProteinAsCdna());
475                   }
476                   ap.getAlignViewport().setViewStyle(vs);
477
478                   /*
479                    * Also rescale ViewStyle of SplitFrame complement if there is
480                    * one _and_ it is set to 'scaledProteinAsCdna'; we don't copy
481                    * the whole ViewStyle (allow cDNA protein to have different
482                    * fonts)
483                    */
484                   AlignViewportI complement = ap.getAlignViewport()
485                           .getCodingComplement();
486                   if (complement != null && vs.isScaleProteinAsCdna())
487                   {
488                     AlignFrame af = Desktop.getAlignFrameFor(complement);
489                     ((SplitFrame) af.getSplitViewContainer())
490                             .adjustLayout();
491                     af.setMenusForViewport();
492                   }
493
494                   ap.updateLayout();
495                   ap.setSelected(true);
496                   ap.alignFrame.setMenusForViewport();
497
498                 }
499               }
500             });
501     if (Cache.getDefault("VERSION", "DEVELOPMENT").toLowerCase(Locale.ROOT)
502             .indexOf("devel") > -1
503             || Cache.getDefault("VERSION", "DEVELOPMENT")
504                     .toLowerCase(Locale.ROOT).indexOf("test") > -1)
505     {
506       formatMenu.add(vsel);
507     }
508     addFocusListener(new FocusAdapter()
509     {
510       @Override
511       public void focusGained(FocusEvent e)
512       {
513         Jalview.getInstance().setCurrentAlignFrame(AlignFrame.this);
514       }
515     });
516
517   }
518
519   /**
520    * Change the filename and format for the alignment, and enable the 'reload'
521    * button functionality.
522    * 
523    * @param file
524    *          valid filename
525    * @param format
526    *          format of file
527    */
528   public void setFileName(String file, FileFormatI format)
529   {
530     fileName = file;
531     setFileFormat(format);
532     reload.setEnabled(true);
533   }
534
535   /**
536    * JavaScript will have this, maybe others. More dependable than a file name
537    * and maintains a reference to the actual bytes loaded.
538    * 
539    * @param file
540    */
541   public void setFileObject(File file)
542   {
543     this.fileObject = file;
544   }
545
546   /**
547    * Add a KeyListener with handlers for various KeyPressed and KeyReleased
548    * events
549    */
550   void addKeyListener()
551   {
552     addKeyListener(new KeyAdapter()
553     {
554       @Override
555       public void keyPressed(KeyEvent evt)
556       {
557         if (viewport.cursorMode
558                 && ((evt.getKeyCode() >= KeyEvent.VK_0
559                         && evt.getKeyCode() <= KeyEvent.VK_9)
560                         || (evt.getKeyCode() >= KeyEvent.VK_NUMPAD0
561                                 && evt.getKeyCode() <= KeyEvent.VK_NUMPAD9))
562                 && Character.isDigit(evt.getKeyChar()))
563         {
564           alignPanel.getSeqPanel().numberPressed(evt.getKeyChar());
565         }
566
567         switch (evt.getKeyCode())
568         {
569
570         case 27: // escape key
571           deselectAllSequenceMenuItem_actionPerformed(null);
572
573           break;
574
575         case KeyEvent.VK_DOWN:
576           if (evt.isAltDown() || !viewport.cursorMode)
577           {
578             moveSelectedSequences(false);
579           }
580           if (viewport.cursorMode)
581           {
582             alignPanel.getSeqPanel().moveCursor(0, 1, evt.isShiftDown());
583           }
584           break;
585
586         case KeyEvent.VK_UP:
587           if (evt.isAltDown() || !viewport.cursorMode)
588           {
589             moveSelectedSequences(true);
590           }
591           if (viewport.cursorMode)
592           {
593             alignPanel.getSeqPanel().moveCursor(0, -1, evt.isShiftDown());
594           }
595
596           break;
597
598         case KeyEvent.VK_LEFT:
599           if (evt.isAltDown() || !viewport.cursorMode)
600           {
601             slideSequences(false,
602                     alignPanel.getSeqPanel().getKeyboardNo1());
603           }
604           else
605           {
606             alignPanel.getSeqPanel().moveCursor(-1, 0, evt.isShiftDown());
607           }
608
609           break;
610
611         case KeyEvent.VK_RIGHT:
612           if (evt.isAltDown() || !viewport.cursorMode)
613           {
614             slideSequences(true, alignPanel.getSeqPanel().getKeyboardNo1());
615           }
616           else
617           {
618             alignPanel.getSeqPanel().moveCursor(1, 0, evt.isShiftDown());
619           }
620           break;
621
622         case KeyEvent.VK_SPACE:
623           if (viewport.cursorMode)
624           {
625             alignPanel.getSeqPanel().insertGapAtCursor(evt.isControlDown()
626                     || evt.isShiftDown() || evt.isAltDown());
627           }
628           break;
629
630         // case KeyEvent.VK_A:
631         // if (viewport.cursorMode)
632         // {
633         // alignPanel.seqPanel.insertNucAtCursor(false,"A");
634         // //jalview.bin.Console.outPrintln("A");
635         // }
636         // break;
637         /*
638          * case KeyEvent.VK_CLOSE_BRACKET: if (viewport.cursorMode) {
639          * jalview.bin.Console.outPrintln("closing bracket"); } break;
640          */
641         case KeyEvent.VK_DELETE:
642         case KeyEvent.VK_BACK_SPACE:
643           if (!viewport.cursorMode)
644           {
645             cut_actionPerformed();
646           }
647           else
648           {
649             alignPanel.getSeqPanel().deleteGapAtCursor(evt.isControlDown()
650                     || evt.isShiftDown() || evt.isAltDown());
651           }
652
653           break;
654
655         case KeyEvent.VK_S:
656           if (viewport.cursorMode)
657           {
658             alignPanel.getSeqPanel().setCursorRow();
659           }
660           break;
661         case KeyEvent.VK_C:
662           if (viewport.cursorMode && !evt.isControlDown())
663           {
664             alignPanel.getSeqPanel().setCursorColumn();
665           }
666           break;
667         case KeyEvent.VK_P:
668           if (viewport.cursorMode)
669           {
670             alignPanel.getSeqPanel().setCursorPosition();
671           }
672           break;
673
674         case KeyEvent.VK_ENTER:
675         case KeyEvent.VK_COMMA:
676           if (viewport.cursorMode)
677           {
678             alignPanel.getSeqPanel().setCursorRowAndColumn();
679           }
680           break;
681
682         case KeyEvent.VK_Q:
683           if (viewport.cursorMode)
684           {
685             alignPanel.getSeqPanel().setSelectionAreaAtCursor(true);
686           }
687           break;
688         case KeyEvent.VK_M:
689           if (viewport.cursorMode)
690           {
691             alignPanel.getSeqPanel().setSelectionAreaAtCursor(false);
692           }
693           break;
694
695         case KeyEvent.VK_F2:
696           viewport.cursorMode = !viewport.cursorMode;
697           setStatus(MessageManager
698                   .formatMessage("label.keyboard_editing_mode", new String[]
699                   { (viewport.cursorMode ? "on" : "off") }));
700           if (viewport.cursorMode)
701           {
702             ViewportRanges ranges = viewport.getRanges();
703             alignPanel.getSeqPanel().seqCanvas.cursorX = ranges
704                     .getStartRes();
705             alignPanel.getSeqPanel().seqCanvas.cursorY = ranges
706                     .getStartSeq();
707           }
708           alignPanel.getSeqPanel().seqCanvas.repaint();
709           break;
710
711         case KeyEvent.VK_F1:
712           try
713           {
714             Help.showHelpWindow();
715           } catch (Exception ex)
716           {
717             ex.printStackTrace();
718           }
719           break;
720         case KeyEvent.VK_H:
721         {
722           boolean toggleSeqs = !evt.isControlDown();
723           boolean toggleCols = !evt.isShiftDown();
724           toggleHiddenRegions(toggleSeqs, toggleCols);
725           break;
726         }
727         case KeyEvent.VK_B:
728         {
729           boolean toggleSel = evt.isControlDown() || evt.isMetaDown();
730           boolean modifyExisting = true; // always modify, don't clear
731                                          // evt.isShiftDown();
732           boolean invertHighlighted = evt.isAltDown();
733           avc.markHighlightedColumns(invertHighlighted, modifyExisting,
734                   toggleSel);
735           break;
736         }
737         case KeyEvent.VK_PAGE_UP:
738           viewport.getRanges().pageUp();
739           break;
740         case KeyEvent.VK_PAGE_DOWN:
741           viewport.getRanges().pageDown();
742           break;
743         }
744       }
745
746       @Override
747       public void keyReleased(KeyEvent evt)
748       {
749         switch (evt.getKeyCode())
750         {
751         case KeyEvent.VK_LEFT:
752           if (evt.isAltDown() || !viewport.cursorMode)
753           {
754             viewport.firePropertyChange("alignment", null,
755                     viewport.getAlignment().getSequences());
756           }
757           break;
758
759         case KeyEvent.VK_RIGHT:
760           if (evt.isAltDown() || !viewport.cursorMode)
761           {
762             viewport.firePropertyChange("alignment", null,
763                     viewport.getAlignment().getSequences());
764           }
765           break;
766         }
767       }
768     });
769   }
770
771   public void addAlignmentPanel(final AlignmentPanel ap, boolean newPanel)
772   {
773     ap.alignFrame = this;
774     avc = new jalview.controller.AlignViewController(this, viewport,
775             alignPanel);
776
777     alignPanels.add(ap);
778
779     PaintRefresher.Register(ap, ap.av.getSequenceSetId());
780
781     int aSize = alignPanels.size();
782
783     tabbedPane.setVisible(aSize > 1 || ap.av.getViewName() != null);
784
785     if (aSize == 1 && ap.av.getViewName() == null)
786     {
787       this.getContentPane().add(ap, BorderLayout.CENTER);
788     }
789     else
790     {
791       if (aSize == 2)
792       {
793         setInitialTabVisible();
794       }
795
796       expandViews.setEnabled(true);
797       gatherViews.setEnabled(true);
798       tabbedPane.addTab(ap.av.getViewName(), ap);
799
800       ap.setVisible(false);
801     }
802
803     if (newPanel)
804     {
805       if (ap.av.isPadGaps())
806       {
807         ap.av.getAlignment().padGaps();
808       }
809       ap.av.updateConservation(ap);
810       ap.av.updateConsensus(ap);
811       ap.av.updateSecondaryStructureConsensus(ap);
812       ap.av.updateStrucConsensus(ap);
813     }
814   }
815
816   public void setInitialTabVisible()
817   {
818     expandViews.setEnabled(true);
819     gatherViews.setEnabled(true);
820     tabbedPane.setVisible(true);
821     AlignmentPanel first = alignPanels.get(0);
822     tabbedPane.addTab(first.av.getViewName(), first);
823     this.getContentPane().add(tabbedPane, BorderLayout.CENTER);
824   }
825
826   public AlignViewport getViewport()
827   {
828     return viewport;
829   }
830
831   /* Set up intrinsic listeners for dynamically generated GUI bits. */
832   private void addServiceListeners()
833   {
834     final java.beans.PropertyChangeListener thisListener;
835     Desktop.instance.addJalviewPropertyChangeListener("services",
836             thisListener = new java.beans.PropertyChangeListener()
837             {
838               @Override
839               public void propertyChange(PropertyChangeEvent evt)
840               {
841                 // // jalview.bin.Console.outPrintln("Discoverer property
842                 // change.");
843                 // if (evt.getPropertyName().equals("services"))
844                 {
845                   SwingUtilities.invokeLater(new Runnable()
846                   {
847
848                     @Override
849                     public void run()
850                     {
851                       jalview.bin.Console.errPrintln(
852                               "Rebuild WS Menu for service change");
853                       BuildWebServiceMenu();
854                     }
855
856                   });
857                 }
858               }
859             });
860     addInternalFrameListener(new javax.swing.event.InternalFrameAdapter()
861     {
862       @Override
863       public void internalFrameClosed(
864               javax.swing.event.InternalFrameEvent evt)
865       {
866         // jalview.bin.Console.outPrintln("deregistering discoverer listener");
867         Desktop.instance.removeJalviewPropertyChangeListener("services",
868                 thisListener);
869         closeMenuItem_actionPerformed(true);
870       }
871     });
872     // Finally, build the menu once to get current service state
873     new Thread(new Runnable()
874     {
875       @Override
876       public void run()
877       {
878         BuildWebServiceMenu();
879       }
880     }).start();
881   }
882
883   /**
884    * Configure menu items that vary according to whether the alignment is
885    * nucleotide or protein
886    */
887   public void setGUINucleotide()
888   {
889     AlignmentI al = getViewport().getAlignment();
890     boolean nucleotide = al.isNucleotide();
891
892     loadVcf.setVisible(nucleotide);
893     showTranslation.setVisible(nucleotide);
894     showReverse.setVisible(nucleotide);
895     showReverseComplement.setVisible(nucleotide);
896     conservationMenuItem.setEnabled(!nucleotide);
897     modifyConservation
898             .setEnabled(!nucleotide && conservationMenuItem.isSelected());
899     showGroupConservation.setEnabled(!nucleotide);
900
901     showComplementMenuItem
902             .setText(nucleotide ? MessageManager.getString("label.protein")
903                     : MessageManager.getString("label.nucleotide"));
904   }
905
906   /**
907    * set up menus for the current viewport. This may be called after any
908    * operation that affects the data in the current view (selection changed,
909    * etc) to update the menus to reflect the new state.
910    */
911   @Override
912   public void setMenusForViewport()
913   {
914     setMenusFromViewport(viewport);
915   }
916
917   /**
918    * Need to call this method when tabs are selected for multiple views, or when
919    * loading from Jalview2XML.java
920    * 
921    * @param av
922    *          AlignViewport
923    */
924   public void setMenusFromViewport(AlignViewport av)
925   {
926     padGapsMenuitem.setSelected(av.isPadGaps());
927     colourTextMenuItem.setSelected(av.isShowColourText());
928     abovePIDThreshold.setSelected(av.getAbovePIDThreshold());
929     modifyPID.setEnabled(abovePIDThreshold.isSelected());
930     conservationMenuItem.setSelected(av.getConservationSelected());
931     modifyConservation.setEnabled(conservationMenuItem.isSelected());
932     seqLimits.setSelected(av.getShowJVSuffix());
933     idRightAlign.setSelected(av.isRightAlignIds());
934     centreColumnLabelsMenuItem.setState(av.isCentreColumnLabels());
935     renderGapsMenuItem.setSelected(av.isRenderGaps());
936     wrapMenuItem.setSelected(av.getWrapAlignment());
937     scaleAbove.setVisible(av.getWrapAlignment());
938     scaleLeft.setVisible(av.getWrapAlignment());
939     scaleRight.setVisible(av.getWrapAlignment());
940     annotationPanelMenuItem.setState(av.isShowAnnotation());
941     /*
942      * Show/hide annotations only enabled if annotation panel is shown
943      */
944     showAllSeqAnnotations.setEnabled(annotationPanelMenuItem.getState());
945     hideAllSeqAnnotations.setEnabled(annotationPanelMenuItem.getState());
946     showAllAlAnnotations.setEnabled(annotationPanelMenuItem.getState());
947     hideAllAlAnnotations.setEnabled(annotationPanelMenuItem.getState());
948     viewBoxesMenuItem.setSelected(av.getShowBoxes());
949     viewTextMenuItem.setSelected(av.getShowText());
950     showNonconservedMenuItem.setSelected(av.getShowUnconserved());
951     showGroupConsensus.setSelected(av.isShowGroupConsensus());
952     showGroupConservation.setSelected(av.isShowGroupConservation());
953     showConsensusHistogram.setSelected(av.isShowConsensusHistogram());
954     showSequenceLogo.setSelected(av.isShowSequenceLogo());
955     normaliseSequenceLogo.setSelected(av.isNormaliseSequenceLogo());
956
957     ColourMenuHelper.setColourSelected(colourMenu,
958             av.getGlobalColourScheme());
959
960     showSeqFeatures.setSelected(av.isShowSequenceFeatures());
961     hiddenMarkers.setState(av.getShowHiddenMarkers());
962     applyToAllGroups.setState(av.getColourAppliesToAllGroups());
963     showNpFeatsMenuitem.setSelected(av.isShowNPFeats());
964     showDbRefsMenuitem.setSelected(av.isShowDBRefs());
965     autoCalculate.setSelected(av.autoCalculateConsensus);
966     sortByTree.setSelected(av.sortByTree);
967     listenToViewSelections.setSelected(av.followSelection);
968
969     showProducts.setEnabled(canShowProducts());
970     setGroovyEnabled(Desktop.getGroovyConsole() != null);
971
972     updateEditMenuBar();
973   }
974
975   /**
976    * Set the enabled state of the 'Run Groovy' option in the Calculate menu
977    * 
978    * @param b
979    */
980   public void setGroovyEnabled(boolean b)
981   {
982     runGroovy.setEnabled(b);
983   }
984
985   private IProgressIndicator progressBar;
986
987   /*
988    * (non-Javadoc)
989    * 
990    * @see jalview.gui.IProgressIndicator#setProgressBar(java.lang.String, long)
991    */
992   @Override
993   public void setProgressBar(String message, long id)
994   {
995     if (!Platform.isHeadless() && progressBar != null)
996       progressBar.setProgressBar(message, id);
997   }
998
999   @Override
1000   public JProgressBar getProgressBar(long id)
1001   {
1002     if (progressBar != null)
1003       return progressBar.getProgressBar(id);
1004     return null;
1005   }
1006
1007   @Override
1008   public void registerHandler(final long id,
1009           final IProgressIndicatorHandler handler)
1010   {
1011     if (progressBar != null)
1012       progressBar.registerHandler(id, handler);
1013   }
1014
1015   /**
1016    * 
1017    * @return true if any progress bars are still active
1018    */
1019   @Override
1020   public boolean operationInProgress()
1021   {
1022     return progressBar == null ? false : progressBar.operationInProgress();
1023   }
1024
1025   /**
1026    * Sets the text of the status bar. Note that setting a null or empty value
1027    * will cause the status bar to be hidden, with possibly undesirable flicker
1028    * of the screen layout.
1029    */
1030   @Override
1031   public void setStatus(String text)
1032   {
1033     statusBar.setText(text == null || text.isEmpty() ? " " : text);
1034   }
1035
1036   /*
1037    * Added so Castor Mapping file can obtain Jalview Version
1038    */
1039   public String getVersion()
1040   {
1041     return Cache.getProperty("VERSION");
1042   }
1043
1044   public FeatureRenderer getFeatureRenderer()
1045   {
1046     return alignPanel.getSeqPanel().seqCanvas.getFeatureRenderer();
1047   }
1048
1049   @Override
1050   public void fetchSequence_actionPerformed()
1051   {
1052     new SequenceFetcher(this);
1053   }
1054
1055   @Override
1056   public void addFromFile_actionPerformed(ActionEvent e)
1057   {
1058     Desktop.instance.inputLocalFileMenuItem_actionPerformed(viewport);
1059   }
1060
1061   @Override
1062   public void reload_actionPerformed(ActionEvent e)
1063   {
1064     if (fileName != null)
1065     {
1066       // TODO: JAL-1108 - ensure all associated frames are closed regardless of
1067       // originating file's format
1068       // TODO: work out how to recover feature settings for correct view(s) when
1069       // file is reloaded.
1070       if (FileFormat.Jalview.equals(currentFileFormat))
1071       {
1072         JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1073         for (int i = 0; i < frames.length; i++)
1074         {
1075           if (frames[i] instanceof AlignFrame && frames[i] != this
1076                   && ((AlignFrame) frames[i]).fileName != null
1077                   && ((AlignFrame) frames[i]).fileName.equals(fileName))
1078           {
1079             try
1080             {
1081               frames[i].setSelected(true);
1082               Desktop.instance.closeAssociatedWindows();
1083             } catch (java.beans.PropertyVetoException ex)
1084             {
1085             }
1086           }
1087
1088         }
1089         Desktop.instance.closeAssociatedWindows();
1090
1091         FileLoader loader = new FileLoader();
1092         DataSourceType protocol = HttpUtils.startsWithHttpOrHttps(fileName)
1093                 ? DataSourceType.URL
1094                 : DataSourceType.FILE;
1095         loader.LoadFile(viewport, fileName, protocol, currentFileFormat);
1096       }
1097       else
1098       {
1099         Rectangle bounds = this.getBounds();
1100
1101         FileLoader loader = new FileLoader();
1102
1103         AlignFrame newframe = null;
1104
1105         if (fileObject == null)
1106         {
1107
1108           DataSourceType protocol = HttpUtils.startsWithHttpOrHttps(
1109                   fileName) ? DataSourceType.URL : DataSourceType.FILE;
1110           newframe = loader.LoadFileWaitTillLoaded(fileName, protocol,
1111                   currentFileFormat);
1112         }
1113         else
1114         {
1115           newframe = loader.LoadFileWaitTillLoaded(fileObject,
1116                   DataSourceType.FILE, currentFileFormat);
1117         }
1118
1119         newframe.setBounds(bounds);
1120         if (featureSettings != null && featureSettings.isShowing())
1121         {
1122           final Rectangle fspos = featureSettings.frame.getBounds();
1123           // TODO: need a 'show feature settings' function that takes bounds -
1124           // need to refactor Desktop.addFrame
1125           newframe.featureSettings_actionPerformed(null);
1126           final FeatureSettings nfs = newframe.featureSettings;
1127           SwingUtilities.invokeLater(new Runnable()
1128           {
1129             @Override
1130             public void run()
1131             {
1132               nfs.frame.setBounds(fspos);
1133             }
1134           });
1135           this.featureSettings.close();
1136           this.featureSettings = null;
1137         }
1138         this.closeMenuItem_actionPerformed(true);
1139       }
1140     }
1141   }
1142
1143   @Override
1144   public void addFromText_actionPerformed(ActionEvent e)
1145   {
1146     Desktop.instance
1147             .inputTextboxMenuItem_actionPerformed(viewport.getAlignPanel());
1148   }
1149
1150   @Override
1151   public void addFromURL_actionPerformed(ActionEvent e)
1152   {
1153     Desktop.instance.inputURLMenuItem_actionPerformed(viewport);
1154   }
1155
1156   @Override
1157   public void save_actionPerformed(ActionEvent e)
1158   {
1159     if (fileName == null || (currentFileFormat == null)
1160             || HttpUtils.startsWithHttpOrHttps(fileName))
1161     {
1162       saveAs_actionPerformed();
1163     }
1164     else
1165     {
1166       saveAlignment(fileName, currentFileFormat);
1167     }
1168   }
1169
1170   /**
1171    * Saves the alignment to a file with a name chosen by the user, if necessary
1172    * warning if a file would be overwritten
1173    */
1174   @Override
1175   public void saveAs_actionPerformed()
1176   {
1177     String format = currentFileFormat == null ? null
1178             : currentFileFormat.getName();
1179     JalviewFileChooser chooser = JalviewFileChooser
1180             .forWrite(Cache.getProperty("LAST_DIRECTORY"), format);
1181
1182     chooser.setFileView(new JalviewFileView());
1183     chooser.setDialogTitle(
1184             MessageManager.getString("label.save_alignment_to_file"));
1185     chooser.setToolTipText(MessageManager.getString("action.save"));
1186
1187     int value = chooser.showSaveDialog(this);
1188
1189     if (value != JalviewFileChooser.APPROVE_OPTION)
1190     {
1191       return;
1192     }
1193     currentFileFormat = chooser.getSelectedFormat();
1194     // todo is this (2005) test now obsolete - value is never null?
1195     while (currentFileFormat == null)
1196     {
1197       JvOptionPane.showInternalMessageDialog(Desktop.desktop,
1198               MessageManager
1199                       .getString("label.select_file_format_before_saving"),
1200               MessageManager.getString("label.file_format_not_specified"),
1201               JvOptionPane.WARNING_MESSAGE);
1202       currentFileFormat = chooser.getSelectedFormat();
1203       value = chooser.showSaveDialog(this);
1204       if (value != JalviewFileChooser.APPROVE_OPTION)
1205       {
1206         return;
1207       }
1208     }
1209
1210     fileName = chooser.getSelectedFile().getPath();
1211
1212     Cache.setProperty("DEFAULT_FILE_FORMAT", currentFileFormat.getName());
1213     Cache.setProperty("LAST_DIRECTORY", fileName);
1214     saveAlignment(fileName, currentFileFormat);
1215   }
1216
1217   boolean lastSaveSuccessful = false;
1218
1219   FileFormatI lastFormatSaved;
1220
1221   String lastFilenameSaved;
1222
1223   /**
1224    * Raise a dialog or status message for the last call to saveAlignment.
1225    *
1226    * @return true if last call to saveAlignment(file, format) was successful.
1227    */
1228   public boolean isSaveAlignmentSuccessful()
1229   {
1230
1231     if (!lastSaveSuccessful)
1232     {
1233       if (!Platform.isHeadless())
1234       {
1235         JvOptionPane.showInternalMessageDialog(this, MessageManager
1236                 .formatMessage("label.couldnt_save_file", new Object[]
1237                 { lastFilenameSaved }),
1238                 MessageManager.getString("label.error_saving_file"),
1239                 JvOptionPane.WARNING_MESSAGE);
1240       }
1241       else
1242       {
1243         Console.error(MessageManager
1244                 .formatMessage("label.couldnt_save_file", new Object[]
1245                 { lastFilenameSaved }));
1246       }
1247     }
1248     else
1249     {
1250
1251       setStatus(MessageManager.formatMessage(
1252               "label.successfully_saved_to_file_in_format", new Object[]
1253               { lastFilenameSaved, lastFormatSaved }));
1254
1255     }
1256     return lastSaveSuccessful;
1257   }
1258
1259   /**
1260    * Saves the alignment to the specified file path, in the specified format,
1261    * which may be an alignment format, or Jalview project format. If the
1262    * alignment has hidden regions, or the format is one capable of including
1263    * non-sequence data (features, annotations, groups), then the user may be
1264    * prompted to specify what to include in the output.
1265    * 
1266    * @param file
1267    * @param format
1268    */
1269   public void saveAlignment(String file, FileFormatI format)
1270   {
1271     saveAlignment(file, format, false, false);
1272   }
1273
1274   public void saveAlignment(String file, FileFormatI format, boolean stdout,
1275           boolean forceBackup)
1276   {
1277     lastSaveSuccessful = true;
1278     if (!stdout)
1279     {
1280       lastFilenameSaved = file;
1281     }
1282     lastFormatSaved = format;
1283
1284     if (FileFormat.Jalview.equals(format))
1285     {
1286       String shortName = title;
1287       if (shortName.indexOf(File.separatorChar) > -1)
1288       {
1289         shortName = shortName
1290                 .substring(shortName.lastIndexOf(File.separatorChar) + 1);
1291       }
1292       // TODO deal with stdout=true
1293       lastSaveSuccessful = new Jalview2XML().saveAlignment(this, file,
1294               shortName);
1295
1296       Console.debug("lastSaveSuccessful=" + lastSaveSuccessful);
1297       if (lastSaveSuccessful)
1298       {
1299         this.getViewport().setSavedUpToDate(true);
1300       }
1301
1302       statusBar.setText(MessageManager.formatMessage(
1303               "label.successfully_saved_to_file_in_format", new Object[]
1304               { file, format }));
1305
1306       return;
1307     }
1308
1309     AlignExportSettingsI options = new AlignExportSettingsAdapter(false);
1310     Runnable cancelAction = () -> {
1311       lastSaveSuccessful = false;
1312     };
1313     Runnable outputAction = () -> {
1314       // todo defer this to inside formatSequences (or later)
1315       AlignmentExportData exportData = viewport.getAlignExportData(options);
1316       String output = new FormatAdapter(alignPanel, options)
1317               .formatSequences(format, exportData.getAlignment(),
1318                       exportData.getOmitHidden(),
1319                       exportData.getStartEndPostions(),
1320                       viewport.getAlignment().getHiddenColumns());
1321       if (output == null)
1322       {
1323         lastSaveSuccessful = false;
1324       }
1325       else
1326       {
1327         // create backupfiles object and get new temp filename destination
1328         boolean doBackup = forceBackup
1329                 || (BackupFiles.getEnabled() && !stdout);
1330         BackupFiles backupfiles = null;
1331         if (doBackup)
1332         {
1333           Console.trace("ALIGNFRAME making backupfiles object for " + file);
1334           backupfiles = new BackupFiles(file);
1335         }
1336         try
1337         {
1338           String tempFilePath = doBackup ? backupfiles.getTempFilePath()
1339                   : file;
1340           Console.trace("ALIGNFRAME setting PrintWriter");
1341           PrintWriter out = stdout
1342                   ? new PrintWriter(new OutputStreamWriter(System.out))
1343                   : new PrintWriter(new FileWriter(tempFilePath));
1344
1345           if (backupfiles != null)
1346           {
1347             Console.trace("ALIGNFRAME about to write to temp file "
1348                     + backupfiles.getTempFilePath());
1349           }
1350
1351           out.print(output);
1352           out.flush();
1353           if (!stdout)
1354           {
1355             Console.trace("ALIGNFRAME about to close file");
1356             out.close();
1357             Console.trace("ALIGNFRAME closed file");
1358           }
1359           AlignFrame.this.setTitle(stdout ? "STDOUT" : file);
1360           if (stdout)
1361           {
1362             statusBar.setText(MessageManager.formatMessage(
1363                     "label.successfully_printed_to_stdout_in_format",
1364                     new Object[]
1365                     { format.getName() }));
1366           }
1367           else
1368           {
1369             statusBar.setText(MessageManager.formatMessage(
1370                     "label.successfully_saved_to_file_in_format",
1371                     new Object[]
1372                     { fileName, format.getName() }));
1373           }
1374           lastSaveSuccessful = true;
1375         } catch (IOException e)
1376         {
1377           lastSaveSuccessful = false;
1378           Console.error(
1379                   "ALIGNFRAME Something happened writing the temp file");
1380           Console.error(e.getMessage());
1381           Console.debug(Cache.getStackTraceString(e));
1382         } catch (Exception ex)
1383         {
1384           lastSaveSuccessful = false;
1385           Console.error(
1386                   "ALIGNFRAME Something unexpected happened writing the temp file");
1387           Console.error(ex.getMessage());
1388           Console.debug(Cache.getStackTraceString(ex));
1389         }
1390
1391         if (doBackup)
1392         {
1393           backupfiles.setWriteSuccess(lastSaveSuccessful);
1394           Console.debug("ALIGNFRAME writing temp file was "
1395                   + (lastSaveSuccessful ? "" : "NOT ") + "successful");
1396           // do the backup file roll and rename the temp file to actual file
1397           Console.trace("ALIGNFRAME about to rollBackupsAndRenameTempFile");
1398           lastSaveSuccessful = backupfiles.rollBackupsAndRenameTempFile();
1399           Console.debug("ALIGNFRAME performed rollBackupsAndRenameTempFile "
1400                   + (lastSaveSuccessful ? "" : "un") + "successfully");
1401         }
1402
1403         Console.debug("lastSaveSuccessful=" + lastSaveSuccessful);
1404         if (lastSaveSuccessful)
1405         {
1406           AlignFrame.this.getViewport().setSavedUpToDate(true);
1407         }
1408       }
1409     };
1410
1411     /*
1412      * show dialog with export options if applicable; else just do it
1413      */
1414     if (AlignExportOptions.isNeeded(viewport, format))
1415     {
1416       AlignExportOptions choices = new AlignExportOptions(
1417               alignPanel.getAlignViewport(), format, options);
1418       choices.setResponseAction(0, outputAction);
1419       choices.setResponseAction(1, cancelAction);
1420       choices.showDialog();
1421     }
1422     else
1423     {
1424       try
1425       {
1426         outputAction.run();
1427       } catch (Exception e)
1428       {
1429         // TODO Auto-generated catch block
1430         e.printStackTrace();
1431       }
1432     }
1433   }
1434
1435   /**
1436    * Outputs the alignment to textbox in the requested format, if necessary
1437    * first prompting the user for whether to include hidden regions or
1438    * non-sequence data
1439    * 
1440    * @param fileFormatName
1441    */
1442   @Override
1443   protected void outputText_actionPerformed(String fileFormatName)
1444   {
1445     FileFormatI fileFormat = FileFormats.getInstance()
1446             .forName(fileFormatName);
1447     AlignExportSettingsI options = new AlignExportSettingsAdapter(false);
1448     Runnable outputAction = () -> {
1449       // todo defer this to inside formatSequences (or later)
1450       AlignmentExportData exportData = viewport.getAlignExportData(options);
1451       CutAndPasteTransfer cap = new CutAndPasteTransfer();
1452       cap.setForInput(null);
1453       try
1454       {
1455         FileFormatI format = fileFormat;
1456         cap.setText(new FormatAdapter(alignPanel, options).formatSequences(
1457                 format, exportData.getAlignment(),
1458                 exportData.getOmitHidden(),
1459                 exportData.getStartEndPostions(),
1460                 viewport.getAlignment().getHiddenColumns()));
1461         Desktop.addInternalFrame(cap, MessageManager.formatMessage(
1462                 "label.alignment_output_command", new Object[]
1463                 { fileFormat.getName() }), 600, 500);
1464       } catch (OutOfMemoryError oom)
1465       {
1466         new OOMWarning("Outputting alignment as " + fileFormat.getName(),
1467                 oom);
1468         cap.dispose();
1469       }
1470     };
1471
1472     /*
1473      * show dialog with export options if applicable; else just do it
1474      */
1475     if (AlignExportOptions.isNeeded(viewport, fileFormat))
1476     {
1477       AlignExportOptions choices = new AlignExportOptions(
1478               alignPanel.getAlignViewport(), fileFormat, options);
1479       choices.setResponseAction(0, outputAction);
1480       choices.showDialog();
1481     }
1482     else
1483     {
1484       try
1485       {
1486         outputAction.run();
1487       } catch (Exception e)
1488       {
1489         e.printStackTrace();
1490       }
1491     }
1492   }
1493
1494   /**
1495    * DOCUMENT ME!
1496    * 
1497    * @param e
1498    *          DOCUMENT ME!
1499    */
1500   @Override
1501   protected void htmlMenuItem_actionPerformed(ActionEvent e)
1502   {
1503     HtmlSvgOutput htmlSVG = new HtmlSvgOutput(alignPanel);
1504     try
1505     {
1506       htmlSVG.exportHTML(null);
1507     } catch (ImageOutputException x)
1508     {
1509       // report problem to console and raise dialog
1510     }
1511   }
1512
1513   @Override
1514   public void bioJSMenuItem_actionPerformed(ActionEvent e)
1515   {
1516     BioJsHTMLOutput bjs = new BioJsHTMLOutput(alignPanel);
1517     try
1518     {
1519       bjs.exportHTML(null);
1520     } catch (ImageOutputException x)
1521     {
1522       // report problem to console and raise dialog
1523     }
1524   }
1525
1526   public void createImageMap(File file, String image)
1527   {
1528     try
1529     {
1530       alignPanel.makePNGImageMap(file, image);
1531     } catch (ImageOutputException x)
1532     {
1533       // report problem to console and raise dialog
1534     }
1535   }
1536
1537   @Override
1538   public void createPNG_actionPerformed(ActionEvent e)
1539   {
1540     try
1541     {
1542       createPNG(null);
1543     } catch (ImageOutputException ioex)
1544     {
1545       // raise dialog, and report via console
1546     }
1547   }
1548
1549   @Override
1550   public void createEPS_actionPerformed(ActionEvent e)
1551   {
1552     try
1553     {
1554       createEPS(null);
1555     } catch (ImageOutputException ioex)
1556     {
1557       // raise dialog, and report via console
1558     }
1559
1560   }
1561
1562   @Override
1563   public void createSVG_actionPerformed(ActionEvent e)
1564   {
1565     try
1566     {
1567       createSVG(null);
1568     } catch (ImageOutputException ioex)
1569     {
1570       // raise dialog, and report via console
1571     }
1572
1573   }
1574
1575   /**
1576    * Creates a PNG image of the alignment and writes it to the given file. If
1577    * the file is null, the user is prompted to choose a file.
1578    * 
1579    * @param f
1580    */
1581   public void createPNG(File f) throws ImageOutputException
1582   {
1583     createPNG(f, null, BitmapImageSizing.defaultBitmapImageSizing());
1584   }
1585
1586   public void createPNG(File f, String renderer, BitmapImageSizing userBis)
1587           throws ImageOutputException
1588   {
1589     alignPanel.makeAlignmentImage(TYPE.PNG, f, renderer, userBis);
1590   }
1591
1592   /**
1593    * Creates an EPS image of the alignment and writes it to the given file. If
1594    * the file is null, the user is prompted to choose a file.
1595    * 
1596    * @param f
1597    */
1598   public void createEPS(File f) throws ImageOutputException
1599   {
1600     createEPS(f, null);
1601   }
1602
1603   public void createEPS(File f, String renderer) throws ImageOutputException
1604   {
1605     alignPanel.makeAlignmentImage(TYPE.EPS, f, renderer);
1606   }
1607
1608   /**
1609    * Creates an SVG image of the alignment and writes it to the given file. If
1610    * the file is null, the user is prompted to choose a file.
1611    * 
1612    * @param f
1613    */
1614   public void createSVG(File f) throws ImageOutputException
1615   {
1616     createSVG(f, null);
1617   }
1618
1619   public void createSVG(File f, String renderer) throws ImageOutputException
1620   {
1621     alignPanel.makeAlignmentImage(TYPE.SVG, f, renderer);
1622   }
1623
1624   @Override
1625   public void pageSetup_actionPerformed(ActionEvent e)
1626   {
1627     PrinterJob printJob = PrinterJob.getPrinterJob();
1628     PrintThread.pf = printJob.pageDialog(printJob.defaultPage());
1629   }
1630
1631   /**
1632    * DOCUMENT ME!
1633    * 
1634    * @param e
1635    *          DOCUMENT ME!
1636    */
1637   @Override
1638   public void printMenuItem_actionPerformed(ActionEvent e)
1639   {
1640     // Putting in a thread avoids Swing painting problems
1641     PrintThread thread = new PrintThread(alignPanel);
1642     thread.start();
1643   }
1644
1645   @Override
1646   public void exportFeatures_actionPerformed(ActionEvent e)
1647   {
1648     new AnnotationExporter(alignPanel).exportFeatures();
1649   }
1650
1651   @Override
1652   public void exportAnnotations_actionPerformed(ActionEvent e)
1653   {
1654     new AnnotationExporter(alignPanel).exportAnnotations();
1655   }
1656
1657   @Override
1658   public void associatedData_actionPerformed(ActionEvent e)
1659   {
1660     final JalviewFileChooser chooser = new JalviewFileChooser(
1661             Cache.getProperty("LAST_DIRECTORY"));
1662     chooser.setFileView(new JalviewFileView());
1663     String tooltip = MessageManager
1664             .getString("label.load_jalview_annotations");
1665     chooser.setDialogTitle(tooltip);
1666     chooser.setToolTipText(tooltip);
1667     chooser.setResponseHandler(0, () -> {
1668       String choice = chooser.getSelectedFile().getPath();
1669       Cache.setProperty("LAST_DIRECTORY", choice);
1670       loadJalviewDataFile(chooser.getSelectedFile(), null, null, null);
1671     });
1672
1673     chooser.showOpenDialog(this);
1674   }
1675
1676   /**
1677    * Close the current view or all views in the alignment frame. If the frame
1678    * only contains one view then the alignment will be removed from memory.
1679    * 
1680    * @param closeAllTabs
1681    */
1682   @Override
1683   public void closeMenuItem_actionPerformed(boolean closeAllTabs)
1684   {
1685     if (alignPanels != null && alignPanels.size() < 2)
1686     {
1687       closeAllTabs = true;
1688     }
1689
1690     Desktop.closeModal(this);
1691
1692     try
1693     {
1694       if (alignPanels != null)
1695       {
1696         if (closeAllTabs)
1697         {
1698           if (this.isClosed())
1699           {
1700             // really close all the windows - otherwise wait till
1701             // setClosed(true) is called
1702             for (int i = 0; i < alignPanels.size(); i++)
1703             {
1704               AlignmentPanel ap = alignPanels.get(i);
1705               ap.closePanel();
1706             }
1707           }
1708         }
1709         else
1710         {
1711           closeView(alignPanel);
1712         }
1713       }
1714       if (closeAllTabs)
1715       {
1716         if (featureSettings != null && featureSettings.isOpen())
1717         {
1718           featureSettings.close();
1719           featureSettings = null;
1720         }
1721
1722         /*
1723          * this will raise an INTERNAL_FRAME_CLOSED event and this method will
1724          * be called recursively, with the frame now in 'closed' state
1725          */
1726         this.setClosed(true);
1727       }
1728     } catch (Exception ex)
1729     {
1730       ex.printStackTrace();
1731     }
1732   }
1733
1734   /**
1735    * Close the specified panel and close up tabs appropriately.
1736    * 
1737    * @param panelToClose
1738    */
1739   public void closeView(AlignmentPanel panelToClose)
1740   {
1741     int index = tabbedPane.getSelectedIndex();
1742     int closedindex = tabbedPane.indexOfComponent(panelToClose);
1743     alignPanels.remove(panelToClose);
1744     panelToClose.closePanel();
1745     panelToClose = null;
1746
1747     tabbedPane.removeTabAt(closedindex);
1748     tabbedPane.validate();
1749
1750     if (index > closedindex || index == tabbedPane.getTabCount())
1751     {
1752       // modify currently selected tab index if necessary.
1753       index--;
1754     }
1755
1756     this.tabSelectionChanged(index);
1757   }
1758
1759   /**
1760    * DOCUMENT ME!
1761    */
1762   void updateEditMenuBar()
1763   {
1764
1765     if (viewport.getHistoryList().size() > 0)
1766     {
1767       undoMenuItem.setEnabled(true);
1768       CommandI command = viewport.getHistoryList().peek();
1769       undoMenuItem.setText(MessageManager
1770               .formatMessage("label.undo_command", new Object[]
1771               { command.getDescription() }));
1772     }
1773     else
1774     {
1775       undoMenuItem.setEnabled(false);
1776       undoMenuItem.setText(MessageManager.getString("action.undo"));
1777     }
1778
1779     if (viewport.getRedoList().size() > 0)
1780     {
1781       redoMenuItem.setEnabled(true);
1782
1783       CommandI command = viewport.getRedoList().peek();
1784       redoMenuItem.setText(MessageManager
1785               .formatMessage("label.redo_command", new Object[]
1786               { command.getDescription() }));
1787     }
1788     else
1789     {
1790       redoMenuItem.setEnabled(false);
1791       redoMenuItem.setText(MessageManager.getString("action.redo"));
1792     }
1793   }
1794
1795   @Override
1796   public void addHistoryItem(CommandI command)
1797   {
1798     if (command.getSize() > 0)
1799     {
1800       viewport.addToHistoryList(command);
1801       viewport.clearRedoList();
1802       updateEditMenuBar();
1803       viewport.updateHiddenColumns();
1804       // viewport.hasHiddenColumns = (viewport.getColumnSelection() != null
1805       // && viewport.getColumnSelection().getHiddenColumns() != null &&
1806       // viewport.getColumnSelection()
1807       // .getHiddenColumns().size() > 0);
1808     }
1809   }
1810
1811   /**
1812    * 
1813    * @return alignment objects for all views
1814    */
1815   public AlignmentI[] getViewAlignments()
1816   {
1817     if (alignPanels != null)
1818     {
1819       AlignmentI[] als = new AlignmentI[alignPanels.size()];
1820       int i = 0;
1821       for (AlignmentPanel ap : alignPanels)
1822       {
1823         als[i++] = ap.av.getAlignment();
1824       }
1825       return als;
1826     }
1827     if (viewport != null)
1828     {
1829       return new AlignmentI[] { viewport.getAlignment() };
1830     }
1831     return null;
1832   }
1833
1834   /**
1835    * DOCUMENT ME!
1836    * 
1837    * @param e
1838    *          DOCUMENT ME!
1839    */
1840   @Override
1841   protected void undoMenuItem_actionPerformed(ActionEvent e)
1842   {
1843     if (viewport.getHistoryList().isEmpty())
1844     {
1845       return;
1846     }
1847     CommandI command = viewport.getHistoryList().pop();
1848     viewport.addToRedoList(command);
1849     command.undoCommand(getViewAlignments());
1850
1851     AlignmentViewport originalSource = getOriginatingSource(command);
1852     updateEditMenuBar();
1853
1854     if (originalSource != null)
1855     {
1856       if (originalSource != viewport)
1857       {
1858         Console.warn(
1859                 "Implementation worry: mismatch of viewport origin for undo");
1860       }
1861       originalSource.updateHiddenColumns();
1862       // originalSource.hasHiddenColumns = (viewport.getColumnSelection() !=
1863       // null
1864       // && viewport.getColumnSelection().getHiddenColumns() != null &&
1865       // viewport.getColumnSelection()
1866       // .getHiddenColumns().size() > 0);
1867       originalSource.firePropertyChange("alignment", null,
1868               originalSource.getAlignment().getSequences());
1869     }
1870   }
1871
1872   /**
1873    * DOCUMENT ME!
1874    * 
1875    * @param e
1876    *          DOCUMENT ME!
1877    */
1878   @Override
1879   protected void redoMenuItem_actionPerformed(ActionEvent e)
1880   {
1881     if (viewport.getRedoList().size() < 1)
1882     {
1883       return;
1884     }
1885
1886     CommandI command = viewport.getRedoList().pop();
1887     viewport.addToHistoryList(command);
1888     command.doCommand(getViewAlignments());
1889
1890     AlignmentViewport originalSource = getOriginatingSource(command);
1891     updateEditMenuBar();
1892
1893     if (originalSource != null)
1894     {
1895
1896       if (originalSource != viewport)
1897       {
1898         Console.warn(
1899                 "Implementation worry: mismatch of viewport origin for redo");
1900       }
1901       originalSource.updateHiddenColumns();
1902       // originalSource.hasHiddenColumns = (viewport.getColumnSelection() !=
1903       // null
1904       // && viewport.getColumnSelection().getHiddenColumns() != null &&
1905       // viewport.getColumnSelection()
1906       // .getHiddenColumns().size() > 0);
1907       originalSource.firePropertyChange("alignment", null,
1908               originalSource.getAlignment().getSequences());
1909     }
1910   }
1911
1912   AlignmentViewport getOriginatingSource(CommandI command)
1913   {
1914     AlignmentViewport originalSource = null;
1915     // For sequence removal and addition, we need to fire
1916     // the property change event FROM the viewport where the
1917     // original alignment was altered
1918     AlignmentI al = null;
1919     if (command instanceof EditCommand)
1920     {
1921       EditCommand editCommand = (EditCommand) command;
1922       al = editCommand.getAlignment();
1923       List<Component> comps = PaintRefresher.components
1924               .get(viewport.getSequenceSetId());
1925
1926       for (Component comp : comps)
1927       {
1928         if (comp instanceof AlignmentPanel)
1929         {
1930           if (al == ((AlignmentPanel) comp).av.getAlignment())
1931           {
1932             originalSource = ((AlignmentPanel) comp).av;
1933             break;
1934           }
1935         }
1936       }
1937     }
1938
1939     if (originalSource == null)
1940     {
1941       // The original view is closed, we must validate
1942       // the current view against the closed view first
1943       if (al != null)
1944       {
1945         PaintRefresher.validateSequences(al, viewport.getAlignment());
1946       }
1947
1948       originalSource = viewport;
1949     }
1950
1951     return originalSource;
1952   }
1953
1954   /**
1955    * Calls AlignmentI.moveSelectedSequencesByOne with current sequence selection
1956    * or the sequence under cursor in keyboard mode
1957    * 
1958    * @param up
1959    *          or down (if !up)
1960    */
1961   public void moveSelectedSequences(boolean up)
1962   {
1963     SequenceGroup sg = viewport.getSelectionGroup();
1964
1965     if (sg == null)
1966     {
1967       if (viewport.cursorMode)
1968       {
1969         sg = new SequenceGroup();
1970         sg.addSequence(viewport.getAlignment().getSequenceAt(
1971                 alignPanel.getSeqPanel().seqCanvas.cursorY), false);
1972       }
1973       else
1974       {
1975         return;
1976       }
1977     }
1978
1979     if (sg.getSize() < 1)
1980     {
1981       return;
1982     }
1983
1984     // TODO: JAL-3733 - add an event to the undo buffer for this !
1985
1986     viewport.getAlignment().moveSelectedSequencesByOne(sg,
1987             viewport.getHiddenRepSequences(), up);
1988     alignPanel.paintAlignment(true, false);
1989   }
1990
1991   synchronized void slideSequences(boolean right, int size)
1992   {
1993     List<SequenceI> sg = new ArrayList<>();
1994     if (viewport.cursorMode)
1995     {
1996       sg.add(viewport.getAlignment()
1997               .getSequenceAt(alignPanel.getSeqPanel().seqCanvas.cursorY));
1998     }
1999     else if (viewport.getSelectionGroup() != null
2000             && viewport.getSelectionGroup().getSize() != viewport
2001                     .getAlignment().getHeight())
2002     {
2003       sg = viewport.getSelectionGroup()
2004               .getSequences(viewport.getHiddenRepSequences());
2005     }
2006
2007     if (sg.size() < 1)
2008     {
2009       return;
2010     }
2011
2012     List<SequenceI> invertGroup = new ArrayList<>();
2013
2014     for (SequenceI seq : viewport.getAlignment().getSequences())
2015     {
2016       if (!sg.contains(seq))
2017       {
2018         invertGroup.add(seq);
2019       }
2020     }
2021
2022     SequenceI[] seqs1 = sg.toArray(new SequenceI[0]);
2023
2024     SequenceI[] seqs2 = new SequenceI[invertGroup.size()];
2025     for (int i = 0; i < invertGroup.size(); i++)
2026     {
2027       seqs2[i] = invertGroup.get(i);
2028     }
2029
2030     SlideSequencesCommand ssc;
2031     if (right)
2032     {
2033       ssc = new SlideSequencesCommand("Slide Sequences", seqs2, seqs1, size,
2034               viewport.getGapCharacter());
2035     }
2036     else
2037     {
2038       ssc = new SlideSequencesCommand("Slide Sequences", seqs1, seqs2, size,
2039               viewport.getGapCharacter());
2040     }
2041
2042     int groupAdjustment = 0;
2043     if (ssc.getGapsInsertedBegin() && right)
2044     {
2045       if (viewport.cursorMode)
2046       {
2047         alignPanel.getSeqPanel().moveCursor(size, 0);
2048       }
2049       else
2050       {
2051         groupAdjustment = size;
2052       }
2053     }
2054     else if (!ssc.getGapsInsertedBegin() && !right)
2055     {
2056       if (viewport.cursorMode)
2057       {
2058         alignPanel.getSeqPanel().moveCursor(-size, 0);
2059       }
2060       else
2061       {
2062         groupAdjustment = -size;
2063       }
2064     }
2065
2066     if (groupAdjustment != 0)
2067     {
2068       viewport.getSelectionGroup().setStartRes(
2069               viewport.getSelectionGroup().getStartRes() + groupAdjustment);
2070       viewport.getSelectionGroup().setEndRes(
2071               viewport.getSelectionGroup().getEndRes() + groupAdjustment);
2072     }
2073
2074     /*
2075      * just extend the last slide command if compatible; but not if in
2076      * SplitFrame mode (to ensure all edits are broadcast - JAL-1802)
2077      */
2078     boolean appendHistoryItem = false;
2079     Deque<CommandI> historyList = viewport.getHistoryList();
2080     boolean inSplitFrame = getSplitViewContainer() != null;
2081     if (!inSplitFrame && historyList != null && historyList.size() > 0
2082             && historyList.peek() instanceof SlideSequencesCommand)
2083     {
2084       appendHistoryItem = ssc.appendSlideCommand(
2085               (SlideSequencesCommand) historyList.peek());
2086     }
2087
2088     if (!appendHistoryItem)
2089     {
2090       addHistoryItem(ssc);
2091     }
2092
2093     repaint();
2094   }
2095
2096   /**
2097    * DOCUMENT ME!
2098    * 
2099    * @param e
2100    *          DOCUMENT ME!
2101    */
2102   @Override
2103   protected void copy_actionPerformed()
2104   {
2105     if (viewport.getSelectionGroup() == null)
2106     {
2107       return;
2108     }
2109     // TODO: preserve the ordering of displayed alignment annotation in any
2110     // internal paste (particularly sequence associated annotation)
2111     SequenceI[] seqs = viewport.getSelectionAsNewSequence();
2112     String[] omitHidden = null;
2113
2114     if (viewport.hasHiddenColumns())
2115     {
2116       omitHidden = viewport.getViewAsString(true);
2117     }
2118
2119     String output = new FormatAdapter().formatSequences(FileFormat.Fasta,
2120             seqs, omitHidden, null);
2121
2122     StringSelection ss = new StringSelection(output);
2123
2124     try
2125     {
2126       jalview.gui.Desktop.internalCopy = true;
2127       // Its really worth setting the clipboard contents
2128       // to empty before setting the large StringSelection!!
2129       Toolkit.getDefaultToolkit().getSystemClipboard()
2130               .setContents(new StringSelection(""), null);
2131
2132       Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss,
2133               Desktop.instance);
2134     } catch (OutOfMemoryError er)
2135     {
2136       new OOMWarning("copying region", er);
2137       return;
2138     }
2139
2140     HiddenColumns hiddenColumns = null;
2141     if (viewport.hasHiddenColumns())
2142     {
2143       int hiddenOffset = viewport.getSelectionGroup().getStartRes();
2144       int hiddenCutoff = viewport.getSelectionGroup().getEndRes();
2145
2146       // create new HiddenColumns object with copy of hidden regions
2147       // between startRes and endRes, offset by startRes
2148       hiddenColumns = new HiddenColumns(
2149               viewport.getAlignment().getHiddenColumns(), hiddenOffset,
2150               hiddenCutoff, hiddenOffset);
2151     }
2152
2153     Desktop.jalviewClipboard = new Object[] { seqs,
2154         viewport.getAlignment().getDataset(), hiddenColumns };
2155     setStatus(MessageManager.formatMessage(
2156             "label.copied_sequences_to_clipboard", new Object[]
2157             { Integer.valueOf(seqs.length).toString() }));
2158   }
2159
2160   /**
2161    * DOCUMENT ME!
2162    * 
2163    * @param e
2164    *          DOCUMENT ME!
2165    */
2166   @Override
2167   protected void pasteNew_actionPerformed(ActionEvent e)
2168   {
2169     paste(true);
2170   }
2171
2172   /**
2173    * DOCUMENT ME!
2174    * 
2175    * @param e
2176    *          DOCUMENT ME!
2177    */
2178   @Override
2179   protected void pasteThis_actionPerformed(ActionEvent e)
2180   {
2181     paste(false);
2182   }
2183
2184   /**
2185    * Paste contents of Jalview clipboard
2186    * 
2187    * @param newAlignment
2188    *          true to paste to a new alignment, otherwise add to this.
2189    */
2190   void paste(boolean newAlignment)
2191   {
2192     boolean externalPaste = true;
2193     try
2194     {
2195       Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
2196       Transferable contents = c.getContents(this);
2197
2198       if (contents == null)
2199       {
2200         return;
2201       }
2202
2203       String str;
2204       FileFormatI format;
2205       try
2206       {
2207         str = (String) contents.getTransferData(DataFlavor.stringFlavor);
2208         if (str.length() < 1)
2209         {
2210           return;
2211         }
2212
2213         format = new IdentifyFile().identify(str, DataSourceType.PASTE);
2214
2215       } catch (OutOfMemoryError er)
2216       {
2217         new OOMWarning("Out of memory pasting sequences!!", er);
2218         return;
2219       }
2220
2221       SequenceI[] sequences;
2222       boolean annotationAdded = false;
2223       AlignmentI alignment = null;
2224
2225       if (Desktop.jalviewClipboard != null)
2226       {
2227         // The clipboard was filled from within Jalview, we must use the
2228         // sequences
2229         // And dataset from the copied alignment
2230         SequenceI[] newseq = (SequenceI[]) Desktop.jalviewClipboard[0];
2231         // be doubly sure that we create *new* sequence objects.
2232         sequences = new SequenceI[newseq.length];
2233         for (int i = 0; i < newseq.length; i++)
2234         {
2235           sequences[i] = new Sequence(newseq[i]);
2236         }
2237         alignment = new Alignment(sequences);
2238         externalPaste = false;
2239       }
2240       else
2241       {
2242         // parse the clipboard as an alignment.
2243         alignment = new FormatAdapter().readFile(str, DataSourceType.PASTE,
2244                 format);
2245         sequences = alignment.getSequencesArray();
2246       }
2247
2248       int alwidth = 0;
2249       ArrayList<Integer> newGraphGroups = new ArrayList<>();
2250       int fgroup = -1;
2251
2252       if (newAlignment)
2253       {
2254
2255         if (Desktop.jalviewClipboard != null)
2256         {
2257           // dataset is inherited
2258           alignment.setDataset((Alignment) Desktop.jalviewClipboard[1]);
2259         }
2260         else
2261         {
2262           // new dataset is constructed
2263           alignment.setDataset(null);
2264         }
2265         alwidth = alignment.getWidth() + 1;
2266       }
2267       else
2268       {
2269         AlignmentI pastedal = alignment; // preserve pasted alignment object
2270         // Add pasted sequences and dataset into existing alignment.
2271         alignment = viewport.getAlignment();
2272         alwidth = alignment.getWidth() + 1;
2273         // decide if we need to import sequences from an existing dataset
2274         boolean importDs = Desktop.jalviewClipboard != null
2275                 && Desktop.jalviewClipboard[1] != alignment.getDataset();
2276         // importDs==true instructs us to copy over new dataset sequences from
2277         // an existing alignment
2278         Vector<SequenceI> newDs = (importDs) ? new Vector<>() : null; // used to
2279                                                                       // create
2280         // minimum dataset set
2281
2282         for (int i = 0; i < sequences.length; i++)
2283         {
2284           if (importDs)
2285           {
2286             newDs.addElement(null);
2287           }
2288           SequenceI ds = sequences[i].getDatasetSequence(); // null for a simple
2289           // paste
2290           if (importDs && ds != null)
2291           {
2292             if (!newDs.contains(ds))
2293             {
2294               newDs.setElementAt(ds, i);
2295               ds = new Sequence(ds);
2296               // update with new dataset sequence
2297               sequences[i].setDatasetSequence(ds);
2298             }
2299             else
2300             {
2301               ds = sequences[newDs.indexOf(ds)].getDatasetSequence();
2302             }
2303           }
2304           else
2305           {
2306             // copy and derive new dataset sequence
2307             sequences[i] = sequences[i].deriveSequence();
2308             alignment.getDataset()
2309                     .addSequence(sequences[i].getDatasetSequence());
2310             // TODO: avoid creation of duplicate dataset sequences with a
2311             // 'contains' method using SequenceI.equals()/SequenceI.contains()
2312           }
2313           alignment.addSequence(sequences[i]); // merges dataset
2314         }
2315         if (newDs != null)
2316         {
2317           newDs.clear(); // tidy up
2318         }
2319         if (alignment.getAlignmentAnnotation() != null)
2320         {
2321           for (AlignmentAnnotation alan : alignment
2322                   .getAlignmentAnnotation())
2323           {
2324             if (alan.graphGroup > fgroup)
2325             {
2326               fgroup = alan.graphGroup;
2327             }
2328           }
2329         }
2330         if (pastedal.getAlignmentAnnotation() != null)
2331         {
2332           // Add any annotation attached to alignment.
2333           AlignmentAnnotation[] alann = pastedal.getAlignmentAnnotation();
2334           for (int i = 0; i < alann.length; i++)
2335           {
2336             annotationAdded = true;
2337             if (alann[i].sequenceRef == null && !alann[i].autoCalculated)
2338             {
2339               AlignmentAnnotation newann = new AlignmentAnnotation(
2340                       alann[i]);
2341               if (newann.graphGroup > -1)
2342               {
2343                 if (newGraphGroups.size() <= newann.graphGroup
2344                         || newGraphGroups.get(newann.graphGroup) == null)
2345                 {
2346                   for (int q = newGraphGroups
2347                           .size(); q <= newann.graphGroup; q++)
2348                   {
2349                     newGraphGroups.add(q, null);
2350                   }
2351                   newGraphGroups.set(newann.graphGroup,
2352                           Integer.valueOf(++fgroup));
2353                 }
2354                 newann.graphGroup = newGraphGroups.get(newann.graphGroup)
2355                         .intValue();
2356               }
2357
2358               newann.padAnnotation(alwidth);
2359               alignment.addAnnotation(newann);
2360             }
2361           }
2362         }
2363       }
2364       if (!newAlignment)
2365       {
2366         // /////
2367         // ADD HISTORY ITEM
2368         //
2369         addHistoryItem(new EditCommand(
2370                 MessageManager.getString("label.add_sequences"),
2371                 Action.PASTE, sequences, 0, alignment.getWidth(),
2372                 alignment));
2373       }
2374       // Add any annotations attached to sequences
2375       for (int i = 0; i < sequences.length; i++)
2376       {
2377         if (sequences[i].getAnnotation() != null)
2378         {
2379           AlignmentAnnotation newann;
2380           for (int a = 0; a < sequences[i].getAnnotation().length; a++)
2381           {
2382             annotationAdded = true;
2383             newann = sequences[i].getAnnotation()[a];
2384             newann.adjustForAlignment();
2385             newann.padAnnotation(alwidth);
2386             if (newann.graphGroup > -1)
2387             {
2388               if (newann.graphGroup > -1)
2389               {
2390                 if (newGraphGroups.size() <= newann.graphGroup
2391                         || newGraphGroups.get(newann.graphGroup) == null)
2392                 {
2393                   for (int q = newGraphGroups
2394                           .size(); q <= newann.graphGroup; q++)
2395                   {
2396                     newGraphGroups.add(q, null);
2397                   }
2398                   newGraphGroups.set(newann.graphGroup,
2399                           Integer.valueOf(++fgroup));
2400                 }
2401                 newann.graphGroup = newGraphGroups.get(newann.graphGroup)
2402                         .intValue();
2403               }
2404             }
2405             // annotation was duplicated earlier
2406             alignment.addAnnotation(sequences[i].getAnnotation()[a]);
2407             // take care of contact matrix too
2408             ContactMatrixI cm = sequences[i]
2409                     .getContactMatrixFor(sequences[i].getAnnotation()[a]);
2410             if (cm != null)
2411             {
2412               alignment.addContactListFor(sequences[i].getAnnotation()[a],
2413                       cm);
2414             }
2415
2416             alignment.setAnnotationIndex(sequences[i].getAnnotation()[a],
2417                     a);
2418           }
2419         }
2420       }
2421       if (!newAlignment)
2422       {
2423
2424         // propagate alignment changed.
2425         viewport.getRanges().setEndSeq(alignment.getHeight() - 1);
2426         if (annotationAdded)
2427         {
2428           // Duplicate sequence annotation in all views.
2429           AlignmentI[] alview = this.getViewAlignments();
2430           for (int i = 0; i < sequences.length; i++)
2431           {
2432             AlignmentAnnotation sann[] = sequences[i].getAnnotation();
2433             if (sann == null)
2434             {
2435               continue;
2436             }
2437             for (int avnum = 0; avnum < alview.length; avnum++)
2438             {
2439               if (alview[avnum] != alignment)
2440               {
2441                 // duplicate in a view other than the one with input focus
2442                 int avwidth = alview[avnum].getWidth() + 1;
2443                 // this relies on sann being preserved after we
2444                 // modify the sequence's annotation array for each duplication
2445                 for (int a = 0; a < sann.length; a++)
2446                 {
2447                   AlignmentAnnotation newann = new AlignmentAnnotation(
2448                           sann[a]);
2449                   sequences[i].addAlignmentAnnotation(newann);
2450                   newann.padAnnotation(avwidth);
2451                   alview[avnum].addAnnotation(newann); // annotation was
2452                   // duplicated earlier
2453                   // TODO JAL-1145 graphGroups are not updated for sequence
2454                   // annotation added to several views. This may cause
2455                   // strangeness
2456                   alview[avnum].setAnnotationIndex(newann, a);
2457                 }
2458               }
2459             }
2460           }
2461           buildSortByAnnotationScoresMenu();
2462         }
2463         viewport.firePropertyChange("alignment", null,
2464                 alignment.getSequences());
2465         if (alignPanels != null)
2466         {
2467           for (AlignmentPanel ap : alignPanels)
2468           {
2469             ap.validateAnnotationDimensions(false);
2470           }
2471         }
2472         else
2473         {
2474           alignPanel.validateAnnotationDimensions(false);
2475         }
2476
2477       }
2478       else
2479       {
2480         AlignFrame af = new AlignFrame(alignment, DEFAULT_WIDTH,
2481                 DEFAULT_HEIGHT);
2482         String newtitle = new String("Copied sequences");
2483
2484         if (Desktop.jalviewClipboard != null
2485                 && Desktop.jalviewClipboard[2] != null)
2486         {
2487           HiddenColumns hc = (HiddenColumns) Desktop.jalviewClipboard[2];
2488           af.viewport.setHiddenColumns(hc);
2489         }
2490
2491         // >>>This is a fix for the moment, until a better solution is
2492         // found!!<<<
2493         af.alignPanel.getSeqPanel().seqCanvas.getFeatureRenderer()
2494                 .transferSettings(alignPanel.getSeqPanel().seqCanvas
2495                         .getFeatureRenderer());
2496
2497         // TODO: maintain provenance of an alignment, rather than just make the
2498         // title a concatenation of operations.
2499         if (!externalPaste)
2500         {
2501           if (title.startsWith("Copied sequences"))
2502           {
2503             newtitle = title;
2504           }
2505           else
2506           {
2507             newtitle = newtitle.concat("- from " + title);
2508           }
2509         }
2510         else
2511         {
2512           newtitle = new String("Pasted sequences");
2513         }
2514
2515         Desktop.addInternalFrame(af, newtitle, DEFAULT_WIDTH,
2516                 DEFAULT_HEIGHT);
2517
2518       }
2519
2520     } catch (Exception ex)
2521     {
2522       ex.printStackTrace();
2523       jalview.bin.Console.outPrintln("Exception whilst pasting: " + ex);
2524       // could be anything being pasted in here
2525     }
2526
2527   }
2528
2529   @Override
2530   protected void expand_newalign(ActionEvent e)
2531   {
2532     try
2533     {
2534       AlignmentI alignment = AlignmentUtils
2535               .expandContext(getViewport().getAlignment(), -1);
2536       AlignFrame af = new AlignFrame(alignment, DEFAULT_WIDTH,
2537               DEFAULT_HEIGHT);
2538       String newtitle = new String("Flanking alignment");
2539
2540       if (Desktop.jalviewClipboard != null
2541               && Desktop.jalviewClipboard[2] != null)
2542       {
2543         HiddenColumns hc = (HiddenColumns) Desktop.jalviewClipboard[2];
2544         af.viewport.setHiddenColumns(hc);
2545       }
2546
2547       // >>>This is a fix for the moment, until a better solution is
2548       // found!!<<<
2549       af.alignPanel.getSeqPanel().seqCanvas.getFeatureRenderer()
2550               .transferSettings(alignPanel.getSeqPanel().seqCanvas
2551                       .getFeatureRenderer());
2552
2553       // TODO: maintain provenance of an alignment, rather than just make the
2554       // title a concatenation of operations.
2555       {
2556         if (title.startsWith("Copied sequences"))
2557         {
2558           newtitle = title;
2559         }
2560         else
2561         {
2562           newtitle = newtitle.concat("- from " + title);
2563         }
2564       }
2565
2566       Desktop.addInternalFrame(af, newtitle, DEFAULT_WIDTH, DEFAULT_HEIGHT);
2567
2568     } catch (Exception ex)
2569     {
2570       ex.printStackTrace();
2571       jalview.bin.Console.outPrintln("Exception whilst pasting: " + ex);
2572       // could be anything being pasted in here
2573     } catch (OutOfMemoryError oom)
2574     {
2575       new OOMWarning("Viewing flanking region of alignment", oom);
2576     }
2577   }
2578
2579   /**
2580    * Action Cut (delete and copy) the selected region
2581    */
2582   @Override
2583   protected void cut_actionPerformed()
2584   {
2585     copy_actionPerformed();
2586     delete_actionPerformed();
2587   }
2588
2589   /**
2590    * Performs menu option to Delete the currently selected region
2591    */
2592   @Override
2593   protected void delete_actionPerformed()
2594   {
2595
2596     SequenceGroup sg = viewport.getSelectionGroup();
2597     if (sg == null)
2598     {
2599       return;
2600     }
2601
2602     Runnable okAction = () -> {
2603       SequenceI[] cut = sg.getSequences()
2604               .toArray(new SequenceI[sg.getSize()]);
2605
2606       addHistoryItem(new EditCommand(
2607               MessageManager.getString("label.cut_sequences"), Action.CUT,
2608               cut, sg.getStartRes(), sg.getEndRes() - sg.getStartRes() + 1,
2609               viewport.getAlignment()));
2610
2611       viewport.setSelectionGroup(null);
2612       viewport.sendSelection();
2613       viewport.getAlignment().deleteGroup(sg);
2614
2615       viewport.firePropertyChange("alignment", null,
2616               viewport.getAlignment().getSequences());
2617       if (viewport.getAlignment().getHeight() < 1)
2618       {
2619         try
2620         {
2621           AlignFrame.this.setClosed(true);
2622         } catch (Exception ex)
2623         {
2624         }
2625       }
2626     };
2627
2628     /*
2629      * If the cut affects all sequences, prompt for confirmation
2630      */
2631     boolean wholeHeight = sg.getSize() == viewport.getAlignment()
2632             .getHeight();
2633     boolean wholeWidth = (((sg.getEndRes() - sg.getStartRes())
2634             + 1) == viewport.getAlignment().getWidth()) ? true : false;
2635     if (wholeHeight && wholeWidth)
2636     {
2637       JvOptionPane dialog = JvOptionPane.newOptionDialog(Desktop.desktop);
2638       dialog.setResponseHandler(0, okAction); // 0 = OK_OPTION
2639       Object[] options = new Object[] {
2640           MessageManager.getString("action.ok"),
2641           MessageManager.getString("action.cancel") };
2642       dialog.showDialog(MessageManager.getString("warn.delete_all"),
2643               MessageManager.getString("label.delete_all"),
2644               JvOptionPane.DEFAULT_OPTION, JvOptionPane.PLAIN_MESSAGE, null,
2645               options, options[0]);
2646     }
2647     else
2648     {
2649       try
2650       {
2651         okAction.run();
2652       } catch (Exception e)
2653       {
2654         e.printStackTrace();
2655       }
2656     }
2657   }
2658
2659   /**
2660    * DOCUMENT ME!
2661    * 
2662    * @param e
2663    *          DOCUMENT ME!
2664    */
2665   @Override
2666   protected void deleteGroups_actionPerformed(ActionEvent e)
2667   {
2668     if (avc.deleteGroups())
2669     {
2670       PaintRefresher.Refresh(this, viewport.getSequenceSetId());
2671       alignPanel.updateAnnotation();
2672       alignPanel.paintAlignment(true, true);
2673     }
2674   }
2675
2676   /**
2677    * DOCUMENT ME!
2678    * 
2679    * @param e
2680    *          DOCUMENT ME!
2681    */
2682   @Override
2683   public void selectAllSequenceMenuItem_actionPerformed(ActionEvent e)
2684   {
2685     SequenceGroup sg = new SequenceGroup(
2686             viewport.getAlignment().getSequences());
2687
2688     sg.setEndRes(viewport.getAlignment().getWidth() - 1);
2689     viewport.setSelectionGroup(sg);
2690     viewport.isSelectionGroupChanged(true);
2691     viewport.sendSelection();
2692     // JAL-2034 - should delegate to
2693     // alignPanel to decide if overview needs
2694     // updating.
2695     alignPanel.paintAlignment(false, false);
2696     PaintRefresher.Refresh(alignPanel, viewport.getSequenceSetId());
2697   }
2698
2699   /**
2700    * DOCUMENT ME!
2701    * 
2702    * @param e
2703    *          DOCUMENT ME!
2704    */
2705   @Override
2706   public void deselectAllSequenceMenuItem_actionPerformed(ActionEvent e)
2707   {
2708     if (viewport.cursorMode)
2709     {
2710       alignPanel.getSeqPanel().keyboardNo1 = null;
2711       alignPanel.getSeqPanel().keyboardNo2 = null;
2712     }
2713     viewport.setSelectionGroup(null);
2714     viewport.getColumnSelection().clear();
2715     viewport.setSearchResults(null);
2716     alignPanel.getIdPanel().getIdCanvas().searchResults = null;
2717     // JAL-2034 - should delegate to
2718     // alignPanel to decide if overview needs
2719     // updating.
2720     alignPanel.paintAlignment(false, false);
2721     PaintRefresher.Refresh(alignPanel, viewport.getSequenceSetId());
2722     viewport.sendSelection();
2723   }
2724
2725   /**
2726    * DOCUMENT ME!
2727    * 
2728    * @param e
2729    *          DOCUMENT ME!
2730    */
2731   @Override
2732   public void invertSequenceMenuItem_actionPerformed(ActionEvent e)
2733   {
2734     SequenceGroup sg = viewport.getSelectionGroup();
2735
2736     if (sg == null)
2737     {
2738       selectAllSequenceMenuItem_actionPerformed(null);
2739
2740       return;
2741     }
2742
2743     for (int i = 0; i < viewport.getAlignment().getSequences().size(); i++)
2744     {
2745       sg.addOrRemove(viewport.getAlignment().getSequenceAt(i), false);
2746     }
2747     // JAL-2034 - should delegate to
2748     // alignPanel to decide if overview needs
2749     // updating.
2750
2751     alignPanel.paintAlignment(true, false);
2752     PaintRefresher.Refresh(alignPanel, viewport.getSequenceSetId());
2753     viewport.sendSelection();
2754   }
2755
2756   @Override
2757   public void invertColSel_actionPerformed(ActionEvent e)
2758   {
2759     viewport.invertColumnSelection();
2760     alignPanel.paintAlignment(true, false);
2761     PaintRefresher.Refresh(alignPanel, viewport.getSequenceSetId());
2762     viewport.sendSelection();
2763   }
2764
2765   /**
2766    * DOCUMENT ME!
2767    * 
2768    * @param e
2769    *          DOCUMENT ME!
2770    */
2771   @Override
2772   public void remove2LeftMenuItem_actionPerformed(ActionEvent e)
2773   {
2774     trimAlignment(true);
2775   }
2776
2777   /**
2778    * DOCUMENT ME!
2779    * 
2780    * @param e
2781    *          DOCUMENT ME!
2782    */
2783   @Override
2784   public void remove2RightMenuItem_actionPerformed(ActionEvent e)
2785   {
2786     trimAlignment(false);
2787   }
2788
2789   void trimAlignment(boolean trimLeft)
2790   {
2791     ColumnSelection colSel = viewport.getColumnSelection();
2792     int column;
2793
2794     if (!colSel.isEmpty())
2795     {
2796       if (trimLeft)
2797       {
2798         column = colSel.getMin();
2799       }
2800       else
2801       {
2802         column = colSel.getMax();
2803       }
2804
2805       SequenceI[] seqs;
2806       if (viewport.getSelectionGroup() != null)
2807       {
2808         seqs = viewport.getSelectionGroup()
2809                 .getSequencesAsArray(viewport.getHiddenRepSequences());
2810       }
2811       else
2812       {
2813         seqs = viewport.getAlignment().getSequencesArray();
2814       }
2815
2816       TrimRegionCommand trimRegion;
2817       if (trimLeft)
2818       {
2819         trimRegion = new TrimRegionCommand("Remove Left", true, seqs,
2820                 column, viewport.getAlignment());
2821         viewport.getRanges().setStartRes(0);
2822       }
2823       else
2824       {
2825         trimRegion = new TrimRegionCommand("Remove Right", false, seqs,
2826                 column, viewport.getAlignment());
2827       }
2828
2829       setStatus(MessageManager.formatMessage("label.removed_columns",
2830               new String[]
2831               { Integer.valueOf(trimRegion.getSize()).toString() }));
2832
2833       addHistoryItem(trimRegion);
2834
2835       for (SequenceGroup sg : viewport.getAlignment().getGroups())
2836       {
2837         if ((trimLeft && !sg.adjustForRemoveLeft(column))
2838                 || (!trimLeft && !sg.adjustForRemoveRight(column)))
2839         {
2840           viewport.getAlignment().deleteGroup(sg);
2841         }
2842       }
2843
2844       viewport.firePropertyChange("alignment", null,
2845               viewport.getAlignment().getSequences());
2846     }
2847   }
2848
2849   /**
2850    * DOCUMENT ME!
2851    * 
2852    * @param e
2853    *          DOCUMENT ME!
2854    */
2855   @Override
2856   public void removeGappedColumnMenuItem_actionPerformed(ActionEvent e)
2857   {
2858     int start = 0, end = viewport.getAlignment().getWidth() - 1;
2859
2860     SequenceI[] seqs;
2861     if (viewport.getSelectionGroup() != null)
2862     {
2863       seqs = viewport.getSelectionGroup()
2864               .getSequencesAsArray(viewport.getHiddenRepSequences());
2865       start = viewport.getSelectionGroup().getStartRes();
2866       end = viewport.getSelectionGroup().getEndRes();
2867     }
2868     else
2869     {
2870       seqs = viewport.getAlignment().getSequencesArray();
2871     }
2872
2873     RemoveGapColCommand removeGapCols = new RemoveGapColCommand(
2874             "Remove Gapped Columns", seqs, start, end,
2875             viewport.getAlignment());
2876
2877     addHistoryItem(removeGapCols);
2878
2879     setStatus(MessageManager.formatMessage("label.removed_empty_columns",
2880             new Object[]
2881             { Integer.valueOf(removeGapCols.getSize()).toString() }));
2882
2883     // This is to maintain viewport position on first residue
2884     // of first sequence
2885     SequenceI seq = viewport.getAlignment().getSequenceAt(0);
2886     ViewportRanges ranges = viewport.getRanges();
2887     int startRes = seq.findPosition(ranges.getStartRes());
2888     // ShiftList shifts;
2889     // viewport.getAlignment().removeGaps(shifts=new ShiftList());
2890     // edit.alColumnChanges=shifts.getInverse();
2891     // if (viewport.hasHiddenColumns)
2892     // viewport.getColumnSelection().compensateForEdits(shifts);
2893     ranges.setStartRes(seq.findIndex(startRes) - 1);
2894     viewport.firePropertyChange("alignment", null,
2895             viewport.getAlignment().getSequences());
2896
2897   }
2898
2899   /**
2900    * DOCUMENT ME!
2901    * 
2902    * @param e
2903    *          DOCUMENT ME!
2904    */
2905   @Override
2906   public void removeAllGapsMenuItem_actionPerformed(ActionEvent e)
2907   {
2908     int start = 0, end = viewport.getAlignment().getWidth() - 1;
2909
2910     SequenceI[] seqs;
2911     if (viewport.getSelectionGroup() != null)
2912     {
2913       seqs = viewport.getSelectionGroup()
2914               .getSequencesAsArray(viewport.getHiddenRepSequences());
2915       start = viewport.getSelectionGroup().getStartRes();
2916       end = viewport.getSelectionGroup().getEndRes();
2917     }
2918     else
2919     {
2920       seqs = viewport.getAlignment().getSequencesArray();
2921     }
2922
2923     // This is to maintain viewport position on first residue
2924     // of first sequence
2925     SequenceI seq = viewport.getAlignment().getSequenceAt(0);
2926     int startRes = seq.findPosition(viewport.getRanges().getStartRes());
2927
2928     addHistoryItem(new RemoveGapsCommand("Remove Gaps", seqs, start, end,
2929             viewport.getAlignment()));
2930
2931     viewport.getRanges().setStartRes(seq.findIndex(startRes) - 1);
2932
2933     viewport.firePropertyChange("alignment", null,
2934             viewport.getAlignment().getSequences());
2935
2936   }
2937
2938   /**
2939    * DOCUMENT ME!
2940    * 
2941    * @param e
2942    *          DOCUMENT ME!
2943    */
2944   @Override
2945   public void padGapsMenuitem_actionPerformed(ActionEvent e)
2946   {
2947     viewport.setPadGaps(padGapsMenuitem.isSelected());
2948     viewport.firePropertyChange("alignment", null,
2949             viewport.getAlignment().getSequences());
2950   }
2951
2952   /**
2953    * Opens a Finder dialog
2954    * 
2955    * @param e
2956    */
2957   @Override
2958   public void findMenuItem_actionPerformed(ActionEvent e)
2959   {
2960     new Finder(alignPanel, false, null);
2961   }
2962
2963   /**
2964    * Create a new view of the current alignment.
2965    */
2966   @Override
2967   public void newView_actionPerformed(ActionEvent e)
2968   {
2969     newView(null, true);
2970   }
2971
2972   /**
2973    * Creates and shows a new view of the current alignment.
2974    * 
2975    * @param viewTitle
2976    *          title of newly created view; if null, one will be generated
2977    * @param copyAnnotation
2978    *          if true then duplicate all annnotation, groups and settings
2979    * @return new alignment panel, already displayed.
2980    */
2981   public AlignmentPanel newView(String viewTitle, boolean copyAnnotation)
2982   {
2983     /*
2984      * Create a new AlignmentPanel (with its own, new Viewport)
2985      */
2986     AlignmentPanel newap = new jalview.project.Jalview2XML()
2987             .copyAlignPanel(alignPanel);
2988     if (!copyAnnotation)
2989     {
2990       /*
2991        * remove all groups and annotation except for the automatic stuff
2992        */
2993       newap.av.getAlignment().deleteAllGroups();
2994       newap.av.getAlignment().deleteAllAnnotations(false);
2995     }
2996
2997     newap.av.setGatherViewsHere(false);
2998
2999     if (viewport.getViewName() == null)
3000     {
3001       viewport.setViewName(
3002               MessageManager.getString("label.view_name_original"));
3003     }
3004
3005     /*
3006      * Views share the same edits undo and redo stacks
3007      */
3008     newap.av.setHistoryList(viewport.getHistoryList());
3009     newap.av.setRedoList(viewport.getRedoList());
3010
3011     /*
3012      * copy any visualisation settings that are not saved in the project
3013      */
3014     newap.av.setColourAppliesToAllGroups(
3015             viewport.getColourAppliesToAllGroups());
3016
3017     /*
3018      * Views share the same mappings; need to deregister any new mappings
3019      * created by copyAlignPanel, and register the new reference to the shared
3020      * mappings
3021      */
3022     newap.av.replaceMappings(viewport.getAlignment());
3023
3024     /*
3025      * start up cDNA consensus (if applicable) now mappings are in place
3026      */
3027     if (newap.av.initComplementConsensus())
3028     {
3029       newap.refresh(true); // adjust layout of annotations
3030     }
3031
3032     newap.av.setViewName(getNewViewName(viewTitle));
3033
3034     addAlignmentPanel(newap, true);
3035     newap.alignmentChanged();
3036
3037     if (alignPanels.size() == 2)
3038     {
3039       viewport.setGatherViewsHere(true);
3040     }
3041     tabbedPane.setSelectedIndex(tabbedPane.getTabCount() - 1);
3042
3043     return newap;
3044   }
3045
3046   /**
3047    * Make a new name for the view, ensuring it is unique within the current
3048    * sequenceSetId. (This used to be essential for Jalview Project archives, but
3049    * these now use viewId. Unique view names are still desirable for usability.)
3050    * 
3051    * @param viewTitle
3052    * @return
3053    */
3054   protected String getNewViewName(String viewTitle)
3055   {
3056     int index = Desktop.getViewCount(viewport.getSequenceSetId());
3057     boolean addFirstIndex = false;
3058     if (viewTitle == null || viewTitle.trim().length() == 0)
3059     {
3060       viewTitle = MessageManager.getString("action.view");
3061       addFirstIndex = true;
3062     }
3063     else
3064     {
3065       index = 1;// we count from 1 if given a specific name
3066     }
3067     String newViewName = viewTitle + ((addFirstIndex) ? " " + index : "");
3068
3069     List<Component> comps = PaintRefresher.components
3070             .get(viewport.getSequenceSetId());
3071
3072     List<String> existingNames = getExistingViewNames(comps);
3073
3074     while (existingNames.contains(newViewName))
3075     {
3076       newViewName = viewTitle + " " + (++index);
3077     }
3078     return newViewName;
3079   }
3080
3081   /**
3082    * Returns a list of distinct view names found in the given list of
3083    * components. View names are held on the viewport of an AlignmentPanel.
3084    * 
3085    * @param comps
3086    * @return
3087    */
3088   protected List<String> getExistingViewNames(List<Component> comps)
3089   {
3090     List<String> existingNames = new ArrayList<>();
3091     for (Component comp : comps)
3092     {
3093       if (comp instanceof AlignmentPanel)
3094       {
3095         AlignmentPanel ap = (AlignmentPanel) comp;
3096         if (!existingNames.contains(ap.av.getViewName()))
3097         {
3098           existingNames.add(ap.av.getViewName());
3099         }
3100       }
3101     }
3102     return existingNames;
3103   }
3104
3105   /**
3106    * Explode tabbed views into separate windows.
3107    */
3108   @Override
3109   public void expandViews_actionPerformed(ActionEvent e)
3110   {
3111     Desktop.explodeViews(this);
3112   }
3113
3114   /**
3115    * Gather views in separate windows back into a tabbed presentation.
3116    */
3117   @Override
3118   public void gatherViews_actionPerformed(ActionEvent e)
3119   {
3120     Desktop.instance.gatherViews(this);
3121   }
3122
3123   /**
3124    * DOCUMENT ME!
3125    * 
3126    * @param e
3127    *          DOCUMENT ME!
3128    */
3129   @Override
3130   public void font_actionPerformed(ActionEvent e)
3131   {
3132     new FontChooser(alignPanel);
3133   }
3134
3135   /**
3136    * DOCUMENT ME!
3137    * 
3138    * @param e
3139    *          DOCUMENT ME!
3140    */
3141   @Override
3142   protected void seqLimit_actionPerformed(ActionEvent e)
3143   {
3144     viewport.setShowJVSuffix(seqLimits.isSelected());
3145
3146     alignPanel.getIdPanel().getIdCanvas()
3147             .setPreferredSize(alignPanel.calculateIdWidth());
3148     alignPanel.paintAlignment(true, false);
3149   }
3150
3151   @Override
3152   public void idRightAlign_actionPerformed(ActionEvent e)
3153   {
3154     viewport.setRightAlignIds(idRightAlign.isSelected());
3155     alignPanel.paintAlignment(false, false);
3156   }
3157
3158   @Override
3159   public void centreColumnLabels_actionPerformed(ActionEvent e)
3160   {
3161     viewport.setCentreColumnLabels(centreColumnLabelsMenuItem.getState());
3162     alignPanel.paintAlignment(false, false);
3163   }
3164
3165   /*
3166    * (non-Javadoc)
3167    * 
3168    * @see jalview.jbgui.GAlignFrame#followHighlight_actionPerformed()
3169    */
3170   @Override
3171   protected void followHighlight_actionPerformed()
3172   {
3173     /*
3174      * Set the 'follow' flag on the Viewport (and scroll to position if now
3175      * true).
3176      */
3177     final boolean state = this.followHighlightMenuItem.getState();
3178     viewport.setFollowHighlight(state);
3179     if (state)
3180     {
3181       alignPanel.scrollToPosition(viewport.getSearchResults());
3182     }
3183   }
3184
3185   /**
3186    * DOCUMENT ME!
3187    * 
3188    * @param e
3189    *          DOCUMENT ME!
3190    */
3191   @Override
3192   protected void colourTextMenuItem_actionPerformed(ActionEvent e)
3193   {
3194     viewport.setColourText(colourTextMenuItem.isSelected());
3195     alignPanel.paintAlignment(false, false);
3196   }
3197
3198   /**
3199    * DOCUMENT ME!
3200    * 
3201    * @param e
3202    *          DOCUMENT ME!
3203    */
3204   @Override
3205   public void wrapMenuItem_actionPerformed(ActionEvent e)
3206   {
3207     setWrapFormat(wrapMenuItem.isSelected(), false);
3208   }
3209
3210   public void setWrapFormat(boolean b, boolean setMenuItem)
3211   {
3212     scaleAbove.setVisible(b);
3213     scaleLeft.setVisible(b);
3214     scaleRight.setVisible(b);
3215     viewport.setWrapAlignment(b);
3216     alignPanel.updateLayout();
3217     if (setMenuItem)
3218     {
3219       wrapMenuItem.setSelected(b);
3220     }
3221   }
3222
3223   @Override
3224   public void showAllSeqs_actionPerformed(ActionEvent e)
3225   {
3226     viewport.showAllHiddenSeqs();
3227   }
3228
3229   @Override
3230   public void showAllColumns_actionPerformed(ActionEvent e)
3231   {
3232     viewport.showAllHiddenColumns();
3233     alignPanel.paintAlignment(true, true);
3234     viewport.sendSelection();
3235   }
3236
3237   @Override
3238   public void hideSelSequences_actionPerformed(ActionEvent e)
3239   {
3240     viewport.hideAllSelectedSeqs();
3241   }
3242
3243   /**
3244    * called by key handler and the hide all/show all menu items
3245    * 
3246    * @param toggleSeqs
3247    * @param toggleCols
3248    */
3249   protected void toggleHiddenRegions(boolean toggleSeqs, boolean toggleCols)
3250   {
3251
3252     boolean hide = false;
3253     SequenceGroup sg = viewport.getSelectionGroup();
3254     if (!toggleSeqs && !toggleCols)
3255     {
3256       // Hide everything by the current selection - this is a hack - we do the
3257       // invert and then hide
3258       // first check that there will be visible columns after the invert.
3259       if (viewport.hasSelectedColumns() || (sg != null && sg.getSize() > 0
3260               && sg.getStartRes() <= sg.getEndRes()))
3261       {
3262         // now invert the sequence set, if required - empty selection implies
3263         // that no hiding is required.
3264         if (sg != null)
3265         {
3266           invertSequenceMenuItem_actionPerformed(null);
3267           sg = viewport.getSelectionGroup();
3268           toggleSeqs = true;
3269
3270         }
3271         viewport.expandColSelection(sg, true);
3272         // finally invert the column selection and get the new sequence
3273         // selection.
3274         invertColSel_actionPerformed(null);
3275         toggleCols = true;
3276       }
3277     }
3278
3279     if (toggleSeqs)
3280     {
3281       if (sg != null && sg.getSize() != viewport.getAlignment().getHeight())
3282       {
3283         hideSelSequences_actionPerformed(null);
3284         hide = true;
3285       }
3286       else if (!(toggleCols && viewport.hasSelectedColumns()))
3287       {
3288         showAllSeqs_actionPerformed(null);
3289       }
3290     }
3291
3292     if (toggleCols)
3293     {
3294       if (viewport.hasSelectedColumns())
3295       {
3296         hideSelColumns_actionPerformed(null);
3297         if (!toggleSeqs)
3298         {
3299           viewport.setSelectionGroup(sg);
3300         }
3301       }
3302       else if (!hide)
3303       {
3304         showAllColumns_actionPerformed(null);
3305       }
3306     }
3307   }
3308
3309   /*
3310    * (non-Javadoc)
3311    * 
3312    * @see
3313    * jalview.jbgui.GAlignFrame#hideAllButSelection_actionPerformed(java.awt.
3314    * event.ActionEvent)
3315    */
3316   @Override
3317   public void hideAllButSelection_actionPerformed(ActionEvent e)
3318   {
3319     toggleHiddenRegions(false, false);
3320     viewport.sendSelection();
3321   }
3322
3323   /*
3324    * (non-Javadoc)
3325    * 
3326    * @see
3327    * jalview.jbgui.GAlignFrame#hideAllSelection_actionPerformed(java.awt.event
3328    * .ActionEvent)
3329    */
3330   @Override
3331   public void hideAllSelection_actionPerformed(ActionEvent e)
3332   {
3333     SequenceGroup sg = viewport.getSelectionGroup();
3334     viewport.expandColSelection(sg, false);
3335     viewport.hideAllSelectedSeqs();
3336     viewport.hideSelectedColumns();
3337     alignPanel.updateLayout();
3338     alignPanel.paintAlignment(true, true);
3339     viewport.sendSelection();
3340   }
3341
3342   /*
3343    * (non-Javadoc)
3344    * 
3345    * @see
3346    * jalview.jbgui.GAlignFrame#showAllhidden_actionPerformed(java.awt.event.
3347    * ActionEvent)
3348    */
3349   @Override
3350   public void showAllhidden_actionPerformed(ActionEvent e)
3351   {
3352     viewport.showAllHiddenColumns();
3353     viewport.showAllHiddenSeqs();
3354     alignPanel.paintAlignment(true, true);
3355     viewport.sendSelection();
3356   }
3357
3358   @Override
3359   public void hideSelColumns_actionPerformed(ActionEvent e)
3360   {
3361     viewport.hideSelectedColumns();
3362     alignPanel.updateLayout();
3363     alignPanel.paintAlignment(true, true);
3364     viewport.sendSelection();
3365   }
3366
3367   @Override
3368   public void hiddenMarkers_actionPerformed(ActionEvent e)
3369   {
3370     viewport.setShowHiddenMarkers(hiddenMarkers.isSelected());
3371     repaint();
3372   }
3373
3374   /**
3375    * DOCUMENT ME!
3376    * 
3377    * @param e
3378    *          DOCUMENT ME!
3379    */
3380   @Override
3381   protected void scaleAbove_actionPerformed(ActionEvent e)
3382   {
3383     viewport.setScaleAboveWrapped(scaleAbove.isSelected());
3384     alignPanel.updateLayout();
3385     alignPanel.paintAlignment(true, false);
3386   }
3387
3388   /**
3389    * DOCUMENT ME!
3390    * 
3391    * @param e
3392    *          DOCUMENT ME!
3393    */
3394   @Override
3395   protected void scaleLeft_actionPerformed(ActionEvent e)
3396   {
3397     viewport.setScaleLeftWrapped(scaleLeft.isSelected());
3398     alignPanel.updateLayout();
3399     alignPanel.paintAlignment(true, false);
3400   }
3401
3402   /**
3403    * DOCUMENT ME!
3404    * 
3405    * @param e
3406    *          DOCUMENT ME!
3407    */
3408   @Override
3409   protected void scaleRight_actionPerformed(ActionEvent e)
3410   {
3411     viewport.setScaleRightWrapped(scaleRight.isSelected());
3412     alignPanel.updateLayout();
3413     alignPanel.paintAlignment(true, false);
3414   }
3415
3416   /**
3417    * DOCUMENT ME!
3418    * 
3419    * @param e
3420    *          DOCUMENT ME!
3421    */
3422   @Override
3423   public void viewBoxesMenuItem_actionPerformed(ActionEvent e)
3424   {
3425     viewport.setShowBoxes(viewBoxesMenuItem.isSelected());
3426     alignPanel.paintAlignment(false, false);
3427   }
3428
3429   /**
3430    * DOCUMENT ME!
3431    * 
3432    * @param e
3433    *          DOCUMENT ME!
3434    */
3435   @Override
3436   public void viewTextMenuItem_actionPerformed(ActionEvent e)
3437   {
3438     viewport.setShowText(viewTextMenuItem.isSelected());
3439     alignPanel.paintAlignment(false, false);
3440   }
3441
3442   /**
3443    * DOCUMENT ME!
3444    * 
3445    * @param e
3446    *          DOCUMENT ME!
3447    */
3448   @Override
3449   protected void renderGapsMenuItem_actionPerformed(ActionEvent e)
3450   {
3451     viewport.setRenderGaps(renderGapsMenuItem.isSelected());
3452     alignPanel.paintAlignment(false, false);
3453   }
3454
3455   public FeatureSettings featureSettings;
3456
3457   @Override
3458   public FeatureSettingsControllerI getFeatureSettingsUI()
3459   {
3460     return featureSettings;
3461   }
3462
3463   @Override
3464   public void featureSettings_actionPerformed(ActionEvent e)
3465   {
3466     showFeatureSettingsUI();
3467   }
3468
3469   @Override
3470   public FeatureSettingsControllerI showFeatureSettingsUI()
3471   {
3472     if (featureSettings != null)
3473     {
3474       featureSettings.closeOldSettings();
3475       featureSettings = null;
3476     }
3477     if (!showSeqFeatures.isSelected())
3478     {
3479       // make sure features are actually displayed
3480       showSeqFeatures.setSelected(true);
3481       showSeqFeatures_actionPerformed(null);
3482     }
3483     featureSettings = new FeatureSettings(this);
3484     return featureSettings;
3485   }
3486
3487   /**
3488    * Set or clear 'Show Sequence Features'
3489    * 
3490    * @param evt
3491    *          DOCUMENT ME!
3492    */
3493   @Override
3494   public void showSeqFeatures_actionPerformed(ActionEvent evt)
3495   {
3496     viewport.setShowSequenceFeatures(showSeqFeatures.isSelected());
3497     alignPanel.paintAlignment(true, true);
3498   }
3499
3500   /**
3501    * Action on toggle of the 'Show annotations' menu item. This shows or hides
3502    * the annotations panel as a whole.
3503    * 
3504    * The options to show/hide all annotations should be enabled when the panel
3505    * is shown, and disabled when the panel is hidden.
3506    * 
3507    * @param e
3508    */
3509   @Override
3510   public void annotationPanelMenuItem_actionPerformed(ActionEvent e)
3511   {
3512     final boolean setVisible = annotationPanelMenuItem.isSelected();
3513     viewport.setShowAnnotation(setVisible);
3514     this.showAllSeqAnnotations.setEnabled(setVisible);
3515     this.hideAllSeqAnnotations.setEnabled(setVisible);
3516     this.showAllAlAnnotations.setEnabled(setVisible);
3517     this.hideAllAlAnnotations.setEnabled(setVisible);
3518     alignPanel.updateLayout();
3519   }
3520
3521   @Override
3522   public void alignmentProperties()
3523   {
3524     JComponent pane;
3525     StringBuffer contents = new AlignmentProperties(viewport.getAlignment())
3526
3527             .formatAsHtml();
3528     String content = MessageManager.formatMessage("label.html_content",
3529             new Object[]
3530             { contents.toString() });
3531     contents = null;
3532
3533     if (Platform.isJS())
3534     {
3535       JLabel textLabel = new JLabel();
3536       textLabel.setText(content);
3537       textLabel.setBackground(Color.WHITE);
3538
3539       pane = new JPanel(new BorderLayout());
3540       ((JPanel) pane).setOpaque(true);
3541       pane.setBackground(Color.WHITE);
3542       ((JPanel) pane).add(textLabel, BorderLayout.NORTH);
3543     }
3544     else
3545     /**
3546      * Java only
3547      * 
3548      * @j2sIgnore
3549      */
3550     {
3551       JEditorPane editPane = new JEditorPane("text/html", "");
3552       editPane.setEditable(false);
3553       editPane.setText(content);
3554       pane = editPane;
3555     }
3556
3557     JInternalFrame frame = new JInternalFrame();
3558     frame.setFrameIcon(null);
3559     frame.getContentPane().add(new JScrollPane(pane));
3560
3561     Desktop.addInternalFrame(frame, MessageManager
3562             .formatMessage("label.alignment_properties", new Object[]
3563             { getTitle() }), 500, 400);
3564   }
3565
3566   /**
3567    * Opens an Overview panel for the alignment, unless one is open already
3568    * 
3569    * @param e
3570    */
3571   @Override
3572   public void overviewMenuItem_actionPerformed(ActionEvent e)
3573   {
3574     boolean showHiddenRegions = Cache
3575             .getDefault(Preferences.SHOW_OV_HIDDEN_AT_START, false);
3576     openOverviewPanel(showHiddenRegions);
3577   }
3578
3579   public OverviewPanel openOverviewPanel(boolean showHidden)
3580   {
3581     if (alignPanel.overviewPanel != null)
3582     {
3583       return alignPanel.overviewPanel;
3584     }
3585     JInternalFrame frame = new JInternalFrame();
3586     frame.setFrameIcon(null);
3587     final OverviewPanel overview = new OverviewPanel(alignPanel, frame,
3588             showHidden);
3589     frame.setContentPane(overview);
3590
3591     alignPanel.setOverviewPanel(overview);
3592     alignPanel.setOverviewTitle(this);
3593
3594     Desktop.addInternalFrame(frame, overview.getTitle(), true,
3595             frame.getWidth(), frame.getHeight(), true, true);
3596     frame.pack();
3597     frame.setLayer(JLayeredPane.PALETTE_LAYER);
3598     final AlignmentPanel thePanel = this.alignPanel;
3599     frame.addInternalFrameListener(
3600             new javax.swing.event.InternalFrameAdapter()
3601             {
3602               @Override
3603               public void internalFrameClosed(
3604                       javax.swing.event.InternalFrameEvent evt)
3605               {
3606                 overview.dispose();
3607                 thePanel.setOverviewPanel(null);
3608               }
3609             });
3610     if (getKeyListeners().length > 0)
3611     {
3612       frame.addKeyListener(getKeyListeners()[0]);
3613     }
3614
3615     return overview;
3616   }
3617
3618   @Override
3619   public void textColour_actionPerformed()
3620   {
3621     new TextColourChooser().chooseColour(alignPanel, null);
3622   }
3623
3624   /*
3625    * public void covariationColour_actionPerformed() {
3626    * changeColour(new
3627    * CovariationColourScheme(viewport.getAlignment().getAlignmentAnnotation
3628    * ()[0])); }
3629    */
3630   @Override
3631   public void annotationColour_actionPerformed()
3632   {
3633     new AnnotationColourChooser(viewport, alignPanel);
3634   }
3635
3636   @Override
3637   public void annotationColumn_actionPerformed(ActionEvent e)
3638   {
3639     new AnnotationColumnChooser(viewport, alignPanel);
3640   }
3641
3642   /**
3643    * Action on the user checking or unchecking the option to apply the selected
3644    * colour scheme to all groups. If unchecked, groups may have their own
3645    * independent colour schemes.
3646    * 
3647    * @param selected
3648    */
3649   @Override
3650   public void applyToAllGroups_actionPerformed(boolean selected)
3651   {
3652     viewport.setColourAppliesToAllGroups(selected);
3653   }
3654
3655   /**
3656    * Action on user selecting a colour from the colour menu
3657    * 
3658    * @param name
3659    *          the name (not the menu item label!) of the colour scheme
3660    */
3661   @Override
3662   public void changeColour_actionPerformed(String name)
3663   {
3664     /*
3665      * 'User Defined' opens a panel to configure or load a
3666      * user-defined colour scheme
3667      */
3668     if (ResidueColourScheme.USER_DEFINED_MENU.equals(name))
3669     {
3670       new UserDefinedColours(alignPanel);
3671       return;
3672     }
3673
3674     /*
3675      * otherwise set the chosen colour scheme (or null for 'None')
3676      */
3677     ColourSchemeI cs = ColourSchemes.getInstance().getColourScheme(name,
3678             viewport, viewport.getAlignment(),
3679             viewport.getHiddenRepSequences());
3680     changeColour(cs);
3681   }
3682
3683   /**
3684    * Actions on setting or changing the alignment colour scheme
3685    * 
3686    * @param cs
3687    */
3688   @Override
3689   public void changeColour(ColourSchemeI cs)
3690   {
3691     // TODO: pull up to controller method
3692     ColourMenuHelper.setColourSelected(colourMenu, cs);
3693
3694     viewport.setGlobalColourScheme(cs);
3695
3696     alignPanel.paintAlignment(true, true);
3697   }
3698
3699   /**
3700    * Show the PID threshold slider panel
3701    */
3702   @Override
3703   protected void modifyPID_actionPerformed()
3704   {
3705     SliderPanel.setPIDSliderSource(alignPanel, viewport.getResidueShading(),
3706             alignPanel.getViewName());
3707     SliderPanel.showPIDSlider();
3708   }
3709
3710   /**
3711    * Show the Conservation slider panel
3712    */
3713   @Override
3714   protected void modifyConservation_actionPerformed()
3715   {
3716     SliderPanel.setConservationSlider(alignPanel,
3717             viewport.getResidueShading(), alignPanel.getViewName());
3718     SliderPanel.showConservationSlider();
3719   }
3720
3721   /**
3722    * Action on selecting or deselecting (Colour) By Conservation
3723    */
3724   @Override
3725   public void conservationMenuItem_actionPerformed(boolean selected)
3726   {
3727     modifyConservation.setEnabled(selected);
3728     viewport.setConservationSelected(selected);
3729     viewport.getResidueShading().setConservationApplied(selected);
3730
3731     changeColour(viewport.getGlobalColourScheme());
3732     if (selected)
3733     {
3734       modifyConservation_actionPerformed();
3735     }
3736     else
3737     {
3738       SliderPanel.hideConservationSlider();
3739     }
3740   }
3741
3742   /**
3743    * Action on selecting or deselecting (Colour) Above PID Threshold
3744    */
3745   @Override
3746   public void abovePIDThreshold_actionPerformed(boolean selected)
3747   {
3748     modifyPID.setEnabled(selected);
3749     viewport.setAbovePIDThreshold(selected);
3750     if (!selected)
3751     {
3752       viewport.getResidueShading().setThreshold(0,
3753               viewport.isIgnoreGapsConsensus());
3754     }
3755
3756     changeColour(viewport.getGlobalColourScheme());
3757     if (selected)
3758     {
3759       modifyPID_actionPerformed();
3760     }
3761     else
3762     {
3763       SliderPanel.hidePIDSlider();
3764     }
3765   }
3766
3767   /**
3768    * DOCUMENT ME!
3769    * 
3770    * @param e
3771    *          DOCUMENT ME!
3772    */
3773   @Override
3774   public void sortPairwiseMenuItem_actionPerformed(ActionEvent e)
3775   {
3776     SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray();
3777     AlignmentSorter.sortByPID(viewport.getAlignment(),
3778             viewport.getAlignment().getSequenceAt(0));
3779     addHistoryItem(new OrderCommand("Pairwise Sort", oldOrder,
3780             viewport.getAlignment()));
3781     alignPanel.paintAlignment(true, false);
3782   }
3783
3784   /**
3785    * DOCUMENT ME!
3786    * 
3787    * @param e
3788    *          DOCUMENT ME!
3789    */
3790   @Override
3791   public void sortIDMenuItem_actionPerformed(ActionEvent e)
3792   {
3793     SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray();
3794     AlignmentSorter.sortByID(viewport.getAlignment());
3795     addHistoryItem(
3796             new OrderCommand("ID Sort", oldOrder, viewport.getAlignment()));
3797     alignPanel.paintAlignment(true, false);
3798   }
3799
3800   /**
3801    * DOCUMENT ME!
3802    * 
3803    * @param e
3804    *          DOCUMENT ME!
3805    */
3806   @Override
3807   public void sortLengthMenuItem_actionPerformed(ActionEvent e)
3808   {
3809     SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray();
3810     AlignmentSorter.sortByLength(viewport.getAlignment());
3811     addHistoryItem(new OrderCommand("Length Sort", oldOrder,
3812             viewport.getAlignment()));
3813     alignPanel.paintAlignment(true, false);
3814   }
3815
3816   /**
3817    * DOCUMENT ME!
3818    * 
3819    * @param e
3820    *          DOCUMENT ME!
3821    */
3822   @Override
3823   public void sortGroupMenuItem_actionPerformed(ActionEvent e)
3824   {
3825     SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray();
3826     AlignmentSorter.sortByGroup(viewport.getAlignment());
3827     addHistoryItem(new OrderCommand("Group Sort", oldOrder,
3828             viewport.getAlignment()));
3829
3830     alignPanel.paintAlignment(true, false);
3831   }
3832
3833   /**
3834    * DOCUMENT ME!
3835    * 
3836    * @param e
3837    *          DOCUMENT ME!
3838    */
3839   @Override
3840   public void removeRedundancyMenuItem_actionPerformed(ActionEvent e)
3841   {
3842     new RedundancyPanel(alignPanel, this);
3843   }
3844
3845   /**
3846    * DOCUMENT ME!
3847    * 
3848    * @param e
3849    *          DOCUMENT ME!
3850    */
3851   @Override
3852   public void pairwiseAlignmentMenuItem_actionPerformed(ActionEvent e)
3853   {
3854     if ((viewport.getSelectionGroup() == null)
3855             || (viewport.getSelectionGroup().getSize() < 2))
3856     {
3857       JvOptionPane.showInternalMessageDialog(this,
3858               MessageManager.getString(
3859                       "label.you_must_select_least_two_sequences"),
3860               MessageManager.getString("label.invalid_selection"),
3861               JvOptionPane.WARNING_MESSAGE);
3862     }
3863     else
3864     {
3865       new Thread(new Runnable() {
3866         @Override
3867         public void run()
3868         {
3869                JInternalFrame frame = new JInternalFrame();
3870       frame.setFrameIcon(null);
3871       frame.setContentPane(new PairwiseAlignPanel(viewport));
3872       Desktop.addInternalFrame(frame,
3873               MessageManager.getString("action.pairwise_alignment"), 600,
3874               500);
3875         }
3876       }).start();
3877       
3878     }
3879   }
3880
3881   @Override
3882   public void autoCalculate_actionPerformed(ActionEvent e)
3883   {
3884     viewport.autoCalculateConsensus = autoCalculate.isSelected();
3885     if (viewport.autoCalculateConsensus)
3886     {
3887       viewport.firePropertyChange("alignment", null,
3888               viewport.getAlignment().getSequences());
3889     }
3890   }
3891
3892   @Override
3893   public void sortByTreeOption_actionPerformed(ActionEvent e)
3894   {
3895     viewport.sortByTree = sortByTree.isSelected();
3896   }
3897
3898   @Override
3899   protected void listenToViewSelections_actionPerformed(ActionEvent e)
3900   {
3901     viewport.followSelection = listenToViewSelections.isSelected();
3902   }
3903
3904   /**
3905    * Constructs a tree panel and adds it to the desktop
3906    * 
3907    * @param type
3908    *          tree type (NJ or AV)
3909    * @param modelName
3910    *          name of score model used to compute the tree
3911    * @param options
3912    *          parameters for the distance or similarity calculation
3913    */
3914   void newTreePanel(String type, String modelName,
3915           SimilarityParamsI options)
3916   {
3917     String frameTitle = "";
3918     TreePanel tp;
3919
3920     boolean onSelection = false;
3921     if (viewport.getSelectionGroup() != null
3922             && viewport.getSelectionGroup().getSize() > 0)
3923     {
3924       SequenceGroup sg = viewport.getSelectionGroup();
3925
3926       /* Decide if the selection is a column region */
3927       for (SequenceI _s : sg.getSequences())
3928       {
3929         if (_s.getLength() < sg.getEndRes())
3930         {
3931           JvOptionPane.showMessageDialog(Desktop.desktop,
3932                   MessageManager.getString(
3933                           "label.selected_region_to_tree_may_only_contain_residues_or_gaps"),
3934                   MessageManager.getString(
3935                           "label.sequences_selection_not_aligned"),
3936                   JvOptionPane.WARNING_MESSAGE);
3937
3938           return;
3939         }
3940       }
3941       onSelection = true;
3942     }
3943     else
3944     {
3945       if (viewport.getAlignment().getHeight() < 2)
3946       {
3947         return;
3948       }
3949     }
3950
3951     tp = new TreePanel(alignPanel, type, modelName, options);
3952     frameTitle = tp.getPanelTitle() + (onSelection ? " on region" : "");
3953
3954     frameTitle += " from ";
3955
3956     if (viewport.getViewName() != null)
3957     {
3958       frameTitle += viewport.getViewName() + " of ";
3959     }
3960
3961     frameTitle += this.title;
3962
3963     Desktop.addInternalFrame(tp, frameTitle, 600, 500);
3964   }
3965
3966   /**
3967    * DOCUMENT ME!
3968    * 
3969    * @param title
3970    *          DOCUMENT ME!
3971    * @param order
3972    *          DOCUMENT ME!
3973    */
3974   public void addSortByOrderMenuItem(String title,
3975           final AlignmentOrder order)
3976   {
3977     final JMenuItem item = new JMenuItem(MessageManager
3978             .formatMessage("action.by_title_param", new Object[]
3979             { title }));
3980     sort.add(item);
3981     item.addActionListener(new java.awt.event.ActionListener()
3982     {
3983       @Override
3984       public void actionPerformed(ActionEvent e)
3985       {
3986         SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray();
3987
3988         // TODO: JBPNote - have to map order entries to curent SequenceI
3989         // pointers
3990         AlignmentSorter.sortBy(viewport.getAlignment(), order);
3991
3992         addHistoryItem(new OrderCommand(order.getName(), oldOrder,
3993                 viewport.getAlignment()));
3994
3995         alignPanel.paintAlignment(true, false);
3996       }
3997     });
3998   }
3999
4000   /**
4001    * Add a new sort by annotation score menu item
4002    * 
4003    * @param sort
4004    *          the menu to add the option to
4005    * @param scoreLabel
4006    *          the label used to retrieve scores for each sequence on the
4007    *          alignment
4008    */
4009   public void addSortByAnnotScoreMenuItem(JMenu sort,
4010           final String scoreLabel)
4011   {
4012     final JMenuItem item = new JMenuItem(scoreLabel);
4013     sort.add(item);
4014     item.addActionListener(new java.awt.event.ActionListener()
4015     {
4016       @Override
4017       public void actionPerformed(ActionEvent e)
4018       {
4019         SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray();
4020         AlignmentSorter.sortByAnnotationScore(scoreLabel,
4021                 viewport.getAlignment());// ,viewport.getSelectionGroup());
4022         addHistoryItem(new OrderCommand("Sort by " + scoreLabel, oldOrder,
4023                 viewport.getAlignment()));
4024         alignPanel.paintAlignment(true, false);
4025       }
4026     });
4027   }
4028
4029   /**
4030    * last hash for alignment's annotation array - used to minimise cost of
4031    * rebuild.
4032    */
4033   protected int _annotationScoreVectorHash;
4034
4035   /**
4036    * search the alignment and rebuild the sort by annotation score submenu the
4037    * last alignment annotation vector hash is stored to minimize cost of
4038    * rebuilding in subsequence calls.
4039    * 
4040    */
4041   @Override
4042   public void buildSortByAnnotationScoresMenu()
4043   {
4044     if (viewport.getAlignment().getAlignmentAnnotation() == null)
4045     {
4046       return;
4047     }
4048
4049     if (viewport.getAlignment().getAlignmentAnnotation()
4050             .hashCode() != _annotationScoreVectorHash)
4051     {
4052       sortByAnnotScore.removeAll();
4053       // almost certainly a quicker way to do this - but we keep it simple
4054       Hashtable<String, String> scoreSorts = new Hashtable<>();
4055       AlignmentAnnotation aann[];
4056       for (SequenceI sqa : viewport.getAlignment().getSequences())
4057       {
4058         aann = sqa.getAnnotation();
4059         for (int i = 0; aann != null && i < aann.length; i++)
4060         {
4061           if (aann[i].hasScore() && aann[i].sequenceRef != null)
4062           {
4063             scoreSorts.put(aann[i].label, aann[i].label);
4064           }
4065         }
4066       }
4067       Enumeration<String> labels = scoreSorts.keys();
4068       while (labels.hasMoreElements())
4069       {
4070         addSortByAnnotScoreMenuItem(sortByAnnotScore, labels.nextElement());
4071       }
4072       sortByAnnotScore.setVisible(scoreSorts.size() > 0);
4073       scoreSorts.clear();
4074
4075       _annotationScoreVectorHash = viewport.getAlignment()
4076               .getAlignmentAnnotation().hashCode();
4077     }
4078   }
4079
4080   /**
4081    * Maintain the Order by->Displayed Tree menu. Creates a new menu item for a
4082    * TreePanel with an appropriate <code>jalview.analysis.AlignmentSorter</code>
4083    * call. Listeners are added to remove the menu item when the treePanel is
4084    * closed, and adjust the tree leaf to sequence mapping when the alignment is
4085    * modified.
4086    */
4087   @Override
4088   public void buildTreeSortMenu()
4089   {
4090     sortByTreeMenu.removeAll();
4091
4092     List<Component> comps = PaintRefresher.components
4093             .get(viewport.getSequenceSetId());
4094     List<TreePanel> treePanels = new ArrayList<>();
4095     for (Component comp : comps)
4096     {
4097       if (comp instanceof TreePanel)
4098       {
4099         treePanels.add((TreePanel) comp);
4100       }
4101     }
4102
4103     if (treePanels.size() < 1)
4104     {
4105       sortByTreeMenu.setVisible(false);
4106       return;
4107     }
4108
4109     sortByTreeMenu.setVisible(true);
4110
4111     for (final TreePanel tp : treePanels)
4112     {
4113       final JMenuItem item = new JMenuItem(tp.getTitle());
4114       item.addActionListener(new java.awt.event.ActionListener()
4115       {
4116         @Override
4117         public void actionPerformed(ActionEvent e)
4118         {
4119           tp.sortByTree_actionPerformed();
4120           addHistoryItem(tp.sortAlignmentIn(alignPanel));
4121
4122         }
4123       });
4124
4125       sortByTreeMenu.add(item);
4126     }
4127   }
4128
4129   public boolean sortBy(AlignmentOrder alorder, String undoname)
4130   {
4131     SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray();
4132     AlignmentSorter.sortBy(viewport.getAlignment(), alorder);
4133     if (undoname != null)
4134     {
4135       addHistoryItem(new OrderCommand(undoname, oldOrder,
4136               viewport.getAlignment()));
4137     }
4138     alignPanel.paintAlignment(true, false);
4139     return true;
4140   }
4141
4142   /**
4143    * Work out whether the whole set of sequences or just the selected set will
4144    * be submitted for multiple alignment.
4145    * 
4146    */
4147   public jalview.datamodel.AlignmentView gatherSequencesForAlignment()
4148   {
4149     // Now, check we have enough sequences
4150     AlignmentView msa = null;
4151
4152     if ((viewport.getSelectionGroup() != null)
4153             && (viewport.getSelectionGroup().getSize() > 1))
4154     {
4155       // JBPNote UGLY! To prettify, make SequenceGroup and Alignment conform to
4156       // some common interface!
4157       /*
4158        * SequenceGroup seqs = viewport.getSelectionGroup(); int sz; msa = new
4159        * SequenceI[sz = seqs.getSize(false)];
4160        * 
4161        * for (int i = 0; i < sz; i++) { msa[i] = (SequenceI)
4162        * seqs.getSequenceAt(i); }
4163        */
4164       msa = viewport.getAlignmentView(true);
4165     }
4166     else if (viewport.getSelectionGroup() != null
4167             && viewport.getSelectionGroup().getSize() == 1)
4168     {
4169       int option = JvOptionPane.showConfirmDialog(this,
4170               MessageManager.getString("warn.oneseq_msainput_selection"),
4171               MessageManager.getString("label.invalid_selection"),
4172               JvOptionPane.OK_CANCEL_OPTION);
4173       if (option == JvOptionPane.OK_OPTION)
4174       {
4175         msa = viewport.getAlignmentView(false);
4176       }
4177     }
4178     else
4179     {
4180       msa = viewport.getAlignmentView(false);
4181     }
4182     return msa;
4183   }
4184
4185   /**
4186    * Decides what is submitted to a secondary structure prediction service: the
4187    * first sequence in the alignment, or in the current selection, or, if the
4188    * alignment is 'aligned' (ie padded with gaps), then the currently selected
4189    * region or the whole alignment. (where the first sequence in the set is the
4190    * one that the prediction will be for).
4191    */
4192   public AlignmentView gatherSeqOrMsaForSecStrPrediction()
4193   {
4194     AlignmentView seqs = null;
4195
4196     if ((viewport.getSelectionGroup() != null)
4197             && (viewport.getSelectionGroup().getSize() > 0))
4198     {
4199       seqs = viewport.getAlignmentView(true);
4200     }
4201     else
4202     {
4203       seqs = viewport.getAlignmentView(false);
4204     }
4205     // limit sequences - JBPNote in future - could spawn multiple prediction
4206     // jobs
4207     // TODO: viewport.getAlignment().isAligned is a global state - the local
4208     // selection may well be aligned - we preserve 2.0.8 behaviour for moment.
4209     if (!viewport.getAlignment().isAligned(false))
4210     {
4211       seqs.setSequences(new SeqCigar[] { seqs.getSequences()[0] });
4212       // TODO: if seqs.getSequences().length>1 then should really have warned
4213       // user!
4214
4215     }
4216     return seqs;
4217   }
4218
4219   /**
4220    * DOCUMENT ME!
4221    * 
4222    * @param e
4223    *          DOCUMENT ME!
4224    */
4225   @Override
4226   protected void loadTreeMenuItem_actionPerformed(ActionEvent e)
4227   {
4228     // Pick the tree file
4229     JalviewFileChooser chooser = new JalviewFileChooser(
4230             Cache.getProperty("LAST_DIRECTORY"));
4231     chooser.setFileView(new JalviewFileView());
4232     chooser.setDialogTitle(
4233             MessageManager.getString("label.select_newick_like_tree_file"));
4234     chooser.setToolTipText(
4235             MessageManager.getString("label.load_tree_file"));
4236
4237     chooser.setResponseHandler(0, () -> {
4238       String filePath = chooser.getSelectedFile().getPath();
4239       Cache.setProperty("LAST_DIRECTORY", filePath);
4240       NewickFile fin = null;
4241       try
4242       {
4243         fin = new NewickFile(new FileParse(chooser.getSelectedFile(),
4244                 DataSourceType.FILE));
4245         viewport.setCurrentTree(showNewickTree(fin, filePath).getTree());
4246       } catch (Exception ex)
4247       {
4248         JvOptionPane.showMessageDialog(Desktop.desktop, ex.getMessage(),
4249                 MessageManager.getString("label.problem_reading_tree_file"),
4250                 JvOptionPane.WARNING_MESSAGE);
4251         ex.printStackTrace();
4252       }
4253       if (fin != null && fin.hasWarningMessage())
4254       {
4255         JvOptionPane.showMessageDialog(Desktop.desktop,
4256                 fin.getWarningMessage(),
4257                 MessageManager
4258                         .getString("label.possible_problem_with_tree_file"),
4259                 JvOptionPane.WARNING_MESSAGE);
4260       }
4261     });
4262     chooser.showOpenDialog(this);
4263   }
4264
4265   public TreePanel showNewickTree(NewickFile nf, String treeTitle)
4266   {
4267     return showNewickTree(nf, treeTitle, 600, 500, 4, 5);
4268   }
4269
4270   public TreePanel showNewickTree(NewickFile nf, String treeTitle, int w,
4271           int h, int x, int y)
4272   {
4273     return showNewickTree(nf, treeTitle, null, w, h, x, y);
4274   }
4275
4276   /**
4277    * Add a treeviewer for the tree extracted from a Newick file object to the
4278    * current alignment view
4279    * 
4280    * @param nf
4281    *          the tree
4282    * @param title
4283    *          tree viewer title
4284    * @param input
4285    *          Associated alignment input data (or null)
4286    * @param w
4287    *          width
4288    * @param h
4289    *          height
4290    * @param x
4291    *          position
4292    * @param y
4293    *          position
4294    * @return TreePanel handle
4295    */
4296   public TreePanel showNewickTree(NewickFile nf, String treeTitle,
4297           AlignmentView input, int w, int h, int x, int y)
4298   {
4299     TreePanel tp = null;
4300
4301     try
4302     {
4303       nf.parse();
4304
4305       if (nf.getTree() != null)
4306       {
4307         tp = new TreePanel(alignPanel, nf, treeTitle, input);
4308
4309         tp.setSize(w, h);
4310
4311         if (x > 0 && y > 0)
4312         {
4313           tp.setLocation(x, y);
4314         }
4315
4316         Desktop.addInternalFrame(tp, treeTitle, w, h);
4317       }
4318     } catch (Exception ex)
4319     {
4320       ex.printStackTrace();
4321     }
4322
4323     return tp;
4324   }
4325
4326   public void showContactMapTree(AlignmentAnnotation aa, ContactMatrixI cm)
4327   {
4328     int x = 4, y = 5;
4329     int w = 400, h = 500;
4330
4331     try
4332     {
4333       NewickFile fin = new NewickFile(
4334               new FileParse(cm.getNewick(), DataSourceType.PASTE));
4335       String title = aa.label + " " + cm.getTreeMethod() + " tree"
4336               + (aa.sequenceRef != null
4337                       ? (" for " + aa.sequenceRef.getDisplayId(false))
4338                       : "");
4339
4340       showColumnWiseTree(fin, aa, title, w, h, x, y);
4341     } catch (Throwable xx)
4342     {
4343       Console.error("Unexpected exception showing tree for contact matrix",
4344               xx);
4345     }
4346   }
4347
4348   public TreePanel showColumnWiseTree(NewickFile nf, AlignmentAnnotation aa,
4349           String treeTitle, int w, int h, int x, int y)
4350   {
4351     try
4352     {
4353       nf.parse();
4354       if (nf.getTree() == null)
4355       {
4356         return null;
4357       }
4358       TreePanel tp = new TreePanel(alignPanel, nf, aa, treeTitle);
4359
4360       tp.setSize(w, h);
4361
4362       if (x > 0 && y > 0)
4363       {
4364         tp.setLocation(x, y);
4365       }
4366
4367       Desktop.addInternalFrame(tp, treeTitle, w, h);
4368       return tp;
4369     } catch (Throwable xx)
4370     {
4371       Console.error("Unexpected exception showing tree for contact matrix",
4372               xx);
4373     }
4374     return null;
4375   }
4376
4377   private boolean buildingMenu = false;
4378
4379   /**
4380    * Generates menu items and listener event actions for web service clients
4381    * 
4382    */
4383   public void BuildWebServiceMenu()
4384   {
4385     while (buildingMenu)
4386     {
4387       try
4388       {
4389         jalview.bin.Console
4390                 .errPrintln("Waiting for building menu to finish.");
4391         Thread.sleep(10);
4392       } catch (Exception e)
4393       {
4394       }
4395     }
4396     final AlignFrame me = this;
4397     buildingMenu = true;
4398     new Thread(new Runnable()
4399     {
4400       @Override
4401       public void run()
4402       {
4403         final List<JMenuItem> legacyItems = new ArrayList<>();
4404         try
4405         {
4406           // jalview.bin.Console.errPrintln("Building ws menu again "
4407           // + Thread.currentThread());
4408           // TODO: add support for context dependent disabling of services based
4409           // on
4410           // alignment and current selection
4411           // TODO: add additional serviceHandle parameter to specify abstract
4412           // handler
4413           // class independently of AbstractName
4414           // TODO: add in rediscovery GUI function to restart discoverer
4415           // TODO: group services by location as well as function and/or
4416           // introduce
4417           // object broker mechanism.
4418           final Vector<JMenu> wsmenu = new Vector<>();
4419           final IProgressIndicator af = me;
4420
4421           /*
4422            * do not i18n these strings - they are hard-coded in class
4423            * compbio.data.msa.Category, Jws2Discoverer.isRecalculable() and
4424            * SequenceAnnotationWSClient.initSequenceAnnotationWSClient()
4425            */
4426           final JMenu msawsmenu = new JMenu("Alignment");
4427           final JMenu secstrmenu = new JMenu(
4428                   "Secondary Structure Prediction");
4429           final JMenu seqsrchmenu = new JMenu("Sequence Database Search");
4430           final JMenu analymenu = new JMenu("Analysis");
4431           final JMenu dismenu = new JMenu("Protein Disorder");
4432           // JAL-940 - only show secondary structure prediction services from
4433           // the legacy server
4434           if (// Cache.getDefault("SHOW_JWS1_SERVICES", true)
4435               // &&
4436           Discoverer.services != null && (Discoverer.services.size() > 0))
4437           {
4438             // TODO: refactor to allow list of AbstractName/Handler bindings to
4439             // be
4440             // stored or retrieved from elsewhere
4441             // No MSAWS used any more:
4442             // Vector msaws = null; // (Vector)
4443             // Discoverer.services.get("MsaWS");
4444             Vector<ServiceHandle> secstrpr = Discoverer.services
4445                     .get("SecStrPred");
4446             if (secstrpr != null)
4447             {
4448               // Add any secondary structure prediction services
4449               for (int i = 0, j = secstrpr.size(); i < j; i++)
4450               {
4451                 final ext.vamsas.ServiceHandle sh = secstrpr.get(i);
4452                 jalview.ws.WSMenuEntryProviderI impl = jalview.ws.jws1.Discoverer
4453                         .getServiceClient(sh);
4454                 int p = secstrmenu.getItemCount();
4455                 impl.attachWSMenuEntry(secstrmenu, me);
4456                 int q = secstrmenu.getItemCount();
4457                 for (int litm = p; litm < q; litm++)
4458                 {
4459                   legacyItems.add(secstrmenu.getItem(litm));
4460                 }
4461               }
4462             }
4463           }
4464
4465           // Add all submenus in the order they should appear on the web
4466           // services menu
4467           wsmenu.add(msawsmenu);
4468           wsmenu.add(secstrmenu);
4469           wsmenu.add(dismenu);
4470           wsmenu.add(analymenu);
4471           // No search services yet
4472           // wsmenu.add(seqsrchmenu);
4473
4474           javax.swing.SwingUtilities.invokeLater(new Runnable()
4475           {
4476             @Override
4477             public void run()
4478             {
4479               try
4480               {
4481                 webService.removeAll();
4482                 // first, add discovered services onto the webservices menu
4483                 if (wsmenu.size() > 0)
4484                 {
4485                   for (int i = 0, j = wsmenu.size(); i < j; i++)
4486                   {
4487                     webService.add(wsmenu.get(i));
4488                   }
4489                 }
4490                 else
4491                 {
4492                   webService.add(me.webServiceNoServices);
4493                 }
4494                 // TODO: move into separate menu builder class.
4495                 {
4496                   // logic for 2.11.1.4 is
4497                   // always look to see if there is a discover. if there isn't
4498                   // we can't show any Jws2 services
4499                   // if there are services available, show them - regardless of
4500                   // the 'show JWS2 preference'
4501                   // if the discoverer is running then say so
4502                   // otherwise offer to trigger discovery if 'show JWS2' is not
4503                   // enabled
4504                   Jws2Discoverer jws2servs = Jws2Discoverer.getDiscoverer();
4505                   if (jws2servs != null)
4506                   {
4507                     if (jws2servs.hasServices())
4508                     {
4509                       jws2servs.attachWSMenuEntry(webService, me);
4510                       for (Jws2Instance sv : jws2servs.getServices())
4511                       {
4512                         if (sv.description.toLowerCase(Locale.ROOT)
4513                                 .contains("jpred"))
4514                         {
4515                           for (JMenuItem jmi : legacyItems)
4516                           {
4517                             jmi.setVisible(false);
4518                           }
4519                         }
4520                       }
4521                     }
4522
4523                     if (jws2servs.isRunning())
4524                     {
4525                       JMenuItem tm = new JMenuItem(
4526                               "Still discovering JABA Services");
4527                       tm.setEnabled(false);
4528                       webService.add(tm);
4529                     }
4530                     else if (!Cache.getDefault("SHOW_JWS2_SERVICES", true))
4531                     {
4532                       JMenuItem enableJws2 = new JMenuItem(
4533                               "Discover Web Services");
4534                       enableJws2.setToolTipText(
4535                               "Select to start JABA Web Service discovery (or enable option in Web Service preferences)");
4536                       enableJws2.setEnabled(true);
4537                       enableJws2.addActionListener(new ActionListener()
4538                       {
4539
4540                         @Override
4541                         public void actionPerformed(ActionEvent e)
4542                         {
4543                           // start service discoverer, but ignore preference
4544                           Desktop.instance.startServiceDiscovery(false,
4545                                   true);
4546                         }
4547                       });
4548                       webService.add(enableJws2);
4549                     }
4550                   }
4551                 }
4552                 build_urlServiceMenu(me.webService);
4553                 build_fetchdbmenu(webService);
4554                 for (JMenu item : wsmenu)
4555                 {
4556                   if (item.getItemCount() == 0)
4557                   {
4558                     item.setEnabled(false);
4559                   }
4560                   else
4561                   {
4562                     item.setEnabled(true);
4563                   }
4564                 }
4565               } catch (Exception e)
4566               {
4567                 Console.debug(
4568                         "Exception during web service menu building process.",
4569                         e);
4570               }
4571             }
4572           });
4573         } catch (Exception e)
4574         {
4575         }
4576         buildingMenu = false;
4577       }
4578     }).start();
4579
4580   }
4581
4582   /**
4583    * construct any groupURL type service menu entries.
4584    * 
4585    * @param webService
4586    */
4587   protected void build_urlServiceMenu(JMenu webService)
4588   {
4589     // TODO: remove this code when 2.7 is released
4590     // DEBUG - alignmentView
4591     /*
4592      * JMenuItem testAlView = new JMenuItem("Test AlignmentView"); final
4593      * AlignFrame af = this; testAlView.addActionListener(new ActionListener() {
4594      * 
4595      * @Override public void actionPerformed(ActionEvent e) {
4596      * jalview.datamodel.AlignmentView
4597      * .testSelectionViews(af.viewport.getAlignment(),
4598      * af.viewport.getColumnSelection(), af.viewport.selectionGroup); }
4599      * 
4600      * }); webService.add(testAlView);
4601      */
4602     // TODO: refactor to RestClient discoverer and merge menu entries for
4603     // rest-style services with other types of analysis/calculation service
4604     // SHmmr test client - still being implemented.
4605     // DEBUG - alignmentView
4606
4607     for (jalview.ws.rest.RestClient client : jalview.ws.rest.RestClient
4608             .getRestClients())
4609     {
4610       client.attachWSMenuEntry(
4611               JvSwingUtils.findOrCreateMenu(webService, client.getAction()),
4612               this);
4613     }
4614   }
4615
4616   /**
4617    * Searches the alignment sequences for xRefs and builds the Show
4618    * Cross-References menu (formerly called Show Products), with database
4619    * sources for which cross-references are found (protein sources for a
4620    * nucleotide alignment and vice versa)
4621    * 
4622    * @return true if Show Cross-references menu should be enabled
4623    */
4624   public boolean canShowProducts()
4625   {
4626     SequenceI[] seqs = viewport.getAlignment().getSequencesArray();
4627     AlignmentI dataset = viewport.getAlignment().getDataset();
4628
4629     showProducts.removeAll();
4630     final boolean dna = viewport.getAlignment().isNucleotide();
4631
4632     if (seqs == null || seqs.length == 0)
4633     {
4634       // nothing to see here.
4635       return false;
4636     }
4637
4638     boolean showp = false;
4639     try
4640     {
4641       List<String> ptypes = new CrossRef(seqs, dataset)
4642               .findXrefSourcesForSequences(dna);
4643
4644       for (final String source : ptypes)
4645       {
4646         showp = true;
4647         final AlignFrame af = this;
4648         JMenuItem xtype = new JMenuItem(source);
4649         xtype.addActionListener(new ActionListener()
4650         {
4651           @Override
4652           public void actionPerformed(ActionEvent e)
4653           {
4654             showProductsFor(af.viewport.getSequenceSelection(), dna,
4655                     source);
4656           }
4657         });
4658         showProducts.add(xtype);
4659       }
4660       showProducts.setVisible(showp);
4661       showProducts.setEnabled(showp);
4662     } catch (Exception e)
4663     {
4664       Console.warn(
4665               "canShowProducts threw an exception - please report to help@jalview.org",
4666               e);
4667       return false;
4668     }
4669     return showp;
4670   }
4671
4672   /**
4673    * Finds and displays cross-references for the selected sequences (protein
4674    * products for nucleotide sequences, dna coding sequences for peptides).
4675    * 
4676    * @param sel
4677    *          the sequences to show cross-references for
4678    * @param dna
4679    *          true if from a nucleotide alignment (so showing proteins)
4680    * @param source
4681    *          the database to show cross-references for
4682    */
4683   protected void showProductsFor(final SequenceI[] sel, final boolean _odna,
4684           final String source)
4685   {
4686     new Thread(CrossRefAction.getHandlerFor(sel, _odna, source, this))
4687             .start();
4688   }
4689
4690   /**
4691    * Construct and display a new frame containing the translation of this
4692    * frame's DNA sequences to their aligned protein (amino acid) equivalents.
4693    */
4694   @Override
4695   public void showTranslation_actionPerformed(GeneticCodeI codeTable)
4696   {
4697     AlignmentI al = null;
4698     try
4699     {
4700       Dna dna = new Dna(viewport, viewport.getViewAsVisibleContigs(true));
4701
4702       al = dna.translateCdna(codeTable);
4703     } catch (Exception ex)
4704     {
4705       Console.error("Exception during translation. Please report this !",
4706               ex);
4707       final String msg = MessageManager.getString(
4708               "label.error_when_translating_sequences_submit_bug_report");
4709       final String errorTitle = MessageManager
4710               .getString("label.implementation_error")
4711               + MessageManager.getString("label.translation_failed");
4712       JvOptionPane.showMessageDialog(Desktop.desktop, msg, errorTitle,
4713               JvOptionPane.ERROR_MESSAGE);
4714       return;
4715     }
4716     if (al == null || al.getHeight() == 0)
4717     {
4718       final String msg = MessageManager.getString(
4719               "label.select_at_least_three_bases_in_at_least_one_sequence_to_cDNA_translation");
4720       final String errorTitle = MessageManager
4721               .getString("label.translation_failed");
4722       JvOptionPane.showMessageDialog(Desktop.desktop, msg, errorTitle,
4723               JvOptionPane.WARNING_MESSAGE);
4724     }
4725     else
4726     {
4727       AlignFrame af = new AlignFrame(al, DEFAULT_WIDTH, DEFAULT_HEIGHT);
4728       af.setFileFormat(this.currentFileFormat);
4729       final String newTitle = MessageManager
4730               .formatMessage("label.translation_of_params", new Object[]
4731               { this.getTitle(), codeTable.getId() });
4732       af.setTitle(newTitle);
4733       if (Cache.getDefault(Preferences.ENABLE_SPLIT_FRAME, true))
4734       {
4735         final SequenceI[] seqs = viewport.getSelectionAsNewSequence();
4736         viewport.openSplitFrame(af, new Alignment(seqs));
4737       }
4738       else
4739       {
4740         Desktop.addInternalFrame(af, newTitle, DEFAULT_WIDTH,
4741                 DEFAULT_HEIGHT);
4742       }
4743     }
4744   }
4745
4746   /**
4747    * Set the file format
4748    * 
4749    * @param format
4750    */
4751   public void setFileFormat(FileFormatI format)
4752   {
4753     this.currentFileFormat = format;
4754   }
4755
4756   /**
4757    * Try to load a features file onto the alignment.
4758    * 
4759    * @param file
4760    *          contents or path to retrieve file or a File object
4761    * @param sourceType
4762    *          access mode of file (see jalview.io.AlignFile)
4763    * @return true if features file was parsed correctly.
4764    */
4765   public boolean parseFeaturesFile(Object file, DataSourceType sourceType)
4766   {
4767     // BH 2018
4768     return avc.parseFeaturesFile(file, sourceType,
4769             Cache.getDefault("RELAXEDSEQIDMATCHING", false));
4770
4771   }
4772
4773   @Override
4774   public void refreshFeatureUI(boolean enableIfNecessary)
4775   {
4776     // note - currently this is only still here rather than in the controller
4777     // because of the featureSettings hard reference that is yet to be
4778     // abstracted
4779     if (enableIfNecessary)
4780     {
4781       viewport.setShowSequenceFeatures(true);
4782       showSeqFeatures.setSelected(true);
4783     }
4784
4785   }
4786
4787   @Override
4788   public void dragEnter(DropTargetDragEvent evt)
4789   {
4790   }
4791
4792   @Override
4793   public void dragExit(DropTargetEvent evt)
4794   {
4795   }
4796
4797   @Override
4798   public void dragOver(DropTargetDragEvent evt)
4799   {
4800   }
4801
4802   @Override
4803   public void dropActionChanged(DropTargetDragEvent evt)
4804   {
4805   }
4806
4807   @Override
4808   public void drop(DropTargetDropEvent evt)
4809   {
4810     // JAL-1552 - acceptDrop required before getTransferable call for
4811     // Java's Transferable for native dnd
4812     evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
4813     Transferable t = evt.getTransferable();
4814
4815     final AlignFrame thisaf = this;
4816     final List<Object> files = new ArrayList<>();
4817     List<DataSourceType> protocols = new ArrayList<>();
4818
4819     try
4820     {
4821       Desktop.transferFromDropTarget(files, protocols, evt, t);
4822     } catch (Exception e)
4823     {
4824       e.printStackTrace();
4825     }
4826     if (files != null)
4827     {
4828       new Thread(new Runnable()
4829       {
4830         @Override
4831         public void run()
4832         {
4833           try
4834           {
4835             // check to see if any of these files have names matching sequences
4836             // in
4837             // the alignment
4838             SequenceIdMatcher idm = new SequenceIdMatcher(
4839                     viewport.getAlignment().getSequencesArray());
4840             /**
4841              * Object[] { String,SequenceI}
4842              */
4843             ArrayList<Object[]> filesmatched = new ArrayList<>();
4844             ArrayList<Object[]> filesnotmatched = new ArrayList<>();
4845             for (int i = 0; i < files.size(); i++)
4846             {
4847               // BH 2018
4848               Object file = files.get(i);
4849               String fileName = file.toString();
4850               String pdbfn = "";
4851               DataSourceType protocol = (file instanceof File
4852                       ? DataSourceType.FILE
4853                       : FormatAdapter.checkProtocol(fileName));
4854               if (protocol == DataSourceType.FILE)
4855               {
4856                 File fl;
4857                 if (file instanceof File)
4858                 {
4859                   fl = (File) file;
4860                   Platform.cacheFileData(fl);
4861                 }
4862                 else
4863                 {
4864                   fl = new File(fileName);
4865                 }
4866                 pdbfn = fl.getName();
4867               }
4868               else if (protocol == DataSourceType.URL)
4869               {
4870                 URL url = new URL(fileName);
4871                 pdbfn = url.getFile();
4872               }
4873               if (pdbfn.length() > 0)
4874               {
4875                 // attempt to find a match in the alignment
4876                 SequenceI[] mtch = idm.findAllIdMatches(pdbfn);
4877                 int l = 0, c = pdbfn.indexOf(".");
4878                 while (mtch == null && c != -1)
4879                 {
4880                   do
4881                   {
4882                     l = c;
4883                   } while ((c = pdbfn.indexOf(".", l)) > l);
4884                   if (l > -1)
4885                   {
4886                     pdbfn = pdbfn.substring(0, l);
4887                   }
4888                   mtch = idm.findAllIdMatches(pdbfn);
4889                 }
4890                 FileFormatI type = null;
4891                 if (mtch != null)
4892                 {
4893                   try
4894                   {
4895                     type = new IdentifyFile().identify(file, protocol);
4896                   } catch (Exception ex)
4897                   {
4898                     type = null;
4899                   }
4900                   if (type != null && type.isStructureFile())
4901                   {
4902                     filesmatched.add(new Object[] { file, protocol, mtch });
4903                     continue;
4904                   }
4905                 }
4906                 // File wasn't named like one of the sequences or wasn't a PDB
4907                 // file.
4908                 filesnotmatched.add(new Object[] { file, protocol, type });
4909               }
4910             }
4911             int assocfiles = 0;
4912             if (filesmatched.size() > 0)
4913             {
4914               boolean autoAssociate = Cache
4915                       .getDefault("AUTOASSOCIATE_PDBANDSEQS", false);
4916               if (!autoAssociate)
4917               {
4918                 String msg = MessageManager.formatMessage(
4919                         "label.automatically_associate_structure_files_with_sequences_same_name",
4920                         new Object[]
4921                         { Integer.valueOf(filesmatched.size())
4922                                 .toString() });
4923                 String ttl = MessageManager.getString(
4924                         "label.automatically_associate_structure_files_by_name");
4925                 int choice = JvOptionPane.showConfirmDialog(thisaf, msg,
4926                         ttl, JvOptionPane.YES_NO_OPTION);
4927                 autoAssociate = choice == JvOptionPane.YES_OPTION;
4928               }
4929               if (autoAssociate)
4930               {
4931                 for (Object[] fm : filesmatched)
4932                 {
4933                   // try and associate
4934                   // TODO: may want to set a standard ID naming formalism for
4935                   // associating PDB files which have no IDs.
4936                   for (SequenceI toassoc : (SequenceI[]) fm[2])
4937                   {
4938                     PDBEntry pe = new AssociatePdbFileWithSeq()
4939                             .associatePdbWithSeq(fm[0].toString(),
4940                                     (DataSourceType) fm[1], toassoc, false,
4941                                     Desktop.instance);
4942                     if (pe != null)
4943                     {
4944                       jalview.bin.Console.errPrintln("Associated file : "
4945                               + (fm[0].toString()) + " with "
4946                               + toassoc.getDisplayId(true));
4947                       assocfiles++;
4948                     }
4949                   }
4950                   // TODO: do we need to update overview ? only if features are
4951                   // shown I guess
4952                   alignPanel.paintAlignment(true, false);
4953                 }
4954               }
4955               else
4956               {
4957                 /*
4958                  * add declined structures as sequences
4959                  */
4960                 for (Object[] o : filesmatched)
4961                 {
4962                   filesnotmatched.add(new Object[] { o[0], o[1] });
4963                 }
4964               }
4965             }
4966             if (filesnotmatched.size() > 0)
4967             {
4968               if (assocfiles > 0 && (Cache.getDefault(
4969                       "AUTOASSOCIATE_PDBANDSEQS_IGNOREOTHERS", false)
4970                       || JvOptionPane.showConfirmDialog(thisaf,
4971                               "<html>" + MessageManager.formatMessage(
4972                                       "label.ignore_unmatched_dropped_files_info",
4973                                       new Object[]
4974                                       { Integer.valueOf(
4975                                               filesnotmatched.size())
4976                                               .toString() })
4977                                       + "</html>",
4978                               MessageManager.getString(
4979                                       "label.ignore_unmatched_dropped_files"),
4980                               JvOptionPane.YES_NO_OPTION) == JvOptionPane.YES_OPTION))
4981               {
4982                 return;
4983               }
4984               for (Object[] fn : filesnotmatched)
4985               {
4986                 loadJalviewDataFile(fn[0], (DataSourceType) fn[1],
4987                         (FileFormatI) fn[2], null);
4988               }
4989
4990             }
4991           } catch (Exception ex)
4992           {
4993             ex.printStackTrace();
4994           }
4995         }
4996       }).start();
4997     }
4998   }
4999
5000   /**
5001    * Attempt to load a "dropped" file or URL string, by testing in turn for
5002    * <ul>
5003    * <li>an Annotation file</li>
5004    * <li>a JNet file</li>
5005    * <li>a features file</li>
5006    * <li>else try to interpret as an alignment file</li>
5007    * </ul>
5008    * 
5009    * @param file
5010    *          either a filename or a URL string.
5011    */
5012   public void loadJalviewDataFile(Object file, DataSourceType sourceType,
5013           FileFormatI format, SequenceI assocSeq)
5014   {
5015     // BH 2018 was String file
5016     try
5017     {
5018       if (sourceType == null)
5019       {
5020         sourceType = FormatAdapter.checkProtocol(file);
5021       }
5022       // if the file isn't identified, or not positively identified as some
5023       // other filetype (PFAM is default unidentified alignment file type) then
5024       // try to parse as annotation.
5025       boolean isAnnotation = (format == null
5026               || FileFormat.Pfam.equals(format))
5027                       ? new AnnotationFile().annotateAlignmentView(viewport,
5028                               file, sourceType)
5029                       : false;
5030
5031       if (!isAnnotation)
5032       {
5033         // first see if its a T-COFFEE score file
5034         TCoffeeScoreFile tcf = null;
5035         try
5036         {
5037           tcf = new TCoffeeScoreFile(file, sourceType);
5038           if (tcf.isValid())
5039           {
5040             if (tcf.annotateAlignment(viewport.getAlignment(), true))
5041             {
5042               buildColourMenu();
5043               changeColour(
5044                       new TCoffeeColourScheme(viewport.getAlignment()));
5045               isAnnotation = true;
5046               setStatus(MessageManager.getString(
5047                       "label.successfully_pasted_tcoffee_scores_to_alignment"));
5048             }
5049             else
5050             {
5051               // some problem - if no warning its probable that the ID matching
5052               // process didn't work
5053               JvOptionPane.showMessageDialog(Desktop.desktop,
5054                       tcf.getWarningMessage() == null
5055                               ? MessageManager.getString(
5056                                       "label.check_file_matches_sequence_ids_alignment")
5057                               : tcf.getWarningMessage(),
5058                       MessageManager.getString(
5059                               "label.problem_reading_tcoffee_score_file"),
5060                       JvOptionPane.WARNING_MESSAGE);
5061             }
5062           }
5063           else
5064           {
5065             tcf = null;
5066           }
5067         } catch (Exception x)
5068         {
5069           Console.debug(
5070                   "Exception when processing data source as T-COFFEE score file",
5071                   x);
5072           tcf = null;
5073         }
5074         if (tcf == null)
5075         {
5076           // try to see if its a JNet 'concise' style annotation file *before*
5077           // we
5078           // try to parse it as a features file
5079           if (format == null)
5080           {
5081             format = new IdentifyFile().identify(file, sourceType);
5082           }
5083           if (FileFormat.FeatureSettings == format)
5084           {
5085             if (featureSettings != null)
5086             {
5087               featureSettings.load(file, sourceType);
5088             }
5089             else
5090             {
5091               FeatureSettings.loadFeatureSettingsFile(getFeatureRenderer(),
5092                       fileObject, sourceType);
5093             }
5094           }
5095           else if (FileFormat.ScoreMatrix == format)
5096           {
5097             ScoreMatrixFile sm = new ScoreMatrixFile(
5098                     new FileParse(file, sourceType));
5099             sm.parse();
5100             // todo: i18n this message
5101             setStatus(MessageManager.formatMessage(
5102                     "label.successfully_loaded_matrix",
5103                     sm.getMatrixName()));
5104           }
5105           else if (FileFormat.Jnet.equals(format))
5106           {
5107             JPredFile predictions = new JPredFile(file, sourceType);
5108             new JnetAnnotationMaker();
5109             JnetAnnotationMaker.add_annotation(predictions,
5110                     viewport.getAlignment(), 0, false);
5111             viewport.getAlignment().setupJPredAlignment();
5112             isAnnotation = true;
5113           }
5114           // else if (IdentifyFile.FeaturesFile.equals(format))
5115           else if (FileFormat.Features.equals(format))
5116           {
5117             if (parseFeaturesFile(file, sourceType))
5118             {
5119               SplitFrame splitFrame = (SplitFrame) getSplitViewContainer();
5120               if (splitFrame != null)
5121               {
5122                 splitFrame.repaint();
5123               }
5124               else
5125               {
5126                 alignPanel.paintAlignment(true, true);
5127               }
5128             }
5129           }
5130           else
5131           {
5132             new FileLoader().LoadFile(viewport, file, sourceType, format);
5133           }
5134         }
5135       }
5136       if (isAnnotation)
5137       {
5138
5139         alignPanel.adjustAnnotationHeight();
5140         viewport.updateSequenceIdColours();
5141         buildSortByAnnotationScoresMenu();
5142         alignPanel.paintAlignment(true, true);
5143       }
5144     } catch (Exception ex)
5145     {
5146       ex.printStackTrace();
5147     } catch (OutOfMemoryError oom)
5148     {
5149       try
5150       {
5151         System.gc();
5152       } catch (Exception x)
5153       {
5154       }
5155       new OOMWarning(
5156               "loading data "
5157                       + (sourceType != null
5158                               ? (sourceType == DataSourceType.PASTE
5159                                       ? "from clipboard."
5160                                       : "using " + sourceType + " from "
5161                                               + file)
5162                               : ".")
5163                       + (format != null
5164                               ? "(parsing as '" + format + "' file)"
5165                               : ""),
5166               oom, Desktop.desktop);
5167     }
5168   }
5169
5170   /**
5171    * Method invoked by the ChangeListener on the tabbed pane, in other words
5172    * when a different tabbed pane is selected by the user or programmatically.
5173    */
5174   @Override
5175   public void tabSelectionChanged(int index)
5176   {
5177     if (index > -1)
5178     {
5179       /*
5180        * update current Overview window title (if there is one)
5181        * to add view name "Original" if necessary
5182        */
5183       alignPanel.setOverviewTitle(this);
5184
5185       /*
5186        * switch panels and set Overview title (if there is one
5187        * because it was opened automatically)
5188        */
5189       alignPanel = alignPanels.get(index);
5190       alignPanel.setOverviewTitle(this);
5191
5192       viewport = alignPanel.av;
5193       avc.setViewportAndAlignmentPanel(viewport, alignPanel);
5194       setMenusFromViewport(viewport);
5195       if (featureSettings != null && featureSettings.isOpen()
5196               && featureSettings.fr.getViewport() != viewport)
5197       {
5198         if (viewport.isShowSequenceFeatures())
5199         {
5200           // refresh the featureSettings to reflect UI change
5201           showFeatureSettingsUI();
5202         }
5203         else
5204         {
5205           // close feature settings for this view.
5206           featureSettings.close();
5207         }
5208       }
5209
5210     }
5211
5212     /*
5213      * 'focus' any colour slider that is open to the selected viewport
5214      */
5215     if (viewport.getConservationSelected())
5216     {
5217       SliderPanel.setConservationSlider(alignPanel,
5218               viewport.getResidueShading(), alignPanel.getViewName());
5219     }
5220     else
5221     {
5222       SliderPanel.hideConservationSlider();
5223     }
5224     if (viewport.getAbovePIDThreshold())
5225     {
5226       SliderPanel.setPIDSliderSource(alignPanel,
5227               viewport.getResidueShading(), alignPanel.getViewName());
5228     }
5229     else
5230     {
5231       SliderPanel.hidePIDSlider();
5232     }
5233
5234     /*
5235      * If there is a frame linked to this one in a SplitPane, switch it to the
5236      * same view tab index. No infinite recursion of calls should happen, since
5237      * tabSelectionChanged() should not get invoked on setting the selected
5238      * index to an unchanged value. Guard against setting an invalid index
5239      * before the new view peer tab has been created.
5240      */
5241     final AlignViewportI peer = viewport.getCodingComplement();
5242     if (peer != null)
5243     {
5244       AlignFrame linkedAlignFrame = ((AlignViewport) peer)
5245               .getAlignPanel().alignFrame;
5246       if (linkedAlignFrame.tabbedPane.getTabCount() > index)
5247       {
5248         linkedAlignFrame.tabbedPane.setSelectedIndex(index);
5249       }
5250     }
5251   }
5252
5253   /**
5254    * On right mouse click on view tab, prompt for and set new view name.
5255    */
5256   @Override
5257   public void tabbedPane_mousePressed(MouseEvent e)
5258   {
5259     if (e.isPopupTrigger())
5260     {
5261       String msg = MessageManager.getString("label.enter_view_name");
5262       String ttl = tabbedPane.getTitleAt(tabbedPane.getSelectedIndex());
5263       String reply = JvOptionPane.showInputDialog(msg, ttl);
5264
5265       if (reply != null)
5266       {
5267         viewport.setViewName(reply);
5268         // TODO warn if reply is in getExistingViewNames()?
5269         tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(), reply);
5270       }
5271     }
5272   }
5273
5274   public AlignViewport getCurrentView()
5275   {
5276     return viewport;
5277   }
5278
5279   /**
5280    * Open the dialog for regex description parsing.
5281    */
5282   @Override
5283   protected void extractScores_actionPerformed(ActionEvent e)
5284   {
5285     ParseProperties pp = new jalview.analysis.ParseProperties(
5286             viewport.getAlignment());
5287     // TODO: verify regex and introduce GUI dialog for version 2.5
5288     // if (pp.getScoresFromDescription("col", "score column ",
5289     // "\\W*([-+]?\\d*\\.?\\d*e?-?\\d*)\\W+([-+]?\\d*\\.?\\d*e?-?\\d*)",
5290     // true)>0)
5291     if (pp.getScoresFromDescription("description column",
5292             "score in description column ", "\\W*([-+eE0-9.]+)", true) > 0)
5293     {
5294       buildSortByAnnotationScoresMenu();
5295     }
5296   }
5297
5298   /*
5299    * (non-Javadoc)
5300    * 
5301    * @see
5302    * jalview.jbgui.GAlignFrame#showDbRefs_actionPerformed(java.awt.event.ActionEvent
5303    * )
5304    */
5305   @Override
5306   protected void showDbRefs_actionPerformed(ActionEvent e)
5307   {
5308     viewport.setShowDBRefs(showDbRefsMenuitem.isSelected());
5309   }
5310
5311   /*
5312    * (non-Javadoc)
5313    * 
5314    * @seejalview.jbgui.GAlignFrame#showNpFeats_actionPerformed(java.awt.event.
5315    * ActionEvent)
5316    */
5317   @Override
5318   protected void showNpFeats_actionPerformed(ActionEvent e)
5319   {
5320     viewport.setShowNPFeats(showNpFeatsMenuitem.isSelected());
5321   }
5322
5323   /**
5324    * find the viewport amongst the tabs in this alignment frame and close that
5325    * tab
5326    * 
5327    * @param av
5328    */
5329   public boolean closeView(AlignViewportI av)
5330   {
5331     if (viewport == av)
5332     {
5333       this.closeMenuItem_actionPerformed(false);
5334       return true;
5335     }
5336     Component[] comp = tabbedPane.getComponents();
5337     for (int i = 0; comp != null && i < comp.length; i++)
5338     {
5339       if (comp[i] instanceof AlignmentPanel)
5340       {
5341         if (((AlignmentPanel) comp[i]).av == av)
5342         {
5343           // close the view.
5344           closeView((AlignmentPanel) comp[i]);
5345           return true;
5346         }
5347       }
5348     }
5349     return false;
5350   }
5351
5352   protected void build_fetchdbmenu(JMenu webService)
5353   {
5354     // Temporary hack - DBRef Fetcher always top level ws entry.
5355     // TODO We probably want to store a sequence database checklist in
5356     // preferences and have checkboxes.. rather than individual sources selected
5357     // here
5358     final JMenu rfetch = new JMenu(
5359             MessageManager.getString("action.fetch_db_references"));
5360     rfetch.setToolTipText(MessageManager.getString(
5361             "label.retrieve_parse_sequence_database_records_alignment_or_selected_sequences"));
5362     webService.add(rfetch);
5363
5364     final JCheckBoxMenuItem trimrs = new JCheckBoxMenuItem(
5365             MessageManager.getString("option.trim_retrieved_seqs"));
5366     trimrs.setToolTipText(
5367             MessageManager.getString("label.trim_retrieved_sequences"));
5368     trimrs.setSelected(
5369             Cache.getDefault(DBRefFetcher.TRIM_RETRIEVED_SEQUENCES, true));
5370     trimrs.addActionListener(new ActionListener()
5371     {
5372       @Override
5373       public void actionPerformed(ActionEvent e)
5374       {
5375         trimrs.setSelected(trimrs.isSelected());
5376         Cache.setProperty(DBRefFetcher.TRIM_RETRIEVED_SEQUENCES,
5377                 Boolean.valueOf(trimrs.isSelected()).toString());
5378       }
5379     });
5380     rfetch.add(trimrs);
5381     JMenuItem fetchr = new JMenuItem(
5382             MessageManager.getString("label.standard_databases"));
5383     fetchr.setToolTipText(
5384             MessageManager.getString("label.fetch_embl_uniprot"));
5385     fetchr.addActionListener(new ActionListener()
5386     {
5387
5388       @Override
5389       public void actionPerformed(ActionEvent e)
5390       {
5391         new Thread(new Runnable()
5392         {
5393           @Override
5394           public void run()
5395           {
5396             boolean isNucleotide = alignPanel.alignFrame.getViewport()
5397                     .getAlignment().isNucleotide();
5398             DBRefFetcher dbRefFetcher = new DBRefFetcher(
5399                     alignPanel.av.getSequenceSelection(),
5400                     alignPanel.alignFrame, null,
5401                     alignPanel.alignFrame.featureSettings, isNucleotide);
5402             dbRefFetcher.addListener(new FetchFinishedListenerI()
5403             {
5404               @Override
5405               public void finished()
5406               {
5407
5408                 for (FeatureSettingsModelI srcSettings : dbRefFetcher
5409                         .getFeatureSettingsModels())
5410                 {
5411
5412                   alignPanel.av.mergeFeaturesStyle(srcSettings);
5413                 }
5414                 AlignFrame.this.setMenusForViewport();
5415               }
5416             });
5417             dbRefFetcher.fetchDBRefs(false);
5418           }
5419         }).start();
5420
5421       }
5422
5423     });
5424     rfetch.add(fetchr);
5425     new Thread(new Runnable()
5426     {
5427       @Override
5428       public void run()
5429       {
5430         final jalview.ws.SequenceFetcher sf = jalview.gui.SequenceFetcher
5431                 .getSequenceFetcherSingleton();
5432         javax.swing.SwingUtilities.invokeLater(new Runnable()
5433         {
5434           @Override
5435           public void run()
5436           {
5437             String[] dbclasses = sf.getNonAlignmentSources();
5438             List<DbSourceProxy> otherdb;
5439             JMenu dfetch = new JMenu();
5440             JMenu ifetch = new JMenu();
5441             JMenuItem fetchr = null;
5442             int comp = 0, icomp = 0, mcomp = 15;
5443             String mname = null;
5444             int dbi = 0;
5445             for (String dbclass : dbclasses)
5446             {
5447               otherdb = sf.getSourceProxy(dbclass);
5448               // add a single entry for this class, or submenu allowing 'fetch
5449               // all' or pick one
5450               if (otherdb == null || otherdb.size() < 1)
5451               {
5452                 continue;
5453               }
5454               if (mname == null)
5455               {
5456                 mname = "From " + dbclass;
5457               }
5458               if (otherdb.size() == 1)
5459               {
5460                 final DbSourceProxy[] dassource = otherdb
5461                         .toArray(new DbSourceProxy[0]);
5462                 DbSourceProxy src = otherdb.get(0);
5463                 fetchr = new JMenuItem(src.getDbSource());
5464                 fetchr.addActionListener(new ActionListener()
5465                 {
5466
5467                   @Override
5468                   public void actionPerformed(ActionEvent e)
5469                   {
5470                     new Thread(new Runnable()
5471                     {
5472
5473                       @Override
5474                       public void run()
5475                       {
5476                         boolean isNucleotide = alignPanel.alignFrame
5477                                 .getViewport().getAlignment()
5478                                 .isNucleotide();
5479                         DBRefFetcher dbRefFetcher = new DBRefFetcher(
5480                                 alignPanel.av.getSequenceSelection(),
5481                                 alignPanel.alignFrame, dassource,
5482                                 alignPanel.alignFrame.featureSettings,
5483                                 isNucleotide);
5484                         dbRefFetcher
5485                                 .addListener(new FetchFinishedListenerI()
5486                                 {
5487                                   @Override
5488                                   public void finished()
5489                                   {
5490                                     FeatureSettingsModelI srcSettings = dassource[0]
5491                                             .getFeatureColourScheme();
5492                                     alignPanel.av.mergeFeaturesStyle(
5493                                             srcSettings);
5494                                     AlignFrame.this.setMenusForViewport();
5495                                   }
5496                                 });
5497                         dbRefFetcher.fetchDBRefs(false);
5498                       }
5499                     }).start();
5500                   }
5501
5502                 });
5503                 fetchr.setToolTipText(JvSwingUtils.wrapTooltip(true,
5504                         MessageManager.formatMessage(
5505                                 "label.fetch_retrieve_from", new Object[]
5506                                 { src.getDbName() })));
5507                 dfetch.add(fetchr);
5508                 comp++;
5509               }
5510               else
5511               {
5512                 final DbSourceProxy[] dassource = otherdb
5513                         .toArray(new DbSourceProxy[0]);
5514                 // fetch all entry
5515                 DbSourceProxy src = otherdb.get(0);
5516                 fetchr = new JMenuItem(MessageManager
5517                         .formatMessage("label.fetch_all_param", new Object[]
5518                         { src.getDbSource() }));
5519                 fetchr.addActionListener(new ActionListener()
5520                 {
5521                   @Override
5522                   public void actionPerformed(ActionEvent e)
5523                   {
5524                     new Thread(new Runnable()
5525                     {
5526
5527                       @Override
5528                       public void run()
5529                       {
5530                         boolean isNucleotide = alignPanel.alignFrame
5531                                 .getViewport().getAlignment()
5532                                 .isNucleotide();
5533                         DBRefFetcher dbRefFetcher = new DBRefFetcher(
5534                                 alignPanel.av.getSequenceSelection(),
5535                                 alignPanel.alignFrame, dassource,
5536                                 alignPanel.alignFrame.featureSettings,
5537                                 isNucleotide);
5538                         dbRefFetcher
5539                                 .addListener(new FetchFinishedListenerI()
5540                                 {
5541                                   @Override
5542                                   public void finished()
5543                                   {
5544                                     AlignFrame.this.setMenusForViewport();
5545                                   }
5546                                 });
5547                         dbRefFetcher.fetchDBRefs(false);
5548                       }
5549                     }).start();
5550                   }
5551                 });
5552
5553                 fetchr.setToolTipText(JvSwingUtils.wrapTooltip(true,
5554                         MessageManager.formatMessage(
5555                                 "label.fetch_retrieve_from_all_sources",
5556                                 new Object[]
5557                                 { Integer.valueOf(otherdb.size())
5558                                         .toString(),
5559                                     src.getDbSource(), src.getDbName() })));
5560                 dfetch.add(fetchr);
5561                 comp++;
5562                 // and then build the rest of the individual menus
5563                 ifetch = new JMenu(MessageManager.formatMessage(
5564                         "label.source_from_db_source", new Object[]
5565                         { src.getDbSource() }));
5566                 icomp = 0;
5567                 String imname = null;
5568                 int i = 0;
5569                 for (DbSourceProxy sproxy : otherdb)
5570                 {
5571                   String dbname = sproxy.getDbName();
5572                   String sname = dbname.length() > 5
5573                           ? dbname.substring(0, 5) + "..."
5574                           : dbname;
5575                   String msname = dbname.length() > 10
5576                           ? dbname.substring(0, 10) + "..."
5577                           : dbname;
5578                   if (imname == null)
5579                   {
5580                     imname = MessageManager
5581                             .formatMessage("label.from_msname", new Object[]
5582                             { sname });
5583                   }
5584                   fetchr = new JMenuItem(msname);
5585                   final DbSourceProxy[] dassrc = { sproxy };
5586                   fetchr.addActionListener(new ActionListener()
5587                   {
5588
5589                     @Override
5590                     public void actionPerformed(ActionEvent e)
5591                     {
5592                       new Thread(new Runnable()
5593                       {
5594
5595                         @Override
5596                         public void run()
5597                         {
5598                           boolean isNucleotide = alignPanel.alignFrame
5599                                   .getViewport().getAlignment()
5600                                   .isNucleotide();
5601                           DBRefFetcher dbRefFetcher = new DBRefFetcher(
5602                                   alignPanel.av.getSequenceSelection(),
5603                                   alignPanel.alignFrame, dassrc,
5604                                   alignPanel.alignFrame.featureSettings,
5605                                   isNucleotide);
5606                           dbRefFetcher
5607                                   .addListener(new FetchFinishedListenerI()
5608                                   {
5609                                     @Override
5610                                     public void finished()
5611                                     {
5612                                       AlignFrame.this.setMenusForViewport();
5613                                     }
5614                                   });
5615                           dbRefFetcher.fetchDBRefs(false);
5616                         }
5617                       }).start();
5618                     }
5619
5620                   });
5621                   fetchr.setToolTipText(
5622                           "<html>" + MessageManager.formatMessage(
5623                                   "label.fetch_retrieve_from", new Object[]
5624                                   { dbname }));
5625                   ifetch.add(fetchr);
5626                   ++i;
5627                   if (++icomp >= mcomp || i == (otherdb.size()))
5628                   {
5629                     ifetch.setText(MessageManager.formatMessage(
5630                             "label.source_to_target", imname, sname));
5631                     dfetch.add(ifetch);
5632                     ifetch = new JMenu();
5633                     imname = null;
5634                     icomp = 0;
5635                     comp++;
5636                   }
5637                 }
5638               }
5639               ++dbi;
5640               if (comp >= mcomp || dbi >= (dbclasses.length))
5641               {
5642                 dfetch.setText(MessageManager.formatMessage(
5643                         "label.source_to_target", mname, dbclass));
5644                 rfetch.add(dfetch);
5645                 dfetch = new JMenu();
5646                 mname = null;
5647                 comp = 0;
5648               }
5649             }
5650           }
5651         });
5652       }
5653     }).start();
5654
5655   }
5656
5657   /**
5658    * Left justify the whole alignment.
5659    */
5660   @Override
5661   protected void justifyLeftMenuItem_actionPerformed(ActionEvent e)
5662   {
5663     avc.justify_Region(true);
5664   }
5665
5666   /**
5667    * Right justify the whole alignment.
5668    */
5669   @Override
5670   protected void justifyRightMenuItem_actionPerformed(ActionEvent e)
5671   {
5672     avc.justify_Region(false);
5673   }
5674
5675   @Override
5676   public void setShowSeqFeatures(boolean b)
5677   {
5678     showSeqFeatures.setSelected(b);
5679     viewport.setShowSequenceFeatures(b);
5680   }
5681
5682   /*
5683    * (non-Javadoc)
5684    * 
5685    * @see
5686    * jalview.jbgui.GAlignFrame#showUnconservedMenuItem_actionPerformed(java.
5687    * awt.event.ActionEvent)
5688    */
5689   @Override
5690   protected void showUnconservedMenuItem_actionPerformed(ActionEvent e)
5691   {
5692     viewport.setShowUnconserved(showNonconservedMenuItem.getState());
5693     alignPanel.paintAlignment(false, false);
5694   }
5695   
5696   @Override
5697   protected void updateShowSSRadioButtons(JMenu showSS, ButtonGroup ssButtonGroup){
5698     
5699     List<String> ssSources = new ArrayList<String>();
5700     AlignmentAnnotation[] anns = alignPanel.getAlignment()
5701             .getAlignmentAnnotation();
5702     
5703     ssSources = AlignmentUtils.extractSSSourceInAlignmentAnnotation(anns);
5704     
5705     // Get the currently selected radio button
5706     String selectedButtonModelName = getSelectedRadioButtonDisplayString(ssButtonGroup);
5707     boolean selectedButtonModel =  false;
5708     
5709     // Clear existing radio buttons
5710     showSS.removeAll();
5711     JRadioButtonMenuItem radioButtonAllSS = new JRadioButtonMenuItem("All");
5712     radioButtonAllSS.addActionListener(new ActionListener() {
5713       @Override
5714       public void actionPerformed(ActionEvent e) {
5715           showSS_actionPerformed("All");
5716       }
5717     });
5718     ssButtonGroup.add(radioButtonAllSS);
5719     showSS.add(radioButtonAllSS);
5720     
5721     JRadioButtonMenuItem radioButtonNoneSS = new JRadioButtonMenuItem("None");
5722     radioButtonNoneSS.addActionListener(new ActionListener() {
5723       @Override
5724       public void actionPerformed(ActionEvent e) {
5725           showSS_actionPerformed("None");
5726       }
5727     });
5728     ssButtonGroup.add(radioButtonNoneSS);
5729     showSS.add(radioButtonNoneSS);
5730     showSS.addSeparator();
5731
5732        for(String ssSource : ssSources) {
5733         
5734         JRadioButtonMenuItem radioButton = new JRadioButtonMenuItem(ssSource);
5735         radioButton.addActionListener(new ActionListener() {
5736           @Override
5737           public void actionPerformed(ActionEvent e) {
5738               showSS_actionPerformed(ssSource);
5739           }
5740         });
5741         ssButtonGroup.add(radioButton);
5742         showSS.add(radioButton);
5743         
5744         // Check if this radio button's name matches the selected radio button's name
5745         if (ssSource.equals(selectedButtonModelName)) {
5746             radioButton.setSelected(true); // Select this radio button
5747             selectedButtonModel = true;
5748         }
5749         
5750       } 
5751
5752        if (selectedButtonModelName == "None") {
5753          // If no radio button was previously selected, select "All"
5754          ssButtonGroup.setSelected(radioButtonNoneSS.getModel(), true);
5755          selectedButtonModel = true;
5756        }
5757        if (!selectedButtonModel) {
5758            // If no radio button was previously selected, select "All"
5759            ssButtonGroup.setSelected(radioButtonAllSS.getModel(), true);
5760        }
5761   }
5762   
5763   @Override
5764   protected void showSS_actionPerformed(String ssSourceSelection){
5765     
5766
5767     AlignmentAnnotation[] annotations = alignPanel.getAlignment()
5768             .getAlignmentAnnotation();
5769     
5770     for (AlignmentAnnotation aa: annotations) {
5771       
5772       for (String label : Constants.SECONDARY_STRUCTURE_LABELS.keySet()) {
5773         
5774         if (label.equals(aa.label)) { 
5775           
5776           aa.visible = false;
5777           
5778           if(ssSourceSelection == "All") {
5779             aa.visible = true;
5780           }
5781           
5782           else {
5783             String ssSource = AlignmentUtils.extractSSSourceFromAnnotationDescription(aa);
5784             if(ssSource.equals(ssSourceSelection)) {
5785               aa.visible = true;
5786             }
5787             
5788             
5789           }
5790         }
5791       }
5792       
5793     }
5794     
5795       
5796       List<AlignCalcWorkerI> workers = viewport.getCalcManager()
5797               .getRegisteredWorkersOfClass(SecondaryStructureConsensusThread.class);
5798       if (!workers.isEmpty()) {
5799           
5800         viewport.getCalcManager().startWorker(workers.remove(0));
5801
5802       }
5803       
5804       PaintRefresher.Refresh(this, viewport.getSequenceSetId());
5805       alignPanel.updateAnnotation();
5806       alignPanel.paintAlignment(true, true);
5807           
5808     
5809     
5810   }
5811   
5812   protected String getSelectedRadioButtonDisplayString(ButtonGroup ssButtonGroup) {
5813     Enumeration<AbstractButton> buttons = ssButtonGroup.getElements();
5814     while (buttons.hasMoreElements()) {
5815         AbstractButton button = buttons.nextElement();
5816         if (button.isSelected()) {
5817             return button.getText();
5818         }
5819     }
5820     return null; // No radio button is selected
5821 }
5822
5823   /*
5824    * (non-Javadoc)
5825    * 
5826    * @see
5827    * jalview.jbgui.GAlignFrame#showGroupConsensus_actionPerformed(java.awt.event
5828    * .ActionEvent)
5829    */
5830   @Override
5831   protected void showGroupConsensus_actionPerformed(ActionEvent e)
5832   {
5833     viewport.setShowGroupConsensus(showGroupConsensus.getState());
5834     alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
5835
5836   }
5837
5838   /*
5839    * (non-Javadoc)
5840    * 
5841    * @see
5842    * jalview.jbgui.GAlignFrame#showGroupConservation_actionPerformed(java.awt
5843    * .event.ActionEvent)
5844    */
5845   @Override
5846   protected void showGroupConservation_actionPerformed(ActionEvent e)
5847   {
5848     viewport.setShowGroupConservation(showGroupConservation.getState());
5849     alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
5850   }
5851
5852   /*
5853    * (non-Javadoc)
5854    * 
5855    * @see
5856    * jalview.jbgui.GAlignFrame#showConsensusHistogram_actionPerformed(java.awt
5857    * .event.ActionEvent)
5858    */
5859   @Override
5860   protected void showConsensusHistogram_actionPerformed(ActionEvent e)
5861   {
5862     viewport.setShowConsensusHistogram(showConsensusHistogram.getState());
5863     alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
5864   }
5865   
5866   @Override
5867   protected void showSSConsensusHistogram_actionPerformed(ActionEvent e)
5868   {
5869     viewport.setShowSSConsensusHistogram(showSSConsensusHistogram.getState());
5870     alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
5871   }
5872
5873   /*
5874    * (non-Javadoc)
5875    * 
5876    * @see
5877    * jalview.jbgui.GAlignFrame#showConsensusProfile_actionPerformed(java.awt
5878    * .event.ActionEvent)
5879    */
5880   @Override
5881   protected void showSequenceLogo_actionPerformed(ActionEvent e)
5882   {
5883     viewport.setShowSequenceLogo(showSequenceLogo.getState());
5884     alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
5885   }
5886
5887   @Override
5888   protected void normaliseSequenceLogo_actionPerformed(ActionEvent e)
5889   {
5890     showSequenceLogo.setState(true);
5891     viewport.setShowSequenceLogo(true);
5892     viewport.setNormaliseSequenceLogo(normaliseSequenceLogo.getState());
5893     alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
5894   }
5895
5896   @Override
5897   protected void applyAutoAnnotationSettings_actionPerformed(ActionEvent e)
5898   {
5899     alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
5900   }
5901
5902   /*
5903    * (non-Javadoc)
5904    * 
5905    * @see
5906    * jalview.jbgui.GAlignFrame#makeGrpsFromSelection_actionPerformed(java.awt
5907    * .event.ActionEvent)
5908    */
5909   @Override
5910   protected void makeGrpsFromSelection_actionPerformed(ActionEvent e)
5911   {
5912     if (avc.makeGroupsFromSelection())
5913     {
5914       PaintRefresher.Refresh(this, viewport.getSequenceSetId());
5915       alignPanel.updateAnnotation();
5916       alignPanel.paintAlignment(true,
5917               viewport.needToUpdateStructureViews());
5918     }
5919   }
5920
5921   public void clearAlignmentSeqRep()
5922   {
5923     // TODO refactor alignmentseqrep to controller
5924     if (viewport.getAlignment().hasSeqrep())
5925     {
5926       viewport.getAlignment().setSeqrep(null);
5927       PaintRefresher.Refresh(this, viewport.getSequenceSetId());
5928       alignPanel.updateAnnotation();
5929       alignPanel.paintAlignment(true, true);
5930     }
5931   }
5932
5933   @Override
5934   protected void createGroup_actionPerformed(ActionEvent e)
5935   {
5936     if (avc.createGroup())
5937     {
5938       if (applyAutoAnnotationSettings.isSelected())
5939       {
5940         alignPanel.updateAnnotation(true, false);
5941       }
5942       alignPanel.alignmentChanged();
5943     }
5944   }
5945
5946   @Override
5947   protected void unGroup_actionPerformed(ActionEvent e)
5948   {
5949     if (avc.unGroup())
5950     {
5951       alignPanel.alignmentChanged();
5952     }
5953   }
5954
5955   /**
5956    * make the given alignmentPanel the currently selected tab
5957    * 
5958    * @param alignmentPanel
5959    */
5960   public void setDisplayedView(AlignmentPanel alignmentPanel)
5961   {
5962     if (!viewport.getSequenceSetId()
5963             .equals(alignmentPanel.av.getSequenceSetId()))
5964     {
5965       throw new Error(MessageManager.getString(
5966               "error.implementation_error_cannot_show_view_alignment_frame"));
5967     }
5968     if (tabbedPane != null && tabbedPane.getTabCount() > 0 && alignPanels
5969             .indexOf(alignmentPanel) != tabbedPane.getSelectedIndex())
5970     {
5971       tabbedPane.setSelectedIndex(alignPanels.indexOf(alignmentPanel));
5972     }
5973   }
5974
5975   /**
5976    * Action on selection of menu options to Show or Hide annotations.
5977    * 
5978    * @param visible
5979    * @param forSequences
5980    *          update sequence-related annotations
5981    * @param forAlignment
5982    *          update non-sequence-related annotations
5983    */
5984   @Override
5985   public void setAnnotationsVisibility(boolean visible,
5986           boolean forSequences, boolean forAlignment)
5987   {
5988     AlignmentAnnotation[] anns = alignPanel.getAlignment()
5989             .getAlignmentAnnotation();
5990     if (anns == null)
5991     {
5992       return;
5993     }
5994     for (AlignmentAnnotation aa : anns)
5995     {
5996       /*
5997        * don't display non-positional annotations on an alignment
5998        */
5999       if (aa.annotations == null)
6000       {
6001         continue;
6002       }
6003       boolean apply = (aa.sequenceRef == null && forAlignment)
6004               || (aa.sequenceRef != null && forSequences);
6005       if (apply)
6006       {
6007         aa.visible = visible;
6008       }
6009     }
6010     alignPanel.validateAnnotationDimensions(true);
6011     // TODO this triggers relayout of annotation panel - otherwise annotation
6012     // label height is different to panel height
6013     alignPanel.fontChanged();
6014     alignPanel.alignmentChanged();
6015   }
6016
6017   /**
6018    * Store selected annotation sort order for the view and repaint.
6019    */
6020   @Override
6021   protected void sortAnnotations_actionPerformed()
6022   {
6023     this.alignPanel.av.setSortAnnotationsBy(getAnnotationSortOrder());
6024     this.alignPanel.av
6025             .setShowAutocalculatedAbove(isShowAutoCalculatedAbove());
6026     alignPanel.paintAlignment(false, false);
6027   }
6028
6029   /**
6030    * 
6031    * @return alignment panels in this alignment frame
6032    */
6033   public List<? extends AlignmentViewPanel> getAlignPanels()
6034   {
6035     // alignPanels is never null
6036     // return alignPanels == null ? Arrays.asList(alignPanel) : alignPanels;
6037     return alignPanels;
6038   }
6039
6040   /**
6041    * Open a new alignment window, with the cDNA associated with this (protein)
6042    * alignment, aligned as is the protein.
6043    */
6044   protected void viewAsCdna_actionPerformed()
6045   {
6046     // TODO no longer a menu action - refactor as required
6047     final AlignmentI alignment = getViewport().getAlignment();
6048     List<AlignedCodonFrame> mappings = alignment.getCodonFrames();
6049     if (mappings == null)
6050     {
6051       return;
6052     }
6053     List<SequenceI> cdnaSeqs = new ArrayList<>();
6054     for (SequenceI aaSeq : alignment.getSequences())
6055     {
6056       for (AlignedCodonFrame acf : mappings)
6057       {
6058         SequenceI dnaSeq = acf.getDnaForAaSeq(aaSeq.getDatasetSequence());
6059         if (dnaSeq != null)
6060         {
6061           /*
6062            * There is a cDNA mapping for this protein sequence - add to new
6063            * alignment. It will share the same dataset sequence as other mapped
6064            * cDNA (no new mappings need to be created).
6065            */
6066           final Sequence newSeq = new Sequence(dnaSeq);
6067           newSeq.setDatasetSequence(dnaSeq);
6068           cdnaSeqs.add(newSeq);
6069         }
6070       }
6071     }
6072     if (cdnaSeqs.size() == 0)
6073     {
6074       // show a warning dialog no mapped cDNA
6075       return;
6076     }
6077     AlignmentI cdna = new Alignment(
6078             cdnaSeqs.toArray(new SequenceI[cdnaSeqs.size()]));
6079     GAlignFrame alignFrame = new AlignFrame(cdna, AlignFrame.DEFAULT_WIDTH,
6080             AlignFrame.DEFAULT_HEIGHT);
6081     cdna.alignAs(alignment);
6082     String newtitle = "cDNA " + MessageManager.getString("label.for") + " "
6083             + this.title;
6084     Desktop.addInternalFrame(alignFrame, newtitle, AlignFrame.DEFAULT_WIDTH,
6085             AlignFrame.DEFAULT_HEIGHT);
6086   }
6087
6088   /**
6089    * Set visibility of dna/protein complement view (available when shown in a
6090    * split frame).
6091    * 
6092    * @param show
6093    */
6094   @Override
6095   protected void showComplement_actionPerformed(boolean show)
6096   {
6097     SplitContainerI sf = getSplitViewContainer();
6098     if (sf != null)
6099     {
6100       sf.setComplementVisible(this, show);
6101     }
6102   }
6103
6104   /**
6105    * Generate the reverse (optionally complemented) of the selected sequences,
6106    * and add them to the alignment
6107    */
6108   @Override
6109   protected void showReverse_actionPerformed(boolean complement)
6110   {
6111     AlignmentI al = null;
6112     try
6113     {
6114       Dna dna = new Dna(viewport, viewport.getViewAsVisibleContigs(true));
6115       al = dna.reverseCdna(complement);
6116       viewport.addAlignment(al, "");
6117       addHistoryItem(new EditCommand(
6118               MessageManager.getString("label.add_sequences"), Action.PASTE,
6119               al.getSequencesArray(), 0, al.getWidth(),
6120               viewport.getAlignment()));
6121     } catch (Exception ex)
6122     {
6123       jalview.bin.Console.errPrintln(ex.getMessage());
6124       return;
6125     }
6126   }
6127
6128   /**
6129    * Try to run a script in the Groovy console, having first ensured that this
6130    * AlignFrame is set as currentAlignFrame in Desktop, to allow the script to
6131    * be targeted at this alignment.
6132    */
6133   @Override
6134   protected void runGroovy_actionPerformed()
6135   {
6136     Jalview.getInstance().setCurrentAlignFrame(this);
6137     groovy.console.ui.Console console = Desktop.getGroovyConsole();
6138     if (console != null)
6139     {
6140       try
6141       {
6142         console.setVariable(JalviewObjectI.currentAlFrameName, this);
6143         console.runScript();
6144       } catch (Exception ex)
6145       {
6146         jalview.bin.Console.errPrintln((ex.toString()));
6147         JvOptionPane.showInternalMessageDialog(Desktop.desktop,
6148                 MessageManager.getString("label.couldnt_run_groovy_script"),
6149                 MessageManager.getString("label.groovy_support_failed"),
6150                 JvOptionPane.ERROR_MESSAGE);
6151       }
6152     }
6153     else
6154     {
6155       jalview.bin.Console
6156               .errPrintln("Can't run Groovy script as console not found");
6157     }
6158   }
6159
6160   /**
6161    * Hides columns containing (or not containing) a specified feature, provided
6162    * that would not leave all columns hidden
6163    * 
6164    * @param featureType
6165    * @param columnsContaining
6166    * @return
6167    */
6168   public boolean hideFeatureColumns(String featureType,
6169           boolean columnsContaining)
6170   {
6171     boolean notForHiding = avc.markColumnsContainingFeatures(
6172             columnsContaining, false, false, featureType);
6173     if (notForHiding)
6174     {
6175       if (avc.markColumnsContainingFeatures(!columnsContaining, false,
6176               false, featureType))
6177       {
6178         getViewport().hideSelectedColumns();
6179         return true;
6180       }
6181     }
6182     return false;
6183   }
6184
6185   @Override
6186   protected void selectHighlightedColumns_actionPerformed(
6187           ActionEvent actionEvent)
6188   {
6189     // include key modifier check in case user selects from menu
6190     avc.markHighlightedColumns(
6191             (actionEvent.getModifiers() & ActionEvent.ALT_MASK) != 0, true,
6192             (actionEvent.getModifiers() & (ActionEvent.META_MASK
6193                     | ActionEvent.CTRL_MASK)) != 0);
6194   }
6195
6196   @Override
6197   protected void copyHighlightedColumns_actionPerformed(
6198           ActionEvent actionEvent)
6199   {
6200     avc.copyHighlightedRegionsToClipboard();
6201   }
6202
6203   /**
6204    * Rebuilds the Colour menu, including any user-defined colours which have
6205    * been loaded either on startup or during the session
6206    */
6207   public void buildColourMenu()
6208   {
6209     colourMenu.removeAll();
6210
6211     colourMenu.add(applyToAllGroups);
6212     colourMenu.add(textColour);
6213     colourMenu.addSeparator();
6214
6215     ButtonGroup bg = ColourMenuHelper.addMenuItems(colourMenu, this,
6216             viewport.getAlignment(), false);
6217
6218     colourMenu.add(annotationColour);
6219     bg.add(annotationColour);
6220     colourMenu.addSeparator();
6221     colourMenu.add(conservationMenuItem);
6222     colourMenu.add(modifyConservation);
6223     colourMenu.add(abovePIDThreshold);
6224     colourMenu.add(modifyPID);
6225
6226     ColourSchemeI colourScheme = viewport.getGlobalColourScheme();
6227     ColourMenuHelper.setColourSelected(colourMenu, colourScheme);
6228   }
6229
6230   /**
6231    * Open a dialog (if not already open) that allows the user to select and
6232    * calculate PCA or Tree analysis
6233    */
6234   protected void openTreePcaDialog()
6235   {
6236     if (alignPanel.getCalculationDialog() == null)
6237     {
6238       new CalculationChooser(AlignFrame.this);
6239     }
6240   }
6241
6242   @Override
6243   protected void loadVcf_actionPerformed()
6244   {
6245     JalviewFileChooser chooser = new JalviewFileChooser(
6246             Cache.getProperty("LAST_DIRECTORY"));
6247     chooser.setFileView(new JalviewFileView());
6248     chooser.setDialogTitle(MessageManager.getString("label.load_vcf_file"));
6249     chooser.setToolTipText(MessageManager.getString("label.load_vcf_file"));
6250     final AlignFrame us = this;
6251     chooser.setResponseHandler(0, () -> {
6252       String choice = chooser.getSelectedFile().getPath();
6253       Cache.setProperty("LAST_DIRECTORY", choice);
6254       SequenceI[] seqs = viewport.getAlignment().getSequencesArray();
6255       new VCFLoader(choice).loadVCF(seqs, us);
6256     });
6257     chooser.showOpenDialog(null);
6258
6259   }
6260
6261   private Rectangle lastFeatureSettingsBounds = null;
6262
6263   @Override
6264   public void setFeatureSettingsGeometry(Rectangle bounds)
6265   {
6266     lastFeatureSettingsBounds = bounds;
6267   }
6268
6269   @Override
6270   public Rectangle getFeatureSettingsGeometry()
6271   {
6272     return lastFeatureSettingsBounds;
6273   }
6274
6275 }
6276
6277 class PrintThread extends Thread
6278 {
6279   AlignmentPanel ap;
6280
6281   public PrintThread(AlignmentPanel ap)
6282   {
6283     this.ap = ap;
6284   }
6285
6286   static PageFormat pf;
6287
6288   @Override
6289   public void run()
6290   {
6291     PrinterJob printJob = PrinterJob.getPrinterJob();
6292
6293     if (pf != null)
6294     {
6295       printJob.setPrintable(ap, pf);
6296     }
6297     else
6298     {
6299       printJob.setPrintable(ap);
6300     }
6301
6302     if (printJob.printDialog())
6303     {
6304       try
6305       {
6306         printJob.print();
6307       } catch (Exception PrintException)
6308       {
6309         PrintException.printStackTrace();
6310       }
6311     }
6312   }
6313 }