2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.appletgui;
23 import jalview.analysis.AlignmentUtils;
24 import jalview.bin.JalviewLite;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.Annotation;
27 import jalview.datamodel.SequenceGroup;
28 import jalview.datamodel.SequenceI;
29 import jalview.util.MessageManager;
30 import jalview.util.ParseHtmlBodyAndLinks;
32 import awt2swing.Checkbox;
33 import awt2swing.CheckboxMenuItem;
34 import java.awt.Color;
35 import java.awt.Dimension;
36 import java.awt.FlowLayout;
37 import java.awt.FontMetrics;
38 import awt2swing.Frame;
39 import java.awt.Graphics;
40 import java.awt.Image;
41 import awt2swing.MenuItem;
42 import awt2swing.Panel;
43 import awt2swing.PopupMenu;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.awt.event.InputEvent;
47 import java.awt.event.ItemEvent;
48 import java.awt.event.ItemListener;
49 import java.awt.event.MouseEvent;
50 import java.awt.event.MouseListener;
51 import java.awt.event.MouseMotionListener;
52 import java.util.Arrays;
53 import java.util.Collections;
54 import java.util.Vector;
56 public class AnnotationLabels extends Panel implements ActionListener,
57 MouseListener, MouseMotionListener
61 boolean active = false;
67 boolean resizing = false;
71 static String ADDNEW = "Add New Row";
73 static String EDITNAME = "Edit Label/Description";
75 static String HIDE = "Hide This Row";
77 static String SHOWALL = "Show All Hidden Rows";
79 static String OUTPUT_TEXT = "Show Values In Textbox";
81 static String COPYCONS_SEQ = "Copy Consensus Sequence";
89 private boolean hasHiddenRows;
91 public AnnotationLabels(AlignmentPanel ap)
98 * this retrieves the adjustable height glyph from resources. we don't use
99 * it at the moment. java.net.URL url =
100 * getClass().getResource("/images/idwidth.gif"); Image temp = null;
102 * if (url != null) { temp =
103 * java.awt.Toolkit.getDefaultToolkit().createImage(url); }
105 * try { MediaTracker mt = new MediaTracker(this); mt.addImage(temp, 0);
106 * mt.waitForID(0); } catch (Exception ex) { }
108 * BufferedImage bi = new BufferedImage(temp.getHeight(this),
109 * temp.getWidth(this), BufferedImage.TYPE_INT_RGB); Graphics2D g =
110 * (Graphics2D) bi.getGraphics(); g.rotate(Math.toRadians(90));
111 * g.drawImage(temp, 0, -bi.getWidth(this), this); image = (Image) bi;
113 addMouseListener(this);
114 addMouseMotionListener(this);
117 public AnnotationLabels(AlignViewport av)
122 public void setScrollOffset(int y, boolean repaint)
134 * @return -2 if no rows are visible at all, -1 if no visible rows were
137 int getSelectedRow(int y)
140 AlignmentAnnotation[] aa = ap.av.getAlignment()
141 .getAlignmentAnnotation();
148 for (int i = 0; i < aa.length; i++)
155 height += aa[i].height;
166 public void actionPerformed(ActionEvent evt)
168 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
170 if (evt.getActionCommand().equals(ADDNEW))
172 AlignmentAnnotation newAnnotation = new AlignmentAnnotation("", null,
173 new Annotation[ap.av.getAlignment().getWidth()]);
175 if (!editLabelDescription(newAnnotation))
180 ap.av.getAlignment().addAnnotation(newAnnotation);
181 ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
183 else if (evt.getActionCommand().equals(EDITNAME))
185 editLabelDescription(aa[selectedRow]);
187 else if (evt.getActionCommand().equals(HIDE))
189 aa[selectedRow].visible = false;
191 else if (evt.getActionCommand().equals(SHOWALL))
193 for (int i = 0; i < aa.length; i++)
195 aa[i].visible = (aa[i].annotations == null) ? false : true;
198 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
200 CutAndPasteTransfer cap = new CutAndPasteTransfer(false,
202 Frame frame = new Frame();
204 JalviewLite.addFrame(frame, ap.alignFrame.getTitle()
205 + " - " + aa[selectedRow].label, 500, 100);
206 cap.setText(aa[selectedRow].toString());
208 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
210 SequenceI cons = av.getConsensusSeq();
213 copy_annotseqtoclipboard(cons);
221 * Adjust size and repaint
223 protected void refresh()
225 ap.annotationPanel.adjustPanelHeight();
226 setSize(getSize().width, ap.annotationPanel.getSize().height);
228 ap.paintAlignment(true);
231 boolean editLabelDescription(AlignmentAnnotation annotation)
233 Checkbox padGaps = new Checkbox("Fill Empty Gaps With \""
234 + ap.av.getGapCharacter() + "\"", annotation.padGaps);
236 EditNameDialog dialog = new EditNameDialog(annotation.label,
237 annotation.description, " Annotation Label",
238 "Annotation Description", ap.alignFrame,
239 "Edit Annotation Name / Description", 500, 180, false);
241 Panel empty = new Panel(new FlowLayout());
246 dialog.setVisible(true);
250 annotation.label = dialog.getName();
251 annotation.description = dialog.getDescription();
252 annotation.setPadGaps(padGaps.getState(), av.getGapCharacter());
263 boolean resizePanel = false;
265 public void mouseMoved(MouseEvent evt)
267 resizePanel = evt.getY() < 10 && evt.getX() < 14;
268 int row = getSelectedRow(evt.getY() + scrollOffset);
272 ParseHtmlBodyAndLinks phb = new ParseHtmlBodyAndLinks(
273 av.getAlignment().getAlignmentAnnotation()[row]
274 .getDescription(true),
278 tooltip = new Tooltip(phb.getNonHtmlContent(), this);
282 tooltip.setTip(phb.getNonHtmlContent());
285 else if (tooltip != null)
292 * curent drag position
294 MouseEvent dragEvent = null;
297 * flag to indicate drag events should be ignored
299 private boolean dragCancelled = false;
302 * clear any drag events in progress
304 public void cancelDrag()
307 dragCancelled = true;
310 public void mouseDragged(MouseEvent evt)
321 Dimension d = ap.annotationPanelHolder.getSize(), e = ap.annotationSpaceFillerHolder
322 .getSize(), f = ap.seqPanelHolder.getSize();
323 int dif = evt.getY() - oldY;
325 dif /= ap.av.getCharHeight();
326 dif *= ap.av.getCharHeight();
328 if ((d.height - dif) > 20 && (f.height + dif) > 20)
330 ap.annotationPanel.setSize(d.width, d.height - dif);
331 setSize(new Dimension(e.width, d.height - dif));
332 ap.annotationSpaceFillerHolder.setSize(new Dimension(e.width,
334 ap.annotationPanelHolder.setSize(new Dimension(d.width, d.height
336 ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height - dif, 0,
337 av.calcPanelHeight());
339 ap.seqPanelHolder.setPreferredSize(f);
340 ap.setScrollValues(av.getStartRes(), av.getStartSeq());
342 // ap.paintAlignment(true);
350 if ((diff = 6 - evt.getY()) > 0)
353 ap.apvscroll.setValue(ap.apvscroll.getValue() - diff);
354 ap.adjustmentValueChanged(null);
357 else if ((0 < (diff = 6
358 - ap.annotationSpaceFillerHolder.getSize().height
362 ap.apvscroll.setValue(ap.apvscroll.getValue() + diff);
363 ap.adjustmentValueChanged(null);
369 public void mouseClicked(MouseEvent evt)
373 public void mouseReleased(MouseEvent evt)
375 if (!resizePanel && !dragCancelled)
377 int start = selectedRow;
379 int end = getSelectedRow(evt.getY() + scrollOffset);
381 if (start > -1 && start != end)
383 // Swap these annotations
384 AlignmentAnnotation startAA = ap.av.getAlignment()
385 .getAlignmentAnnotation()[start];
388 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
390 AlignmentAnnotation endAA = ap.av.getAlignment()
391 .getAlignmentAnnotation()[end];
393 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
394 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
399 dragCancelled = false;
401 ap.annotationPanel.repaint();
404 public void mouseEntered(MouseEvent evt)
406 if (evt.getY() < 10 && evt.getX() < 14)
413 public void mouseExited(MouseEvent evt)
415 dragCancelled = false;
417 if (dragEvent == null)
431 public void mousePressed(MouseEvent evt)
438 dragCancelled = false;
439 // todo: move below to mouseClicked ?
440 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
442 AlignmentAnnotation[] aa = ap.av.getAlignment()
443 .getAlignmentAnnotation();
445 // DETECT RIGHT MOUSE BUTTON IN AWT
446 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
449 PopupMenu popup = new PopupMenu(
450 MessageManager.getString("label.annotations"));
452 MenuItem item = new MenuItem(ADDNEW);
453 item.addActionListener(this);
457 // this never happens at moment: - see comment on JAL-563
460 item = new MenuItem(SHOWALL);
461 item.addActionListener(this);
465 popup.show(this, evt.getX(), evt.getY());
468 // add the rest if there are actually rows to show
469 item = new MenuItem(EDITNAME);
470 item.addActionListener(this);
472 item = new MenuItem(HIDE);
473 item.addActionListener(this);
479 if (selectedRow < aa.length)
481 if (aa[selectedRow].sequenceRef != null)
483 final String label = aa[selectedRow].label;
484 MenuItem hideType = new MenuItem(
485 MessageManager.getString("label.hide_all") + " " + label);
486 hideType.addActionListener(new ActionListener()
489 public void actionPerformed(ActionEvent e)
491 AlignmentUtils.showOrHideSequenceAnnotations(
492 ap.av.getAlignment(), Collections.singleton(label),
503 item = new MenuItem(SHOWALL);
504 item.addActionListener(this);
508 item = new MenuItem(OUTPUT_TEXT);
509 item.addActionListener(this);
511 if (selectedRow < aa.length)
513 if (aa[selectedRow].autoCalculated)
515 if (aa[selectedRow].label.indexOf("Consensus") > -1)
517 popup.addSeparator();
518 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
519 MessageManager.getString("label.ignore_gaps_consensus"),
520 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
521 .getIgnoreGapsConsensus() : ap.av
522 .isIgnoreGapsConsensus());
523 final AlignmentAnnotation aaa = aa[selectedRow];
524 cbmi.addItemListener(new ItemListener()
526 public void itemStateChanged(ItemEvent e)
528 if (aaa.groupRef != null)
530 // TODO: pass on reference to ap so the view can be updated.
531 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
535 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
537 ap.paintAlignment(true);
541 if (aaa.groupRef != null)
543 final CheckboxMenuItem chist = new CheckboxMenuItem(
544 MessageManager.getString("label.show_group_histogram"),
545 aa[selectedRow].groupRef.isShowConsensusHistogram());
546 chist.addItemListener(new ItemListener()
548 public void itemStateChanged(ItemEvent e)
550 // TODO: pass on reference
556 aaa.groupRef.setShowConsensusHistogram(chist.getState());
558 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
562 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
563 MessageManager.getString("label.show_group_logo"),
564 aa[selectedRow].groupRef.isShowSequenceLogo());
565 cprofl.addItemListener(new ItemListener()
567 public void itemStateChanged(ItemEvent e)
569 // TODO: pass on reference
575 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
577 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
582 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
583 MessageManager.getString("label.normalise_group_logo"),
584 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
585 cprofn.addItemListener(new ItemListener()
587 public void itemStateChanged(ItemEvent e)
589 // TODO: pass on reference
595 aaa.groupRef.setshowSequenceLogo(true);
596 aaa.groupRef.setNormaliseSequenceLogo(cprofn.getState());
598 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
605 final CheckboxMenuItem chist = new CheckboxMenuItem(
606 MessageManager.getString("label.show_histogram"), av.isShowConsensusHistogram());
607 chist.addItemListener(new ItemListener()
609 public void itemStateChanged(ItemEvent e)
611 // TODO: pass on reference
617 av.setShowConsensusHistogram(chist.getState());
618 ap.alignFrame.showConsensusHistogram.setState(chist
619 .getState()); // TODO: implement
620 // ap.updateGUI()/alignFrame.updateGUI
623 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
627 final CheckboxMenuItem cprof = new CheckboxMenuItem(
628 MessageManager.getString("label.show_logo"), av.isShowSequenceLogo());
629 cprof.addItemListener(new ItemListener()
631 public void itemStateChanged(ItemEvent e)
633 // TODO: pass on reference
639 av.setShowSequenceLogo(cprof.getState());
640 ap.alignFrame.showSequenceLogo.setState(cprof.getState()); // TODO:
642 // ap.updateGUI()/alignFrame.updateGUI
646 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
650 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
651 MessageManager.getString("label.normalise_logo"), av.isNormaliseSequenceLogo());
652 cprofn.addItemListener(new ItemListener()
654 public void itemStateChanged(ItemEvent e)
656 // TODO: pass on reference
662 av.setShowSequenceLogo(true);
663 ap.alignFrame.normSequenceLogo.setState(cprofn.getState()); // TODO:
665 // ap.updateGUI()/alignFrame.updateGUI
668 av.setNormaliseSequenceLogo(cprofn.getState());
670 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
676 item = new MenuItem(COPYCONS_SEQ);
677 item.addActionListener(this);
682 popup.show(this, evt.getX(), evt.getY());
687 if (selectedRow > -1 && selectedRow < aa.length)
689 if (aa[selectedRow].groupRef != null)
691 if (evt.getClickCount() >= 2)
693 // todo: make the ap scroll to the selection - not necessary, first
694 // click highlights/scrolls, second selects
695 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
696 ap.av.setSelectionGroup(// new SequenceGroup(
697 aa[selectedRow].groupRef); // );
698 ap.av.sendSelection();
699 ap.paintAlignment(false);
700 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
704 ap.seqPanel.ap.idPanel
705 .highlightSearchResults(aa[selectedRow].groupRef
706 .getSequences(null));
710 else if (aa[selectedRow].sequenceRef != null)
712 if (evt.getClickCount() == 1)
714 ap.seqPanel.ap.idPanel.highlightSearchResults(Arrays
715 .asList(new SequenceI[]
716 { aa[selectedRow].sequenceRef }));
718 else if (evt.getClickCount() >= 2)
720 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
721 SequenceGroup sg = ap.av.getSelectionGroup();
724 // we make a copy rather than edit the current selection if no modifiers pressed
725 // see Enhancement JAL-1557
726 if (!(evt.isControlDown() || evt.isShiftDown()))
728 sg = new SequenceGroup(sg);
730 sg.addSequence(aa[selectedRow].sequenceRef, false);
732 if (evt.isControlDown())
734 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
736 // notionally, we should also add intermediate sequences from last added sequence ?
737 sg.addSequence(aa[selectedRow].sequenceRef, true);
741 sg = new SequenceGroup();
743 sg.setEndRes(ap.av.getAlignment().getWidth()-1);
744 sg.addSequence(aa[selectedRow].sequenceRef, false);
746 ap.av.setSelectionGroup(sg);
747 ap.paintAlignment(false);
748 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
749 ap.av.sendSelection();
764 protected void copy_annotseqtoclipboard(SequenceI sq)
766 if (sq == null || sq.getLength() < 1)
770 AlignFrame.copiedSequences = new StringBuffer();
771 AlignFrame.copiedSequences.append(sq.getName() + "\t"
772 + sq.getStart() + "\t" + sq.getEnd() + "\t"
773 + sq.getSequenceAsString() + "\n");
774 if (av.hasHiddenColumns())
776 AlignFrame.copiedHiddenColumns = new Vector();
777 for (int[] region : av.getColumnSelection().getHiddenColumns())
779 AlignFrame.copiedHiddenColumns
780 .addElement(new int[]
781 { region[0], region[1] });
786 public void paintComponent(Graphics g)
789 if (av.getWrapAlignment() || !av.isShowAnnotation())
792 int w = getSize().width;
793 int h = getSize().height;
794 if (image == null || w != image.getWidth(this)
795 || h != image.getHeight(this))
797 image = createImage(w, ap.annotationPanel.getSize().height);
800 drawComponent(image.getGraphics(), w);
801 g.drawImage(image, 0, 0, this);
804 public void drawComponent(Graphics g, int width)
806 g.setFont(av.getFont());
807 FontMetrics fm = g.getFontMetrics(av.getFont());
808 g.setColor(Color.white);
809 g.fillRect(0, 0, getSize().width, getSize().height);
811 g.translate(0, -scrollOffset);
812 g.setColor(Color.black);
814 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
815 int y = 0, fy = g.getFont().getSize();
820 hasHiddenRows = false;
821 for (int i = 0; i < aa.length; i++)
825 hasHiddenRows = true;
829 x = width - fm.stringWidth(aa[i].label) - 3;
832 offset = -(aa[i].height - fy) / 2;
834 g.drawString(aa[i].label, x, y + offset);
837 g.translate(0, +scrollOffset);
840 g.setColor(Color.red);
842 g.drawLine(2, 8, 5, 2);
843 g.drawLine(5, 2, 8, 8);
845 else if (!dragCancelled && dragEvent != null && aa != null)
847 g.setColor(Color.lightGray);
848 g.drawString(aa[selectedRow].label, dragEvent.getX(),
852 if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
854 g.setColor(Color.black);
855 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
856 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,