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