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