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.HiddenColumns;
27 import jalview.datamodel.SequenceGroup;
28 import jalview.datamodel.SequenceI;
29 import jalview.util.MessageManager;
30 import jalview.util.ParseHtmlBodyAndLinks;
32 import java.awt.Checkbox;
33 import java.awt.CheckboxMenuItem;
34 import java.awt.Color;
35 import java.awt.Cursor;
36 import java.awt.Dimension;
37 import java.awt.FlowLayout;
38 import java.awt.FontMetrics;
39 import java.awt.Frame;
40 import java.awt.Graphics;
41 import java.awt.Image;
42 import java.awt.MenuItem;
43 import java.awt.Panel;
44 import java.awt.PopupMenu;
45 import java.awt.event.ActionEvent;
46 import java.awt.event.ActionListener;
47 import java.awt.event.InputEvent;
48 import java.awt.event.ItemEvent;
49 import java.awt.event.ItemListener;
50 import java.awt.event.MouseEvent;
51 import java.awt.event.MouseListener;
52 import java.awt.event.MouseMotionListener;
53 import java.util.Arrays;
54 import java.util.Collections;
56 public class AnnotationLabels extends Panel
57 implements ActionListener, MouseListener, MouseMotionListener
62 * width in pixels within which height adjuster arrows are shown and active
64 private static final int HEIGHT_ADJUSTER_WIDTH = 50;
67 * height in pixels for allowing height adjuster to be active
69 private static int HEIGHT_ADJUSTER_HEIGHT = 10;
71 boolean active = false;
77 boolean resizing = false;
81 static String ADDNEW = "Add New Row";
83 static String EDITNAME = "Edit Label/Description";
85 static String HIDE = "Hide This Row";
87 static String SHOWALL = "Show All Hidden Rows";
89 static String OUTPUT_TEXT = "Show Values In Textbox";
91 static String COPYCONS_SEQ = "Copy Consensus Sequence";
99 private boolean hasHiddenRows;
101 public AnnotationLabels(AlignmentPanel ap)
106 addMouseListener(this);
107 addMouseMotionListener(this);
110 public AnnotationLabels(AlignViewport av)
115 public void setScrollOffset(int y, boolean repaint)
127 * @return -2 if no rows are visible at all, -1 if no visible rows were
130 int getSelectedRow(int y)
133 AlignmentAnnotation[] aa = ap.av.getAlignment()
134 .getAlignmentAnnotation();
141 for (int i = 0; i < aa.length; i++)
148 height += aa[i].height;
160 public void actionPerformed(ActionEvent evt)
162 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
164 if (evt.getActionCommand().equals(ADDNEW))
166 AlignmentAnnotation newAnnotation = new AlignmentAnnotation("", null,
167 new Annotation[ap.av.getAlignment().getWidth()]);
169 if (!editLabelDescription(newAnnotation))
174 ap.av.getAlignment().addAnnotation(newAnnotation);
175 ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
177 else if (evt.getActionCommand().equals(EDITNAME))
179 editLabelDescription(aa[selectedRow]);
181 else if (evt.getActionCommand().equals(HIDE))
183 aa[selectedRow].visible = false;
185 else if (evt.getActionCommand().equals(SHOWALL))
187 for (int i = 0; i < aa.length; i++)
189 aa[i].visible = (aa[i].annotations == null) ? false : true;
192 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
194 CutAndPasteTransfer cap = new CutAndPasteTransfer(false,
196 Frame frame = new Frame();
198 jalview.bin.JalviewLite.addFrame(frame,
199 ap.alignFrame.getTitle() + " - " + aa[selectedRow].label, 500,
201 cap.setText(aa[selectedRow].toString());
203 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
205 SequenceGroup group = aa[selectedRow].groupRef;
206 SequenceI cons = group == null ? av.getConsensusSeq()
207 : group.getConsensusSeq();
210 copy_annotseqtoclipboard(cons);
218 * Adjust size and repaint
220 protected void refresh()
222 ap.annotationPanel.adjustPanelHeight();
223 setSize(getSize().width, ap.annotationPanel.getSize().height);
225 // TODO: only paint if we needed to
226 ap.paintAlignment(true, true);
229 boolean editLabelDescription(AlignmentAnnotation annotation)
231 Checkbox padGaps = new Checkbox(
232 "Fill Empty Gaps With \"" + ap.av.getGapCharacter() + "\"",
235 EditNameDialog dialog = new EditNameDialog(annotation.label,
236 annotation.description, " Annotation Label",
237 "Annotation Description", ap.alignFrame,
238 "Edit Annotation Name / Description", 500, 180, false);
240 Panel empty = new Panel(new FlowLayout());
245 dialog.setVisible(true);
249 annotation.label = dialog.getName();
250 annotation.description = dialog.getDescription();
251 annotation.setPadGaps(padGaps.getState(), av.getGapCharacter());
262 boolean resizePanel = false;
265 public void mouseMoved(MouseEvent evt)
267 resizePanel = evt.getY() < HEIGHT_ADJUSTER_HEIGHT
268 && evt.getX() < HEIGHT_ADJUSTER_WIDTH;
269 setCursor(Cursor.getPredefinedCursor(
270 resizePanel ? Cursor.S_RESIZE_CURSOR : Cursor.DEFAULT_CURSOR));
271 int row = getSelectedRow(evt.getY() + scrollOffset);
275 ParseHtmlBodyAndLinks phb = new ParseHtmlBodyAndLinks(
276 av.getAlignment().getAlignmentAnnotation()[row]
277 .getDescription(true),
281 tooltip = new Tooltip(phb.getNonHtmlContent(), this);
285 tooltip.setTip(phb.getNonHtmlContent());
288 else if (tooltip != null)
295 * curent drag position
297 MouseEvent dragEvent = null;
300 * flag to indicate drag events should be ignored
302 private boolean dragCancelled = false;
305 * clear any drag events in progress
307 public void cancelDrag()
310 dragCancelled = true;
314 public void mouseDragged(MouseEvent evt)
325 Dimension d = ap.annotationPanelHolder.getSize(),
326 e = ap.annotationSpaceFillerHolder.getSize(),
327 f = ap.seqPanelHolder.getSize();
328 int dif = evt.getY() - oldY;
330 dif /= ap.av.getCharHeight();
331 dif *= ap.av.getCharHeight();
333 if ((d.height - dif) > 20 && (f.height + dif) > 20)
335 ap.annotationPanel.setSize(d.width, d.height - dif);
336 setSize(new Dimension(e.width, d.height - dif));
337 ap.annotationSpaceFillerHolder
338 .setSize(new Dimension(e.width, d.height - dif));
339 ap.annotationPanelHolder
340 .setSize(new Dimension(d.width, d.height - dif));
341 ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height - dif, 0,
342 av.calcPanelHeight());
344 ap.seqPanelHolder.setPreferredSize(f);
345 ap.setScrollValues(av.getRanges().getStartRes(),
346 av.getRanges().getStartSeq());
348 // ap.paintAlignment(true);
356 if ((diff = 6 - evt.getY()) > 0)
359 ap.apvscroll.setValue(ap.apvscroll.getValue() - diff);
360 ap.adjustmentValueChanged(null);
363 else if ((0 < (diff = 6
364 - ap.annotationSpaceFillerHolder.getSize().height
368 ap.apvscroll.setValue(ap.apvscroll.getValue() + diff);
369 ap.adjustmentValueChanged(null);
376 public void mouseClicked(MouseEvent evt)
381 public void mouseReleased(MouseEvent evt)
383 if (!resizePanel && !dragCancelled)
385 int start = selectedRow;
387 int end = getSelectedRow(evt.getY() + scrollOffset);
389 if (start > -1 && start != end)
391 // Swap these annotations
392 AlignmentAnnotation startAA = ap.av.getAlignment()
393 .getAlignmentAnnotation()[start];
396 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
398 AlignmentAnnotation endAA = ap.av.getAlignment()
399 .getAlignmentAnnotation()[end];
401 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
402 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
407 dragCancelled = false;
408 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
410 ap.annotationPanel.repaint();
414 public void mouseEntered(MouseEvent evt)
416 if (evt.getY() < 10 && evt.getX() < 14)
421 setCursor(Cursor.getPredefinedCursor(
422 resizePanel ? Cursor.S_RESIZE_CURSOR : Cursor.DEFAULT_CURSOR));
426 public void mouseExited(MouseEvent evt)
428 dragCancelled = false;
430 if (dragEvent == null)
445 public void mousePressed(MouseEvent evt)
452 dragCancelled = false;
453 // todo: move below to mouseClicked ?
454 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
456 AlignmentAnnotation[] aa = ap.av.getAlignment()
457 .getAlignmentAnnotation();
459 // DETECT RIGHT MOUSE BUTTON IN AWT
460 if ((evt.getModifiers()
461 & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
464 PopupMenu popup = new PopupMenu(
465 MessageManager.getString("label.annotations"));
467 MenuItem item = new MenuItem(ADDNEW);
468 item.addActionListener(this);
472 // this never happens at moment: - see comment on JAL-563
475 item = new MenuItem(SHOWALL);
476 item.addActionListener(this);
480 popup.show(this, evt.getX(), evt.getY());
483 // add the rest if there are actually rows to show
484 item = new MenuItem(EDITNAME);
485 item.addActionListener(this);
487 item = new MenuItem(HIDE);
488 item.addActionListener(this);
494 if (selectedRow < aa.length)
496 if (aa[selectedRow].sequenceRef != null)
498 final String label = aa[selectedRow].label;
499 MenuItem hideType = new MenuItem(
500 MessageManager.getString("label.hide_all") + " " + label);
501 hideType.addActionListener(new ActionListener()
504 public void actionPerformed(ActionEvent e)
506 AlignmentUtils.showOrHideSequenceAnnotations(
507 ap.av.getAlignment(), Collections.singleton(label),
518 item = new MenuItem(SHOWALL);
519 item.addActionListener(this);
523 item = new MenuItem(OUTPUT_TEXT);
524 item.addActionListener(this);
526 if (selectedRow < aa.length)
528 if (aa[selectedRow].autoCalculated)
530 if (aa[selectedRow].label.indexOf("Consensus") > -1)
532 popup.addSeparator();
533 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
534 MessageManager.getString("label.ignore_gaps_consensus"),
535 (aa[selectedRow].groupRef != null)
536 ? aa[selectedRow].groupRef
537 .getIgnoreGapsConsensus()
538 : ap.av.isIgnoreGapsConsensus());
539 final AlignmentAnnotation aaa = aa[selectedRow];
540 cbmi.addItemListener(new ItemListener()
543 public void itemStateChanged(ItemEvent e)
545 if (aaa.groupRef != null)
547 // TODO: pass on reference to ap so the view can be updated.
548 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
552 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
554 ap.paintAlignment(true, true);
558 if (aaa.groupRef != null)
560 final CheckboxMenuItem chist = new CheckboxMenuItem(
562 .getString("label.show_group_histogram"),
563 aa[selectedRow].groupRef.isShowConsensusHistogram());
564 chist.addItemListener(new ItemListener()
567 public void itemStateChanged(ItemEvent e)
569 // TODO: pass on reference
575 aaa.groupRef.setShowConsensusHistogram(chist.getState());
577 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
581 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
582 MessageManager.getString("label.show_group_logo"),
583 aa[selectedRow].groupRef.isShowSequenceLogo());
584 cprofl.addItemListener(new ItemListener()
587 public void itemStateChanged(ItemEvent e)
589 // TODO: pass on reference
595 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
597 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
602 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
604 .getString("label.normalise_group_logo"),
605 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
606 cprofn.addItemListener(new ItemListener()
609 public void itemStateChanged(ItemEvent e)
611 // TODO: pass on reference
617 aaa.groupRef.setshowSequenceLogo(true);
618 aaa.groupRef.setNormaliseSequenceLogo(cprofn.getState());
620 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
627 final CheckboxMenuItem chist = new CheckboxMenuItem(
628 MessageManager.getString("label.show_histogram"),
629 av.isShowConsensusHistogram());
630 chist.addItemListener(new ItemListener()
633 public void itemStateChanged(ItemEvent e)
635 // TODO: pass on reference
641 av.setShowConsensusHistogram(chist.getState());
642 ap.alignFrame.showConsensusHistogram
643 .setState(chist.getState()); // TODO: implement
644 // ap.updateGUI()/alignFrame.updateGUI
647 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
651 final CheckboxMenuItem cprof = new CheckboxMenuItem(
652 MessageManager.getString("label.show_logo"),
653 av.isShowSequenceLogo());
654 cprof.addItemListener(new ItemListener()
657 public void itemStateChanged(ItemEvent e)
659 // TODO: pass on reference
665 av.setShowSequenceLogo(cprof.getState());
666 ap.alignFrame.showSequenceLogo.setState(cprof.getState()); // TODO:
668 // ap.updateGUI()/alignFrame.updateGUI
672 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
676 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
677 MessageManager.getString("label.normalise_logo"),
678 av.isNormaliseSequenceLogo());
679 cprofn.addItemListener(new ItemListener()
682 public void itemStateChanged(ItemEvent e)
684 // TODO: pass on reference
690 av.setShowSequenceLogo(true);
691 ap.alignFrame.normSequenceLogo
692 .setState(cprofn.getState()); // TODO:
694 // ap.updateGUI()/alignFrame.updateGUI
697 av.setNormaliseSequenceLogo(cprofn.getState());
699 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
705 item = new MenuItem(COPYCONS_SEQ);
706 item.addActionListener(this);
711 popup.show(this, evt.getX(), evt.getY());
716 if (selectedRow > -1 && selectedRow < aa.length)
718 if (aa[selectedRow].groupRef != null)
720 if (evt.getClickCount() >= 2)
722 // todo: make the ap scroll to the selection - not necessary, first
723 // click highlights/scrolls, second selects
724 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
726 SequenceGroup sg = ap.av.getSelectionGroup();
727 if (sg == null || sg == aa[selectedRow].groupRef
728 || !(jalview.util.Platform.isControlDown(evt)
729 || evt.isShiftDown()))
731 if (jalview.util.Platform.isControlDown(evt)
732 || evt.isShiftDown())
734 // clone a new selection group from the associated group
735 ap.av.setSelectionGroup(
736 new SequenceGroup(aa[selectedRow].groupRef));
740 // set selection to the associated group so it can be edited
741 ap.av.setSelectionGroup(aa[selectedRow].groupRef);
746 // modify current selection with associated group
747 int remainToAdd = aa[selectedRow].groupRef.getSize();
748 for (SequenceI sgs : aa[selectedRow].groupRef.getSequences())
750 if (jalview.util.Platform.isControlDown(evt))
752 sg.addOrRemove(sgs, --remainToAdd == 0);
756 // notionally, we should also add intermediate sequences from
757 // last added sequence ?
758 sg.addSequence(sgs, --remainToAdd == 0);
762 ap.paintAlignment(false, false);
763 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
764 ap.av.sendSelection();
768 ap.seqPanel.ap.idPanel.highlightSearchResults(
769 aa[selectedRow].groupRef.getSequences(null));
773 else if (aa[selectedRow].sequenceRef != null)
775 if (evt.getClickCount() == 1)
777 ap.seqPanel.ap.idPanel
778 .highlightSearchResults(Arrays.asList(new SequenceI[]
779 { aa[selectedRow].sequenceRef }));
781 else if (evt.getClickCount() >= 2)
783 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
784 SequenceGroup sg = ap.av.getSelectionGroup();
787 // we make a copy rather than edit the current selection if no
789 // see Enhancement JAL-1557
790 if (!(jalview.util.Platform.isControlDown(evt)
791 || evt.isShiftDown()))
793 sg = new SequenceGroup(sg);
795 sg.addSequence(aa[selectedRow].sequenceRef, false);
799 if (jalview.util.Platform.isControlDown(evt))
801 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
805 // notionally, we should also add intermediate sequences from
806 // last added sequence ?
807 sg.addSequence(aa[selectedRow].sequenceRef, true);
813 sg = new SequenceGroup();
815 sg.setEndRes(ap.av.getAlignment().getWidth() - 1);
816 sg.addSequence(aa[selectedRow].sequenceRef, false);
818 ap.av.setSelectionGroup(sg);
819 ap.paintAlignment(false, false);
820 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
821 ap.av.sendSelection();
836 protected void copy_annotseqtoclipboard(SequenceI sq)
838 if (sq == null || sq.getLength() < 1)
842 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
843 jalview.appletgui.AlignFrame.copiedSequences
844 .append(sq.getName() + "\t" + sq.getStart() + "\t" + sq.getEnd()
845 + "\t" + sq.getSequenceAsString() + "\n");
846 if (av.hasHiddenColumns())
848 jalview.appletgui.AlignFrame.copiedHiddenColumns = new HiddenColumns(
849 av.getAlignment().getHiddenColumns());
854 public void update(Graphics g)
860 public void paint(Graphics g)
862 int w = getSize().width;
863 int h = getSize().height;
864 if (image == null || w != image.getWidth(this)
865 || h != image.getHeight(this))
867 image = createImage(w, ap.annotationPanel.getSize().height);
870 drawComponent(image.getGraphics(), w);
871 g.drawImage(image, 0, 0, this);
874 public void drawComponent(Graphics g, int width)
876 g.setFont(av.getFont());
877 FontMetrics fm = g.getFontMetrics(av.getFont());
878 g.setColor(Color.white);
879 g.fillRect(0, 0, getSize().width, getSize().height);
881 g.translate(0, -scrollOffset);
882 g.setColor(Color.black);
884 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
885 int y = 0, fy = g.getFont().getSize();
890 hasHiddenRows = false;
891 for (int i = 0; i < aa.length; i++)
895 hasHiddenRows = true;
899 x = width - fm.stringWidth(aa[i].label) - 3;
902 offset = -(aa[i].height - fy) / 2;
904 g.drawString(aa[i].label, x, y + offset);
907 g.translate(0, +scrollOffset);
909 if (!resizePanel && !dragCancelled && dragEvent != null && aa != null)
911 g.setColor(Color.lightGray);
912 g.drawString(aa[selectedRow].label, dragEvent.getX(),
916 if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
918 g.setColor(Color.black);
919 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
920 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,