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;
31 import jalview.viewmodel.AlignmentViewport.AutoAnnotation;
33 import java.awt.Checkbox;
34 import java.awt.CheckboxMenuItem;
35 import java.awt.Color;
36 import java.awt.Cursor;
37 import java.awt.Dimension;
38 import java.awt.FlowLayout;
39 import java.awt.FontMetrics;
40 import java.awt.Frame;
41 import java.awt.Graphics;
42 import java.awt.Image;
43 import java.awt.MenuItem;
44 import java.awt.Panel;
45 import java.awt.PopupMenu;
46 import java.awt.event.ActionEvent;
47 import java.awt.event.ActionListener;
48 import java.awt.event.InputEvent;
49 import java.awt.event.ItemEvent;
50 import java.awt.event.ItemListener;
51 import java.awt.event.MouseEvent;
52 import java.awt.event.MouseListener;
53 import java.awt.event.MouseMotionListener;
54 import java.util.Arrays;
55 import java.util.Collections;
57 public class AnnotationLabels extends Panel
58 implements ActionListener, MouseListener, MouseMotionListener
63 * width in pixels within which height adjuster arrows are shown and active
65 private static final int HEIGHT_ADJUSTER_WIDTH = 50;
68 * height in pixels for allowing height adjuster to be active
70 private static int HEIGHT_ADJUSTER_HEIGHT = 10;
72 boolean active = false;
78 boolean resizing = false;
82 static String ADDNEW = "Add New Row";
84 static String EDITNAME = "Edit Label/Description";
86 static String HIDE = "Hide This Row";
88 static String SHOWALL = "Show All Hidden Rows";
90 static String OUTPUT_TEXT = "Show Values In Textbox";
92 static String COPYCONS_SEQ = "Copy Consensus Sequence";
100 private boolean hasHiddenRows;
102 public AnnotationLabels(AlignmentPanel ap)
107 addMouseListener(this);
108 addMouseMotionListener(this);
111 public AnnotationLabels(AlignViewport av)
116 public void setScrollOffset(int y, boolean repaint)
128 * @return -2 if no rows are visible at all, -1 if no visible rows were
131 int getSelectedRow(int y)
134 AlignmentAnnotation[] aa = ap.av.getAlignment()
135 .getAlignmentAnnotation();
142 for (int i = 0; i < aa.length; i++)
149 height += aa[i].height;
161 public void actionPerformed(ActionEvent evt)
163 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
165 if (evt.getActionCommand().equals(ADDNEW))
167 AlignmentAnnotation newAnnotation = new AlignmentAnnotation("", null,
168 new Annotation[ap.av.getAlignment().getWidth()]);
170 if (!editLabelDescription(newAnnotation))
175 ap.av.getAlignment().addAnnotation(newAnnotation);
176 ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
178 else if (evt.getActionCommand().equals(EDITNAME))
180 editLabelDescription(aa[selectedRow]);
182 else if (evt.getActionCommand().equals(HIDE))
184 aa[selectedRow].visible = false;
186 else if (evt.getActionCommand().equals(SHOWALL))
188 for (int i = 0; i < aa.length; i++)
190 aa[i].visible = (aa[i].annotations == null) ? false : true;
193 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
195 CutAndPasteTransfer cap = new CutAndPasteTransfer(false,
197 Frame frame = new Frame();
199 jalview.bin.JalviewLite.addFrame(frame,
200 ap.alignFrame.getTitle() + " - " + aa[selectedRow].label, 500,
202 cap.setText(aa[selectedRow].toString());
204 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
206 SequenceGroup group = aa[selectedRow].groupRef;
207 SequenceI cons = group == null ? av.getConsensusSeq()
208 : group.getConsensusSeq();
211 copy_annotseqtoclipboard(cons);
219 * Adjust size and repaint
221 protected void refresh()
223 ap.annotationPanel.adjustPanelHeight();
224 setSize(getSize().width, ap.annotationPanel.getSize().height);
226 // TODO: only paint if we needed to
227 ap.paintAlignment(true, true);
230 boolean editLabelDescription(AlignmentAnnotation annotation)
232 Checkbox padGaps = new Checkbox(
233 "Fill Empty Gaps With \"" + ap.av.getGapCharacter() + "\"",
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;
266 public void mouseMoved(MouseEvent evt)
268 resizePanel = evt.getY() < HEIGHT_ADJUSTER_HEIGHT
269 && evt.getX() < HEIGHT_ADJUSTER_WIDTH;
270 setCursor(Cursor.getPredefinedCursor(
271 resizePanel ? Cursor.S_RESIZE_CURSOR : Cursor.DEFAULT_CURSOR));
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;
409 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
411 ap.annotationPanel.repaint();
415 public void mouseEntered(MouseEvent evt)
417 if (evt.getY() < 10 && evt.getX() < 14)
422 setCursor(Cursor.getPredefinedCursor(
423 resizePanel ? Cursor.S_RESIZE_CURSOR : Cursor.DEFAULT_CURSOR));
427 public void mouseExited(MouseEvent evt)
429 dragCancelled = false;
431 if (dragEvent == null)
446 public void mousePressed(MouseEvent evt)
453 dragCancelled = false;
454 // todo: move below to mouseClicked ?
455 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
457 AlignmentAnnotation[] aa = ap.av.getAlignment()
458 .getAlignmentAnnotation();
460 // DETECT RIGHT MOUSE BUTTON IN AWT
461 if ((evt.getModifiers()
462 & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
465 PopupMenu popup = new PopupMenu(
466 MessageManager.getString("label.annotations"));
468 MenuItem item = new MenuItem(ADDNEW);
469 item.addActionListener(this);
473 // this never happens at moment: - see comment on JAL-563
476 item = new MenuItem(SHOWALL);
477 item.addActionListener(this);
481 popup.show(this, evt.getX(), evt.getY());
484 // add the rest if there are actually rows to show
485 item = new MenuItem(EDITNAME);
486 item.addActionListener(this);
488 item = new MenuItem(HIDE);
489 item.addActionListener(this);
495 if (selectedRow < aa.length)
497 if (aa[selectedRow].sequenceRef != null)
499 final String label = aa[selectedRow].label;
500 MenuItem hideType = new MenuItem(
501 MessageManager.getString("label.hide_all") + " " + label);
502 hideType.addActionListener(new ActionListener()
505 public void actionPerformed(ActionEvent e)
507 AlignmentUtils.showOrHideSequenceAnnotations(
508 ap.av.getAlignment(), Collections.singleton(label),
519 item = new MenuItem(SHOWALL);
520 item.addActionListener(this);
524 item = new MenuItem(OUTPUT_TEXT);
525 item.addActionListener(this);
527 if (selectedRow < aa.length)
529 if (aa[selectedRow].autoCalculated)
531 if (aa[selectedRow].label
532 .indexOf(AutoAnnotation.CONSENSUS.label) > -1)
534 popup.addSeparator();
535 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
536 MessageManager.getString("label.ignore_gaps_consensus"),
537 (aa[selectedRow].groupRef != null)
538 ? aa[selectedRow].groupRef
539 .getIgnoreGapsConsensus()
540 : ap.av.isIgnoreGapsConsensus());
541 final AlignmentAnnotation aaa = aa[selectedRow];
542 cbmi.addItemListener(new ItemListener()
545 public void itemStateChanged(ItemEvent e)
547 if (aaa.groupRef != null)
549 // TODO: pass on reference to ap so the view can be updated.
550 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
554 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
556 ap.paintAlignment(true, true);
560 if (aaa.groupRef != null)
562 final CheckboxMenuItem chist = new CheckboxMenuItem(
564 .getString("label.show_group_histogram"),
565 aa[selectedRow].groupRef.isShowConsensusHistogram());
566 chist.addItemListener(new ItemListener()
569 public void itemStateChanged(ItemEvent e)
571 // TODO: pass on reference
577 aaa.groupRef.setShowConsensusHistogram(chist.getState());
579 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
583 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
584 MessageManager.getString("label.show_group_logo"),
585 aa[selectedRow].groupRef.isShowSequenceLogo());
586 cprofl.addItemListener(new ItemListener()
589 public void itemStateChanged(ItemEvent e)
591 // TODO: pass on reference
597 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
599 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
604 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
606 .getString("label.normalise_group_logo"),
607 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
608 cprofn.addItemListener(new ItemListener()
611 public void itemStateChanged(ItemEvent e)
613 // TODO: pass on reference
619 aaa.groupRef.setshowSequenceLogo(true);
620 aaa.groupRef.setNormaliseSequenceLogo(cprofn.getState());
622 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
629 final CheckboxMenuItem chist = new CheckboxMenuItem(
630 MessageManager.getString("label.show_histogram"),
631 av.isShowConsensusHistogram());
632 chist.addItemListener(new ItemListener()
635 public void itemStateChanged(ItemEvent e)
637 // TODO: pass on reference
643 av.setShowConsensusHistogram(chist.getState());
644 ap.alignFrame.showConsensusHistogram
645 .setState(chist.getState()); // TODO: implement
646 // ap.updateGUI()/alignFrame.updateGUI
649 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
653 final CheckboxMenuItem cprof = new CheckboxMenuItem(
654 MessageManager.getString("label.show_logo"),
655 av.isShowSequenceLogo());
656 cprof.addItemListener(new ItemListener()
659 public void itemStateChanged(ItemEvent e)
661 // TODO: pass on reference
667 av.setShowSequenceLogo(cprof.getState());
668 ap.alignFrame.showSequenceLogo.setState(cprof.getState()); // TODO:
670 // ap.updateGUI()/alignFrame.updateGUI
674 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
678 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
679 MessageManager.getString("label.normalise_logo"),
680 av.isNormaliseSequenceLogo());
681 cprofn.addItemListener(new ItemListener()
684 public void itemStateChanged(ItemEvent e)
686 // TODO: pass on reference
692 av.setShowSequenceLogo(true);
693 ap.alignFrame.normSequenceLogo
694 .setState(cprofn.getState()); // TODO:
696 // ap.updateGUI()/alignFrame.updateGUI
699 av.setNormaliseSequenceLogo(cprofn.getState());
701 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
707 item = new MenuItem(COPYCONS_SEQ);
708 item.addActionListener(this);
713 popup.show(this, evt.getX(), evt.getY());
718 if (selectedRow > -1 && selectedRow < aa.length)
720 if (aa[selectedRow].groupRef != null)
722 if (evt.getClickCount() >= 2)
724 // todo: make the ap scroll to the selection - not necessary, first
725 // click highlights/scrolls, second selects
726 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
728 SequenceGroup sg = ap.av.getSelectionGroup();
729 if (sg == null || sg == aa[selectedRow].groupRef
730 || !(jalview.util.Platform.isControlDown(evt)
731 || evt.isShiftDown()))
733 if (jalview.util.Platform.isControlDown(evt)
734 || evt.isShiftDown())
736 // clone a new selection group from the associated group
737 ap.av.setSelectionGroup(
738 new SequenceGroup(aa[selectedRow].groupRef));
742 // set selection to the associated group so it can be edited
743 ap.av.setSelectionGroup(aa[selectedRow].groupRef);
748 // modify current selection with associated group
749 int remainToAdd = aa[selectedRow].groupRef.getSize();
750 for (SequenceI sgs : aa[selectedRow].groupRef.getSequences())
752 if (jalview.util.Platform.isControlDown(evt))
754 sg.addOrRemove(sgs, --remainToAdd == 0);
758 // notionally, we should also add intermediate sequences from
759 // last added sequence ?
760 sg.addSequence(sgs, --remainToAdd == 0);
764 ap.paintAlignment(false, false);
765 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
766 ap.av.sendSelection();
770 ap.seqPanel.ap.idPanel.highlightSearchResults(
771 aa[selectedRow].groupRef.getSequences(null));
775 else if (aa[selectedRow].sequenceRef != null)
777 if (evt.getClickCount() == 1)
779 ap.seqPanel.ap.idPanel
780 .highlightSearchResults(Arrays.asList(new SequenceI[]
781 { aa[selectedRow].sequenceRef }));
783 else if (evt.getClickCount() >= 2)
785 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
786 SequenceGroup sg = ap.av.getSelectionGroup();
789 // we make a copy rather than edit the current selection if no
791 // see Enhancement JAL-1557
792 if (!(jalview.util.Platform.isControlDown(evt)
793 || evt.isShiftDown()))
795 sg = new SequenceGroup(sg);
797 sg.addSequence(aa[selectedRow].sequenceRef, false);
801 if (jalview.util.Platform.isControlDown(evt))
803 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
807 // notionally, we should also add intermediate sequences from
808 // last added sequence ?
809 sg.addSequence(aa[selectedRow].sequenceRef, true);
815 sg = new SequenceGroup();
817 sg.setEndRes(ap.av.getAlignment().getWidth() - 1);
818 sg.addSequence(aa[selectedRow].sequenceRef, false);
820 ap.av.setSelectionGroup(sg);
821 ap.paintAlignment(false, false);
822 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
823 ap.av.sendSelection();
838 protected void copy_annotseqtoclipboard(SequenceI sq)
840 if (sq == null || sq.getLength() < 1)
844 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
845 jalview.appletgui.AlignFrame.copiedSequences
846 .append(sq.getName() + "\t" + sq.getStart() + "\t" + sq.getEnd()
847 + "\t" + sq.getSequenceAsString() + "\n");
848 if (av.hasHiddenColumns())
850 jalview.appletgui.AlignFrame.copiedHiddenColumns = new HiddenColumns(
851 av.getAlignment().getHiddenColumns());
856 public void update(Graphics g)
862 public void paint(Graphics g)
864 int w = getSize().width;
865 int h = getSize().height;
866 if (image == null || w != image.getWidth(this)
867 || h != image.getHeight(this))
869 image = createImage(w, ap.annotationPanel.getSize().height);
872 drawComponent(image.getGraphics(), w);
873 g.drawImage(image, 0, 0, this);
876 public void drawComponent(Graphics g, int width)
878 g.setFont(av.getFont());
879 FontMetrics fm = g.getFontMetrics(av.getFont());
880 g.setColor(Color.white);
881 g.fillRect(0, 0, getSize().width, getSize().height);
883 g.translate(0, -scrollOffset);
884 g.setColor(Color.black);
886 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
887 int y = 0, fy = g.getFont().getSize();
892 hasHiddenRows = false;
893 for (int i = 0; i < aa.length; i++)
897 hasHiddenRows = true;
901 x = width - fm.stringWidth(aa[i].label) - 3;
904 offset = -(aa[i].height - fy) / 2;
906 g.drawString(aa[i].label, x, y + offset);
909 g.translate(0, +scrollOffset);
911 if (!resizePanel && !dragCancelled && dragEvent != null && aa != null)
913 g.setColor(Color.lightGray);
914 g.drawString(aa[selectedRow].label, dragEvent.getX(),
918 if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
920 g.setColor(Color.black);
921 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
922 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,