JAL-2388 Hidden cols separated from column selection (almost complete)
[jalview.git] / src / jalview / gui / 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.gui;
22
23 import jalview.datamodel.SequenceI;
24 import jalview.viewmodel.ViewportRanges;
25
26 import java.awt.BorderLayout;
27 import java.awt.Color;
28 import java.awt.Font;
29 import java.awt.FontMetrics;
30 import java.awt.Graphics;
31 import java.awt.Graphics2D;
32 import java.awt.RenderingHints;
33 import java.awt.image.BufferedImage;
34 import java.util.List;
35
36 import javax.swing.JPanel;
37
38 /**
39  * DOCUMENT ME!
40  * 
41  * @author $author$
42  * @version $Revision$
43  */
44 public class IdCanvas extends JPanel
45 {
46   protected AlignViewport av;
47
48   protected boolean showScores = true;
49
50   protected int maxIdLength = -1;
51
52   protected String maxIdStr = null;
53
54   BufferedImage image;
55
56   Graphics2D gg;
57
58   int imgHeight = 0;
59
60   boolean fastPaint = false;
61
62   List<SequenceI> searchResults;
63
64   FontMetrics fm;
65
66   AnnotationLabels labels = null;
67
68   AnnotationPanel ap;
69
70   private Font idfont;
71
72   /**
73    * Creates a new IdCanvas object.
74    * 
75    * @param av
76    *          DOCUMENT ME!
77    */
78   public IdCanvas(AlignViewport av)
79   {
80     setLayout(new BorderLayout());
81     this.av = av;
82     PaintRefresher.Register(this, av.getSequenceSetId());
83   }
84
85   /**
86    * DOCUMENT ME!
87    * 
88    * @param gg
89    *          DOCUMENT ME!
90    * @param hiddenRows
91    *          true - check and display hidden row marker if need be
92    * @param s
93    *          DOCUMENT ME!
94    * @param i
95    *          DOCUMENT ME!
96    * @param starty
97    *          DOCUMENT ME!
98    * @param ypos
99    *          DOCUMENT ME!
100    */
101   public void drawIdString(Graphics2D gg, boolean hiddenRows, SequenceI s,
102           int i, int starty, int ypos)
103   {
104     int xPos = 0;
105     int panelWidth = getWidth();
106     int charHeight = av.getCharHeight();
107
108     if ((searchResults != null) && searchResults.contains(s))
109     {
110       gg.setColor(Color.black);
111       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
112               charHeight);
113       gg.setColor(Color.white);
114     }
115     else if ((av.getSelectionGroup() != null)
116             && av.getSelectionGroup().getSequences(null).contains(s))
117     {
118       gg.setColor(Color.lightGray);
119       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
120               charHeight);
121       gg.setColor(Color.white);
122     }
123     else
124     {
125       gg.setColor(av.getSequenceColour(s));
126       gg.fillRect(0, ((i - starty) * charHeight) + ypos, getWidth(),
127               charHeight);
128       gg.setColor(Color.black);
129     }
130
131     if (av.isRightAlignIds())
132     {
133       xPos = panelWidth
134               - fm.stringWidth(s.getDisplayId(av.getShowJVSuffix())) - 4;
135     }
136
137     gg.drawString(s.getDisplayId(av.getShowJVSuffix()), xPos,
138             (((i - starty + 1) * charHeight) + ypos) - (charHeight / 5));
139
140     if (hiddenRows)
141     {
142       drawMarker(i, starty, ypos);
143     }
144
145   }
146
147   /**
148    * DOCUMENT ME!
149    * 
150    * @param vertical
151    *          DOCUMENT ME!
152    */
153   public void fastPaint(int vertical)
154   {
155     if (gg == null)
156     {
157       repaint();
158
159       return;
160     }
161
162     ViewportRanges ranges = av.getRanges();
163
164     gg.copyArea(0, 0, getWidth(), imgHeight, 0,
165             -vertical * av.getCharHeight());
166
167     int ss = ranges.getStartSeq();
168     int es = ranges.getEndSeq();
169     int transY = 0;
170
171     if (vertical > 0) // scroll down
172     {
173       ss = es - vertical;
174
175       if (ss < ranges.getStartSeq())
176       { // ie scrolling too fast, more than a page at a time
177         ss = ranges.getStartSeq();
178       }
179       else
180       {
181         transY = imgHeight - ((vertical + 1) * av.getCharHeight());
182       }
183     }
184     else if (vertical < 0) // scroll up
185     {
186       es = ss - vertical;
187
188       if (es > ranges.getEndSeq())
189       {
190         es = ranges.getEndSeq();
191       }
192     }
193
194     gg.translate(0, transY);
195
196     drawIds(ss, es);
197
198     gg.translate(0, -transY);
199
200     fastPaint = true;
201     repaint();
202   }
203
204   /**
205    * DOCUMENT ME!
206    * 
207    * @param g
208    *          DOCUMENT ME!
209    */
210   @Override
211   public void paintComponent(Graphics g)
212   {
213     g.setColor(Color.white);
214     g.fillRect(0, 0, getWidth(), getHeight());
215
216     if (fastPaint)
217     {
218       fastPaint = false;
219       g.drawImage(image, 0, 0, this);
220
221       return;
222     }
223
224     int oldHeight = imgHeight;
225
226     imgHeight = getHeight();
227     imgHeight -= (imgHeight % av.getCharHeight());
228
229     if (imgHeight < 1)
230     {
231       return;
232     }
233
234     if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
235     {
236       image = new BufferedImage(getWidth(), imgHeight,
237               BufferedImage.TYPE_INT_RGB);
238     }
239
240     gg = (Graphics2D) image.getGraphics();
241
242     // Fill in the background
243     gg.setColor(Color.white);
244     gg.fillRect(0, 0, getWidth(), imgHeight);
245
246     drawIds(av.getRanges().getStartSeq(), av.getRanges().getEndSeq());
247
248     g.drawImage(image, 0, 0, this);
249   }
250
251   /**
252    * DOCUMENT ME!
253    * 
254    * @param starty
255    *          DOCUMENT ME!
256    * @param endy
257    *          DOCUMENT ME!
258    */
259   void drawIds(int starty, int endy)
260   {
261     if (av.isSeqNameItalics())
262     {
263       setIdfont(new Font(av.getFont().getName(), Font.ITALIC, av.getFont()
264               .getSize()));
265     }
266     else
267     {
268       setIdfont(av.getFont());
269     }
270
271     gg.setFont(getIdfont());
272     fm = gg.getFontMetrics();
273
274     if (av.antiAlias)
275     {
276       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
277               RenderingHints.VALUE_ANTIALIAS_ON);
278     }
279
280     Color currentColor = Color.white;
281     Color currentTextColor = Color.black;
282
283     final boolean doHiddenCheck = av.isDisplayReferenceSeq()
284             || av.hasHiddenRows(), hiddenRows = av.hasHiddenRows();
285
286     if (av.getWrapAlignment())
287     {
288       int maxwidth = av.getAlignment().getWidth();
289       int alheight = av.getAlignment().getHeight();
290
291       if (av.hasHiddenColumns())
292       {
293         maxwidth = av.getAlignment().getHiddenColumns()
294                 .findColumnPosition(maxwidth) - 1;
295       }
296
297       int annotationHeight = 0;
298
299       if (av.isShowAnnotation())
300       {
301         if (ap == null)
302         {
303           ap = new AnnotationPanel(av);
304         }
305
306         annotationHeight = ap.adjustPanelHeight();
307         if (labels == null)
308         {
309           labels = new AnnotationLabels(av);
310         }
311       }
312
313       int hgap = av.getCharHeight();
314       if (av.getScaleAboveWrapped())
315       {
316         hgap += av.getCharHeight();
317       }
318
319       int cHeight = alheight * av.getCharHeight() + hgap + annotationHeight;
320
321       int rowSize = av.getRanges().getEndRes()
322               - av.getRanges().getStartRes();
323
324       // Draw the rest of the panels
325       for (int ypos = hgap, row = av.getRanges().getStartRes(); (ypos <= getHeight())
326               && (row < maxwidth); ypos += cHeight, row += rowSize)
327       {
328         for (int i = starty; i < alheight; i++)
329         {
330           SequenceI s = av.getAlignment().getSequenceAt(i);
331           if (doHiddenCheck)
332           {
333             setHiddenFont(s);
334           }
335           else
336           {
337             gg.setFont(getIdfont());
338           }
339
340           drawIdString(gg, hiddenRows, s, i, 0, ypos);
341         }
342
343         if (labels != null && av.isShowAnnotation())
344         {
345           gg.translate(0, ypos + (alheight * av.getCharHeight()));
346           labels.drawComponent(gg, getWidth());
347           gg.translate(0, -ypos - (alheight * av.getCharHeight()));
348         }
349       }
350     }
351     else
352     {
353       // No need to hang on to labels if we're not wrapped
354       labels = null;
355
356       // Now draw the id strings
357       int panelWidth = getWidth();
358       int xPos = 0;
359
360       SequenceI sequence;
361       // Now draw the id strings
362       for (int i = starty; i <= endy; i++)
363       {
364         sequence = av.getAlignment().getSequenceAt(i);
365
366         if (sequence == null)
367         {
368           continue;
369         }
370
371         if (doHiddenCheck)
372         {
373           setHiddenFont(sequence);
374         }
375
376         // Selected sequence colours
377         if ((searchResults != null) && searchResults.contains(sequence))
378         {
379           currentColor = Color.black;
380           currentTextColor = Color.white;
381         }
382         else if ((av.getSelectionGroup() != null)
383                 && av.getSelectionGroup().getSequences(null)
384                         .contains(sequence))
385         {
386           currentColor = Color.lightGray;
387           currentTextColor = Color.black;
388         }
389         else
390         {
391           currentColor = av.getSequenceColour(sequence);
392           currentTextColor = Color.black;
393         }
394
395         gg.setColor(currentColor);
396
397         gg.fillRect(0, (i - starty) * av.getCharHeight(), getWidth(),
398                 av.getCharHeight());
399
400         gg.setColor(currentTextColor);
401
402         String string = sequence.getDisplayId(av.getShowJVSuffix());
403
404         if (av.isRightAlignIds())
405         {
406           xPos = panelWidth - fm.stringWidth(string) - 4;
407         }
408
409         gg.drawString(string, xPos,
410                 (((i - starty) * av.getCharHeight()) + av.getCharHeight())
411                         - (av.getCharHeight() / 5));
412
413         if (hiddenRows)
414         {
415           drawMarker(i, starty, 0);
416         }
417
418       }
419
420     }
421   }
422
423   void drawMarker(int i, int starty, int yoffset)
424   {
425
426     SequenceI[] hseqs = av.getAlignment().getHiddenSequences().hiddenSequences;
427     // Use this method here instead of calling hiddenSeq adjust
428     // 3 times.
429     int hSize = hseqs.length;
430
431     int hiddenIndex = i;
432     int lastIndex = i - 1;
433     int nextIndex = i + 1;
434
435     for (int j = 0; j < hSize; j++)
436     {
437       if (hseqs[j] != null)
438       {
439         if (j - 1 < hiddenIndex)
440         {
441           hiddenIndex++;
442         }
443         if (j - 1 < lastIndex)
444         {
445           lastIndex++;
446         }
447         if (j - 1 < nextIndex)
448         {
449           nextIndex++;
450         }
451       }
452     }
453
454     boolean below = (hiddenIndex > lastIndex + 1);
455     boolean above = (nextIndex > hiddenIndex + 1);
456
457     gg.setColor(Color.blue);
458     if (below)
459     {
460       gg.fillPolygon(
461               new int[] { getWidth() - av.getCharHeight(),
462                   getWidth() - av.getCharHeight(), getWidth() },
463               new int[] {
464                   (i - starty) * av.getCharHeight() + yoffset,
465                   (i - starty) * av.getCharHeight() + yoffset
466                           + av.getCharHeight() / 4,
467                   (i - starty) * av.getCharHeight() + yoffset }, 3);
468     }
469     if (above)
470     {
471       gg.fillPolygon(
472               new int[] { getWidth() - av.getCharHeight(),
473                   getWidth() - av.getCharHeight(), getWidth() },
474               new int[] {
475                   (i - starty + 1) * av.getCharHeight() + yoffset,
476                   (i - starty + 1) * av.getCharHeight() + yoffset
477                           - av.getCharHeight() / 4,
478                   (i - starty + 1) * av.getCharHeight() + yoffset }, 3);
479
480     }
481   }
482
483   void setHiddenFont(SequenceI seq)
484   {
485     Font bold = new Font(av.getFont().getName(), Font.BOLD, av.getFont()
486             .getSize());
487
488     if (av.isReferenceSeq(seq) || av.isHiddenRepSequence(seq))
489     {
490       gg.setFont(bold);
491     }
492     else
493     {
494       gg.setFont(getIdfont());
495     }
496   }
497
498   /**
499    * DOCUMENT ME!
500    * 
501    * @param list
502    *          DOCUMENT ME!
503    */
504   public void setHighlighted(List<SequenceI> list)
505   {
506     searchResults = list;
507     repaint();
508   }
509
510   public Font getIdfont()
511   {
512     return idfont;
513   }
514
515   public void setIdfont(Font idfont)
516   {
517     this.idfont = idfont;
518   }
519 }