2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.Annotation;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.MessageManager;
28 import jalview.util.ParseHtmlBodyAndLinks;
30 import java.awt.Checkbox;
31 import java.awt.CheckboxMenuItem;
32 import java.awt.Color;
33 import java.awt.Dimension;
34 import java.awt.FlowLayout;
35 import java.awt.FontMetrics;
36 import java.awt.Frame;
37 import java.awt.Graphics;
38 import java.awt.Image;
39 import java.awt.MenuItem;
40 import java.awt.Panel;
41 import java.awt.PopupMenu;
42 import java.awt.event.ActionEvent;
43 import java.awt.event.ActionListener;
44 import java.awt.event.InputEvent;
45 import java.awt.event.ItemEvent;
46 import java.awt.event.ItemListener;
47 import java.awt.event.MouseEvent;
48 import java.awt.event.MouseListener;
49 import java.awt.event.MouseMotionListener;
50 import java.util.Arrays;
51 import java.util.Vector;
53 public class AnnotationLabels extends Panel implements ActionListener,
54 MouseListener, MouseMotionListener
58 boolean active = false;
64 boolean resizing = false;
68 static String ADDNEW = "Add New Row";
70 static String EDITNAME = "Edit Label/Description";
72 static String HIDE = "Hide This Row";
74 static String SHOWALL = "Show All Hidden Rows";
76 static String OUTPUT_TEXT = "Show Values In Textbox";
78 static String COPYCONS_SEQ = "Copy Consensus Sequence";
86 private boolean hasHiddenRows;
88 public AnnotationLabels(AlignmentPanel ap)
95 * this retrieves the adjustable height glyph from resources. we don't use
96 * it at the moment. java.net.URL url =
97 * getClass().getResource("/images/idwidth.gif"); Image temp = null;
99 * if (url != null) { temp =
100 * java.awt.Toolkit.getDefaultToolkit().createImage(url); }
102 * try { MediaTracker mt = new MediaTracker(this); mt.addImage(temp, 0);
103 * mt.waitForID(0); } catch (Exception ex) { }
105 * BufferedImage bi = new BufferedImage(temp.getHeight(this),
106 * temp.getWidth(this), BufferedImage.TYPE_INT_RGB); Graphics2D g =
107 * (Graphics2D) bi.getGraphics(); g.rotate(Math.toRadians(90));
108 * g.drawImage(temp, 0, -bi.getWidth(this), this); image = (Image) bi;
110 addMouseListener(this);
111 addMouseMotionListener(this);
114 public AnnotationLabels(AlignViewport av)
119 public void setScrollOffset(int y, boolean repaint)
131 * @return -2 if no rows are visible at all, -1 if no visible rows were
134 int getSelectedRow(int y)
137 AlignmentAnnotation[] aa = ap.av.getAlignment()
138 .getAlignmentAnnotation();
145 for (int i = 0; i < aa.length; i++)
152 height += aa[i].height;
163 public void actionPerformed(ActionEvent evt)
165 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
167 if (evt.getActionCommand().equals(ADDNEW))
169 AlignmentAnnotation newAnnotation = new AlignmentAnnotation("", null,
170 new Annotation[ap.av.getAlignment().getWidth()]);
172 if (!editLabelDescription(newAnnotation))
177 ap.av.getAlignment().addAnnotation(newAnnotation);
178 ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
180 else if (evt.getActionCommand().equals(EDITNAME))
182 editLabelDescription(aa[selectedRow]);
184 else if (evt.getActionCommand().equals(HIDE))
186 aa[selectedRow].visible = false;
188 else if (evt.getActionCommand().equals(SHOWALL))
190 for (int i = 0; i < aa.length; i++)
192 aa[i].visible = (aa[i].annotations == null) ? false : true;
195 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
197 CutAndPasteTransfer cap = new CutAndPasteTransfer(false,
199 Frame frame = new Frame();
201 jalview.bin.JalviewLite.addFrame(frame, ap.alignFrame.getTitle()
202 + " - " + aa[selectedRow].label, 500, 100);
203 cap.setText(aa[selectedRow].toString());
205 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
207 SequenceI cons = av.getConsensusSeq();
210 copy_annotseqtoclipboard(cons);
214 ap.annotationPanel.adjustPanelHeight();
215 setSize(getSize().width, ap.annotationPanel.getSize().height);
217 ap.paintAlignment(true);
220 boolean editLabelDescription(AlignmentAnnotation annotation)
222 Checkbox padGaps = new Checkbox("Fill Empty Gaps With \""
223 + ap.av.getGapCharacter() + "\"", annotation.padGaps);
225 EditNameDialog dialog = new EditNameDialog(annotation.label,
226 annotation.description, " Annotation Label",
227 "Annotation Description", ap.alignFrame,
228 "Edit Annotation Name / Description", 500, 180, false);
230 Panel empty = new Panel(new FlowLayout());
235 dialog.setVisible(true);
239 annotation.label = dialog.getName();
240 annotation.description = dialog.getDescription();
241 annotation.setPadGaps(padGaps.getState(), av.getGapCharacter());
252 boolean resizePanel = false;
254 public void mouseMoved(MouseEvent evt)
256 resizePanel = evt.getY() < 10 && evt.getX() < 14;
257 int row = getSelectedRow(evt.getY() + scrollOffset);
261 ParseHtmlBodyAndLinks phb = new ParseHtmlBodyAndLinks(
262 av.getAlignment().getAlignmentAnnotation()[row]
263 .getDescription(true),
267 tooltip = new Tooltip(phb.getNonHtmlContent(), this);
271 tooltip.setTip(phb.getNonHtmlContent());
274 else if (tooltip != null)
281 * curent drag position
283 MouseEvent dragEvent = null;
286 * flag to indicate drag events should be ignored
288 private boolean dragCancelled = false;
291 * clear any drag events in progress
293 public void cancelDrag()
296 dragCancelled = true;
299 public void mouseDragged(MouseEvent evt)
310 Dimension d = ap.annotationPanelHolder.getSize(), e = ap.annotationSpaceFillerHolder
311 .getSize(), f = ap.seqPanelHolder.getSize();
312 int dif = evt.getY() - oldY;
314 dif /= ap.av.getCharHeight();
315 dif *= ap.av.getCharHeight();
317 if ((d.height - dif) > 20 && (f.height + dif) > 20)
319 ap.annotationPanel.setSize(d.width, d.height - dif);
320 setSize(new Dimension(e.width, d.height - dif));
321 ap.annotationSpaceFillerHolder.setSize(new Dimension(e.width,
323 ap.annotationPanelHolder.setSize(new Dimension(d.width, d.height
325 ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height - dif, 0,
326 av.calcPanelHeight());
328 ap.seqPanelHolder.setPreferredSize(f);
329 ap.setScrollValues(av.getStartRes(), av.getStartSeq());
331 // ap.paintAlignment(true);
339 if ((diff = 6 - evt.getY()) > 0)
342 ap.apvscroll.setValue(ap.apvscroll.getValue() - diff);
343 ap.adjustmentValueChanged(null);
346 else if ((0 < (diff = 6
347 - ap.annotationSpaceFillerHolder.getSize().height
351 ap.apvscroll.setValue(ap.apvscroll.getValue() + diff);
352 ap.adjustmentValueChanged(null);
358 public void mouseClicked(MouseEvent evt)
362 public void mouseReleased(MouseEvent evt)
364 if (!resizePanel && !dragCancelled)
366 int start = selectedRow;
368 int end = getSelectedRow(evt.getY() + scrollOffset);
370 if (start > -1 && start != end)
372 // Swap these annotations
373 AlignmentAnnotation startAA = ap.av.getAlignment()
374 .getAlignmentAnnotation()[start];
377 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
379 AlignmentAnnotation endAA = ap.av.getAlignment()
380 .getAlignmentAnnotation()[end];
382 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
383 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
388 dragCancelled = false;
390 ap.annotationPanel.repaint();
393 public void mouseEntered(MouseEvent evt)
395 if (evt.getY() < 10 && evt.getX() < 14)
402 public void mouseExited(MouseEvent evt)
404 dragCancelled = false;
406 if (dragEvent == null)
420 public void mousePressed(MouseEvent evt)
427 dragCancelled = false;
428 // todo: move below to mouseClicked ?
429 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
431 AlignmentAnnotation[] aa = ap.av.getAlignment()
432 .getAlignmentAnnotation();
434 // DETECT RIGHT MOUSE BUTTON IN AWT
435 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
438 PopupMenu popup = new PopupMenu(
439 MessageManager.getString("label.annotations"));
441 MenuItem item = new MenuItem(ADDNEW);
442 item.addActionListener(this);
446 // this never happens at moment: - see comment on JAL-563
449 item = new MenuItem(SHOWALL);
450 item.addActionListener(this);
454 popup.show(this, evt.getX(), evt.getY());
457 // add the rest if there are actually rows to show
458 item = new MenuItem(EDITNAME);
459 item.addActionListener(this);
461 item = new MenuItem(HIDE);
462 item.addActionListener(this);
466 item = new MenuItem(SHOWALL);
467 item.addActionListener(this);
471 item = new MenuItem(OUTPUT_TEXT);
472 item.addActionListener(this);
474 if (selectedRow < aa.length)
476 if (aa[selectedRow].autoCalculated)
478 if (aa[selectedRow].label.indexOf("Consensus") > -1)
480 popup.addSeparator();
481 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
482 MessageManager.getString("label.ignore_gaps_consensus"),
483 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
484 .getIgnoreGapsConsensus() : ap.av
485 .getIgnoreGapsConsensus());
486 final AlignmentAnnotation aaa = aa[selectedRow];
487 cbmi.addItemListener(new ItemListener()
489 public void itemStateChanged(ItemEvent e)
491 if (aaa.groupRef != null)
493 // TODO: pass on reference to ap so the view can be updated.
494 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
498 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
500 ap.paintAlignment(true);
504 if (aaa.groupRef != null)
506 final CheckboxMenuItem chist = new CheckboxMenuItem(
507 MessageManager.getString("label.show_group_histogram"),
508 aa[selectedRow].groupRef.isShowConsensusHistogram());
509 chist.addItemListener(new ItemListener()
511 public void itemStateChanged(ItemEvent e)
513 // TODO: pass on reference
519 aaa.groupRef.setShowConsensusHistogram(chist.getState());
521 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
525 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
526 MessageManager.getString("label.show_group_logo"),
527 aa[selectedRow].groupRef.isShowSequenceLogo());
528 cprofl.addItemListener(new ItemListener()
530 public void itemStateChanged(ItemEvent e)
532 // TODO: pass on reference
538 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
540 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
545 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
546 MessageManager.getString("label.normalise_group_logo"),
547 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
548 cprofn.addItemListener(new ItemListener()
550 public void itemStateChanged(ItemEvent e)
552 // TODO: pass on reference
558 aaa.groupRef.setshowSequenceLogo(true);
559 aaa.groupRef.setNormaliseSequenceLogo(cprofn.getState());
561 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
568 final CheckboxMenuItem chist = new CheckboxMenuItem(
569 MessageManager.getString("label.show_histogram"), av.isShowConsensusHistogram());
570 chist.addItemListener(new ItemListener()
572 public void itemStateChanged(ItemEvent e)
574 // TODO: pass on reference
580 av.setShowConsensusHistogram(chist.getState());
581 ap.alignFrame.showConsensusHistogram.setState(chist
582 .getState()); // TODO: implement
583 // ap.updateGUI()/alignFrame.updateGUI
586 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
590 final CheckboxMenuItem cprof = new CheckboxMenuItem(
591 MessageManager.getString("label.show_logo"), av.isShowSequenceLogo());
592 cprof.addItemListener(new ItemListener()
594 public void itemStateChanged(ItemEvent e)
596 // TODO: pass on reference
602 av.setShowSequenceLogo(cprof.getState());
603 ap.alignFrame.showSequenceLogo.setState(cprof.getState()); // TODO:
605 // ap.updateGUI()/alignFrame.updateGUI
609 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
613 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
614 MessageManager.getString("label.normalise_logo"), av.isNormaliseSequenceLogo());
615 cprofn.addItemListener(new ItemListener()
617 public void itemStateChanged(ItemEvent e)
619 // TODO: pass on reference
625 av.setShowSequenceLogo(true);
626 ap.alignFrame.normSequenceLogo.setState(cprofn.getState()); // TODO:
628 // ap.updateGUI()/alignFrame.updateGUI
631 av.setNormaliseSequenceLogo(cprofn.getState());
633 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
639 item = new MenuItem(COPYCONS_SEQ);
640 item.addActionListener(this);
645 popup.show(this, evt.getX(), evt.getY());
650 if (selectedRow > -1 && selectedRow < aa.length)
652 if (aa[selectedRow].groupRef != null)
654 if (evt.getClickCount() >= 2)
656 // todo: make the ap scroll to the selection - not necessary, first
657 // click highlights/scrolls, second selects
658 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
659 ap.av.setSelectionGroup(// new SequenceGroup(
660 aa[selectedRow].groupRef); // );
661 ap.av.sendSelection();
662 ap.paintAlignment(false);
663 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
667 ap.seqPanel.ap.idPanel
668 .highlightSearchResults(aa[selectedRow].groupRef
669 .getSequences(null));
673 else if (aa[selectedRow].sequenceRef != null)
675 if (evt.getClickCount() == 1)
677 ap.seqPanel.ap.idPanel.highlightSearchResults(Arrays
678 .asList(new SequenceI[]
679 { aa[selectedRow].sequenceRef }));
681 else if (evt.getClickCount() >= 2)
683 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
684 SequenceGroup sg = ap.av.getSelectionGroup();
687 // we make a copy rather than edit the current selection if no modifiers pressed
688 // see Enhancement JAL-1557
689 if (!(evt.isControlDown() || evt.isShiftDown()))
691 sg = new SequenceGroup(sg);
693 sg.addSequence(aa[selectedRow].sequenceRef, false);
695 if (evt.isControlDown())
697 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
699 // notionally, we should also add intermediate sequences from last added sequence ?
700 sg.addSequence(aa[selectedRow].sequenceRef, true);
704 sg = new SequenceGroup();
706 sg.setEndRes(ap.av.getAlignment().getWidth()-1);
707 sg.addSequence(aa[selectedRow].sequenceRef, false);
709 ap.av.setSelectionGroup(sg);
710 ap.paintAlignment(false);
711 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
712 ap.av.sendSelection();
727 protected void copy_annotseqtoclipboard(SequenceI sq)
729 if (sq == null || sq.getLength() < 1)
733 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
734 jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t"
735 + sq.getStart() + "\t" + sq.getEnd() + "\t"
736 + sq.getSequenceAsString() + "\n");
737 if (av.hasHiddenColumns())
739 jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector();
740 for (int[] region : av.getColumnSelection().getHiddenColumns())
742 jalview.appletgui.AlignFrame.copiedHiddenColumns
743 .addElement(new int[]
744 { region[0], region[1] });
749 public void update(Graphics g)
754 public void paint(Graphics g)
756 int w = getSize().width;
757 int h = getSize().height;
758 if (image == null || w != image.getWidth(this)
759 || h != image.getHeight(this))
761 image = createImage(w, ap.annotationPanel.getSize().height);
764 drawComponent(image.getGraphics(), w);
765 g.drawImage(image, 0, 0, this);
768 public void drawComponent(Graphics g, int width)
770 g.setFont(av.getFont());
771 FontMetrics fm = g.getFontMetrics(av.getFont());
772 g.setColor(Color.white);
773 g.fillRect(0, 0, getSize().width, getSize().height);
775 g.translate(0, -scrollOffset);
776 g.setColor(Color.black);
778 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
779 int y = 0, fy = g.getFont().getSize();
784 hasHiddenRows = false;
785 for (int i = 0; i < aa.length; i++)
789 hasHiddenRows = true;
793 x = width - fm.stringWidth(aa[i].label) - 3;
796 offset = -(aa[i].height - fy) / 2;
798 g.drawString(aa[i].label, x, y + offset);
801 g.translate(0, +scrollOffset);
804 g.setColor(Color.red);
806 g.drawLine(2, 8, 5, 2);
807 g.drawLine(5, 2, 8, 8);
809 else if (!dragCancelled && dragEvent != null && aa != null)
811 g.setColor(Color.lightGray);
812 g.drawString(aa[selectedRow].label, dragEvent.getX(),
816 if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
818 g.setColor(Color.black);
819 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
820 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,