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