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 static String HELIX = "Helix";
\r
42 static String SHEET = "Sheet";
\r
43 static String LABEL = "Label";
\r
44 static String REMOVE = "Remove Annotation";
\r
45 static String COLOUR = "Colour";
\r
46 static Color HELIX_COLOUR = Color.red.darker();
\r
47 static Color SHEET_COLOUR = Color.green.darker().darker();
\r
49 /** DOCUMENT ME!! */
\r
50 public static int GRAPH_HEIGHT = 40;
\r
55 BufferedImage image;
\r
59 boolean fastPaint = false;
\r
62 * Creates a new AnnotationPanel object.
\r
64 * @param ap DOCUMENT ME!
\r
66 public AnnotationPanel(AlignmentPanel ap)
\r
68 ToolTipManager.sharedInstance().registerComponent(this);
\r
69 ToolTipManager.sharedInstance().setInitialDelay(0);
\r
70 ToolTipManager.sharedInstance().setDismissDelay(10000);
\r
73 this.setLayout(null);
\r
74 addMouseListener(this);
\r
75 addMouseMotionListener(this);
\r
76 ap.annotationScroller.getVerticalScrollBar().addAdjustmentListener(this);
\r
82 * @param evt DOCUMENT ME!
\r
84 public void adjustmentValueChanged(AdjustmentEvent evt)
\r
86 ap.alabels.setScrollOffset(-evt.getValue());
\r
92 public void adjustPanelHeight()
\r
94 // setHeight of panels
\r
95 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
100 for (int i = 0; i < aa.length; i++)
\r
102 if (!aa[i].visible)
\r
111 aa[i].height += av.charHeight;
\r
114 if (aa[i].hasIcons)
\r
116 aa[i].height += 16;
\r
121 aa[i].height += GRAPH_HEIGHT;
\r
124 if (aa[i].height == 0)
\r
129 height += aa[i].height;
\r
137 this.setPreferredSize(new Dimension(1, height));
\r
143 * @param col DOCUMENT ME!
\r
145 public void removeEditableColumn(int col)
\r
147 if (activeRow == -1)
\r
149 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
151 for (int j = 0; j < aa.length; j++)
\r
153 if (aa[j].editable)
\r
162 if ((activeRes != null) && activeRes.contains(String.valueOf(col)))
\r
164 activeRes.removeElement(String.valueOf(col));
\r
173 * @param col DOCUMENT ME!
\r
175 public void addEditableColumn(int col)
\r
177 if (activeRow == -1)
\r
179 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
181 for (int j = 0; j < aa.length; j++)
\r
183 if (aa[j].editable)
\r
192 if (activeRes == null)
\r
194 activeRes = new Vector();
\r
197 if (!activeRes.contains(String.valueOf(col)))
\r
199 activeRes.addElement(String.valueOf(col));
\r
208 * @param evt DOCUMENT ME!
\r
210 public void actionPerformed(ActionEvent evt)
\r
212 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
213 Annotation[] anot = aa[activeRow].annotations;
\r
215 if (evt.getActionCommand().equals(REMOVE))
\r
217 for (int i = 0; i < activeRes.size(); i++)
\r
219 anot[Integer.parseInt(activeRes.get(i).toString())] = null;
\r
220 anot[Integer.parseInt(activeRes.get(i).toString())] = null;
\r
223 else if (evt.getActionCommand().equals(LABEL))
\r
225 String label = JOptionPane.showInputDialog(this, "Enter Label ",
\r
226 "Enter label", JOptionPane.QUESTION_MESSAGE);
\r
233 if ((label.length() > 0) && !aa[activeRow].hasText)
\r
235 aa[activeRow].hasText = true;
\r
238 for (int i = 0; i < activeRes.size(); i++)
\r
240 int index = Integer.parseInt(activeRes.get(i).toString());
\r
242 if (anot[index] == null)
\r
244 anot[index] = new Annotation(label, "", ' ', 0);
\r
247 anot[index].displayCharacter = label;
\r
250 else if (evt.getActionCommand().equals(COLOUR))
\r
252 Color col = JColorChooser.showDialog(this,
\r
253 "Choose foreground colour", Color.black);
\r
255 for (int i = 0; i < activeRes.size(); i++)
\r
257 int index = Integer.parseInt(activeRes.get(i).toString());
\r
259 if (anot[index] == null)
\r
261 anot[index] = new Annotation("", "", ' ', 0);
\r
264 anot[index].colour = col;
\r
267 else // HELIX OR SHEET
\r
270 String symbol = "\u03B1";
\r
272 if (evt.getActionCommand().equals(HELIX))
\r
276 else if (evt.getActionCommand().equals(SHEET))
\r
282 if (!aa[activeRow].hasIcons)
\r
284 aa[activeRow].hasIcons = true;
\r
287 String label = JOptionPane.showInputDialog("Enter a label for the structure?",
\r
295 if ((label.length() > 0) && !aa[activeRow].hasText)
\r
297 aa[activeRow].hasText = true;
\r
300 for (int i = 0; i < activeRes.size(); i++)
\r
302 int index = Integer.parseInt(activeRes.get(i).toString());
\r
304 if (anot[index] == null)
\r
306 anot[index] = new Annotation(label, "", type, 0);
\r
309 anot[index].secondaryStructure = type;
\r
310 anot[index].displayCharacter = label;
\r
314 adjustPanelHeight();
\r
324 * @param evt DOCUMENT ME!
\r
326 public void mousePressed(MouseEvent evt)
\r
331 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
333 for (int i = 0; i < aa.length; i++)
\r
335 height += aa[i].height;
\r
337 if (evt.getY() < height)
\r
339 if (aa[i].editable)
\r
352 if (SwingUtilities.isRightMouseButton(evt))
\r
354 if (activeRes == null)
\r
359 JPopupMenu pop = new JPopupMenu("Structure type");
\r
360 JMenuItem item = new JMenuItem(HELIX);
\r
361 item.addActionListener(this);
\r
363 item = new JMenuItem(SHEET);
\r
364 item.addActionListener(this);
\r
366 item = new JMenuItem(LABEL);
\r
367 item.addActionListener(this);
\r
369 item = new JMenuItem(COLOUR);
\r
370 item.addActionListener(this);
\r
372 item = new JMenuItem(REMOVE);
\r
373 item.addActionListener(this);
\r
375 pop.show(this, evt.getX(), evt.getY());
\r
385 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
\r
387 if (evt.isControlDown() || evt.isAltDown())
\r
389 addEditableColumn(res);
\r
391 else if (evt.isShiftDown())
\r
393 if (activeRes == null)
\r
395 activeRes = new Vector();
\r
399 int start = Integer.parseInt(activeRes.get(activeRes.size() -
\r
410 for (int n = start; n <= end; n++)
\r
412 addEditableColumn(n);
\r
418 activeRes = new Vector();
\r
419 activeRes.addElement(String.valueOf(res));
\r
428 * @param evt DOCUMENT ME!
\r
430 public void mouseReleased(MouseEvent evt)
\r
437 * @param evt DOCUMENT ME!
\r
439 public void mouseEntered(MouseEvent evt)
\r
446 * @param evt DOCUMENT ME!
\r
448 public void mouseExited(MouseEvent evt)
\r
455 * @param evt DOCUMENT ME!
\r
457 public void mouseDragged(MouseEvent evt)
\r
464 * @param evt DOCUMENT ME!
\r
466 public void mouseMoved(MouseEvent evt)
\r
468 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
478 for (int i = 0; i < aa.length; i++)
\r
482 height += aa[i].height;
\r
485 if (evt.getY() < height)
\r
493 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
\r
495 if ((row > -1) && (res < aa[row].annotations.length) &&
\r
496 (aa[row].annotations[res] != null))
\r
498 this.setToolTipText(aa[row].annotations[res].description);
\r
500 StringBuffer text = new StringBuffer("Sequence position " +
\r
501 (res + 1) + " " + aa[row].annotations[res].description);
\r
502 ap.alignFrame.statusBar.setText(text.toString());
\r
509 * @param evt DOCUMENT ME!
\r
511 public void mouseClicked(MouseEvent evt)
\r
518 * @param g DOCUMENT ME!
\r
520 public void paintComponent(Graphics g)
\r
522 g.setColor(Color.white);
\r
523 g.fillRect(0, 0, getWidth(), getHeight());
\r
527 g.drawImage(image, 0, 0, this);
\r
532 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
\r
534 if (image != null && imgWidth == image.getWidth())
\r
536 //This will be called by a mouse over, tool tip etc
\r
537 // no need to redraw whole image
\r
538 g.drawImage(image, 0, 0, this);
\r
543 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
\r
544 BufferedImage.TYPE_INT_RGB);
\r
545 gg = (Graphics2D) image.getGraphics();
\r
546 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
547 RenderingHints.VALUE_ANTIALIAS_ON);
\r
549 gg.setFont(av.getFont());
\r
550 fm = gg.getFontMetrics();
\r
552 drawComponent(gg, av.startRes, av.endRes + 1);
\r
553 g.drawImage(image, 0, 0, this);
\r
559 * @param horizontal DOCUMENT ME!
\r
561 public void fastPaint(int horizontal)
\r
563 if ((horizontal == 0) ||
\r
564 (av.alignment.getAlignmentAnnotation() == null) ||
\r
565 (av.alignment.getAlignmentAnnotation().length < 1))
\r
572 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
\r
574 int sr = av.startRes;
\r
575 int er = av.endRes + 1;
\r
578 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
580 transX = (er - sr - horizontal) * av.charWidth;
\r
581 sr = er - horizontal;
\r
583 else if (horizontal < 0)
\r
585 er = sr - horizontal;
\r
588 gg.translate(transX, 0);
\r
590 drawComponent(gg, sr, er);
\r
592 gg.translate(-transX, 0);
\r
603 * @param g DOCUMENT ME!
\r
604 * @param startRes DOCUMENT ME!
\r
605 * @param endRes DOCUMENT ME!
\r
607 public void drawComponent(Graphics2D g, int startRes, int endRes)
\r
609 g.setColor(Color.white);
\r
610 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
\r
612 if ((av.alignment.getAlignmentAnnotation() == null) ||
\r
613 (av.alignment.getAlignmentAnnotation().length < 1))
\r
615 g.setColor(Color.white);
\r
616 g.fillRect(0, 0, getWidth(), getHeight());
\r
617 g.setColor(Color.black);
\r
618 g.drawString("Alignment has no annotations", 20, 15);
\r
623 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
628 char[] lastSS = new char[aa.length];
\r
629 int[] lastSSX = new int[aa.length];
\r
630 int iconOffset = av.charHeight / 2;
\r
631 boolean validRes = false;
\r
634 for (int i = 0; i < aa.length; i++)
\r
636 AlignmentAnnotation row = aa[i];
\r
645 // this is so that we draw the characters below the graph
\r
650 y -= av.charHeight;
\r
656 iconOffset = av.charHeight / 2;
\r
663 for (j = startRes; j < endRes; j++)
\r
665 if ((row.annotations.length <= j) ||
\r
666 (row.annotations[j] == null))
\r
675 x = (j - startRes) * av.charWidth;
\r
677 if (activeRow == i)
\r
679 g.setColor(Color.red);
\r
681 if (activeRes != null)
\r
683 for (int n = 0; n < activeRes.size(); n++)
\r
685 int v = Integer.parseInt(activeRes.get(n).toString());
\r
689 g.fillRect((j - startRes) * av.charWidth, y,
\r
690 av.charWidth, row.height);
\r
697 (row.annotations[j].displayCharacter.length() > 0))
\r
700 int charOffset = (av.charWidth -
\r
701 fm.charWidth(row.annotations[j].displayCharacter.charAt(
\r
703 g.setColor(row.annotations[j].colour);
\r
705 if (j == 0 || row.isGraph)
\r
707 g.drawString(row.annotations[j].displayCharacter, x+charOffset,
\r
708 y + iconOffset + 3);
\r
710 else if (((row.annotations[j - 1] == null) ||
\r
711 (row.annotations[j].displayCharacter != row.annotations[j -
\r
712 1].displayCharacter)))
\r
714 g.drawString(row.annotations[j].displayCharacter, x+charOffset,
\r
715 y + iconOffset + 3);
\r
722 (row.annotations[j].secondaryStructure != lastSS[i]))
\r
727 g.setColor(HELIX_COLOUR);
\r
728 g.fillRoundRect(lastSSX[i], y + 4 + iconOffset,
\r
729 x - lastSSX[i], 7, 8, 8);
\r
734 g.setColor(SHEET_COLOUR);
\r
735 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
736 x - lastSSX[i] - 4, 7);
\r
737 g.fillPolygon(new int[] { x - 4, x - 4, x },
\r
740 y + iconOffset, y + 14 + iconOffset,
\r
750 g.setColor(Color.gray);
\r
751 g.fillRect(lastSSX[i], y + 6 + iconOffset,
\r
752 x - lastSSX[i], 2);
\r
759 lastSS[i] = row.annotations[j].secondaryStructure;
\r
770 if (validRes && row.isGraph)
\r
772 g.setColor(new Color(0, 0, 180));
\r
774 int height = (int) ((row.annotations[j].value / row.graphMax) * GRAPH_HEIGHT);
\r
776 if (row.windowLength > 1)
\r
780 for (int i2 = j - (row.windowLength / 2);
\r
781 i2 < (j + (row.windowLength / 2)); i2++)
\r
783 if ((i2 < 0) || (i2 >= av.alignment.getWidth()))
\r
788 total += row.annotations[i2].value;
\r
791 total /= row.windowLength;
\r
792 height = (int) ((total / row.graphMax) * GRAPH_HEIGHT);
\r
795 g.setColor(row.annotations[j].colour);
\r
796 g.fillRect(x, y - height, av.charWidth, height);
\r
807 g.setColor(HELIX_COLOUR);
\r
808 g.fillRoundRect(lastSSX[i], y + 4 + iconOffset,
\r
809 x - lastSSX[i], 7, 8, 8);
\r
814 g.setColor(SHEET_COLOUR);
\r
815 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
816 x - lastSSX[i] - 4, 7);
\r
817 g.fillPolygon(new int[] { x - 4, x - 4, x },
\r
820 y + iconOffset, y + 14 + iconOffset,
\r
830 g.setColor(Color.gray);
\r
831 g.fillRect(lastSSX[i], y + 6 + iconOffset, x - lastSSX[i], 2);
\r
837 if (row.isGraph && row.hasText)
\r
839 y += av.charHeight;
\r
849 // used by overview window
\r
850 public void drawGraph(Graphics g, AlignmentAnnotation aa, int width, int y)
\r
852 g.setColor(Color.white);
\r
853 g.fillRect(0, 0, width, y);
\r
854 g.setColor(new Color(0, 0, 180));
\r
858 for (int j = 0; j < aa.annotations.length; j++)
\r
860 g.setColor(new Color(0, 0, 180));
\r
862 int height = (int) ((aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);
\r
863 g.fillRect(x, y - height, av.charWidth, height);
\r