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