JAL-1807 - Bob's last(?) before leaving Dundee -- adds fast file loading
[jalviewjs.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
25 import java.awt.Color;
26 import java.awt.Font;
27 import java.awt.Graphics;
28 import java.awt.Image;
29 import awt2swing.Panel;
30 import java.util.List;
31
32 public class IdCanvas extends Panel
33 {
34   protected AlignViewport av;
35
36   protected boolean showScores = true;
37
38   protected int maxIdLength = -1;
39
40   protected String maxIdStr = null;
41
42   Image image;
43
44   Graphics gg;
45
46   int imgHeight = 0;
47
48   boolean fastPaint = false;
49
50   List<SequenceI> searchResults;
51
52   public IdCanvas(AlignViewport av)
53   {
54     setLayout(null);
55     this.av = av;
56     PaintRefresher.Register(this, av.getSequenceSetId());
57   }
58
59   public void drawIdString(Graphics gg, boolean hiddenRows, SequenceI s,
60           int i, int starty,
61           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     awt2swing.Util.drawString(gg, 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     gg.copyArea(0, 0, getSize().width, imgHeight, 0,
108             -vertical * av.getCharHeight());
109
110     int ss = av.startSeq, es = av.endSeq, transY = 0;
111     if (vertical > 0) // scroll down
112     {
113       ss = es - vertical;
114       if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
115       {
116         ss = av.startSeq;
117       }
118       else
119       {
120         transY = imgHeight - vertical * av.getCharHeight();
121       }
122     }
123     else if (vertical < 0)
124     {
125       es = ss - vertical;
126       if (es > av.endSeq)
127       {
128         es = av.endSeq;
129       }
130     }
131
132     gg.translate(0, transY);
133
134     drawIds(ss, es);
135
136     gg.translate(0, -transY);
137
138     fastPaint = true;
139     repaint();
140   }
141
142   public void paintComponent(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.getCharHeight();
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   /**
182    * local copy of av.getCharHeight set at top of drawIds
183    */
184   private int avcharHeight;
185   void drawIds(int starty, int endy)
186   {
187     // hardwired italic IDs in applet currently
188     Font italic = new Font(av.getFont().getName(), Font.ITALIC, av
189             .getFont().getSize());
190     // temp variable for speed
191     avcharHeight = av.getCharHeight();
192
193     gg.setFont(italic);
194
195     Color currentColor = Color.white;
196     Color currentTextColor = Color.black;
197
198     final boolean doHiddenCheck = av.isDisplayReferenceSeq()
199             || av.hasHiddenRows(), hiddenRows = av.hasHiddenRows()
200             && av.getShowHiddenMarkers();
201
202     if (av.getWrapAlignment())
203     {
204       int maxwidth = av.getAlignment().getWidth();
205       int alheight = av.getAlignment().getHeight();
206
207       if (av.hasHiddenColumns())
208       {
209         maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
210       }
211
212       int annotationHeight = 0;
213       AnnotationLabels labels = null;
214
215       if (av.isShowAnnotation())
216       {
217         AnnotationPanel ap = new AnnotationPanel(av);
218         annotationHeight = ap.adjustPanelHeight();
219         labels = new AnnotationLabels(av);
220       }
221       int hgap = avcharHeight;
222       if (av.getScaleAboveWrapped())
223       {
224         hgap += avcharHeight;
225       }
226
227       int cHeight = alheight * avcharHeight + hgap + annotationHeight;
228
229       int rowSize = av.getEndRes() - av.getStartRes();
230       // Draw the rest of the panels
231       for (int ypos = hgap, row = av.startRes; (ypos <= getSize().height)
232               && (row < maxwidth); ypos += cHeight, row += rowSize)
233       {
234         for (int i = starty; i < alheight; i++)
235         {
236
237           SequenceI s = av.getAlignment().getSequenceAt(i);
238           gg.setFont(italic);
239           if (doHiddenCheck)
240           {
241             setHiddenFont(s);
242           }
243           drawIdString(gg, hiddenRows, s, i, 0, ypos);
244         }
245
246         if (labels != null)
247         {
248           gg.translate(0, ypos + (alheight * avcharHeight));
249           labels.drawComponent(gg, getSize().width);
250           gg.translate(0, -ypos - (alheight * avcharHeight));
251         }
252
253       }
254     }
255     else
256     {
257       // Now draw the id strings
258       SequenceI seq;
259       for (int i = starty; i < endy; i++)
260       {
261
262         seq = av.getAlignment().getSequenceAt(i);
263         if (seq == null)
264         {
265           continue;
266         }
267         gg.setFont(italic);
268         // boolean isrep=false;
269         if (doHiddenCheck)
270         {
271           // isrep =
272           setHiddenFont(seq);
273         }
274
275         // Selected sequence colours
276         if ((searchResults != null) && searchResults.contains(seq))
277         {
278           currentColor = Color.black;
279           currentTextColor = Color.white;
280         }
281         else if ((av.getSelectionGroup() != null)
282                 && av.getSelectionGroup().getSequences(null).contains(seq))
283         {
284           currentColor = Color.lightGray;
285           currentTextColor = Color.black;
286         }
287         else
288         {
289           currentColor = av.getSequenceColour(seq);
290           currentTextColor = Color.black;
291         }
292
293         gg.setColor(currentColor);
294         // TODO: isrep could be used to highlight the representative in a
295         // different way
296         gg.fillRect(0, (i - starty) * avcharHeight, getSize().width,
297                 avcharHeight);
298         gg.setColor(currentTextColor);
299
300         awt2swing.Util.drawString(gg, seq.getDisplayId(av.getShowJVSuffix()), 0,
301                 (((i - starty) * avcharHeight) + avcharHeight)
302                         - (avcharHeight / 5));
303
304         if (hiddenRows)
305         {
306           drawMarker(i, starty, 0);
307         }
308       }
309     }
310   }
311
312   public void setHighlighted(List<SequenceI> list)
313   {
314     searchResults = list;
315     repaint();
316   }
317
318   void drawMarker(int i, int starty, int yoffset)
319   {
320     SequenceI[] hseqs = av.getAlignment().getHiddenSequences().hiddenSequences;
321     // Use this method here instead of calling hiddenSeq adjust
322     // 3 times.
323     int hSize = hseqs.length;
324
325     int hiddenIndex = i;
326     int lastIndex = i - 1;
327     int nextIndex = i + 1;
328
329     for (int j = 0; j < hSize; j++)
330     {
331       if (hseqs[j] != null)
332       {
333         if (j - 1 < hiddenIndex)
334         {
335           hiddenIndex++;
336         }
337         if (j - 1 < lastIndex)
338         {
339           lastIndex++;
340         }
341         if (j - 1 < nextIndex)
342         {
343           nextIndex++;
344         }
345       }
346     }
347
348     boolean below = (hiddenIndex > lastIndex + 1);
349     boolean above = (nextIndex > hiddenIndex + 1);
350
351     gg.setColor(Color.blue);
352     if (below)
353     {
354       gg.fillPolygon(new int[]
355       { getSize().width - avcharHeight, getSize().width - avcharHeight,
356           getSize().width }, new int[]
357       { (i - starty) * avcharHeight + yoffset,
358           (i - starty) * avcharHeight + yoffset + avcharHeight / 4,
359           (i - starty) * avcharHeight + yoffset }, 3);
360     }
361     if (above)
362     {
363       gg.fillPolygon(new int[]
364       { getSize().width - avcharHeight, getSize().width - avcharHeight,
365           getSize().width }, new int[]
366       { (i - starty + 1) * avcharHeight + yoffset,
367           (i - starty + 1) * avcharHeight + yoffset - avcharHeight / 4,
368           (i - starty + 1) * avcharHeight + yoffset }, 3);
369
370     }
371   }
372
373   boolean setHiddenFont(SequenceI seq)
374   {
375     Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()
376             .getSize());
377
378     if (av.isHiddenRepSequence(seq))
379     {
380       gg.setFont(bold);
381       return true;
382     }
383     return false;
384   }
385 }