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