Merge develop to Release_2_8_3_Branch
[jalview.git] / src / jalview / appletgui / IdCanvas.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.appletgui;
22
23 import jalview.datamodel.SequenceI;
24
25 import java.awt.Color;
26 import java.awt.Font;
27 import java.awt.Graphics;
28 import java.awt.Image;
29 import java.awt.Panel;
30 import java.util.List;
31
32 public class IdCanvas extends Panel
33 {
34   protected AlignViewport av;
35
36   protected boolean showScores = true;
37
38   protected int maxIdLength = -1;
39
40   protected String maxIdStr = null;
41
42   Image image;
43
44   Graphics gg;
45
46   int imgHeight = 0;
47
48   boolean fastPaint = false;
49
50   List<SequenceI> searchResults;
51
52   public IdCanvas(AlignViewport av)
53   {
54     setLayout(null);
55     this.av = av;
56     PaintRefresher.Register(this, av.getSequenceSetId());
57   }
58
59   public void drawIdString(Graphics gg, SequenceI s, int i, int starty,
60           int ypos)
61   {
62     int charHeight = av.getCharHeight();
63
64     if (searchResults != null && searchResults.contains(s))
65     {
66       gg.setColor(Color.black);
67       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getSize().width,
68               charHeight);
69       gg.setColor(Color.white);
70     }
71     else if (av.getSelectionGroup() != null
72             && av.getSelectionGroup().getSequences(null).contains(s))
73     {
74       gg.setColor(Color.lightGray);
75       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getSize().width,
76               charHeight);
77       gg.setColor(Color.white);
78     }
79     else
80     {
81       gg.setColor(av.getSequenceColour(s));
82       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getSize().width,
83               charHeight);
84       gg.setColor(Color.black);
85     }
86
87     gg.drawString(s.getDisplayId(av.getShowJVSuffix()), 0,
88             ((i - starty) * charHeight) + ypos + charHeight
89                     - (charHeight / 5));
90
91     if (av.hasHiddenRows() && av.getShowHiddenMarkers())
92     {
93       drawMarker(i, starty, ypos);
94     }
95
96   }
97
98   public void fastPaint(int vertical)
99   {
100     if (gg == null)
101     {
102       repaint();
103       return;
104     }
105
106     gg.copyArea(0, 0, getSize().width, imgHeight, 0,
107             -vertical * av.getCharHeight());
108
109     int ss = av.startSeq, es = av.endSeq, transY = 0;
110     if (vertical > 0) // scroll down
111     {
112       ss = es - vertical;
113       if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
114       {
115         ss = av.startSeq;
116       }
117       else
118       {
119         transY = imgHeight - vertical * av.getCharHeight();
120       }
121     }
122     else if (vertical < 0)
123     {
124       es = ss - vertical;
125       if (es > av.endSeq)
126       {
127         es = av.endSeq;
128       }
129     }
130
131     gg.translate(0, transY);
132
133     drawIds(ss, es);
134
135     gg.translate(0, -transY);
136
137     fastPaint = true;
138     repaint();
139   }
140
141   public void update(Graphics g)
142   {
143     paint(g);
144   }
145
146   public void paint(Graphics g)
147   {
148     if (getSize().height < 0 || getSize().width < 0)
149     {
150       return;
151     }
152     if (fastPaint)
153     {
154       fastPaint = false;
155       g.drawImage(image, 0, 0, this);
156       return;
157     }
158
159     imgHeight = getSize().height;
160     imgHeight -= imgHeight % av.getCharHeight();
161
162     if (imgHeight < 1)
163     {
164       return;
165     }
166
167     if (image == null || imgHeight != image.getHeight(this))
168     {
169       image = createImage(getSize().width, imgHeight);
170       gg = image.getGraphics();
171       gg.setFont(av.getFont());
172     }
173
174     // Fill in the background
175     gg.setColor(Color.white);
176     Font italic = new Font(av.getFont().getName(), Font.ITALIC, av
177             .getFont().getSize());
178     gg.setFont(italic);
179
180     gg.fillRect(0, 0, getSize().width, getSize().height);
181     drawIds(av.startSeq, av.endSeq);
182     g.drawImage(image, 0, 0, this);
183   }
184
185   /**
186    * local copy of av.getCharHeight set at top of drawIds
187    */
188   private int avcharHeight;
189   void drawIds(int starty, int endy)
190   {
191     // hardwired italic IDs in applet currently
192     Font italic = new Font(av.getFont().getName(), Font.ITALIC, av
193             .getFont().getSize());
194     // temp variable for speed
195     avcharHeight = av.getCharHeight();
196
197     gg.setFont(italic);
198
199     Color currentColor = Color.white;
200     Color currentTextColor = Color.black;
201
202     if (av.getWrapAlignment())
203     {
204       int maxwidth = av.getAlignment().getWidth();
205       int alheight = av.getAlignment().getHeight();
206
207       if (av.hasHiddenColumns())
208       {
209         maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
210       }
211
212       int annotationHeight = 0;
213       AnnotationLabels labels = null;
214
215       if (av.isShowAnnotation())
216       {
217         AnnotationPanel ap = new AnnotationPanel(av);
218         annotationHeight = ap.adjustPanelHeight();
219         labels = new AnnotationLabels(av);
220       }
221       int hgap = avcharHeight;
222       if (av.getScaleAboveWrapped())
223       {
224         hgap += avcharHeight;
225       }
226
227       int cHeight = alheight * avcharHeight + hgap + annotationHeight;
228
229       int rowSize = av.getEndRes() - av.getStartRes();
230
231       // Draw the rest of the panels
232       for (int ypos = hgap, row = av.startRes; (ypos <= getSize().height)
233               && (row < maxwidth); ypos += cHeight, row += rowSize)
234       {
235         for (int i = starty; i < alheight; i++)
236         {
237
238           SequenceI s = av.getAlignment().getSequenceAt(i);
239           gg.setFont(italic);
240           if (av.isDisplayReferenceSeq() || av.hasHiddenRows())
241           {
242             setHiddenFont(s);
243           }
244           drawIdString(gg, s, i, 0, ypos);
245         }
246
247         if (labels != null)
248         {
249           gg.translate(0, ypos + (alheight * avcharHeight));
250           labels.drawComponent(gg, getSize().width);
251           gg.translate(0, -ypos - (alheight * avcharHeight));
252         }
253
254       }
255     }
256     else
257     {
258       // Now draw the id strings
259       SequenceI seq;
260       for (int i = starty; i < endy; i++)
261       {
262
263         seq = av.getAlignment().getSequenceAt(i);
264         if (seq == null)
265         {
266           continue;
267         }
268         gg.setFont(italic);
269         // boolean isrep=false;
270         if (av.isDisplayReferenceSeq() || av.hasHiddenRows())
271         {
272           // isrep =
273           setHiddenFont(seq);
274         }
275
276         // Selected sequence colours
277         if ((searchResults != null) && searchResults.contains(seq))
278         {
279           currentColor = Color.black;
280           currentTextColor = Color.white;
281         }
282         else if ((av.getSelectionGroup() != null)
283                 && av.getSelectionGroup().getSequences(null).contains(seq))
284         {
285           currentColor = Color.lightGray;
286           currentTextColor = Color.black;
287         }
288         else
289         {
290           currentColor = av.getSequenceColour(seq);
291           currentTextColor = Color.black;
292         }
293
294         gg.setColor(currentColor);
295         // TODO: isrep could be used to highlight the representative in a
296         // different way
297         gg.fillRect(0, (i - starty) * avcharHeight, getSize().width,
298                 avcharHeight);
299         gg.setColor(currentTextColor);
300
301         gg.drawString(seq.getDisplayId(av.getShowJVSuffix()), 0,
302                 (((i - starty) * avcharHeight) + avcharHeight)
303                         - (avcharHeight / 5));
304
305         if (av.hasHiddenRows() && av.getShowHiddenMarkers())
306         {
307           drawMarker(i, starty, 0);
308         }
309       }
310     }
311   }
312
313   public void setHighlighted(List<SequenceI> list)
314   {
315     searchResults = list;
316     repaint();
317   }
318
319   void drawMarker(int i, int starty, int yoffset)
320   {
321     SequenceI[] hseqs = av.getAlignment().getHiddenSequences().hiddenSequences;
322     // Use this method here instead of calling hiddenSeq adjust
323     // 3 times.
324     int hSize = hseqs.length;
325
326     int hiddenIndex = i;
327     int lastIndex = i - 1;
328     int nextIndex = i + 1;
329
330     for (int j = 0; j < hSize; j++)
331     {
332       if (hseqs[j] != null)
333       {
334         if (j - 1 < hiddenIndex)
335         {
336           hiddenIndex++;
337         }
338         if (j - 1 < lastIndex)
339         {
340           lastIndex++;
341         }
342         if (j - 1 < nextIndex)
343         {
344           nextIndex++;
345         }
346       }
347     }
348
349     boolean below = (hiddenIndex > lastIndex + 1);
350     boolean above = (nextIndex > hiddenIndex + 1);
351
352     gg.setColor(Color.blue);
353     if (below)
354     {
355       gg.fillPolygon(new int[]
356       { getSize().width - avcharHeight, getSize().width - avcharHeight,
357           getSize().width }, new int[]
358       { (i - starty) * avcharHeight + yoffset,
359           (i - starty) * avcharHeight + yoffset + avcharHeight / 4,
360           (i - starty) * avcharHeight + yoffset }, 3);
361     }
362     if (above)
363     {
364       gg.fillPolygon(new int[]
365       { getSize().width - avcharHeight, getSize().width - avcharHeight,
366           getSize().width }, new int[]
367       { (i - starty + 1) * avcharHeight + yoffset,
368           (i - starty + 1) * avcharHeight + yoffset - avcharHeight / 4,
369           (i - starty + 1) * avcharHeight + yoffset }, 3);
370
371     }
372   }
373
374   boolean setHiddenFont(SequenceI seq)
375   {
376     Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()
377             .getSize());
378
379     if (av.isHiddenRepSequence(seq))
380     {
381       gg.setFont(bold);
382       return true;
383     }
384     return false;
385   }
386 }