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.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.Annotation;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.datamodel.SequenceI;
28 import jalview.util.MessageManager;
29 import jalview.util.ParseHtmlBodyAndLinks;
31 import java.awt.Checkbox;
32 import java.awt.CheckboxMenuItem;
33 import java.awt.Color;
34 import java.awt.Cursor;
35 import java.awt.Dimension;
36 import java.awt.FlowLayout;
37 import java.awt.FontMetrics;
38 import java.awt.Frame;
39 import java.awt.Graphics;
40 import java.awt.Image;
41 import java.awt.MenuItem;
42 import java.awt.Panel;
43 import java.awt.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
57 implements ActionListener, 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;
167 public void actionPerformed(ActionEvent evt)
169 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
171 if (evt.getActionCommand().equals(ADDNEW))
173 AlignmentAnnotation newAnnotation = new AlignmentAnnotation("", null,
174 new Annotation[ap.av.getAlignment().getWidth()]);
176 if (!editLabelDescription(newAnnotation))
181 ap.av.getAlignment().addAnnotation(newAnnotation);
182 ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
184 else if (evt.getActionCommand().equals(EDITNAME))
186 editLabelDescription(aa[selectedRow]);
188 else if (evt.getActionCommand().equals(HIDE))
190 aa[selectedRow].visible = false;
192 else if (evt.getActionCommand().equals(SHOWALL))
194 for (int i = 0; i < aa.length; i++)
196 aa[i].visible = (aa[i].annotations == null) ? false : true;
199 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
201 CutAndPasteTransfer cap = new CutAndPasteTransfer(false,
203 Frame frame = new Frame();
205 jalview.bin.JalviewLite.addFrame(frame,
206 ap.alignFrame.getTitle() + " - " + aa[selectedRow].label, 500,
208 cap.setText(aa[selectedRow].toString());
210 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
212 SequenceI cons = av.getConsensusSeq();
215 copy_annotseqtoclipboard(cons);
223 * Adjust size and repaint
225 protected void refresh()
227 ap.annotationPanel.adjustPanelHeight();
228 setSize(getSize().width, ap.annotationPanel.getSize().height);
230 // TODO: only paint if we needed to
231 ap.paintAlignment(true, true);
234 boolean editLabelDescription(AlignmentAnnotation annotation)
236 Checkbox padGaps = new Checkbox(
237 "Fill Empty Gaps With \"" + ap.av.getGapCharacter() + "\"",
240 EditNameDialog dialog = new EditNameDialog(annotation.label,
241 annotation.description, " Annotation Label",
242 "Annotation Description", ap.alignFrame,
243 "Edit Annotation Name / Description", 500, 180, false);
245 Panel empty = new Panel(new FlowLayout());
250 dialog.setVisible(true);
254 annotation.label = dialog.getName();
255 annotation.description = dialog.getDescription();
256 annotation.setPadGaps(padGaps.getState(), av.getGapCharacter());
267 boolean resizePanel = false;
270 public void mouseMoved(MouseEvent evt)
272 resizePanel = evt.getY() < 10 && evt.getX() < 14;
273 setCursor(Cursor.getPredefinedCursor(
274 resizePanel ? Cursor.S_RESIZE_CURSOR : Cursor.DEFAULT_CURSOR));
275 int row = getSelectedRow(evt.getY() + scrollOffset);
279 ParseHtmlBodyAndLinks phb = new ParseHtmlBodyAndLinks(
280 av.getAlignment().getAlignmentAnnotation()[row]
281 .getDescription(true),
285 tooltip = new Tooltip(phb.getNonHtmlContent(), this);
289 tooltip.setTip(phb.getNonHtmlContent());
292 else if (tooltip != null)
299 * curent drag position
301 MouseEvent dragEvent = null;
304 * flag to indicate drag events should be ignored
306 private boolean dragCancelled = false;
309 * clear any drag events in progress
311 public void cancelDrag()
314 dragCancelled = true;
318 public void mouseDragged(MouseEvent evt)
329 Dimension d = ap.annotationPanelHolder.getSize(),
330 e = ap.annotationSpaceFillerHolder.getSize(),
331 f = ap.seqPanelHolder.getSize();
332 int dif = evt.getY() - oldY;
334 dif /= ap.av.getCharHeight();
335 dif *= ap.av.getCharHeight();
337 if ((d.height - dif) > 20 && (f.height + dif) > 20)
339 ap.annotationPanel.setSize(d.width, d.height - dif);
340 setSize(new Dimension(e.width, d.height - dif));
341 ap.annotationSpaceFillerHolder
342 .setSize(new Dimension(e.width, d.height - dif));
343 ap.annotationPanelHolder
344 .setSize(new Dimension(d.width, d.height - dif));
345 ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height - dif, 0,
346 av.calcPanelHeight());
348 ap.seqPanelHolder.setPreferredSize(f);
349 ap.setScrollValues(av.getRanges().getStartRes(),
350 av.getRanges().getStartSeq());
352 // ap.paintAlignment(true);
360 if ((diff = 6 - evt.getY()) > 0)
363 ap.apvscroll.setValue(ap.apvscroll.getValue() - diff);
364 ap.adjustmentValueChanged(null);
367 else if ((0 < (diff = 6
368 - ap.annotationSpaceFillerHolder.getSize().height
372 ap.apvscroll.setValue(ap.apvscroll.getValue() + diff);
373 ap.adjustmentValueChanged(null);
380 public void mouseClicked(MouseEvent evt)
385 public void mouseReleased(MouseEvent evt)
387 if (!resizePanel && !dragCancelled)
389 int start = selectedRow;
391 int end = getSelectedRow(evt.getY() + scrollOffset);
393 if (start > -1 && start != end)
395 // Swap these annotations
396 AlignmentAnnotation startAA = ap.av.getAlignment()
397 .getAlignmentAnnotation()[start];
400 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
402 AlignmentAnnotation endAA = ap.av.getAlignment()
403 .getAlignmentAnnotation()[end];
405 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
406 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
411 dragCancelled = false;
412 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
414 ap.annotationPanel.repaint();
418 public void mouseEntered(MouseEvent evt)
420 if (evt.getY() < 10 && evt.getX() < 14)
425 setCursor(Cursor.getPredefinedCursor(
426 resizePanel ? Cursor.S_RESIZE_CURSOR : Cursor.DEFAULT_CURSOR));
430 public void mouseExited(MouseEvent evt)
432 dragCancelled = false;
434 if (dragEvent == null)
449 public void mousePressed(MouseEvent evt)
456 dragCancelled = false;
457 // todo: move below to mouseClicked ?
458 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
460 AlignmentAnnotation[] aa = ap.av.getAlignment()
461 .getAlignmentAnnotation();
463 // DETECT RIGHT MOUSE BUTTON IN AWT
464 if ((evt.getModifiers()
465 & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
468 PopupMenu popup = new PopupMenu(
469 MessageManager.getString("label.annotations"));
471 MenuItem item = new MenuItem(ADDNEW);
472 item.addActionListener(this);
476 // this never happens at moment: - see comment on JAL-563
479 item = new MenuItem(SHOWALL);
480 item.addActionListener(this);
484 popup.show(this, evt.getX(), evt.getY());
487 // add the rest if there are actually rows to show
488 item = new MenuItem(EDITNAME);
489 item.addActionListener(this);
491 item = new MenuItem(HIDE);
492 item.addActionListener(this);
498 if (selectedRow < aa.length)
500 if (aa[selectedRow].sequenceRef != null)
502 final String label = aa[selectedRow].label;
503 MenuItem hideType = new MenuItem(
504 MessageManager.getString("label.hide_all") + " " + label);
505 hideType.addActionListener(new ActionListener()
508 public void actionPerformed(ActionEvent e)
510 AlignmentUtils.showOrHideSequenceAnnotations(
511 ap.av.getAlignment(), Collections.singleton(label),
522 item = new MenuItem(SHOWALL);
523 item.addActionListener(this);
527 item = new MenuItem(OUTPUT_TEXT);
528 item.addActionListener(this);
530 if (selectedRow < aa.length)
532 if (aa[selectedRow].autoCalculated)
534 if (aa[selectedRow].label.indexOf("Consensus") > -1)
536 popup.addSeparator();
537 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
538 MessageManager.getString("label.ignore_gaps_consensus"),
539 (aa[selectedRow].groupRef != null)
540 ? aa[selectedRow].groupRef
541 .getIgnoreGapsConsensus()
542 : ap.av.isIgnoreGapsConsensus());
543 final AlignmentAnnotation aaa = aa[selectedRow];
544 cbmi.addItemListener(new ItemListener()
547 public void itemStateChanged(ItemEvent e)
549 if (aaa.groupRef != null)
551 // TODO: pass on reference to ap so the view can be updated.
552 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
556 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
558 ap.paintAlignment(true, true);
562 if (aaa.groupRef != null)
564 final CheckboxMenuItem chist = new CheckboxMenuItem(
566 .getString("label.show_group_histogram"),
567 aa[selectedRow].groupRef.isShowConsensusHistogram());
568 chist.addItemListener(new ItemListener()
571 public void itemStateChanged(ItemEvent e)
573 // TODO: pass on reference
579 aaa.groupRef.setShowConsensusHistogram(chist.getState());
581 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
585 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
586 MessageManager.getString("label.show_group_logo"),
587 aa[selectedRow].groupRef.isShowSequenceLogo());
588 cprofl.addItemListener(new ItemListener()
591 public void itemStateChanged(ItemEvent e)
593 // TODO: pass on reference
599 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
601 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
606 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
608 .getString("label.normalise_group_logo"),
609 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
610 cprofn.addItemListener(new ItemListener()
613 public void itemStateChanged(ItemEvent e)
615 // TODO: pass on reference
621 aaa.groupRef.setshowSequenceLogo(true);
622 aaa.groupRef.setNormaliseSequenceLogo(cprofn.getState());
624 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
631 final CheckboxMenuItem chist = new CheckboxMenuItem(
632 MessageManager.getString("label.show_histogram"),
633 av.isShowConsensusHistogram());
634 chist.addItemListener(new ItemListener()
637 public void itemStateChanged(ItemEvent e)
639 // TODO: pass on reference
645 av.setShowConsensusHistogram(chist.getState());
646 ap.alignFrame.showConsensusHistogram
647 .setState(chist.getState()); // TODO: implement
648 // ap.updateGUI()/alignFrame.updateGUI
651 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
655 final CheckboxMenuItem cprof = new CheckboxMenuItem(
656 MessageManager.getString("label.show_logo"),
657 av.isShowSequenceLogo());
658 cprof.addItemListener(new ItemListener()
661 public void itemStateChanged(ItemEvent e)
663 // TODO: pass on reference
669 av.setShowSequenceLogo(cprof.getState());
670 ap.alignFrame.showSequenceLogo.setState(cprof.getState()); // TODO:
672 // ap.updateGUI()/alignFrame.updateGUI
676 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
680 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
681 MessageManager.getString("label.normalise_logo"),
682 av.isNormaliseSequenceLogo());
683 cprofn.addItemListener(new ItemListener()
686 public void itemStateChanged(ItemEvent e)
688 // TODO: pass on reference
694 av.setShowSequenceLogo(true);
695 ap.alignFrame.normSequenceLogo
696 .setState(cprofn.getState()); // TODO:
698 // ap.updateGUI()/alignFrame.updateGUI
701 av.setNormaliseSequenceLogo(cprofn.getState());
703 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
709 item = new MenuItem(COPYCONS_SEQ);
710 item.addActionListener(this);
715 popup.show(this, evt.getX(), evt.getY());
720 if (selectedRow > -1 && selectedRow < aa.length)
722 if (aa[selectedRow].groupRef != null)
724 if (evt.getClickCount() >= 2)
726 // todo: make the ap scroll to the selection - not necessary, first
727 // click highlights/scrolls, second selects
728 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
730 SequenceGroup sg = ap.av.getSelectionGroup();
731 if (sg == null || sg == aa[selectedRow].groupRef
732 || !(jalview.util.Platform.isControlDown(evt)
733 || evt.isShiftDown()))
735 if (jalview.util.Platform.isControlDown(evt)
736 || evt.isShiftDown())
738 // clone a new selection group from the associated group
739 ap.av.setSelectionGroup(
740 new SequenceGroup(aa[selectedRow].groupRef));
744 // set selection to the associated group so it can be edited
745 ap.av.setSelectionGroup(aa[selectedRow].groupRef);
750 // modify current selection with associated group
751 int remainToAdd = aa[selectedRow].groupRef.getSize();
752 for (SequenceI sgs : aa[selectedRow].groupRef.getSequences())
754 if (jalview.util.Platform.isControlDown(evt))
756 sg.addOrRemove(sgs, --remainToAdd == 0);
760 // notionally, we should also add intermediate sequences from
761 // last added sequence ?
762 sg.addSequence(sgs, --remainToAdd == 0);
766 ap.paintAlignment(false, false);
767 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
768 ap.av.sendSelection();
772 ap.seqPanel.ap.idPanel.highlightSearchResults(
773 aa[selectedRow].groupRef.getSequences(null));
777 else if (aa[selectedRow].sequenceRef != null)
779 if (evt.getClickCount() == 1)
781 ap.seqPanel.ap.idPanel
782 .highlightSearchResults(Arrays.asList(new SequenceI[]
783 { aa[selectedRow].sequenceRef }));
785 else if (evt.getClickCount() >= 2)
787 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
788 SequenceGroup sg = ap.av.getSelectionGroup();
791 // we make a copy rather than edit the current selection if no
793 // see Enhancement JAL-1557
794 if (!(jalview.util.Platform.isControlDown(evt)
795 || evt.isShiftDown()))
797 sg = new SequenceGroup(sg);
799 sg.addSequence(aa[selectedRow].sequenceRef, false);
803 if (jalview.util.Platform.isControlDown(evt))
805 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
809 // notionally, we should also add intermediate sequences from
810 // last added sequence ?
811 sg.addSequence(aa[selectedRow].sequenceRef, true);
817 sg = new SequenceGroup();
819 sg.setEndRes(ap.av.getAlignment().getWidth() - 1);
820 sg.addSequence(aa[selectedRow].sequenceRef, false);
822 ap.av.setSelectionGroup(sg);
823 ap.paintAlignment(false, false);
824 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
825 ap.av.sendSelection();
840 protected void copy_annotseqtoclipboard(SequenceI sq)
842 if (sq == null || sq.getLength() < 1)
846 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
847 jalview.appletgui.AlignFrame.copiedSequences
848 .append(sq.getName() + "\t" + sq.getStart() + "\t" + sq.getEnd()
849 + "\t" + sq.getSequenceAsString() + "\n");
850 if (av.hasHiddenColumns())
852 jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector<>(
853 av.getAlignment().getHiddenColumns().getHiddenColumnsCopy());
858 public void update(Graphics g)
864 public void paint(Graphics g)
866 int w = getSize().width;
867 int h = getSize().height;
868 if (image == null || w != image.getWidth(this)
869 || h != image.getHeight(this))
871 image = createImage(w, ap.annotationPanel.getSize().height);
874 drawComponent(image.getGraphics(), w);
875 g.drawImage(image, 0, 0, this);
878 public void drawComponent(Graphics g, int width)
880 g.setFont(av.getFont());
881 FontMetrics fm = g.getFontMetrics(av.getFont());
882 g.setColor(Color.white);
883 g.fillRect(0, 0, getSize().width, getSize().height);
885 g.translate(0, -scrollOffset);
886 g.setColor(Color.black);
888 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
889 int y = 0, fy = g.getFont().getSize();
894 hasHiddenRows = false;
895 for (int i = 0; i < aa.length; i++)
899 hasHiddenRows = true;
903 x = width - fm.stringWidth(aa[i].label) - 3;
906 offset = -(aa[i].height - fy) / 2;
908 g.drawString(aa[i].label, x, y + offset);
911 g.translate(0, +scrollOffset);
914 // g.setColor(Color.red);
916 // g.drawLine(2, 8, 5, 2);
917 // g.drawLine(5, 2, 8, 8);
919 else if (!dragCancelled && dragEvent != null && aa != null)
921 g.setColor(Color.lightGray);
922 g.drawString(aa[selectedRow].label, dragEvent.getX(),
926 if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
928 g.setColor(Color.black);
929 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
930 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,