7f1e074dea33cc9de1a6abf6d5649ecea2b67829
[jalview.git] / src / jalview / renderer / ScaleRenderer.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.renderer;
22
23 import jalview.api.AlignViewportI;
24 import jalview.datamodel.SequenceI;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
29 /**
30  * Calculate and display alignment rulers
31  * 
32  * @author jprocter
33  *
34  */
35 public class ScaleRenderer
36 {
37   /**
38    * calculate positions markers on the alignment ruler
39    * 
40    * @param av
41    * @param startx
42    *          left-most column in visible view
43    * @param endx
44    *          - right-most column in visible view
45    * @return List { Object { .. } } Boolean: true/false for major/minor mark,
46    *         Integer: marker position in alignment column coords, String: null
47    *         or a String to be rendered at the position.
48    */
49   public static List<Object[]> calculateMarks(AlignViewportI av,
50           int startx, int endx)
51   {
52     new ArrayList<Object[]>();
53
54     int scalestartx = (startx / 10) * 10;
55
56     SequenceI refSeq = av.getAlignment().getSeqrep();
57     int refSp = 0, refStartI = 0, refEndI = -1;
58     if (refSeq != null)
59     {
60       // find bounds and set origin appopriately
61       // locate first visible position for this sequence
62       int[] refbounds = av.getColumnSelection()
63               .locateVisibleBoundsOfSequence(refSeq);
64
65       refSp = refbounds[0];
66       refStartI = refbounds[4];
67       refEndI = refbounds[5];
68       scalestartx = refSp + ((scalestartx - refSp) / 10) * 10;
69     }
70
71     if (refSeq == null && scalestartx % 10 == 0)
72     {
73       scalestartx += 5;
74     }
75     List<Object[]> marks = new ArrayList<Object[]>();
76     String string;
77     int refN, iadj;
78     // todo: add a 'reference origin column' to set column number relative to
79     for (int i = scalestartx; i < endx; i += 5)
80     {
81       Object[] amark = new Object[3];
82       if (((i - refSp) % 10) == 0)
83       {
84         if (refSeq == null)
85         {
86           iadj = av.getColumnSelection().adjustForHiddenColumns(i - 1) + 1;
87           string = String.valueOf(iadj);
88         }
89         else
90         {
91           iadj = av.getColumnSelection().adjustForHiddenColumns(i - 1);
92           refN = refSeq.findPosition(iadj);
93           // TODO show bounds if position is a gap
94           // - ie L--R -> "1L|2R" for
95           // marker
96           if (iadj < refStartI)
97           {
98             string = String.valueOf(iadj - refStartI);
99           }
100           else if (iadj > refEndI)
101           {
102             string = "+" + String.valueOf(iadj - refEndI);
103           }
104           else
105           {
106             string = String.valueOf(refN) + refSeq.getCharAt(iadj);
107           }
108         }
109         amark[0] = Boolean.TRUE;
110         amark[1] = Integer.valueOf(i - startx - 1);
111         amark[2] = string;
112
113       }
114       else
115       {
116         amark[0] = Boolean.FALSE;
117         amark[1] = Integer.valueOf(i - startx - 1);
118         amark[2] = null;
119       }
120       marks.add(amark);
121     }
122     return marks;
123   }
124
125 }