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