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