2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 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
19 package jalview.gui;
\r
21 import jalview.datamodel.*;
\r
24 import java.awt.event.*;
\r
25 import java.awt.image.*;
\r
29 import javax.swing.*;
\r
36 * @version $Revision$
\r
38 public class AnnotationPanel extends JPanel implements MouseListener,
\r
39 MouseMotionListener, ActionListener, AdjustmentListener
\r
41 final String HELIX = "Helix";
\r
42 final String SHEET = "Sheet";
\r
43 final String LABEL = "Label";
\r
44 final String REMOVE = "Remove Annotation";
\r
45 final String COLOUR = "Colour";
\r
46 final Color HELIX_COLOUR = Color.red.darker();
\r
47 final Color SHEET_COLOUR = Color.green.darker().darker();
\r
49 /** DOCUMENT ME!! */
\r
53 BufferedImage image;
\r
57 boolean fastPaint = false;
\r
59 //Used For mouse Dragging and resizing graphs
\r
60 int graphStretch = -1;
\r
61 int graphStretchY = -1;
\r
64 * Creates a new AnnotationPanel object.
\r
66 * @param ap DOCUMENT ME!
\r
68 public AnnotationPanel(AlignmentPanel ap)
\r
70 ToolTipManager.sharedInstance().registerComponent(this);
\r
71 ToolTipManager.sharedInstance().setInitialDelay(0);
\r
72 ToolTipManager.sharedInstance().setDismissDelay(10000);
\r
75 this.setLayout(null);
\r
76 addMouseListener(this);
\r
77 addMouseMotionListener(this);
\r
78 ap.annotationScroller.getVerticalScrollBar().addAdjustmentListener(this);
\r
81 public AnnotationPanel(AlignViewport av)
\r
90 * @param evt DOCUMENT ME!
\r
92 public void adjustmentValueChanged(AdjustmentEvent evt)
\r
94 ap.alabels.setScrollOffset(-evt.getValue());
\r
100 public int adjustPanelHeight()
\r
102 // setHeight of panels
\r
104 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
109 for (int i = 0; i < aa.length; i++)
\r
111 if (!aa[i].visible)
\r
120 aa[i].height += av.charHeight;
\r
123 if (aa[i].hasIcons)
\r
125 aa[i].height += 16;
\r
130 aa[i].height += aa[i].graphHeight;
\r
133 if (aa[i].height == 0)
\r
138 height += aa[i].height;
\r
146 this.setPreferredSize(new Dimension(1, height));
\r
156 * @param evt DOCUMENT ME!
\r
158 public void actionPerformed(ActionEvent evt)
\r
160 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
161 Annotation[] anot = aa[activeRow].annotations;
\r
163 if (evt.getActionCommand().equals(REMOVE))
\r
165 for (int i = 0; i < av.getColumnSelection().size(); i++)
\r
167 anot[av.getColumnSelection().columnAt(i)] = null;
\r
170 else if (evt.getActionCommand().equals(LABEL))
\r
172 String label = JOptionPane.showInputDialog(this, "Enter Label ",
\r
173 "Enter label", JOptionPane.QUESTION_MESSAGE);
\r
180 if ((label.length() > 0) && !aa[activeRow].hasText)
\r
182 aa[activeRow].hasText = true;
\r
185 for (int i = 0; i < av.getColumnSelection().size(); i++)
\r
187 int index = av.getColumnSelection().columnAt(i);
\r
189 if (anot[index] == null)
\r
191 anot[index] = new Annotation(label, "", ' ', 0);
\r
194 anot[index].displayCharacter = label;
\r
197 else if (evt.getActionCommand().equals(COLOUR))
\r
199 Color col = JColorChooser.showDialog(this,
\r
200 "Choose foreground colour", Color.black);
\r
202 for (int i = 0; i < av.getColumnSelection().size(); i++)
\r
204 int index = av.getColumnSelection().columnAt(i);
\r
206 if (anot[index] == null)
\r
208 anot[index] = new Annotation("", "", ' ', 0);
\r
211 anot[index].colour = col;
\r
214 else // HELIX OR SHEET
\r
217 String symbol = "\u03B1";
\r
219 if (evt.getActionCommand().equals(HELIX))
\r
223 else if (evt.getActionCommand().equals(SHEET))
\r
229 if (!aa[activeRow].hasIcons)
\r
231 aa[activeRow].hasIcons = true;
\r
234 String label = JOptionPane.showInputDialog("Enter a label for the structure?",
\r
242 if ((label.length() > 0) && !aa[activeRow].hasText)
\r
244 aa[activeRow].hasText = true;
\r
247 for (int i = 0; i < av.getColumnSelection().size(); i++)
\r
249 int index = av.getColumnSelection().columnAt(i);
\r
251 if (anot[index] == null)
\r
253 anot[index] = new Annotation(label, "", type, 0);
\r
256 anot[index].secondaryStructure = type;
\r
257 anot[index].displayCharacter = label;
\r
261 adjustPanelHeight();
\r
270 * @param evt DOCUMENT ME!
\r
272 public void mousePressed(MouseEvent evt)
\r
277 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
281 for (int i = 0; i < aa.length; i++)
\r
285 height += aa[i].height;
\r
288 if (evt.getY() < height)
\r
290 if (aa[i].editable)
\r
294 else if(aa[i].graph>0)
\r
298 graphStretchY = evt.getY();
\r
305 if (SwingUtilities.isRightMouseButton(evt))
\r
307 if (av.getColumnSelection() == null)
\r
312 JPopupMenu pop = new JPopupMenu("Structure type");
\r
313 JMenuItem item = new JMenuItem(HELIX);
\r
314 item.addActionListener(this);
\r
316 item = new JMenuItem(SHEET);
\r
317 item.addActionListener(this);
\r
319 item = new JMenuItem(LABEL);
\r
320 item.addActionListener(this);
\r
322 item = new JMenuItem(COLOUR);
\r
323 item.addActionListener(this);
\r
325 item = new JMenuItem(REMOVE);
\r
326 item.addActionListener(this);
\r
328 pop.show(this, evt.getX(), evt.getY());
\r
338 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
\r
340 if (evt.isShiftDown())
\r
342 /*int start = Integer.parseInt(activeRes.get(activeRes.size() -
\r
353 for (int n = start; n <= end; n++)
\r
355 addEditableColumn(n);
\r
360 if (av.getColumnSelection().contains(res))
\r
361 av.getColumnSelection().removeElement(res);
\r
363 av.getColumnSelection().addElement(res);
\r
373 * @param evt DOCUMENT ME!
\r
375 public void mouseReleased(MouseEvent evt)
\r
378 graphStretchY = -1;
\r
384 * @param evt DOCUMENT ME!
\r
386 public void mouseEntered(MouseEvent evt)
\r
393 * @param evt DOCUMENT ME!
\r
395 public void mouseExited(MouseEvent evt)
\r
402 * @param evt DOCUMENT ME!
\r
404 public void mouseDragged(MouseEvent evt)
\r
406 if(graphStretch>-1)
\r
408 av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight += graphStretchY - evt.getY();
\r
409 if(av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight <10)
\r
410 av.alignment.getAlignmentAnnotation()[graphStretch].graphHeight = 10;
\r
411 graphStretchY = evt.getY();
\r
412 adjustPanelHeight();
\r
420 * @param evt DOCUMENT ME!
\r
422 public void mouseMoved(MouseEvent evt)
\r
424 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
434 for (int i = 0; i < aa.length; i++)
\r
438 height += aa[i].height;
\r
441 if (evt.getY() < height)
\r
449 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
\r
451 if(av.hasHiddenColumns)
\r
452 res = av.getColumnSelection().adjustForHiddenColumns(res);
\r
454 if (row > -1 && res<aa[row].annotations.length)
\r
456 if(aa[row].graphGroup>-1)
\r
458 StringBuffer tip = new StringBuffer("<html>");
\r
459 for (int gg = 0; gg < aa.length; gg++)
\r
461 if (aa[gg].graphGroup == aa[row].graphGroup && aa[gg].annotations[res]!=null)
\r
462 tip.append(aa[gg].label+" "+aa[gg].annotations[res].description+"<br>" );
\r
464 if(tip.length()!=6)
\r
466 tip.setLength(tip.length() - 4);
\r
467 this.setToolTipText(tip.toString() + "</html>");
\r
470 else if(aa[row].annotations[res] != null)
\r
471 this.setToolTipText(aa[row].annotations[res].description);
\r
473 if(aa[row].annotations[res]!=null)
\r
475 StringBuffer text = new StringBuffer("Sequence position " +
\r
477 aa[row].annotations[res].description);
\r
479 ap.alignFrame.statusBar.setText(text.toString());
\r
487 * @param evt DOCUMENT ME!
\r
489 public void mouseClicked(MouseEvent evt)
\r
496 * @param g DOCUMENT ME!
\r
498 public void paintComponent(Graphics g)
\r
500 g.setColor(Color.white);
\r
501 g.fillRect(0, 0, getWidth(), getHeight());
\r
505 || (getVisibleRect().width != g.getClipBounds().width)
\r
506 || (getVisibleRect().height != g.getClipBounds().height))
\r
508 g.drawImage(image, 0, 0, this);
\r
513 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
\r
515 if (image == null || imgWidth != image.getWidth()
\r
516 || image.getHeight(this) != getHeight())
\r
518 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
\r
519 BufferedImage.TYPE_INT_RGB);
\r
520 gg = (Graphics2D) image.getGraphics();
\r
523 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
524 RenderingHints.VALUE_ANTIALIAS_ON);
\r
526 gg.setFont(av.getFont());
\r
527 fm = gg.getFontMetrics();
\r
531 drawComponent(gg, av.startRes, av.endRes + 1);
\r
532 g.drawImage(image, 0, 0, this);
\r
538 * @param horizontal DOCUMENT ME!
\r
540 public void fastPaint(int horizontal)
\r
542 if ((horizontal == 0) || gg==null ||
\r
543 (av.alignment.getAlignmentAnnotation() == null) ||
\r
544 (av.alignment.getAlignmentAnnotation().length < 1))
\r
550 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
\r
552 int sr = av.startRes;
\r
553 int er = av.endRes + 1;
\r
556 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
558 transX = (er - sr - horizontal) * av.charWidth;
\r
559 sr = er - horizontal;
\r
561 else if (horizontal < 0)
\r
563 er = sr - horizontal;
\r
566 gg.translate(transX, 0);
\r
568 drawComponent(gg, sr, er);
\r
570 gg.translate(-transX, 0);
\r
581 * @param g DOCUMENT ME!
\r
582 * @param startRes DOCUMENT ME!
\r
583 * @param endRes DOCUMENT ME!
\r
585 public void drawComponent(Graphics g, int startRes, int endRes)
\r
587 g.setFont(av.getFont());
\r
590 fm = g.getFontMetrics();
\r
593 g.setColor(Color.white);
\r
594 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
\r
596 if ((av.alignment.getAlignmentAnnotation() == null) ||
\r
597 (av.alignment.getAlignmentAnnotation().length < 1))
\r
599 g.setColor(Color.white);
\r
600 g.fillRect(0, 0, getWidth(), getHeight());
\r
601 g.setColor(Color.black);
\r
602 g.drawString("Alignment has no annotations", 20, 15);
\r
607 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
611 char[] lastSS = new char[aa.length];
\r
612 int[] lastSSX = new int[aa.length];
\r
613 int iconOffset = av.charHeight / 2;
\r
614 boolean validRes = false;
\r
616 boolean [] graphGroupDrawn = new boolean[aa.length];
\r
620 for (int i = 0; i < aa.length; i++)
\r
622 AlignmentAnnotation row = aa[i];
\r
633 if(row.graphGroup>-1 && graphGroupDrawn[ row.graphGroup ] )
\r
636 // this is so that we draw the characters below the graph
\r
641 y -= av.charHeight;
\r
647 iconOffset = av.charHeight / 2;
\r
654 int column = startRes;
\r
655 int yPos = startRes;
\r
656 int aaMax = row.annotations.length-1;
\r
658 while (yPos < endRes)
\r
660 if (av.hasHiddenColumns)
\r
662 column = av.getColumnSelection().adjustForHiddenColumns(yPos);
\r
663 if (column > aaMax)
\r
671 if ((row.annotations.length <= column) ||
\r
672 (row.annotations[column] == null))
\r
681 x = (yPos - startRes) * av.charWidth;
\r
683 if (activeRow == i)
\r
685 g.setColor(Color.red);
\r
687 if (av.getColumnSelection() != null)
\r
689 for (int n = 0; n < av.getColumnSelection().size(); n++)
\r
691 int v = av.getColumnSelection().columnAt(n);
\r
695 g.fillRect((column - startRes) * av.charWidth, y,
\r
696 av.charWidth, row.height);
\r
703 (row.annotations[column].displayCharacter.length() > 0))
\r
706 int charOffset = (av.charWidth -
\r
707 fm.charWidth(row.annotations[column].displayCharacter.charAt(
\r
709 g.setColor(row.annotations[column].colour);
\r
711 if (column == 0 || row.graph>0)
\r
713 g.drawString(row.annotations[column].displayCharacter, x+charOffset,
\r
714 y + iconOffset + 3);
\r
716 else if (((row.annotations[column - 1] == null) ||
\r
717 (row.annotations[column].displayCharacter != row.annotations[column -
\r
718 1].displayCharacter)))
\r
720 g.drawString(row.annotations[column].displayCharacter, x+charOffset,
\r
721 y + iconOffset + 3);
\r
728 (row.annotations[column].secondaryStructure != lastSS[i]))
\r
733 g.setColor(HELIX_COLOUR);
\r
734 g.fillRoundRect(lastSSX[i], y + 4 + iconOffset,
\r
735 x - lastSSX[i], 7, 8, 8);
\r
740 g.setColor(SHEET_COLOUR);
\r
741 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
742 x - lastSSX[i] - 4, 7);
\r
743 g.fillPolygon(new int[] { x - 4, x - 4, x },
\r
746 y + iconOffset, y + 14 + iconOffset,
\r
754 g.setColor(Color.gray);
\r
755 g.fillRect(lastSSX[i], y + 6 + iconOffset,
\r
756 x - lastSSX[i], 2);
\r
763 lastSS[i] = row.annotations[column].secondaryStructure;
\r
784 g.setColor(HELIX_COLOUR);
\r
785 g.fillRoundRect(lastSSX[i], y + 4 + iconOffset,
\r
786 x - lastSSX[i], 7, 8, 8);
\r
791 g.setColor(SHEET_COLOUR);
\r
793 if (row.annotations[endRes] !=null
\r
794 && row.annotations[endRes].secondaryStructure != 'E')
\r
796 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
797 x - lastSSX[i] - 4, 7);
\r
798 g.fillPolygon(new int[]
\r
802 y + iconOffset, y + 14 + iconOffset,
\r
807 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
808 x - lastSSX[i], 7);
\r
816 g.setColor(Color.gray);
\r
817 g.fillRect(lastSSX[i], y + 6 + iconOffset, x - lastSSX[i], 2);
\r
825 if(row.graph == AlignmentAnnotation.LINE_GRAPH )
\r
827 if(row.graphGroup>-1 && !graphGroupDrawn[row.graphGroup])
\r
829 float groupmax=-999999, groupmin=9999999;
\r
830 for(int gg=0; gg<aa.length; gg++)
\r
832 if(aa[gg].graphGroup!=row.graphGroup)
\r
836 aa[gg].visible = false;
\r
838 if(aa[gg].graphMax>groupmax)
\r
839 groupmax = aa[gg].graphMax;
\r
840 if(aa[gg].graphMin<groupmin)
\r
841 groupmin = aa[gg].graphMin;
\r
844 for (int gg = 0; gg < aa.length; gg++)
\r
846 if (aa[gg].graphGroup == row.graphGroup)
\r
848 drawLineGraph(g, aa[gg], startRes, endRes, y,
\r
849 groupmin, groupmax,
\r
854 graphGroupDrawn[ row.graphGroup ] = true;
\r
857 drawLineGraph(g, row, startRes, endRes,
\r
858 y, row.graphMin, row.graphMax, row.graphHeight );
\r
860 else if(row.graph == AlignmentAnnotation.BAR_GRAPH )
\r
861 drawBarGraph(g, row, startRes, endRes,
\r
862 row.graphMin, row.graphMax, y);
\r
865 if (row.graph>0 && row.hasText)
\r
867 y += av.charHeight;
\r
877 public void drawLineGraph(Graphics g, AlignmentAnnotation aa,
\r
878 int sRes, int eRes,
\r
880 float min, float max,
\r
883 if(sRes>aa.annotations.length)
\r
888 //Adjustment for fastpaint to left
\r
899 float range = max - min;
\r
903 y2 = (int)(y - (0-min / range)*graphHeight);
\r
905 g.setColor(Color.gray);
\r
906 g.drawLine(x-av.charWidth,y2,(eRes-sRes+1)*av.charWidth,y2);
\r
909 if(aa.threshold!=null)
\r
911 g.setColor(aa.threshold.colour);
\r
912 Graphics2D g2 = (Graphics2D)g;
\r
913 g2.setStroke(new BasicStroke(1,
\r
914 BasicStroke.CAP_SQUARE,
\r
915 BasicStroke.JOIN_ROUND, 3f,
\r
916 new float[] { 5f, 3f }, 0f));
\r
918 y2 = (int)(y - ((aa.threshold.value-min) / range)*graphHeight);
\r
919 g.drawLine(x-av.charWidth,y2,(eRes-sRes+1)*av.charWidth,y2);
\r
920 g2.setStroke(new BasicStroke());
\r
923 eRes = Math.min(eRes, aa.annotations.length);
\r
927 int aaMax = aa.annotations.length-1;
\r
929 while( yPos < eRes )
\r
931 if(av.hasHiddenColumns)
\r
933 column = av.getColumnSelection().adjustForHiddenColumns(yPos);
\r
944 if(aa.annotations[column]==null || aa.annotations[column-1]==null)
\r
950 g.setColor(aa.annotations[column].colour);
\r
951 y1 = y - (int) (((aa.annotations[column-1].value-min) / range) * graphHeight);
\r
952 y2 = y - (int) (((aa.annotations[column].value-min) / range) * graphHeight);
\r
953 g.drawLine(x-av.charWidth/2, y1, x+av.charWidth/2, y2);
\r
958 public void drawBarGraph(Graphics g, AlignmentAnnotation aa,
\r
959 int sRes, int eRes,
\r
960 float min, float max,
\r
963 if(sRes>aa.annotations.length)
\r
968 float range = max - min;
\r
970 if(aa.threshold!=null)
\r
972 g.setColor(aa.threshold.colour);
\r
973 Graphics2D g2 = (Graphics2D)g;
\r
974 g2.setStroke(new BasicStroke(1,
\r
975 BasicStroke.CAP_SQUARE,
\r
976 BasicStroke.JOIN_ROUND, 3f,
\r
977 new float[] { 5f, 3f }, 0f));
\r
979 y2 = (int)(y - ((aa.threshold.value-min) / range)*aa.graphHeight);
\r
980 g.drawLine(x-av.charWidth,y2,(eRes-sRes+1)*av.charWidth,y2);
\r
981 g2.setStroke(new BasicStroke());
\r
987 y2 = (int)(y - (0-min / (range))*aa.graphHeight);
\r
989 g.setColor(Color.gray);
\r
991 g.drawLine(x,y2,(eRes-sRes+1)*av.charWidth,y2);
\r
996 int aaMax = aa.annotations.length-1;
\r
998 while( yPos < eRes )
\r
1000 if(av.hasHiddenColumns)
\r
1002 column = av.getColumnSelection().adjustForHiddenColumns(yPos);
\r
1003 if(column > aaMax)
\r
1013 if (aa.annotations[column] == null)
\r
1015 x += av.charWidth;
\r
1020 g.setColor(aa.annotations[column].colour);
\r
1021 y1 = y - (int) (((aa.annotations[column].value-min) / (range)) * aa.graphHeight);
\r
1024 g.fillRect(x, y2, av.charWidth, y1-y2 );
\r
1026 g.fillRect(x, y1, av.charWidth, y2-y1 );
\r
1028 x += av.charWidth;
\r
1033 // used by overview window
\r
1034 public void drawGraph(Graphics g, AlignmentAnnotation aa, int width, int y, int sRes, int eRes)
\r
1036 g.setColor(Color.white);
\r
1037 g.fillRect(0, 0, width, y);
\r
1038 g.setColor(new Color(0, 0, 180));
\r
1040 int x = 0, height;
\r
1042 for (int j = sRes; j < eRes; j++)
\r
1044 g.setColor(aa.annotations[j].colour);
\r
1046 height = (int) ((aa.annotations[j].value / aa.graphMax) * y);
\r
1050 g.fillRect(x, y - height, av.charWidth, height);
\r
1051 x += av.charWidth;
\r