JAL-192 JAL-2099 factored out calculation from rendering code #3
[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    * @return List { Object { .. } } Boolean: true/false for major/minor mark,
41    *         Integer: marker position in alignment column coords, String: null
42    *         or a String to be rendered at the position.
43    */
44   public static List<Object[]> calculateMarks(AlignViewportI av,
45           int startx, int endx)
46   {
47     new ArrayList<Object[]>();
48
49     int scalestartx = (startx / 10) * 10;
50
51     SequenceI refSeq = av.getAlignment().getSeqrep();
52     int refSp = 0, refEp = -1, refStart = 0, refEnd = -1, refStartI = 0, refEndI = -1;
53     if (refSeq != null)
54     {
55       // find bounds and set origin appopriately
56       // locate first visible position for this sequence
57       int[] refbounds = av.getColumnSelection()
58               .locateVisibleBoundsOfSequence(refSeq);
59
60       refSp = refbounds[0];
61       refEp = refbounds[1];
62       refStart = refbounds[2];
63       refEnd = refbounds[3];
64       refStartI = refbounds[4];
65       refEndI = refbounds[5];
66       scalestartx = refSp + ((scalestartx - refSp) / 10) * 10;
67     }
68
69     if (refSeq == null && scalestartx % 10 == 0)
70     {
71       scalestartx += 5;
72     }
73     List<Object[]> marks = new ArrayList<Object[]>();
74     String string;
75     int maxX = 0, refN, iadj;
76     // todo: add a 'reference origin column' to set column number relative to
77     for (int i = scalestartx; i < endx; i += 5)
78     {
79       Object[] amark = new Object[3];
80       if (((i - refSp) % 10) == 0)
81       {
82         if (refSeq == null)
83         {
84           iadj = av.getColumnSelection().adjustForHiddenColumns(i - 1) + 1;
85           string = String.valueOf(iadj);
86         }
87         else
88         {
89           iadj = av.getColumnSelection().adjustForHiddenColumns(i - 1);
90           refN = refSeq.findPosition(iadj);
91           // TODO show bounds if position is a gap
92           // - ie L--R -> "1L|2R" for
93           // marker
94           if (iadj < refStartI)
95           {
96             string = String.valueOf(iadj - refStartI);
97           }
98           else if (iadj > refEndI)
99           {
100             string = "+" + String.valueOf(iadj - refEndI);
101           }
102           else
103           {
104             string = String.valueOf(refN) + refSeq.getCharAt(iadj);
105           }
106         }
107         amark[0] = Boolean.TRUE;
108         amark[1] = Integer.valueOf(i - startx - 1);
109         amark[2] = string;
110
111       }
112       else
113       {
114         amark[0] = Boolean.FALSE;
115         amark[1] = Integer.valueOf(i - startx - 1);
116         amark[2] = null;
117       }
118       marks.add(amark);
119     }
120     return marks;
121   }
122
123 }