abcbd70becc49ea109d8aeaa117744d2dd7b41df
[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.getColumnSelection().findColumnPosition(maxwidth) - 1;
222       }
223
224       int annotationHeight = 0;
225       AnnotationLabels labels = null;
226
227       if (av.isShowAnnotation())
228       {
229         AnnotationPanel ap = new AnnotationPanel(av);
230         annotationHeight = ap.adjustPanelHeight();
231         labels = new AnnotationLabels(av);
232       }
233       int hgap = avcharHeight;
234       if (av.getScaleAboveWrapped())
235       {
236         hgap += avcharHeight;
237       }
238
239       int cHeight = alheight * avcharHeight + hgap + annotationHeight;
240
241       int rowSize = av.getRanges().getEndRes()
242               - av.getRanges().getStartRes();
243       // Draw the rest of the panels
244       for (int ypos = hgap, row = av.getRanges().getStartRes(); (ypos <= getSize().height)
245               && (row < maxwidth); ypos += cHeight, row += rowSize)
246       {
247         for (int i = starty; i < alheight; i++)
248         {
249
250           SequenceI s = av.getAlignment().getSequenceAt(i);
251           gg.setFont(italic);
252           if (doHiddenCheck)
253           {
254             setHiddenFont(s);
255           }
256           drawIdString(gg, hiddenRows, s, i, 0, ypos);
257         }
258
259         if (labels != null)
260         {
261           gg.translate(0, ypos + (alheight * avcharHeight));
262           labels.drawComponent(gg, getSize().width);
263           gg.translate(0, -ypos - (alheight * avcharHeight));
264         }
265
266       }
267     }
268     else
269     {
270       // Now draw the id strings
271       SequenceI seq;
272       for (int i = starty; i <= endy; i++)
273       {
274
275         seq = av.getAlignment().getSequenceAt(i);
276         if (seq == null)
277         {
278           continue;
279         }
280         gg.setFont(italic);
281         // boolean isrep=false;
282         if (doHiddenCheck)
283         {
284           // isrep =
285           setHiddenFont(seq);
286         }
287
288         // Selected sequence colours
289         if ((searchResults != null) && searchResults.contains(seq))
290         {
291           currentColor = Color.black;
292           currentTextColor = Color.white;
293         }
294         else if ((av.getSelectionGroup() != null)
295                 && av.getSelectionGroup().getSequences(null).contains(seq))
296         {
297           currentColor = Color.lightGray;
298           currentTextColor = Color.black;
299         }
300         else
301         {
302           currentColor = av.getSequenceColour(seq);
303           currentTextColor = Color.black;
304         }
305
306         gg.setColor(currentColor);
307         // TODO: isrep could be used to highlight the representative in a
308         // different way
309         gg.fillRect(0, (i - starty) * avcharHeight, getSize().width,
310                 avcharHeight);
311         gg.setColor(currentTextColor);
312
313         gg.drawString(seq.getDisplayId(av.getShowJVSuffix()), 0,
314                 (((i - starty) * avcharHeight) + avcharHeight)
315                         - (avcharHeight / 5));
316
317         if (hiddenRows)
318         {
319           drawMarker(i, starty, 0);
320         }
321       }
322     }
323   }
324
325   public void setHighlighted(List<SequenceI> list)
326   {
327     searchResults = list;
328     repaint();
329   }
330
331   void drawMarker(int i, int starty, int yoffset)
332   {
333     SequenceI[] hseqs = av.getAlignment().getHiddenSequences().hiddenSequences;
334     // Use this method here instead of calling hiddenSeq adjust
335     // 3 times.
336     int hSize = hseqs.length;
337
338     int hiddenIndex = i;
339     int lastIndex = i - 1;
340     int nextIndex = i + 1;
341
342     for (int j = 0; j < hSize; j++)
343     {
344       if (hseqs[j] != null)
345       {
346         if (j - 1 < hiddenIndex)
347         {
348           hiddenIndex++;
349         }
350         if (j - 1 < lastIndex)
351         {
352           lastIndex++;
353         }
354         if (j - 1 < nextIndex)
355         {
356           nextIndex++;
357         }
358       }
359     }
360
361     boolean below = (hiddenIndex > lastIndex + 1);
362     boolean above = (nextIndex > hiddenIndex + 1);
363
364     gg.setColor(Color.blue);
365     if (below)
366     {
367       gg.fillPolygon(new int[] { getSize().width - avcharHeight,
368           getSize().width - avcharHeight, getSize().width }, new int[] {
369           (i - starty) * avcharHeight + yoffset,
370           (i - starty) * avcharHeight + yoffset + avcharHeight / 4,
371           (i - starty) * avcharHeight + yoffset }, 3);
372     }
373     if (above)
374     {
375       gg.fillPolygon(new int[] { getSize().width - avcharHeight,
376           getSize().width - avcharHeight, getSize().width }, new int[] {
377           (i - starty + 1) * avcharHeight + yoffset,
378           (i - starty + 1) * avcharHeight + yoffset - avcharHeight / 4,
379           (i - starty + 1) * avcharHeight + yoffset }, 3);
380
381     }
382   }
383
384   boolean setHiddenFont(SequenceI seq)
385   {
386     Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()
387             .getSize());
388
389     if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
390     {
391       gg.setFont(bold);
392       return true;
393     }
394     return false;
395   }
396 }