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