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
533 imgWidth = (av.endRes - av.startRes + 1) * av.charWidth;
\r
535 image = new BufferedImage(imgWidth, ap.annotationPanel.getHeight(),
\r
536 BufferedImage.TYPE_INT_RGB);
\r
537 gg = (Graphics2D) image.getGraphics();
\r
538 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
539 RenderingHints.VALUE_ANTIALIAS_ON);
\r
541 gg.setFont(av.getFont());
\r
543 drawComponent(gg, av.startRes, av.endRes + 1);
\r
544 g.drawImage(image, 0, 0, this);
\r
550 * @param horizontal DOCUMENT ME!
\r
552 public void fastPaint(int horizontal)
\r
554 if ((horizontal == 0) ||
\r
555 (av.alignment.getAlignmentAnnotation() == null) ||
\r
556 (av.alignment.getAlignmentAnnotation().length < 1))
\r
563 gg.copyArea(0, 0, imgWidth, getHeight(), -horizontal * av.charWidth, 0);
\r
565 int sr = av.startRes;
\r
566 int er = av.endRes + 1;
\r
569 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
571 transX = (er - sr - horizontal) * av.charWidth;
\r
572 sr = er - horizontal;
\r
574 else if (horizontal < 0)
\r
576 er = sr - horizontal;
\r
579 gg.translate(transX, 0);
\r
581 drawComponent(gg, sr, er);
\r
583 gg.translate(-transX, 0);
\r
592 * @param g DOCUMENT ME!
\r
593 * @param startRes DOCUMENT ME!
\r
594 * @param endRes DOCUMENT ME!
\r
596 public void drawComponent(Graphics2D g, int startRes, int endRes)
\r
599 fm = g.getFontMetrics();
\r
601 g.setColor(Color.white);
\r
602 g.fillRect(0, 0, (endRes - startRes) * av.charWidth, getHeight());
\r
604 if ((av.alignment.getAlignmentAnnotation() == null) ||
\r
605 (av.alignment.getAlignmentAnnotation().length < 1))
\r
607 g.setColor(Color.white);
\r
608 g.fillRect(0, 0, getWidth(), getHeight());
\r
609 g.setColor(Color.black);
\r
610 g.drawString("Alignment has no annotations", 20, 15);
\r
615 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
\r
620 char[] lastSS = new char[aa.length];
\r
621 int[] lastSSX = new int[aa.length];
\r
622 int iconOffset = av.charHeight / 2;
\r
623 boolean validRes = false;
\r
626 for (int i = 0; i < aa.length; i++)
\r
628 AlignmentAnnotation row = aa[i];
\r
637 // this is so that we draw the characters below the graph
\r
642 y -= av.charHeight;
\r
648 iconOffset = av.charHeight / 2;
\r
655 for (j = startRes; j < endRes; j++)
\r
657 if ((row.annotations.length <= j) ||
\r
658 (row.annotations[j] == null))
\r
667 x = (j - startRes) * av.charWidth;
\r
669 if (activeRow == i)
\r
671 g.setColor(Color.red);
\r
673 if (activeRes != null)
\r
675 for (int n = 0; n < activeRes.size(); n++)
\r
677 int v = Integer.parseInt(activeRes.get(n).toString());
\r
681 g.fillRect((j - startRes) * av.charWidth, y,
\r
682 av.charWidth, row.height);
\r
689 (row.annotations[j].displayCharacter.length() > 0))
\r
691 int charOffset = (av.charWidth -
\r
692 fm.charWidth(row.annotations[j].displayCharacter.charAt(
\r
694 g.setColor(row.annotations[j].colour);
\r
698 g.drawString(row.annotations[j].displayCharacter, x+charOffset,
\r
699 y + iconOffset + 2);
\r
701 else if (((row.annotations[j - 1] == null) ||
\r
702 (row.annotations[j].displayCharacter != row.annotations[j -
\r
703 1].displayCharacter)))
\r
705 g.drawString(row.annotations[j].displayCharacter, x+charOffset,
\r
706 y + iconOffset + 2);
\r
713 (row.annotations[j].secondaryStructure != lastSS[i]))
\r
718 g.setColor(HELIX_COLOUR);
\r
719 g.fillRoundRect(lastSSX[i], y + 4 + iconOffset,
\r
720 x - lastSSX[i], 7, 8, 8);
\r
725 g.setColor(SHEET_COLOUR);
\r
726 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
727 x - lastSSX[i] - 4, 7);
\r
728 g.fillPolygon(new int[] { x - 4, x - 4, x },
\r
731 y + iconOffset, y + 14 + iconOffset,
\r
741 g.setColor(Color.gray);
\r
742 g.fillRect(lastSSX[i], y + 6 + iconOffset,
\r
743 x - lastSSX[i], 2);
\r
750 lastSS[i] = row.annotations[j].secondaryStructure;
\r
761 if (validRes && row.isGraph)
\r
763 g.setColor(new Color(0, 0, 180));
\r
765 int height = (int) ((row.annotations[j].value / row.graphMax) * GRAPH_HEIGHT);
\r
767 if (row.windowLength > 1)
\r
771 for (int i2 = j - (row.windowLength / 2);
\r
772 i2 < (j + (row.windowLength / 2)); i2++)
\r
774 if ((i2 < 0) || (i2 >= av.alignment.getWidth()))
\r
779 total += row.annotations[i2].value;
\r
782 total /= row.windowLength;
\r
783 height = (int) ((total / row.graphMax) * GRAPH_HEIGHT);
\r
786 g.setColor(row.annotations[j].colour);
\r
787 g.fillRect(x, y - height, av.charWidth, height);
\r
798 g.setColor(HELIX_COLOUR);
\r
799 g.fillRoundRect(lastSSX[i], y + 4 + iconOffset,
\r
800 x - lastSSX[i], 7, 8, 8);
\r
805 g.setColor(SHEET_COLOUR);
\r
806 g.fillRect(lastSSX[i], y + 4 + iconOffset,
\r
807 x - lastSSX[i] - 4, 7);
\r
808 g.fillPolygon(new int[] { x - 4, x - 4, x },
\r
811 y + iconOffset, y + 14 + iconOffset,
\r
821 g.setColor(Color.gray);
\r
822 g.fillRect(lastSSX[i], y + 6 + iconOffset, x - lastSSX[i], 2);
\r
828 if (row.isGraph && row.hasText)
\r
830 y += av.charHeight;
\r
840 // used by overview window
\r
841 public void drawGraph(Graphics g, AlignmentAnnotation aa, int width, int y)
\r
843 g.setColor(Color.white);
\r
844 g.fillRect(0, 0, width, y);
\r
845 g.setColor(new Color(0, 0, 180));
\r
849 for (int j = 0; j < aa.annotations.length; j++)
\r
851 g.setColor(new Color(0, 0, 180));
\r
853 int height = (int) ((aa.annotations[j].value / aa.graphMax) * GRAPH_HEIGHT);
\r
854 g.fillRect(x, y - height, av.charWidth, height);
\r