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