new file image
[jalview.git] / src / jalview / gui / AlignViewport.java
1 package jalview.gui;\r
2 \r
3 import java.awt.*;\r
4 import jalview.io.*;\r
5 import jalview.analysis.NJTree;\r
6 import jalview.datamodel.*;\r
7 import jalview.schemes.*;\r
8 import java.util.*;\r
9 \r
10 public class AlignViewport\r
11 {\r
12   int startRes;\r
13   int endRes;\r
14 \r
15   int startSeq;\r
16   int endSeq;\r
17 \r
18   boolean showScores=false;\r
19   boolean showText=true;\r
20   boolean showColourText=false;\r
21   boolean showBoxes=true;\r
22   boolean wrapAlignment=false;\r
23   boolean renderGaps = true;\r
24   boolean showSequenceFeatures = false;\r
25 \r
26   boolean colourAppliesToAllGroups = true;\r
27   ColourSchemeI globalColourScheme = null;\r
28   boolean conservationColourSelected = false;\r
29   boolean abovePIDThreshold = false;\r
30 \r
31   SequenceGroup rubberbandGroup = null;\r
32 \r
33   RendererI renderer = new SequenceRenderer(this);\r
34 \r
35   int             charHeight;\r
36   int             charWidth;\r
37   int             chunkWidth;\r
38   int             chunkHeight;\r
39 \r
40   Font            font = new Font("SansSerif",Font.PLAIN,10);\r
41   AlignmentI      alignment;\r
42 \r
43   Selection       sel    = new Selection(this);\r
44   ColumnSelection colSel = new ColumnSelection();\r
45 \r
46   String visibleConsensus;\r
47 \r
48   int threshold;\r
49   int increment;\r
50 \r
51   NJTree currentTree = null;\r
52 \r
53 \r
54   public AlignViewport(AlignmentI da,\r
55                        boolean showScores,\r
56                        boolean showText,\r
57                        boolean showBoxes,\r
58                        boolean wrapAlignment) {\r
59     this(0,da.getWidth()-1,0,da.getHeight()-1,showScores,\r
60          showText,\r
61          showBoxes,\r
62          wrapAlignment);\r
63 \r
64     setAlignment(da);\r
65   }\r
66 \r
67   public AlignViewport(int startRes, int endRes,\r
68                        int startSeq, int endSeq,\r
69                        boolean showScores,\r
70                        boolean showText,\r
71                        boolean showBoxes,\r
72                        boolean wrapAlignment) {\r
73 \r
74     this.startRes = startRes;\r
75     this.endRes   = endRes;\r
76     this.startSeq = startSeq;\r
77     this.endSeq   = endSeq;\r
78 \r
79     this.showScores    = showScores;\r
80     this.showText      = showText;\r
81     this.showBoxes     = showBoxes;\r
82     this.wrapAlignment = wrapAlignment;\r
83 \r
84    // og  = new AlignmentOutputGenerator(this);\r
85 \r
86     setFont( font );\r
87  }\r
88 \r
89  public void showSequenceFeatures(boolean b)\r
90  {\r
91    showSequenceFeatures = b;\r
92  }\r
93 \r
94   public String getVisibleConsensus()\r
95   {\r
96     return visibleConsensus;\r
97   }\r
98 \r
99   Vector consensus = new Vector();\r
100   public Vector getConsensus(boolean recalculate)\r
101   {\r
102     if(recalculate || consensus.size()<1)\r
103     {\r
104       consensus = alignment.getAAFrequency();\r
105       StringBuffer sb = new StringBuffer();\r
106       Hashtable hash = null;\r
107       for (int i = 0; i < consensus.size(); i++)\r
108       {\r
109         hash = (Hashtable) consensus.elementAt(i);\r
110         sb.append(hash.get("maxResidue").toString().charAt(0));\r
111       }\r
112       visibleConsensus = sb.toString();\r
113     }\r
114 \r
115 \r
116     return consensus;\r
117   }\r
118 \r
119   public SequenceGroup getRubberbandGroup()\r
120   {\r
121     return rubberbandGroup;\r
122   }\r
123 \r
124   public void setRubberbandGroup(SequenceGroup sg)\r
125   {\r
126     rubberbandGroup = sg;\r
127   }\r
128 \r
129  public boolean getConservationSelected()\r
130  {\r
131    return conservationColourSelected;\r
132  }\r
133 \r
134  public void setConservationSelected(boolean b)\r
135  {\r
136    conservationColourSelected = b;\r
137  }\r
138 \r
139  public boolean getAbovePIDThreshold()\r
140  {\r
141    return abovePIDThreshold;\r
142  }\r
143 \r
144  public void setAbovePIDThreshold(boolean b)\r
145  {\r
146    abovePIDThreshold = b;\r
147  }\r
148 \r
149   public int getStartRes() {\r
150     return startRes;\r
151   }\r
152 \r
153   public int getEndRes() {\r
154     return endRes;\r
155   }\r
156 \r
157   public int getStartSeq() {\r
158     return startSeq;\r
159   }\r
160 \r
161   public void setGlobalColourScheme(ColourSchemeI cs)\r
162   {\r
163      globalColourScheme = cs;\r
164   }\r
165 \r
166   public ColourSchemeI getGlobalColourScheme()\r
167   {\r
168     return globalColourScheme;\r
169   }\r
170 \r
171 \r
172   public void setStartRes(int res) {\r
173     this.startRes = res;\r
174   }\r
175   public void setStartSeq(int seq) {\r
176     this.startSeq = seq;\r
177   }\r
178   public void setEndRes(int res) {\r
179     if (res > alignment.getWidth()-1) {\r
180       System.out.println(" Corrected res from " + res + " to maximum " + (alignment.getWidth()-1));\r
181        res = alignment.getWidth() -1;\r
182     }\r
183     if (res < 0) {\r
184       res = 0;\r
185     }\r
186     this.endRes = res;\r
187   }\r
188   public void setEndSeq(int seq) {\r
189     if (seq > alignment.getHeight()) {\r
190       seq = alignment.getHeight();\r
191     }\r
192     if (seq < 0) {\r
193       seq = 0;\r
194     }\r
195     this.endSeq = seq;\r
196   }\r
197   public int getEndSeq() {\r
198     return endSeq;\r
199   }\r
200 \r
201   public void setFont(Font f) {\r
202     font = f;\r
203     javax.swing.JFrame temp = new javax.swing.JFrame();\r
204     temp.addNotify();\r
205     java.awt.FontMetrics fm = temp.getGraphics().getFontMetrics(font);\r
206     setCharHeight(fm.getHeight());\r
207     setCharWidth(fm.charWidth('M'));\r
208   }\r
209 \r
210   public Font getFont() {\r
211     return font;\r
212   }\r
213   public void setCharWidth(int w) {\r
214     this.charWidth = w;\r
215   }\r
216   public int getCharWidth() {\r
217     return charWidth;\r
218   }\r
219   public void setCharHeight(int h) {\r
220     this.charHeight = h;\r
221   }\r
222   public int getCharHeight() {\r
223     return charHeight;\r
224   }\r
225   public void setChunkWidth(int w) {\r
226     this.chunkWidth = w;\r
227   }\r
228   public int getChunkWidth() {\r
229     return chunkWidth;\r
230   }\r
231   public void setChunkHeight(int h) {\r
232     this.chunkHeight = h;\r
233   }\r
234   public int getChunkHeight() {\r
235     return chunkHeight;\r
236   }\r
237   public AlignmentI getAlignment() {\r
238     return alignment;\r
239   }\r
240   public void setAlignment(AlignmentI align) {\r
241     this.alignment = align;\r
242   }\r
243   public void setShowScores(boolean state) {\r
244     showScores = state;\r
245   }\r
246   public void setWrapAlignment(boolean state) {\r
247     wrapAlignment = state;\r
248   }\r
249   public void setShowText(boolean state) {\r
250     showText = state;\r
251   }\r
252 \r
253   public void setRenderGaps(boolean state){\r
254     renderGaps = state;\r
255     if(renderer instanceof SequenceRenderer)\r
256     {\r
257       SequenceRenderer sr = (SequenceRenderer)renderer;\r
258       sr.renderGaps(state);\r
259     }\r
260   }\r
261 \r
262 \r
263   public boolean getColourText()\r
264   {\r
265     return showColourText;\r
266   }\r
267 \r
268   public void setColourText(boolean state)\r
269   {\r
270     showColourText = state;\r
271   }\r
272 \r
273   public void setShowBoxes(boolean state) {\r
274     showBoxes = state;\r
275   }\r
276   public boolean getShowScores() {\r
277     return showScores;\r
278   }\r
279   public boolean getWrapAlignment() {\r
280       return wrapAlignment;\r
281   }\r
282   public boolean getShowText() {\r
283     return showText;\r
284   }\r
285   public boolean getShowBoxes() {\r
286     return showBoxes;\r
287   }\r
288 \r
289   public char getGapCharacter() {\r
290     return getAlignment().getGapCharacter();\r
291   }\r
292   public void setGapCharacter(char gap) {\r
293     if (getAlignment() != null) {\r
294       getAlignment().setGapCharacter(gap);\r
295     }\r
296   }\r
297   public void setThreshold(int thresh) {\r
298     threshold = thresh;\r
299   }\r
300   public int getThreshold() {\r
301     return threshold;\r
302   }\r
303   public void setIncrement(int inc) {\r
304     increment = inc;\r
305   }\r
306   public int getIncrement() {\r
307     return increment;\r
308   }\r
309   public int getIndex(int y) {\r
310     int y1     = 0;\r
311     int starty = getStartSeq();\r
312     int endy   = getEndSeq();\r
313 \r
314     for (int i = starty; i <= endy; i++) {\r
315       if (i < alignment.getHeight() && alignment.getSequenceAt(i) != null) {\r
316         int y2 = y1 + getCharHeight();\r
317 \r
318         if (y>=y1 && y <=y2) {\r
319           return i;\r
320         }\r
321         y1  = y2;\r
322       } else {\r
323         return -1;\r
324       }\r
325     }\r
326     return -1;\r
327   }\r
328   public Selection getSelection() {\r
329     return sel;\r
330   }\r
331   public ColumnSelection getColumnSelection() {\r
332     return colSel;\r
333   }\r
334 \r
335   public void resetSeqLimits(int height) {\r
336     setStartSeq(0);\r
337     setEndSeq(height/getCharHeight());\r
338   }\r
339   public void setCurrentTree(NJTree tree) {\r
340       currentTree = tree;\r
341   }\r
342   public NJTree getCurrentTree() {\r
343     return currentTree;\r
344   }\r
345 \r
346     public void setRenderer(RendererI rend) {\r
347         this.renderer = rend;\r
348     }\r
349 \r
350     public RendererI getRenderer() {\r
351         return renderer;\r
352     }\r
353 \r
354   public void setColourAppliesToAllGroups(boolean b)\r
355   {   colourAppliesToAllGroups = b; }\r
356 \r
357   public boolean getColourAppliesToAllGroups()\r
358   {return colourAppliesToAllGroups; }\r
359 \r
360 \r
361 }\r