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