fix display of hidden sequence markers in applet
[jalview.git] / src / jalview / appletgui / IdCanvas.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19
20 package jalview.appletgui;
21
22 import java.awt.*;
23
24 import jalview.datamodel.*;
25
26 public class IdCanvas
27     extends Panel
28 {
29   protected AlignViewport av;
30
31   protected boolean showScores = true;
32
33   protected int maxIdLength = -1;
34   protected String maxIdStr = null;
35   Image image;
36   Graphics gg;
37   int imgHeight = 0;
38   boolean fastPaint = false;
39
40   java.util.Vector searchResults;
41
42   public IdCanvas(AlignViewport av)
43   {
44     setLayout(null);
45     this.av = av;
46     PaintRefresher.Register(this, av.getSequenceSetId());
47   }
48
49   public void drawIdString(Graphics gg, SequenceI s, int i, int starty,
50                            int ypos)
51   {
52     int charHeight = av.getCharHeight();
53
54     if (searchResults != null && searchResults.contains(s))
55     {
56       gg.setColor(Color.black);
57       gg.fillRect(0, ( (i - starty) * charHeight) + ypos,
58                   getSize().width, charHeight);
59       gg.setColor(Color.white);
60     }
61     else if (av.getSelectionGroup() != null &&
62              av.getSelectionGroup().getSequences(null).contains(s))
63     {
64       gg.setColor(Color.lightGray);
65       gg.fillRect(0, ( (i - starty) * charHeight) + ypos,
66                   getSize().width, charHeight);
67       gg.setColor(Color.white);
68     }
69     else
70     {
71       gg.setColor(av.getSequenceColour(s));
72       gg.fillRect(0, ( (i - starty) * charHeight) + ypos,
73                   getSize().width, charHeight);
74       gg.setColor(Color.black);
75     }
76
77     gg.drawString(s.getDisplayId(av.getShowJVSuffix()), 0,
78                   ( (i - starty) * charHeight) + ypos +
79                   charHeight - (charHeight / 5));
80
81     if (av.hasHiddenRows && av.showHiddenMarkers)
82     {
83       drawMarker(i, starty, ypos);
84     }
85
86   }
87
88   public void fastPaint(int vertical)
89   {
90     if (gg == null)
91     {
92       repaint();
93       return;
94     }
95
96     gg.copyArea(0, 0, getSize().width, imgHeight, 0, -vertical * av.charHeight);
97
98     int ss = av.startSeq, es = av.endSeq, transY = 0;
99     if (vertical > 0) // scroll down
100     {
101       ss = es - vertical;
102       if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
103       {
104         ss = av.startSeq;
105       }
106       else
107       {
108         transY = imgHeight - vertical * av.charHeight;
109       }
110     }
111     else if (vertical < 0)
112     {
113       es = ss - vertical;
114       if (es > av.endSeq)
115       {
116         es = av.endSeq;
117       }
118     }
119
120     gg.translate(0, transY);
121
122     drawIds(ss, es);
123
124     gg.translate(0, -transY);
125
126     fastPaint = true;
127     repaint();
128   }
129
130   public void update(Graphics g)
131   {
132     paint(g);
133   }
134
135   public void paint(Graphics g)
136   {
137     if (getSize().height < 0 || getSize().width < 0)
138     {
139       return;
140     }
141     if (fastPaint)
142     {
143       fastPaint = false;
144       g.drawImage(image, 0, 0, this);
145       return;
146     }
147
148     imgHeight = getSize().height;
149     imgHeight -= imgHeight % av.charHeight;
150
151     if (imgHeight < 1)
152     {
153       return;
154     }
155
156     if (image == null || imgHeight != image.getHeight(this))
157     {
158       image = createImage(getSize().width, imgHeight);
159       gg = image.getGraphics();
160       gg.setFont(av.getFont());
161     }
162
163     //Fill in the background
164     gg.setColor(Color.white);
165     Font italic = new Font(av.getFont().getName(), Font.ITALIC,
166                            av.getFont().getSize());
167     gg.setFont(italic);
168
169     gg.fillRect(0, 0, getSize().width, getSize().height);
170     drawIds(av.startSeq, av.endSeq);
171     g.drawImage(image, 0, 0, this);
172   }
173   
174   void drawIds(int starty, int endy)
175   {
176     Font italic = new Font(av.getFont().getName(), Font.ITALIC,
177                            av.getFont().getSize());
178
179     gg.setFont(italic);
180
181     Color currentColor = Color.white;
182     Color currentTextColor = Color.black;
183
184     if (av.getWrapAlignment())
185     {
186       int maxwidth = av.alignment.getWidth();
187       int alheight = av.alignment.getHeight();
188
189       if (av.hasHiddenColumns)
190       {
191         maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
192       }
193
194       int annotationHeight = 0;
195       AnnotationLabels labels = null;
196
197       if (av.showAnnotation)
198       {
199         AnnotationPanel ap = new AnnotationPanel(av);
200         annotationHeight = ap.adjustPanelHeight();
201         labels = new AnnotationLabels(av);
202       }
203
204       int hgap = av.charHeight;
205       if (av.scaleAboveWrapped)
206       {
207         hgap += av.charHeight;
208       }
209
210       int cHeight = alheight * av.charHeight
211           + hgap
212           + annotationHeight;
213
214       int rowSize = av.getEndRes() - av.getStartRes();
215
216       // Draw the rest of the panels
217       for (int ypos = hgap, row = av.startRes;
218            (ypos <= getSize().height) && (row < maxwidth);
219            ypos += cHeight, row += rowSize)
220       {
221         for (int i = starty; i < alheight; i++)
222         {
223
224           SequenceI s = av.alignment.getSequenceAt(i);
225           gg.setFont(italic);
226           if (av.hasHiddenRows)
227           {
228             setHiddenFont(s);
229           }
230           drawIdString(gg, s, i, 0, ypos);
231         }
232
233         if (labels != null)
234         {
235           gg.translate(0, ypos + (alheight * av.charHeight));
236           labels.drawComponent(gg, getSize().width);
237           gg.translate(0, -ypos - (alheight * av.charHeight));
238         }
239
240       }
241     }
242     else
243     {
244       //Now draw the id strings
245       SequenceI seq;
246       for (int i = starty; i < endy; i++)
247       {
248
249         seq = av.alignment.getSequenceAt(i);
250         if (seq == null)
251         {
252           continue;
253         }
254         gg.setFont(italic);
255         //boolean isrep=false;
256         if (av.hasHiddenRows)
257         {
258           //isrep = 
259           setHiddenFont(seq);
260         }
261
262         // Selected sequence colours
263         if ( (searchResults != null) &&
264             searchResults.contains(seq))
265         {
266           currentColor = Color.black;
267           currentTextColor = Color.white;
268         }
269         else if ( (av.getSelectionGroup() != null) &&
270                  av.getSelectionGroup().getSequences(null).contains(seq))
271         {
272           currentColor = Color.lightGray;
273           currentTextColor = Color.black;
274         }
275         else
276         {
277           currentColor = av.getSequenceColour(seq);
278           currentTextColor = Color.black;
279         }
280
281         gg.setColor(currentColor);
282         // TODO: isrep could be used to highlight the representative in a different way
283         gg.fillRect(0, (i - starty) * av.charHeight, getSize().width,
284                     av.charHeight);
285         gg.setColor(currentTextColor);
286
287         gg.drawString(seq.getDisplayId(av.getShowJVSuffix()),
288                       0,
289                       ( ( (i - starty) * av.charHeight) + av.charHeight) -
290                       (av.charHeight / 5));
291
292         if (av.hasHiddenRows && av.showHiddenMarkers)
293         {
294           drawMarker(i, starty, 0);
295         }
296       }
297     }
298   }
299
300   public void setHighlighted(java.util.Vector found)
301   {
302     searchResults = found;
303     repaint();
304   }
305
306   void drawMarker(int i, int starty, int yoffset)
307   {
308     SequenceI[] hseqs = av.alignment.getHiddenSequences().hiddenSequences;
309     //Use this method here instead of calling hiddenSeq adjust
310     //3 times.
311     int hSize = hseqs.length;
312
313     int hiddenIndex = i;
314     int lastIndex = i - 1;
315     int nextIndex = i + 1;
316
317
318     for (int j = 0; j < hSize; j++)
319     {
320       if (hseqs[j] != null)
321       {
322         if (j - 1 < hiddenIndex)
323         {
324           hiddenIndex++;
325         }
326         if (j - 1 < lastIndex)
327         {
328           lastIndex++;
329         }
330         if (j - 1 < nextIndex)
331         {
332           nextIndex++;
333         }
334       }
335     }
336
337     boolean below = (hiddenIndex > lastIndex + 1);
338     boolean above = (nextIndex > hiddenIndex + 1);
339
340     gg.setColor(Color.blue);
341     if (below)
342     {
343       gg.fillPolygon(new int[]
344                      {getSize().width - av.charHeight,
345                      getSize().width - av.charHeight,
346                      getSize().width},
347                      new int[]
348                      {
349                      (i - starty) * av.charHeight + yoffset,
350                      (i - starty) * av.charHeight + yoffset + av.charHeight / 4,
351                      (i - starty) * av.charHeight + yoffset
352       }, 3);
353     }
354     if (above)
355     {
356       gg.fillPolygon(new int[]
357                      {getSize().width - av.charHeight,
358                      getSize().width - av.charHeight,
359                      getSize().width},
360                      new int[]
361                      {
362                      (i - starty + 1) * av.charHeight + yoffset,
363                      (i - starty + 1) * av.charHeight + yoffset -
364                      av.charHeight / 4,
365                      (i - starty + 1) * av.charHeight + yoffset
366       }, 3);
367
368     }
369   }
370   boolean setHiddenFont(SequenceI seq)
371   {
372     Font bold = new Font(av.getFont().getName(), Font.BOLD
373             ,
374                          av.getFont().getSize());
375
376     if (av.hiddenRepSequences != null &&
377         av.hiddenRepSequences.containsKey(seq))
378     {
379       gg.setFont(bold);
380       return true;
381     }
382     return false;
383   }
384 }