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