2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
23 import java.awt.datatransfer.*;
24 import java.awt.event.*;
25 import java.awt.image.*;
28 import jalview.datamodel.*;
37 public class AnnotationLabels extends JPanel implements MouseListener,
38 MouseMotionListener, ActionListener
40 static String TOGGLE_LABELSCALE = "Scale Label to Column";
42 static String ADDNEW = "Add New Row";
44 static String EDITNAME = "Edit Label/Description";
46 static String HIDE = "Hide This Row";
48 static String DELETE = "Delete This Row";
50 static String SHOWALL = "Show All Hidden Rows";
52 static String OUTPUT_TEXT = "Export Annotation";
54 static String COPYCONS_SEQ = "Copy Consensus Sequence";
56 boolean resizePanel = false;
64 boolean resizing = false;
74 Font font = new Font("Arial", Font.PLAIN, 11);
76 private boolean hasHiddenRows;
79 * Creates a new AnnotationLabels object.
84 public AnnotationLabels(AlignmentPanel ap)
88 ToolTipManager.sharedInstance().registerComponent(this);
90 java.net.URL url = getClass().getResource("/images/idwidth.gif");
95 temp = java.awt.Toolkit.getDefaultToolkit().createImage(url);
100 MediaTracker mt = new MediaTracker(this);
101 mt.addImage(temp, 0);
103 } catch (Exception ex)
107 BufferedImage bi = new BufferedImage(temp.getHeight(this), temp
108 .getWidth(this), BufferedImage.TYPE_INT_RGB);
109 Graphics2D g = (Graphics2D) bi.getGraphics();
110 g.rotate(Math.toRadians(90));
111 g.drawImage(temp, 0, -bi.getWidth(this), this);
114 addMouseListener(this);
115 addMouseMotionListener(this);
118 public AnnotationLabels(AlignViewport av)
129 public void setScrollOffset(int y)
136 * sets selectedRow to -2 if no annotation preset, -1 if no visible row is at
140 * coordinate position to search for a row
142 void getSelectedRow(int y)
145 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
149 for (int i = 0; i < aa.length; i++)
157 height += aa[i].height;
175 public void actionPerformed(ActionEvent evt)
177 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
179 if (evt.getActionCommand().equals(ADDNEW))
181 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
182 null, new Annotation[ap.av.alignment.getWidth()]);
184 if (!editLabelDescription(newAnnotation))
189 ap.av.alignment.addAnnotation(newAnnotation);
190 ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
192 else if (evt.getActionCommand().equals(EDITNAME))
194 editLabelDescription(aa[selectedRow]);
197 else if (evt.getActionCommand().equals(HIDE))
199 aa[selectedRow].visible = false;
201 if (aa[selectedRow].label.equals("Quality"))
203 ap.av.quality = null;
206 else if (evt.getActionCommand().equals(DELETE))
208 ap.av.alignment.deleteAnnotation(aa[selectedRow]);
210 else if (evt.getActionCommand().equals(SHOWALL))
212 for (int i = 0; i < aa.length; i++)
214 if (!aa[i].visible && aa[i].annotations != null)
216 aa[i].visible = true;
220 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
222 new AnnotationExporter().exportAnnotations(ap,
223 new AlignmentAnnotation[]
224 { aa[selectedRow] }, null, null);
226 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
228 SequenceI cons = null;
229 if (aa[selectedRow].groupRef != null)
231 cons = aa[selectedRow].groupRef.getConsensusSeq();
235 cons = av.getConsensusSeq();
239 copy_annotseqtoclipboard(cons);
243 else if (evt.getActionCommand().equals(TOGGLE_LABELSCALE))
245 aa[selectedRow].scaleColLabel = !aa[selectedRow].scaleColLabel;
248 ap.annotationPanel.adjustPanelHeight();
249 ap.annotationScroller.validate();
250 ap.paintAlignment(true);
259 boolean editLabelDescription(AlignmentAnnotation annotation)
261 EditNameDialog dialog = new EditNameDialog(annotation.label,
262 annotation.description, " Annotation Name ",
263 "Annotation Description ", "Edit Annotation Name/Description",
271 annotation.label = dialog.getName();
273 String text = dialog.getDescription();
274 if (text != null && text.length() == 0)
278 annotation.description = text;
289 public void mousePressed(MouseEvent evt)
291 getSelectedRow(evt.getY() - scrollOffset);
301 public void mouseReleased(MouseEvent evt)
303 int start = selectedRow;
304 getSelectedRow(evt.getY() - scrollOffset);
305 int end = selectedRow;
309 // Swap these annotations
310 AlignmentAnnotation startAA = ap.av.alignment
311 .getAlignmentAnnotation()[start];
314 end = ap.av.alignment.getAlignmentAnnotation().length - 1;
316 AlignmentAnnotation endAA = ap.av.alignment.getAlignmentAnnotation()[end];
318 ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
319 ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
325 ap.annotationPanel.repaint();
334 public void mouseEntered(MouseEvent evt)
349 public void mouseExited(MouseEvent evt)
351 if (dragEvent == null)
364 public void mouseDragged(MouseEvent evt)
370 Dimension d = ap.annotationScroller.getPreferredSize();
371 int dif = evt.getY() - oldY;
373 dif /= ap.av.charHeight;
374 dif *= ap.av.charHeight;
376 if ((d.height - dif) > 20)
378 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
380 d = ap.annotationSpaceFillerHolder.getPreferredSize();
381 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
382 d.width, d.height - dif));
383 ap.paintAlignment(true);
400 public void mouseMoved(MouseEvent evt)
402 resizePanel = evt.getY() < 10;
404 getSelectedRow(evt.getY() - scrollOffset);
407 && ap.av.alignment.getAlignmentAnnotation().length > selectedRow)
409 AlignmentAnnotation aa = ap.av.alignment.getAlignmentAnnotation()[selectedRow];
411 StringBuffer desc = new StringBuffer("<html>");
413 if (aa.description != null
414 && !aa.description.equals("New description"))
416 desc.append(aa.getDescription(true));
422 desc.append("Score: " + aa.score);
425 if (desc.length() != 6)
427 desc.append("</html>");
428 this.setToolTipText(desc.toString());
431 this.setToolTipText(null);
442 public void mouseClicked(MouseEvent evt)
444 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
445 if (SwingUtilities.isLeftMouseButton(evt))
447 if (selectedRow > -1 && selectedRow < aa.length)
449 if (aa[selectedRow].groupRef != null)
451 if (evt.getClickCount() >= 2)
453 // todo: make the ap scroll to the selection
454 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
455 ap.av.setSelectionGroup(// new SequenceGroup(
456 aa[selectedRow].groupRef); // );
457 ap.paintAlignment(false);
461 ap.seqPanel.ap.idPanel
462 .highlightSearchResults(aa[selectedRow].groupRef
463 .getSequences(null));
467 else if (aa[selectedRow].sequenceRef != null)
469 Vector sr = new Vector();
470 sr.addElement(aa[selectedRow].sequenceRef);
471 if (evt.getClickCount() == 1)
473 ap.seqPanel.ap.idPanel.highlightSearchResults(sr);
475 else if (evt.getClickCount() >= 2)
477 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
478 SequenceGroup sg = new SequenceGroup();
479 sg.addSequence(aa[selectedRow].sequenceRef, false);
480 ap.av.setSelectionGroup(sg);
481 ap.paintAlignment(false);
482 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
488 if (!SwingUtilities.isRightMouseButton(evt))
493 JPopupMenu pop = new JPopupMenu("Annotations");
494 JMenuItem item = new JMenuItem(ADDNEW);
495 item.addActionListener(this);
500 { // let the user make everything visible again
501 item = new JMenuItem(SHOWALL);
502 item.addActionListener(this);
505 pop.show(this, evt.getX(), evt.getY());
508 item = new JMenuItem(EDITNAME);
509 item.addActionListener(this);
511 item = new JMenuItem(HIDE);
512 item.addActionListener(this);
514 item = new JMenuItem(DELETE);
515 item.addActionListener(this);
519 item = new JMenuItem(SHOWALL);
520 item.addActionListener(this);
523 item = new JMenuItem(OUTPUT_TEXT);
524 item.addActionListener(this);
526 // TODO: annotation object should be typed for autocalculated/derived
528 if (selectedRow < aa.length)
530 if (!aa[selectedRow].autoCalculated)
532 if (aa[selectedRow].graph == AlignmentAnnotation.NO_GRAPH)
534 // display formatting settings for this row.
536 // av and sequencegroup need to implement same interface for
537 item = new JCheckBoxMenuItem(TOGGLE_LABELSCALE,
538 aa[selectedRow].scaleColLabel);
539 item.addActionListener(this);
543 else if (aa[selectedRow].label.indexOf("Consensus") > -1)
546 // av and sequencegroup need to implement same interface for
547 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
548 "Ignore Gaps In Consensus",
549 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
550 .getIgnoreGapsConsensus()
551 : ap.av.getIgnoreGapsConsensus());
552 final AlignmentAnnotation aaa = aa[selectedRow];
553 cbmi.addActionListener(new ActionListener()
555 public void actionPerformed(ActionEvent e)
557 if (aaa.groupRef != null)
559 // TODO: pass on reference to ap so the view can be updated.
560 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
561 ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
565 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
570 // av and sequencegroup need to implement same interface for
571 if (aaa.groupRef != null)
573 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
574 "Show Group Histogram", aa[selectedRow].groupRef
575 .isShowConsensusHistogram());
576 chist.addActionListener(new ActionListener()
578 public void actionPerformed(ActionEvent e)
580 // TODO: pass on reference
586 aaa.groupRef.setShowConsensusHistogram(chist.getState());
588 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
592 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
593 "Show Group Logo", aa[selectedRow].groupRef
594 .isShowSequenceLogo());
595 cprof.addActionListener(new ActionListener()
597 public void actionPerformed(ActionEvent e)
599 // TODO: pass on reference
605 aaa.groupRef.setIncludeAllConsSymbols(cprof.getState());
607 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
614 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
615 "Show Histogram", av.isShowConsensusHistogram());
616 chist.addActionListener(new ActionListener()
618 public void actionPerformed(ActionEvent e)
620 // TODO: pass on reference
626 av.setShowConsensusHistogram(chist.getState());
628 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
632 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
633 "Show Logo", av.isShowSequenceLogo());
634 cprof.addActionListener(new ActionListener()
636 public void actionPerformed(ActionEvent e)
638 // TODO: pass on reference
644 av.setShowSequenceLogo(cprof.getState());
646 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
651 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
652 consclipbrd.addActionListener(this);
653 pop.add(consclipbrd);
656 pop.show(this, evt.getX(), evt.getY());
660 * do a single sequence copy to jalview and the system clipboard
663 * sequence to be copied to clipboard
665 protected void copy_annotseqtoclipboard(SequenceI sq)
667 SequenceI[] seqs = new SequenceI[]
669 String[] omitHidden = null;
670 SequenceI[] dseqs = new SequenceI[]
671 { sq.getDatasetSequence() };
672 if (dseqs[0] == null)
674 dseqs[0] = new Sequence(sq);
675 dseqs[0].setSequence(jalview.analysis.AlignSeq.extractGaps(
676 jalview.util.Comparison.GapChars, sq.getSequenceAsString()));
678 sq.setDatasetSequence(dseqs[0]);
680 Alignment ds = new Alignment(dseqs);
681 if (av.hasHiddenColumns)
683 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0,
684 sq.getLength(), seqs);
687 String output = new FormatAdapter().formatSequences("Fasta", seqs,
690 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
691 new StringSelection(output), Desktop.instance);
693 Vector hiddenColumns = null;
694 if (av.hasHiddenColumns)
696 hiddenColumns = new Vector();
697 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
699 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
702 hiddenColumns.addElement(new int[]
703 { region[0], region[1] });
707 Desktop.jalviewClipboard = new Object[]
708 { seqs, ds, // what is the dataset of a consensus sequence ? need to flag
709 // sequence as special.
719 public void paintComponent(Graphics g)
722 int width = getWidth();
725 width = ap.calculateIdWidth().width + 4;
728 Graphics2D g2 = (Graphics2D) g;
731 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
732 RenderingHints.VALUE_ANTIALIAS_ON);
735 drawComponent(g2, width);
745 public void drawComponent(Graphics g, int width)
747 if (av.getFont().getSize() < 10)
753 g.setFont(av.getFont());
756 FontMetrics fm = g.getFontMetrics(g.getFont());
757 g.setColor(Color.white);
758 g.fillRect(0, 0, getWidth(), getHeight());
760 g.translate(0, scrollOffset);
761 g.setColor(Color.black);
763 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
764 int fontHeight = g.getFont().getSize();
772 hasHiddenRows = false;
773 for (int i = 0; i < aa.length; i++)
775 g.setColor(Color.black);
779 hasHiddenRows = true;
785 offset = -aa[i].height / 2;
789 offset += fm.getHeight() / 2;
790 offset -= fm.getDescent();
793 offset += fm.getDescent();
795 x = width - fm.stringWidth(aa[i].label) - 3;
797 if (aa[i].graphGroup > -1)
800 for (int gg = 0; gg < aa.length; gg++)
802 if (aa[gg].graphGroup == aa[i].graphGroup)
808 if (groupSize * (fontHeight + 8) < aa[i].height)
810 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8))) / 2;
813 for (int gg = 0; gg < aa.length; gg++)
815 if (aa[gg].graphGroup == aa[i].graphGroup)
817 x = width - fm.stringWidth(aa[gg].label) - 3;
818 g.drawString(aa[gg].label, x, y - graphExtras);
819 if (aa[gg].annotations[0] != null)
821 g.setColor(aa[gg].annotations[0].colour);
824 g.drawLine(x, y - graphExtras - 3, x
825 + fm.stringWidth(aa[gg].label), y - graphExtras - 3);
827 g.setColor(Color.black);
828 graphExtras += fontHeight + 8;
834 g.drawString(aa[i].label, x, y + offset);
841 g.drawImage(image, 2, 0 - scrollOffset, this);
843 else if (dragEvent != null && aa != null)
845 g.setColor(Color.lightGray);
846 g.drawString(aa[selectedRow].label, dragEvent.getX(), dragEvent
851 if ((aa == null) || (aa.length < 1))
853 g.drawString("Right click", 2, 8);
854 g.drawString("to add annotation", 2, 18);