JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / commands / OrderCommand.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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.commands;
22
23 import jalview.analysis.AlignmentSorter;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.SequenceI;
26
27 /**
28  * An undoable command to reorder the sequences in an alignment.
29  * 
30  * @author gmcarstairs
31  *
32  */
33 public class OrderCommand implements CommandI
34 {
35   String description;
36
37   /*
38    * The sequence order before sorting (target order for an undo)
39    */
40   SequenceI[] seqs;
41
42   /*
43    * The sequence order specified by this command
44    */
45   SequenceI[] seqs2;
46
47   /*
48    * The alignment the command acts on
49    */
50   AlignmentI al;
51
52   /**
53    * Constructor given the 'undo' sequence order, and the (already) sorted
54    * alignment.
55    * 
56    * @param description
57    *          a text label for the 'undo' menu option
58    * @param seqs
59    *          the sequence order for undo
60    * @param al
61    *          the alignment as ordered by this command
62    */
63   public OrderCommand(String description, SequenceI[] seqs, AlignmentI al)
64   {
65     this.description = description;
66     this.seqs = seqs;
67     this.seqs2 = al.getSequencesArray();
68     this.al = al;
69     doCommand(null);
70   }
71
72   public String getDescription()
73   {
74     return description;
75   }
76
77   public int getSize()
78   {
79     return 1;
80   }
81
82   public void doCommand(AlignmentI[] views)
83   {
84     AlignmentSorter.setOrder(al, seqs2);
85   }
86
87   public void undoCommand(AlignmentI[] views)
88   {
89     AlignmentSorter.setOrder(al, seqs);
90   }
91
92   /**
93    * Returns the sequence order used to sort, or before sorting if undo=true.
94    * 
95    * @param undo
96    * @return
97    */
98   public SequenceI[] getSequenceOrder(boolean undo)
99   {
100     return undo ? seqs : seqs2;
101   }
102 }