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 implements ActionListener,
56 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;
165 public void actionPerformed(ActionEvent evt)
167 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
169 if (evt.getActionCommand().equals(ADDNEW))
171 AlignmentAnnotation newAnnotation = new AlignmentAnnotation("", null,
172 new Annotation[ap.av.getAlignment().getWidth()]);
174 if (!editLabelDescription(newAnnotation))
179 ap.av.getAlignment().addAnnotation(newAnnotation);
180 ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
182 else if (evt.getActionCommand().equals(EDITNAME))
184 editLabelDescription(aa[selectedRow]);
186 else if (evt.getActionCommand().equals(HIDE))
188 aa[selectedRow].visible = false;
190 else if (evt.getActionCommand().equals(SHOWALL))
192 for (int i = 0; i < aa.length; i++)
194 aa[i].visible = (aa[i].annotations == null) ? false : true;
197 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
199 CutAndPasteTransfer cap = new CutAndPasteTransfer(false,
201 Frame frame = new Frame();
203 jalview.bin.JalviewLite.addFrame(frame, ap.alignFrame.getTitle()
204 + " - " + aa[selectedRow].label, 500, 100);
205 cap.setText(aa[selectedRow].toString());
207 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
209 SequenceI cons = av.getConsensusSeq();
212 copy_annotseqtoclipboard(cons);
220 * Adjust size and repaint
222 protected void refresh()
224 ap.annotationPanel.adjustPanelHeight();
225 setSize(getSize().width, ap.annotationPanel.getSize().height);
227 ap.paintAlignment(true);
230 boolean editLabelDescription(AlignmentAnnotation annotation)
232 Checkbox padGaps = new Checkbox("Fill Empty Gaps With \""
233 + ap.av.getGapCharacter() + "\"", annotation.padGaps);
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;
264 public void mouseMoved(MouseEvent evt)
266 resizePanel = evt.getY() < 10 && evt.getX() < 14;
267 int row = getSelectedRow(evt.getY() + scrollOffset);
271 ParseHtmlBodyAndLinks phb = new ParseHtmlBodyAndLinks(
272 av.getAlignment().getAlignmentAnnotation()[row]
273 .getDescription(true),
277 tooltip = new Tooltip(phb.getNonHtmlContent(), this);
281 tooltip.setTip(phb.getNonHtmlContent());
284 else if (tooltip != null)
291 * curent drag position
293 MouseEvent dragEvent = null;
296 * flag to indicate drag events should be ignored
298 private boolean dragCancelled = false;
301 * clear any drag events in progress
303 public void cancelDrag()
306 dragCancelled = true;
309 public void mouseDragged(MouseEvent evt)
320 Dimension d = ap.annotationPanelHolder.getSize(), e = ap.annotationSpaceFillerHolder
321 .getSize(), f = ap.seqPanelHolder.getSize();
322 int dif = evt.getY() - oldY;
324 dif /= ap.av.getCharHeight();
325 dif *= ap.av.getCharHeight();
327 if ((d.height - dif) > 20 && (f.height + dif) > 20)
329 ap.annotationPanel.setSize(d.width, d.height - dif);
330 setSize(new Dimension(e.width, d.height - dif));
331 ap.annotationSpaceFillerHolder.setSize(new Dimension(e.width,
333 ap.annotationPanelHolder.setSize(new Dimension(d.width, d.height
335 ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height - dif, 0,
336 av.calcPanelHeight());
338 ap.seqPanelHolder.setPreferredSize(f);
339 ap.setScrollValues(av.getStartRes(), av.getStartSeq());
341 // ap.paintAlignment(true);
349 if ((diff = 6 - evt.getY()) > 0)
352 ap.apvscroll.setValue(ap.apvscroll.getValue() - diff);
353 ap.adjustmentValueChanged(null);
356 else if ((0 < (diff = 6
357 - ap.annotationSpaceFillerHolder.getSize().height
361 ap.apvscroll.setValue(ap.apvscroll.getValue() + diff);
362 ap.adjustmentValueChanged(null);
368 public void mouseClicked(MouseEvent evt)
372 public void mouseReleased(MouseEvent evt)
374 if (!resizePanel && !dragCancelled)
376 int start = selectedRow;
378 int end = getSelectedRow(evt.getY() + scrollOffset);
380 if (start > -1 && start != end)
382 // Swap these annotations
383 AlignmentAnnotation startAA = ap.av.getAlignment()
384 .getAlignmentAnnotation()[start];
387 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
389 AlignmentAnnotation endAA = ap.av.getAlignment()
390 .getAlignmentAnnotation()[end];
392 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
393 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
398 dragCancelled = false;
400 ap.annotationPanel.repaint();
403 public void mouseEntered(MouseEvent evt)
405 if (evt.getY() < 10 && evt.getX() < 14)
412 public void mouseExited(MouseEvent evt)
414 dragCancelled = false;
416 if (dragEvent == null)
430 public void mousePressed(MouseEvent evt)
437 dragCancelled = false;
438 // todo: move below to mouseClicked ?
439 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
441 AlignmentAnnotation[] aa = ap.av.getAlignment()
442 .getAlignmentAnnotation();
444 // DETECT RIGHT MOUSE BUTTON IN AWT
445 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
448 PopupMenu popup = new PopupMenu(
449 MessageManager.getString("label.annotations"));
451 MenuItem item = new MenuItem(ADDNEW);
452 item.addActionListener(this);
456 // this never happens at moment: - see comment on JAL-563
459 item = new MenuItem(SHOWALL);
460 item.addActionListener(this);
464 popup.show(this, evt.getX(), evt.getY());
467 // add the rest if there are actually rows to show
468 item = new MenuItem(EDITNAME);
469 item.addActionListener(this);
471 item = new MenuItem(HIDE);
472 item.addActionListener(this);
478 if (selectedRow < aa.length)
480 if (aa[selectedRow].sequenceRef != null)
482 final String label = aa[selectedRow].label;
483 MenuItem hideType = new MenuItem(
484 MessageManager.getString("label.hide_all") + " " + label);
485 hideType.addActionListener(new ActionListener()
488 public void actionPerformed(ActionEvent e)
490 AlignmentUtils.showOrHideSequenceAnnotations(
491 ap.av.getAlignment(), Collections.singleton(label),
502 item = new MenuItem(SHOWALL);
503 item.addActionListener(this);
507 item = new MenuItem(OUTPUT_TEXT);
508 item.addActionListener(this);
510 if (selectedRow < aa.length)
512 if (aa[selectedRow].autoCalculated)
514 if (aa[selectedRow].label.indexOf("Consensus") > -1)
516 popup.addSeparator();
517 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
518 MessageManager.getString("label.ignore_gaps_consensus"),
519 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
520 .getIgnoreGapsConsensus() : ap.av
521 .isIgnoreGapsConsensus());
522 final AlignmentAnnotation aaa = aa[selectedRow];
523 cbmi.addItemListener(new ItemListener()
525 public void itemStateChanged(ItemEvent e)
527 if (aaa.groupRef != null)
529 // TODO: pass on reference to ap so the view can be updated.
530 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
534 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
536 ap.paintAlignment(true);
540 if (aaa.groupRef != null)
542 final CheckboxMenuItem chist = new CheckboxMenuItem(
544 .getString("label.show_group_histogram"),
545 aa[selectedRow].groupRef.isShowConsensusHistogram());
546 chist.addItemListener(new ItemListener()
548 public void itemStateChanged(ItemEvent e)
550 // TODO: pass on reference
556 aaa.groupRef.setShowConsensusHistogram(chist.getState());
558 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
562 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
563 MessageManager.getString("label.show_group_logo"),
564 aa[selectedRow].groupRef.isShowSequenceLogo());
565 cprofl.addItemListener(new ItemListener()
567 public void itemStateChanged(ItemEvent e)
569 // TODO: pass on reference
575 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
577 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
582 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
584 .getString("label.normalise_group_logo"),
585 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
586 cprofn.addItemListener(new ItemListener()
588 public void itemStateChanged(ItemEvent e)
590 // TODO: pass on reference
596 aaa.groupRef.setshowSequenceLogo(true);
597 aaa.groupRef.setNormaliseSequenceLogo(cprofn.getState());
599 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
606 final CheckboxMenuItem chist = new CheckboxMenuItem(
607 MessageManager.getString("label.show_histogram"),
608 av.isShowConsensusHistogram());
609 chist.addItemListener(new ItemListener()
611 public void itemStateChanged(ItemEvent e)
613 // TODO: pass on reference
619 av.setShowConsensusHistogram(chist.getState());
620 ap.alignFrame.showConsensusHistogram.setState(chist
621 .getState()); // TODO: implement
622 // ap.updateGUI()/alignFrame.updateGUI
625 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
629 final CheckboxMenuItem cprof = new CheckboxMenuItem(
630 MessageManager.getString("label.show_logo"),
631 av.isShowSequenceLogo());
632 cprof.addItemListener(new ItemListener()
634 public void itemStateChanged(ItemEvent e)
636 // TODO: pass on reference
642 av.setShowSequenceLogo(cprof.getState());
643 ap.alignFrame.showSequenceLogo.setState(cprof.getState()); // TODO:
645 // ap.updateGUI()/alignFrame.updateGUI
649 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
653 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
654 MessageManager.getString("label.normalise_logo"),
655 av.isNormaliseSequenceLogo());
656 cprofn.addItemListener(new ItemListener()
658 public void itemStateChanged(ItemEvent e)
660 // TODO: pass on reference
666 av.setShowSequenceLogo(true);
667 ap.alignFrame.normSequenceLogo.setState(cprofn.getState()); // TODO:
669 // ap.updateGUI()/alignFrame.updateGUI
672 av.setNormaliseSequenceLogo(cprofn.getState());
674 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
680 item = new MenuItem(COPYCONS_SEQ);
681 item.addActionListener(this);
686 popup.show(this, evt.getX(), evt.getY());
691 if (selectedRow > -1 && selectedRow < aa.length)
693 if (aa[selectedRow].groupRef != null)
695 if (evt.getClickCount() >= 2)
697 // todo: make the ap scroll to the selection - not necessary, first
698 // click highlights/scrolls, second selects
699 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
700 ap.av.setSelectionGroup(// new SequenceGroup(
701 aa[selectedRow].groupRef); // );
702 ap.av.sendSelection();
703 ap.paintAlignment(false);
704 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
708 ap.seqPanel.ap.idPanel
709 .highlightSearchResults(aa[selectedRow].groupRef
710 .getSequences(null));
714 else if (aa[selectedRow].sequenceRef != null)
716 if (evt.getClickCount() == 1)
718 ap.seqPanel.ap.idPanel
719 .highlightSearchResults(Arrays
720 .asList(new SequenceI[] { aa[selectedRow].sequenceRef }));
722 else if (evt.getClickCount() >= 2)
724 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
725 SequenceGroup sg = ap.av.getSelectionGroup();
728 // we make a copy rather than edit the current selection if no
730 // see Enhancement JAL-1557
731 if (!(evt.isControlDown() || evt.isShiftDown()))
733 sg = new SequenceGroup(sg);
735 sg.addSequence(aa[selectedRow].sequenceRef, false);
739 if (evt.isControlDown())
741 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
745 // notionally, we should also add intermediate sequences from
746 // last added sequence ?
747 sg.addSequence(aa[selectedRow].sequenceRef, true);
753 sg = new SequenceGroup();
755 sg.setEndRes(ap.av.getAlignment().getWidth() - 1);
756 sg.addSequence(aa[selectedRow].sequenceRef, false);
758 ap.av.setSelectionGroup(sg);
759 ap.paintAlignment(false);
760 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
761 ap.av.sendSelection();
776 protected void copy_annotseqtoclipboard(SequenceI sq)
778 if (sq == null || sq.getLength() < 1)
782 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
783 jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t"
784 + sq.getStart() + "\t" + sq.getEnd() + "\t"
785 + sq.getSequenceAsString() + "\n");
786 if (av.hasHiddenColumns())
788 jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector();
789 for (int[] region : av.getColumnSelection().getHiddenColumns())
791 jalview.appletgui.AlignFrame.copiedHiddenColumns
792 .addElement(new int[] { region[0], region[1] });
797 public void update(Graphics g)
802 public void paint(Graphics g)
804 int w = getSize().width;
805 int h = getSize().height;
806 if (image == null || w != image.getWidth(this)
807 || h != image.getHeight(this))
809 image = createImage(w, ap.annotationPanel.getSize().height);
812 drawComponent(image.getGraphics(), w);
813 g.drawImage(image, 0, 0, this);
816 public void drawComponent(Graphics g, int width)
818 g.setFont(av.getFont());
819 FontMetrics fm = g.getFontMetrics(av.getFont());
820 g.setColor(Color.white);
821 g.fillRect(0, 0, getSize().width, getSize().height);
823 g.translate(0, -scrollOffset);
824 g.setColor(Color.black);
826 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
827 int y = 0, fy = g.getFont().getSize();
832 hasHiddenRows = false;
833 for (int i = 0; i < aa.length; i++)
837 hasHiddenRows = true;
841 x = width - fm.stringWidth(aa[i].label) - 3;
844 offset = -(aa[i].height - fy) / 2;
846 g.drawString(aa[i].label, x, y + offset);
849 g.translate(0, +scrollOffset);
852 g.setColor(Color.red);
854 g.drawLine(2, 8, 5, 2);
855 g.drawLine(5, 2, 8, 8);
857 else if (!dragCancelled && dragEvent != null && aa != null)
859 g.setColor(Color.lightGray);
860 g.drawString(aa[selectedRow].label, dragEvent.getX(),
864 if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
866 g.setColor(Color.black);
867 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
868 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,