2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
20 package jalview.appletgui;
\r
25 import java.awt.event.*;
\r
27 import jalview.datamodel.*;
\r
29 public class AnnotationPanel
\r
30 extends Panel implements AdjustmentListener
\r
37 static String HELIX = "Helix";
\r
38 static String SHEET = "Sheet";
\r
39 static String LABEL = "Label";
\r
40 static String REMOVE = "Remove Annotation";
\r
41 static String COLOUR = "Colour";
\r
42 static Color HELIX_COLOUR = Color.red.darker();
\r
43 static Color SHEET_COLOUR = Color.green.darker().darker();
\r
50 boolean fastPaint = false;
\r
52 public static int GRAPH_HEIGHT = 40;
\r
54 boolean MAC = false;
\r
56 public AnnotationPanel(AlignmentPanel ap)
\r
58 if (System.getProperty("os.name").startsWith("Mac"))
\r
66 adjustPanelHeight();
\r
68 addMouseMotionListener(new MouseMotionAdapter()
\r
70 public void mouseMoved(MouseEvent evt)
\r
76 // ap.annotationScroller.getVAdjustable().addAdjustmentListener( this );
\r
79 public AnnotationPanel(AlignViewport av)
\r
84 public void adjustmentValueChanged(AdjustmentEvent evt)
\r
86 ap.alabels.setScrollOffset( -evt.getValue());
\r
89 public int adjustPanelHeight()
\r
91 // setHeight of panels
\r
92 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
97 for (int i = 0; i < aa.length; i++)
\r
108 aa[i].height += av.charHeight;
\r
110 if (aa[i].hasIcons)
\r
112 aa[i].height += 16;
\r
115 if (aa[i].graph > 0)
\r
117 aa[i].height += GRAPH_HEIGHT;
\r
120 if (aa[i].height == 0)
\r
124 height += aa[i].height;
\r
132 this.setSize(getSize().width, height);
\r
140 public void addEditableColumn(int i)
\r
142 if (activeRow == -1)
\r
144 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
150 for (int j = 0; j < aa.length; j++)
\r
152 if (aa[j].editable)
\r
160 if (activeRes == null)
\r
162 activeRes = new Vector();
\r
163 activeRes.addElement(String.valueOf(i));
\r
167 activeRes.addElement(String.valueOf(i));
\r
170 public void doMouseMoved(MouseEvent evt)
\r
172 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
180 for (int i = 0; i < aa.length; i++)
\r
185 height += aa[i].height;
\r
188 if (evt.getY() < height)
\r
195 int res = evt.getX() / av.getCharWidth() + av.getStartRes();
\r
197 if (av.hasHiddenColumns)
\r
199 res = av.getColumnSelection().adjustForHiddenColumns(res);
\r
202 if (row > -1 && res < aa[row].annotations.length && aa[row].annotations[res] != null)
\r
204 StringBuffer text = new StringBuffer("Sequence position " + (res + 1));
\r
205 if (aa[row].annotations[res].description != null)
\r
207 text.append(" " + aa[row].annotations[res].description);
\r
209 ap.alignFrame.statusBar.setText(text.toString());
\r
213 public void update(Graphics g)
\r
218 public void paint(Graphics g)
\r
221 imgWidth = getSize().width;
\r
222 //(av.endRes - av.startRes + 1) * av.charWidth;
\r
224 if (image == null || imgWidth != image.getWidth(this))
\r
226 image = createImage(imgWidth, ap.annotationPanel.getSize().height);
\r
227 gg = image.getGraphics();
\r
228 gg.setFont(av.getFont());
\r
229 fm = gg.getFontMetrics();
\r
235 g.drawImage(image, 0, 0, this);
\r
240 gg.setColor(Color.white);
\r
241 gg.fillRect(0, 0, getSize().width, getSize().height);
\r
242 drawComponent(gg, av.startRes, av.endRes + 1);
\r
244 g.drawImage(image, 0, 0, this);
\r
247 public void fastPaint(int horizontal)
\r
249 if (horizontal == 0
\r
250 || av.alignment.getAlignmentAnnotation() == null
\r
251 || av.alignment.getAlignmentAnnotation().length < 1
\r
258 gg.copyArea(0, 0, imgWidth, getSize().height, -horizontal * av.charWidth, 0);
\r
259 int sr = av.startRes, er = av.endRes + 1, transX = 0;
\r
261 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
263 transX = (er - sr - horizontal) * av.charWidth;
\r
264 sr = er - horizontal;
\r
266 else if (horizontal < 0)
\r
268 er = sr - horizontal;
\r
271 gg.translate(transX, 0);
\r
273 drawComponent(gg, sr, er);
\r
275 gg.translate( -transX, 0);
\r
284 * @param g DOCUMENT ME!
\r
285 * @param startRes DOCUMENT ME!
\r
286 * @param endRes DOCUMENT ME!
\r
288 public void drawComponent(Graphics g, int startRes, int endRes)
\r
290 g.setFont(av.getFont());
\r
292 g.setColor(Color.white);
\r
293 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getSize().height);
\r
297 fm = g.getFontMetrics();
\r
300 if ( (av.alignment.getAlignmentAnnotation() == null) ||
\r
301 (av.alignment.getAlignmentAnnotation().length < 1))
\r
303 g.setColor(Color.white);
\r
304 g.fillRect(0, 0, getSize().width, getSize().height);
\r
305 g.setColor(Color.black);
\r
306 if (av.validCharWidth)
\r
308 g.drawString("Alignment has no annotations", 20, 15);
\r
314 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
321 int iconOffset = av.charHeight / 2;
\r
322 boolean validRes = false;
\r
324 boolean[] graphGroupDrawn = new boolean[aa.length];
\r
327 for (int i = 0; i < aa.length; i++)
\r
329 AlignmentAnnotation row = aa[i];
\r
341 if (row.graphGroup > -1 && graphGroupDrawn[row.graphGroup])
\r
346 // this is so that we draw the characters below the graph
\r
351 y -= av.charHeight;
\r
357 iconOffset = av.charHeight / 2;
\r
365 while (x < endRes - startRes)
\r
367 if (av.hasHiddenColumns)
\r
369 column = av.getColumnSelection().adjustForHiddenColumns(startRes + x);
\r
370 if (column > row.annotations.length - 1)
\r
377 column = startRes + x;
\r
380 if ( (row.annotations.length <= column) ||
\r
381 (row.annotations[column] == null))
\r
390 if (av.validCharWidth && validRes &&
\r
391 (row.annotations[column].displayCharacter.length() > 0))
\r
393 int charOffset = (av.charWidth -
\r
394 fm.charWidth(row.annotations[column].
\r
395 displayCharacter.charAt(
\r
397 g.setColor(row.annotations[column].colour);
\r
399 if (column == 0 || row.graph > 0)
\r
401 g.drawString(row.annotations[column].displayCharacter,
\r
402 (x * av.charWidth) + charOffset,
\r
403 y + iconOffset + 3);
\r
406 row.annotations[column - 1] == null
\r
407 || (!row.annotations[column].displayCharacter.equals(
\r
408 row.annotations[column - 1].displayCharacter)
\r
410 (row.annotations[column].displayCharacter.length() < 2 &&
\r
411 row.annotations[column].secondaryStructure == ' ')))
\r
413 g.drawString(row.annotations[column].displayCharacter,
\r
414 (x * av.charWidth) + charOffset,
\r
415 y + iconOffset + 3);
\r
422 (row.annotations[column].secondaryStructure != lastSS))
\r
427 g.setColor(HELIX_COLOUR);
\r
430 //Off by 1 offset when drawing rects and ovals
\r
431 //to offscreen image on the MAC
\r
432 g.fillRoundRect(lastSSX, y + 4 + iconOffset,
\r
433 (x * av.charWidth) - lastSSX, 7, 8, 8);
\r
437 int sCol = (lastSSX / av.charWidth) + startRes;
\r
439 int x2 = (x * av.charWidth);
\r
442 row.annotations[sCol - 1] == null ||
\r
443 row.annotations[sCol - 1].secondaryStructure != 'H')
\r
445 g.fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8, 90,
\r
447 x1 += av.charWidth / 2;
\r
450 if (row.annotations[column] == null ||
\r
451 row.annotations[column].secondaryStructure != 'H')
\r
453 g.fillArc( (x * av.charWidth) - av.charWidth,
\r
454 y + 4 + iconOffset, av.charWidth, 8, 270, 180);
\r
455 x2 -= av.charWidth / 2;
\r
458 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
\r
462 g.setColor(SHEET_COLOUR);
\r
463 g.fillRect(lastSSX, y + 4 + iconOffset,
\r
464 (x * av.charWidth) - lastSSX - 4, 7);
\r
465 g.fillPolygon(new int[]
\r
466 { (x * av.charWidth) - 4,
\r
467 (x * av.charWidth) - 4,
\r
468 (x * av.charWidth)},
\r
471 y + iconOffset, y + 14 + iconOffset,
\r
478 g.setColor(Color.gray);
\r
479 g.fillRect(lastSSX, y + 6 + iconOffset,
\r
480 (x * av.charWidth) - lastSSX, 2);
\r
487 lastSS = row.annotations[column].secondaryStructure;
\r
494 lastSSX = (x * av.charWidth);
\r
502 if (column >= row.annotations.length)
\r
504 column = row.annotations.length - 1;
\r
514 g.setColor(HELIX_COLOUR);
\r
517 //Off by 1 offset when drawing rects and ovals
\r
518 //to offscreen image on the MAC
\r
519 g.fillRoundRect(lastSSX, y + 4 + iconOffset,
\r
520 (x * av.charWidth) - lastSSX, 7, 8, 8);
\r
524 int sCol = (lastSSX / av.charWidth) + startRes;
\r
526 int x2 = (x * av.charWidth);
\r
529 row.annotations[sCol - 1] == null ||
\r
530 row.annotations[sCol - 1].secondaryStructure != 'H')
\r
532 g.fillArc(lastSSX, y + 4 + iconOffset, av.charWidth, 8, 90, 180);
\r
533 x1 += av.charWidth / 2;
\r
536 if (row.annotations[column] == null ||
\r
537 row.annotations[column].secondaryStructure != 'H')
\r
539 g.fillArc( (x * av.charWidth) - av.charWidth,
\r
540 y + 4 + iconOffset, av.charWidth, 8, 270,
\r
542 x2 -= av.charWidth / 2;
\r
545 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
\r
550 g.setColor(SHEET_COLOUR);
\r
552 if (row.annotations[endRes] == null
\r
553 || row.annotations[endRes].secondaryStructure != 'E')
\r
555 g.fillRect(lastSSX, y + 4 + iconOffset,
\r
556 (x * av.charWidth) - lastSSX - 4, 7);
\r
557 g.fillPolygon(new int[]
\r
558 { (x * av.charWidth) - 4,
\r
559 (x * av.charWidth) - 4,
\r
560 (x * av.charWidth)},
\r
563 y + iconOffset, y + 14 + iconOffset,
\r
569 g.fillRect(lastSSX, y + 4 + iconOffset,
\r
570 x * av.charWidth - lastSSX, 7);
\r
575 g.setColor(Color.gray);
\r
576 if (!av.wrapAlignment || endRes == av.endRes)
\r
578 g.fillRect(lastSSX, y + 6 + iconOffset,
\r
579 (x * av.charWidth) - lastSSX, 2);
\r
588 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
\r
590 if (row.graphGroup > -1 && !graphGroupDrawn[row.graphGroup])
\r
592 float groupmax = -999999, groupmin = 9999999;
\r
593 for (int gg = 0; gg < aa.length; gg++)
\r
595 if (aa[gg].graphGroup != row.graphGroup)
\r
602 aa[gg].visible = false;
\r
605 if (aa[gg].graphMax > groupmax)
\r
607 groupmax = aa[gg].graphMax;
\r
609 if (aa[gg].graphMin < groupmin)
\r
611 groupmin = aa[gg].graphMin;
\r
615 for (int gg = 0; gg < aa.length; gg++)
\r
617 if (aa[gg].graphGroup == row.graphGroup)
\r
619 drawLineGraph(g, aa[gg], startRes, endRes, y,
\r
620 groupmin, groupmax,
\r
625 graphGroupDrawn[row.graphGroup] = true;
\r
629 drawLineGraph(g, row, startRes, endRes,
\r
630 y, row.graphMin, row.graphMax, row.graphHeight);
\r
633 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
\r
635 drawBarGraph(g, row, startRes, endRes,
\r
636 row.graphMin, row.graphMax, y);
\r
640 if (row.graph > 0 && row.hasText)
\r
642 y += av.charHeight;
\r
645 if (row.graph == 0)
\r
652 public void drawLineGraph(Graphics g, AlignmentAnnotation aa,
\r
653 int sRes, int eRes,
\r
655 float min, float max,
\r
658 if (sRes > aa.annotations.length)
\r
665 //Adjustment for fastpaint to left
\r
666 if (eRes < av.endRes)
\r
671 eRes = Math.min(eRes, aa.annotations.length);
\r
679 int y1 = y, y2 = y;
\r
680 float range = max - min;
\r
685 y2 = y - (int) ( (0 - min / range) * graphHeight);
\r
688 g.setColor(Color.gray);
\r
689 g.drawLine(x - av.charWidth, y2, (eRes - sRes) * av.charWidth, y2);
\r
691 eRes = Math.min(eRes, aa.annotations.length);
\r
694 int aaMax = aa.annotations.length - 1;
\r
696 while (x < eRes - sRes)
\r
699 if (av.hasHiddenColumns)
\r
701 column = av.getColumnSelection().adjustForHiddenColumns(column);
\r
704 if (column > aaMax)
\r
709 if (aa.annotations[column] == null || aa.annotations[column - 1] == null)
\r
715 g.setColor(aa.annotations[column].colour);
\r
717 (int) ( ( (aa.annotations[column - 1].value - min) / range) * graphHeight);
\r
719 (int) ( ( (aa.annotations[column].value - min) / range) * graphHeight);
\r
721 g.drawLine(x * av.charWidth - av.charWidth / 2, y1,
\r
722 x * av.charWidth + av.charWidth / 2, y2);
\r
726 if (aa.threshold != null)
\r
728 g.setColor(aa.threshold.colour);
\r
730 y2 = (int) (y - ( (aa.threshold.value - min) / range) * graphHeight);
\r
731 g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
\r
735 public void drawBarGraph(Graphics g, AlignmentAnnotation aa,
\r
736 int sRes, int eRes,
\r
737 float min, float max,
\r
740 if (sRes > aa.annotations.length)
\r
745 eRes = Math.min(eRes, aa.annotations.length);
\r
747 int x = 0, y1 = y, y2 = y;
\r
749 float range = max - min;
\r
753 y2 = y - (int) ( (0 - min / (range)) * aa.graphHeight);
\r
756 g.setColor(Color.gray);
\r
758 g.drawLine(x, y2, (eRes - sRes) * av.charWidth, y2);
\r
761 int aaMax = aa.annotations.length - 1;
\r
763 while (x < eRes - sRes)
\r
766 if (av.hasHiddenColumns)
\r
768 column = av.getColumnSelection().adjustForHiddenColumns(column);
\r
771 if (column > aaMax)
\r
776 if (aa.annotations[column] == null)
\r
782 g.setColor(aa.annotations[column].colour);
\r
784 (int) ( ( (aa.annotations[column].value - min) / (range)) * aa.graphHeight);
\r
788 g.fillRect(x * av.charWidth, y2, av.charWidth, y1 - y2);
\r
792 g.fillRect(x * av.charWidth, y1, av.charWidth, y2 - y1);
\r
798 if (aa.threshold != null)
\r
800 g.setColor(aa.threshold.colour);
\r
801 y2 = (int) (y - ( (aa.threshold.value - min) / range) * aa.graphHeight);
\r
802 g.drawLine(0, y2, (eRes - sRes) * av.charWidth, y2);
\r
806 // used by overview window
\r
807 public void drawGraph(Graphics g, AlignmentAnnotation aa, int width, int y,
\r
808 int sRes, int eRes)
\r
810 g.setColor(Color.white);
\r
811 g.fillRect(0, 0, width, y);
\r
812 g.setColor(new Color(0, 0, 180));
\r
816 for (int j = sRes; j < eRes; j++)
\r
818 g.setColor(aa.annotations[j].colour);
\r
820 height = (int) ( (aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);
\r
825 g.fillRect(x, y - height, av.charWidth, height);
\r