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