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.Dimension;
35 import java.awt.FlowLayout;
36 import java.awt.FontMetrics;
37 import java.awt.Frame;
38 import java.awt.Graphics;
39 import java.awt.Image;
40 import java.awt.MenuItem;
41 import java.awt.Panel;
42 import java.awt.PopupMenu;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.ActionListener;
45 import java.awt.event.InputEvent;
46 import java.awt.event.ItemEvent;
47 import java.awt.event.ItemListener;
48 import java.awt.event.MouseEvent;
49 import java.awt.event.MouseListener;
50 import java.awt.event.MouseMotionListener;
51 import java.util.Arrays;
52 import java.util.Collections;
53 import java.util.Vector;
55 public class AnnotationLabels extends Panel
56 implements ActionListener, MouseListener, MouseMotionListener
60 boolean active = false;
66 boolean resizing = false;
70 static String ADDNEW = "Add New Row";
72 static String EDITNAME = "Edit Label/Description";
74 static String HIDE = "Hide This Row";
76 static String SHOWALL = "Show All Hidden Rows";
78 static String OUTPUT_TEXT = "Show Values In Textbox";
80 static String COPYCONS_SEQ = "Copy Consensus Sequence";
88 private boolean hasHiddenRows;
90 public AnnotationLabels(AlignmentPanel ap)
97 * this retrieves the adjustable height glyph from resources. we don't use
98 * it at the moment. java.net.URL url =
99 * getClass().getResource("/images/idwidth.gif"); Image temp = null;
101 * if (url != null) { temp =
102 * java.awt.Toolkit.getDefaultToolkit().createImage(url); }
104 * try { MediaTracker mt = new MediaTracker(this); mt.addImage(temp, 0);
105 * mt.waitForID(0); } catch (Exception ex) { }
107 * BufferedImage bi = new BufferedImage(temp.getHeight(this),
108 * temp.getWidth(this), BufferedImage.TYPE_INT_RGB); Graphics2D g =
109 * (Graphics2D) bi.getGraphics(); g.rotate(Math.toRadians(90));
110 * g.drawImage(temp, 0, -bi.getWidth(this), this); image = (Image) bi;
112 addMouseListener(this);
113 addMouseMotionListener(this);
116 public AnnotationLabels(AlignViewport av)
121 public void setScrollOffset(int y, boolean repaint)
133 * @return -2 if no rows are visible at all, -1 if no visible rows were
136 int getSelectedRow(int y)
139 AlignmentAnnotation[] aa = ap.av.getAlignment()
140 .getAlignmentAnnotation();
147 for (int i = 0; i < aa.length; i++)
154 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 jalview.bin.JalviewLite.addFrame(frame,
205 ap.alignFrame.getTitle() + " - " + aa[selectedRow].label, 500,
207 cap.setText(aa[selectedRow].toString());
209 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
211 SequenceI cons = av.getConsensusSeq();
214 copy_annotseqtoclipboard(cons);
222 * Adjust size and repaint
224 protected void refresh()
226 ap.annotationPanel.adjustPanelHeight();
227 setSize(getSize().width, ap.annotationPanel.getSize().height);
229 // TODO: only paint if we needed to
230 ap.paintAlignment(true, true);
233 boolean editLabelDescription(AlignmentAnnotation annotation)
235 Checkbox padGaps = new Checkbox(
236 "Fill Empty Gaps With \"" + ap.av.getGapCharacter() + "\"",
239 EditNameDialog dialog = new EditNameDialog(annotation.label,
240 annotation.description, " Annotation Label",
241 "Annotation Description", ap.alignFrame,
242 "Edit Annotation Name / Description", 500, 180, false);
244 Panel empty = new Panel(new FlowLayout());
249 dialog.setVisible(true);
253 annotation.label = dialog.getName();
254 annotation.description = dialog.getDescription();
255 annotation.setPadGaps(padGaps.getState(), av.getGapCharacter());
266 boolean resizePanel = false;
269 public void mouseMoved(MouseEvent evt)
271 resizePanel = evt.getY() < 10 && evt.getX() < 14;
272 int row = getSelectedRow(evt.getY() + scrollOffset);
276 ParseHtmlBodyAndLinks phb = new ParseHtmlBodyAndLinks(
277 av.getAlignment().getAlignmentAnnotation()[row]
278 .getDescription(true),
282 tooltip = new Tooltip(phb.getNonHtmlContent(), this);
286 tooltip.setTip(phb.getNonHtmlContent());
289 else if (tooltip != null)
296 * curent drag position
298 MouseEvent dragEvent = null;
301 * flag to indicate drag events should be ignored
303 private boolean dragCancelled = false;
306 * clear any drag events in progress
308 public void cancelDrag()
311 dragCancelled = true;
315 public void mouseDragged(MouseEvent evt)
326 Dimension d = ap.annotationPanelHolder.getSize(),
327 e = ap.annotationSpaceFillerHolder.getSize(),
328 f = ap.seqPanelHolder.getSize();
329 int dif = evt.getY() - oldY;
331 dif /= ap.av.getCharHeight();
332 dif *= ap.av.getCharHeight();
334 if ((d.height - dif) > 20 && (f.height + dif) > 20)
336 ap.annotationPanel.setSize(d.width, d.height - dif);
337 setSize(new Dimension(e.width, d.height - dif));
338 ap.annotationSpaceFillerHolder
339 .setSize(new Dimension(e.width, d.height - dif));
340 ap.annotationPanelHolder
341 .setSize(new Dimension(d.width, d.height - dif));
342 ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height - dif, 0,
343 av.calcPanelHeight());
345 ap.seqPanelHolder.setPreferredSize(f);
346 ap.setScrollValues(av.getRanges().getStartRes(),
347 av.getRanges().getStartSeq());
349 // ap.paintAlignment(true);
357 if ((diff = 6 - evt.getY()) > 0)
360 ap.apvscroll.setValue(ap.apvscroll.getValue() - diff);
361 ap.adjustmentValueChanged(null);
364 else if ((0 < (diff = 6
365 - ap.annotationSpaceFillerHolder.getSize().height
369 ap.apvscroll.setValue(ap.apvscroll.getValue() + diff);
370 ap.adjustmentValueChanged(null);
377 public void mouseClicked(MouseEvent evt)
382 public void mouseReleased(MouseEvent evt)
384 if (!resizePanel && !dragCancelled)
386 int start = selectedRow;
388 int end = getSelectedRow(evt.getY() + scrollOffset);
390 if (start > -1 && start != end)
392 // Swap these annotations
393 AlignmentAnnotation startAA = ap.av.getAlignment()
394 .getAlignmentAnnotation()[start];
397 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
399 AlignmentAnnotation endAA = ap.av.getAlignment()
400 .getAlignmentAnnotation()[end];
402 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
403 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
408 dragCancelled = false;
410 ap.annotationPanel.repaint();
414 public void mouseEntered(MouseEvent evt)
416 if (evt.getY() < 10 && evt.getX() < 14)
424 public void mouseExited(MouseEvent evt)
426 dragCancelled = false;
428 if (dragEvent == null)
443 public void mousePressed(MouseEvent evt)
450 dragCancelled = false;
451 // todo: move below to mouseClicked ?
452 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
454 AlignmentAnnotation[] aa = ap.av.getAlignment()
455 .getAlignmentAnnotation();
457 // DETECT RIGHT MOUSE BUTTON IN AWT
458 if ((evt.getModifiers()
459 & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
462 PopupMenu popup = new PopupMenu(
463 MessageManager.getString("label.annotations"));
465 MenuItem item = new MenuItem(ADDNEW);
466 item.addActionListener(this);
470 // this never happens at moment: - see comment on JAL-563
473 item = new MenuItem(SHOWALL);
474 item.addActionListener(this);
478 popup.show(this, evt.getX(), evt.getY());
481 // add the rest if there are actually rows to show
482 item = new MenuItem(EDITNAME);
483 item.addActionListener(this);
485 item = new MenuItem(HIDE);
486 item.addActionListener(this);
492 if (selectedRow < aa.length)
494 if (aa[selectedRow].sequenceRef != null)
496 final String label = aa[selectedRow].label;
497 MenuItem hideType = new MenuItem(
498 MessageManager.getString("label.hide_all") + " " + label);
499 hideType.addActionListener(new ActionListener()
502 public void actionPerformed(ActionEvent e)
504 AlignmentUtils.showOrHideSequenceAnnotations(
505 ap.av.getAlignment(), Collections.singleton(label),
516 item = new MenuItem(SHOWALL);
517 item.addActionListener(this);
521 item = new MenuItem(OUTPUT_TEXT);
522 item.addActionListener(this);
524 if (selectedRow < aa.length)
526 if (aa[selectedRow].autoCalculated)
528 if (aa[selectedRow].label.indexOf("Consensus") > -1)
530 popup.addSeparator();
531 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
532 MessageManager.getString("label.ignore_gaps_consensus"),
533 (aa[selectedRow].groupRef != null)
534 ? aa[selectedRow].groupRef
535 .getIgnoreGapsConsensus()
536 : ap.av.isIgnoreGapsConsensus());
537 final AlignmentAnnotation aaa = aa[selectedRow];
538 cbmi.addItemListener(new ItemListener()
541 public void itemStateChanged(ItemEvent e)
543 if (aaa.groupRef != null)
545 // TODO: pass on reference to ap so the view can be updated.
546 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
550 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
552 ap.paintAlignment(true, true);
556 if (aaa.groupRef != null)
558 final CheckboxMenuItem chist = new CheckboxMenuItem(
560 .getString("label.show_group_histogram"),
561 aa[selectedRow].groupRef.isShowConsensusHistogram());
562 chist.addItemListener(new ItemListener()
565 public void itemStateChanged(ItemEvent e)
567 // TODO: pass on reference
573 aaa.groupRef.setShowConsensusHistogram(chist.getState());
575 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
579 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
580 MessageManager.getString("label.show_group_logo"),
581 aa[selectedRow].groupRef.isShowSequenceLogo());
582 cprofl.addItemListener(new ItemListener()
585 public void itemStateChanged(ItemEvent e)
587 // TODO: pass on reference
593 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
595 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
600 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
602 .getString("label.normalise_group_logo"),
603 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
604 cprofn.addItemListener(new ItemListener()
607 public void itemStateChanged(ItemEvent e)
609 // TODO: pass on reference
615 aaa.groupRef.setshowSequenceLogo(true);
616 aaa.groupRef.setNormaliseSequenceLogo(cprofn.getState());
618 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
625 final CheckboxMenuItem chist = new CheckboxMenuItem(
626 MessageManager.getString("label.show_histogram"),
627 av.isShowConsensusHistogram());
628 chist.addItemListener(new ItemListener()
631 public void itemStateChanged(ItemEvent e)
633 // TODO: pass on reference
639 av.setShowConsensusHistogram(chist.getState());
640 ap.alignFrame.showConsensusHistogram
641 .setState(chist.getState()); // TODO: implement
642 // ap.updateGUI()/alignFrame.updateGUI
645 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
649 final CheckboxMenuItem cprof = new CheckboxMenuItem(
650 MessageManager.getString("label.show_logo"),
651 av.isShowSequenceLogo());
652 cprof.addItemListener(new ItemListener()
655 public void itemStateChanged(ItemEvent e)
657 // TODO: pass on reference
663 av.setShowSequenceLogo(cprof.getState());
664 ap.alignFrame.showSequenceLogo.setState(cprof.getState()); // TODO:
666 // ap.updateGUI()/alignFrame.updateGUI
670 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
674 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
675 MessageManager.getString("label.normalise_logo"),
676 av.isNormaliseSequenceLogo());
677 cprofn.addItemListener(new ItemListener()
680 public void itemStateChanged(ItemEvent e)
682 // TODO: pass on reference
688 av.setShowSequenceLogo(true);
689 ap.alignFrame.normSequenceLogo
690 .setState(cprofn.getState()); // TODO:
692 // ap.updateGUI()/alignFrame.updateGUI
695 av.setNormaliseSequenceLogo(cprofn.getState());
697 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
703 item = new MenuItem(COPYCONS_SEQ);
704 item.addActionListener(this);
709 popup.show(this, evt.getX(), evt.getY());
714 if (selectedRow > -1 && selectedRow < aa.length)
716 if (aa[selectedRow].groupRef != null)
718 if (evt.getClickCount() >= 2)
720 // todo: make the ap scroll to the selection - not necessary, first
721 // click highlights/scrolls, second selects
722 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
724 SequenceGroup sg = ap.av.getSelectionGroup();
725 if (sg == null || sg == aa[selectedRow].groupRef
726 || !(jalview.util.Platform.isControlDown(evt)
727 || evt.isShiftDown()))
729 if (jalview.util.Platform.isControlDown(evt)
730 || evt.isShiftDown())
732 // clone a new selection group from the associated group
733 ap.av.setSelectionGroup(
734 new SequenceGroup(aa[selectedRow].groupRef));
738 // set selection to the associated group so it can be edited
739 ap.av.setSelectionGroup(aa[selectedRow].groupRef);
744 // modify current selection with associated group
745 int remainToAdd = aa[selectedRow].groupRef.getSize();
746 for (SequenceI sgs : aa[selectedRow].groupRef.getSequences())
748 if (jalview.util.Platform.isControlDown(evt))
750 sg.addOrRemove(sgs, --remainToAdd == 0);
754 // notionally, we should also add intermediate sequences from
755 // last added sequence ?
756 sg.addSequence(sgs, --remainToAdd == 0);
760 ap.paintAlignment(false, false);
761 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
762 ap.av.sendSelection();
766 ap.seqPanel.ap.idPanel.highlightSearchResults(
767 aa[selectedRow].groupRef.getSequences(null));
771 else if (aa[selectedRow].sequenceRef != null)
773 if (evt.getClickCount() == 1)
775 ap.seqPanel.ap.idPanel
776 .highlightSearchResults(Arrays.asList(new SequenceI[]
777 { aa[selectedRow].sequenceRef }));
779 else if (evt.getClickCount() >= 2)
781 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
782 SequenceGroup sg = ap.av.getSelectionGroup();
785 // we make a copy rather than edit the current selection if no
787 // see Enhancement JAL-1557
788 if (!(jalview.util.Platform.isControlDown(evt)
789 || evt.isShiftDown()))
791 sg = new SequenceGroup(sg);
793 sg.addSequence(aa[selectedRow].sequenceRef, false);
797 if (jalview.util.Platform.isControlDown(evt))
799 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
803 // notionally, we should also add intermediate sequences from
804 // last added sequence ?
805 sg.addSequence(aa[selectedRow].sequenceRef, true);
811 sg = new SequenceGroup();
813 sg.setEndRes(ap.av.getAlignment().getWidth() - 1);
814 sg.addSequence(aa[selectedRow].sequenceRef, false);
816 ap.av.setSelectionGroup(sg);
817 ap.paintAlignment(false, false);
818 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
819 ap.av.sendSelection();
834 protected void copy_annotseqtoclipboard(SequenceI sq)
836 if (sq == null || sq.getLength() < 1)
840 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
841 jalview.appletgui.AlignFrame.copiedSequences
842 .append(sq.getName() + "\t" + sq.getStart() + "\t" + sq.getEnd()
843 + "\t" + sq.getSequenceAsString() + "\n");
844 if (av.hasHiddenColumns())
846 jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector<>(
847 av.getAlignment().getHiddenColumns().getHiddenColumnsCopy());
852 public void update(Graphics g)
858 public void paint(Graphics g)
860 int w = getSize().width;
861 int h = getSize().height;
862 if (image == null || w != image.getWidth(this)
863 || h != image.getHeight(this))
865 image = createImage(w, ap.annotationPanel.getSize().height);
868 drawComponent(image.getGraphics(), w);
869 g.drawImage(image, 0, 0, this);
872 public void drawComponent(Graphics g, int width)
874 g.setFont(av.getFont());
875 FontMetrics fm = g.getFontMetrics(av.getFont());
876 g.setColor(Color.white);
877 g.fillRect(0, 0, getSize().width, getSize().height);
879 g.translate(0, -scrollOffset);
880 g.setColor(Color.black);
882 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
883 int y = 0, fy = g.getFont().getSize();
888 hasHiddenRows = false;
889 for (int i = 0; i < aa.length; i++)
893 hasHiddenRows = true;
897 x = width - fm.stringWidth(aa[i].label) - 3;
900 offset = -(aa[i].height - fy) / 2;
902 g.drawString(aa[i].label, x, y + offset);
905 g.translate(0, +scrollOffset);
908 g.setColor(Color.red);
910 g.drawLine(2, 8, 5, 2);
911 g.drawLine(5, 2, 8, 8);
913 else if (!dragCancelled && dragEvent != null && aa != null)
915 g.setColor(Color.lightGray);
916 g.drawString(aa[selectedRow].label, dragEvent.getX(),
920 if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
922 g.setColor(Color.black);
923 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
924 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,