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;
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, ap.alignFrame.getTitle()
205 + " - " + aa[selectedRow].label, 500, 100);
206 cap.setText(aa[selectedRow].toString());
208 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
210 SequenceI cons = av.getConsensusSeq();
213 copy_annotseqtoclipboard(cons);
221 * Adjust size and repaint
223 protected void refresh()
225 ap.annotationPanel.adjustPanelHeight();
226 setSize(getSize().width, ap.annotationPanel.getSize().height);
228 ap.paintAlignment(true);
231 boolean editLabelDescription(AlignmentAnnotation annotation)
233 Checkbox padGaps = new Checkbox("Fill Empty Gaps With \""
234 + ap.av.getGapCharacter() + "\"", annotation.padGaps);
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() < 10 && evt.getX() < 14;
269 int row = getSelectedRow(evt.getY() + scrollOffset);
273 ParseHtmlBodyAndLinks phb = new ParseHtmlBodyAndLinks(
274 av.getAlignment().getAlignmentAnnotation()[row]
275 .getDescription(true),
279 tooltip = new Tooltip(phb.getNonHtmlContent(), this);
283 tooltip.setTip(phb.getNonHtmlContent());
286 else if (tooltip != null)
293 * curent drag position
295 MouseEvent dragEvent = null;
298 * flag to indicate drag events should be ignored
300 private boolean dragCancelled = false;
303 * clear any drag events in progress
305 public void cancelDrag()
308 dragCancelled = true;
312 public void mouseDragged(MouseEvent evt)
323 Dimension d = ap.annotationPanelHolder.getSize(), e = ap.annotationSpaceFillerHolder
324 .getSize(), f = ap.seqPanelHolder.getSize();
325 int dif = evt.getY() - oldY;
327 dif /= ap.av.getCharHeight();
328 dif *= ap.av.getCharHeight();
330 if ((d.height - dif) > 20 && (f.height + dif) > 20)
332 ap.annotationPanel.setSize(d.width, d.height - dif);
333 setSize(new Dimension(e.width, d.height - dif));
334 ap.annotationSpaceFillerHolder.setSize(new Dimension(e.width,
336 ap.annotationPanelHolder.setSize(new Dimension(d.width, d.height
338 ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height - dif, 0,
339 av.calcPanelHeight());
341 ap.seqPanelHolder.setPreferredSize(f);
342 ap.setScrollValues(av.getStartRes(), av.getStartSeq());
344 // ap.paintAlignment(true);
352 if ((diff = 6 - evt.getY()) > 0)
355 ap.apvscroll.setValue(ap.apvscroll.getValue() - diff);
356 ap.adjustmentValueChanged(null);
359 else if ((0 < (diff = 6
360 - ap.annotationSpaceFillerHolder.getSize().height
364 ap.apvscroll.setValue(ap.apvscroll.getValue() + diff);
365 ap.adjustmentValueChanged(null);
372 public void mouseClicked(MouseEvent evt)
377 public void mouseReleased(MouseEvent evt)
379 if (!resizePanel && !dragCancelled)
381 int start = selectedRow;
383 int end = getSelectedRow(evt.getY() + scrollOffset);
385 if (start > -1 && start != end)
387 // Swap these annotations
388 AlignmentAnnotation startAA = ap.av.getAlignment()
389 .getAlignmentAnnotation()[start];
392 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
394 AlignmentAnnotation endAA = ap.av.getAlignment()
395 .getAlignmentAnnotation()[end];
397 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
398 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
403 dragCancelled = false;
405 ap.annotationPanel.repaint();
409 public void mouseEntered(MouseEvent evt)
411 if (evt.getY() < 10 && evt.getX() < 14)
419 public void mouseExited(MouseEvent evt)
421 dragCancelled = false;
423 if (dragEvent == null)
438 public void mousePressed(MouseEvent evt)
445 dragCancelled = false;
446 // todo: move below to mouseClicked ?
447 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
449 AlignmentAnnotation[] aa = ap.av.getAlignment()
450 .getAlignmentAnnotation();
452 // DETECT RIGHT MOUSE BUTTON IN AWT
453 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
456 PopupMenu popup = new PopupMenu(
457 MessageManager.getString("label.annotations"));
459 MenuItem item = new MenuItem(ADDNEW);
460 item.addActionListener(this);
464 // this never happens at moment: - see comment on JAL-563
467 item = new MenuItem(SHOWALL);
468 item.addActionListener(this);
472 popup.show(this, evt.getX(), evt.getY());
475 // add the rest if there are actually rows to show
476 item = new MenuItem(EDITNAME);
477 item.addActionListener(this);
479 item = new MenuItem(HIDE);
480 item.addActionListener(this);
486 if (selectedRow < aa.length)
488 if (aa[selectedRow].sequenceRef != null)
490 final String label = aa[selectedRow].label;
491 MenuItem hideType = new MenuItem(
492 MessageManager.getString("label.hide_all") + " " + label);
493 hideType.addActionListener(new ActionListener()
496 public void actionPerformed(ActionEvent e)
498 AlignmentUtils.showOrHideSequenceAnnotations(
499 ap.av.getAlignment(), Collections.singleton(label),
510 item = new MenuItem(SHOWALL);
511 item.addActionListener(this);
515 item = new MenuItem(OUTPUT_TEXT);
516 item.addActionListener(this);
518 if (selectedRow < aa.length)
520 if (aa[selectedRow].autoCalculated)
522 if (aa[selectedRow].label.indexOf("Consensus") > -1)
524 popup.addSeparator();
525 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
526 MessageManager.getString("label.ignore_gaps_consensus"),
527 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
528 .getIgnoreGapsConsensus() : ap.av
529 .isIgnoreGapsConsensus());
530 final AlignmentAnnotation aaa = aa[selectedRow];
531 cbmi.addItemListener(new ItemListener()
534 public void itemStateChanged(ItemEvent e)
536 if (aaa.groupRef != null)
538 // TODO: pass on reference to ap so the view can be updated.
539 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
543 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
545 ap.paintAlignment(true);
549 if (aaa.groupRef != null)
551 final CheckboxMenuItem chist = new CheckboxMenuItem(
553 .getString("label.show_group_histogram"),
554 aa[selectedRow].groupRef.isShowConsensusHistogram());
555 chist.addItemListener(new ItemListener()
558 public void itemStateChanged(ItemEvent e)
560 // TODO: pass on reference
566 aaa.groupRef.setShowConsensusHistogram(chist.getState());
568 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
572 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
573 MessageManager.getString("label.show_group_logo"),
574 aa[selectedRow].groupRef.isShowSequenceLogo());
575 cprofl.addItemListener(new ItemListener()
578 public void itemStateChanged(ItemEvent e)
580 // TODO: pass on reference
586 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
588 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
593 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
595 .getString("label.normalise_group_logo"),
596 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
597 cprofn.addItemListener(new ItemListener()
600 public void itemStateChanged(ItemEvent e)
602 // TODO: pass on reference
608 aaa.groupRef.setshowSequenceLogo(true);
609 aaa.groupRef.setNormaliseSequenceLogo(cprofn.getState());
611 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
618 final CheckboxMenuItem chist = new CheckboxMenuItem(
619 MessageManager.getString("label.show_histogram"),
620 av.isShowConsensusHistogram());
621 chist.addItemListener(new ItemListener()
624 public void itemStateChanged(ItemEvent e)
626 // TODO: pass on reference
632 av.setShowConsensusHistogram(chist.getState());
633 ap.alignFrame.showConsensusHistogram.setState(chist
634 .getState()); // TODO: implement
635 // ap.updateGUI()/alignFrame.updateGUI
638 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
642 final CheckboxMenuItem cprof = new CheckboxMenuItem(
643 MessageManager.getString("label.show_logo"),
644 av.isShowSequenceLogo());
645 cprof.addItemListener(new ItemListener()
648 public void itemStateChanged(ItemEvent e)
650 // TODO: pass on reference
656 av.setShowSequenceLogo(cprof.getState());
657 ap.alignFrame.showSequenceLogo.setState(cprof.getState()); // TODO:
659 // ap.updateGUI()/alignFrame.updateGUI
663 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
667 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
668 MessageManager.getString("label.normalise_logo"),
669 av.isNormaliseSequenceLogo());
670 cprofn.addItemListener(new ItemListener()
673 public void itemStateChanged(ItemEvent e)
675 // TODO: pass on reference
681 av.setShowSequenceLogo(true);
682 ap.alignFrame.normSequenceLogo.setState(cprofn.getState()); // TODO:
684 // ap.updateGUI()/alignFrame.updateGUI
687 av.setNormaliseSequenceLogo(cprofn.getState());
689 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
695 item = new MenuItem(COPYCONS_SEQ);
696 item.addActionListener(this);
701 popup.show(this, evt.getX(), evt.getY());
706 if (selectedRow > -1 && selectedRow < aa.length)
708 if (aa[selectedRow].groupRef != null)
710 if (evt.getClickCount() >= 2)
712 // todo: make the ap scroll to the selection - not necessary, first
713 // click highlights/scrolls, second selects
714 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
716 SequenceGroup sg = ap.av.getSelectionGroup();
718 || sg == aa[selectedRow].groupRef
719 || !(jalview.util.Platform.isControlDown(evt) || evt
722 if (jalview.util.Platform.isControlDown(evt)
723 || evt.isShiftDown())
725 // clone a new selection group from the associated group
726 ap.av.setSelectionGroup(new SequenceGroup(
727 aa[selectedRow].groupRef));
731 // set selection to the associated group so it can be edited
732 ap.av.setSelectionGroup(aa[selectedRow].groupRef);
737 // modify current selection with associated group
738 int remainToAdd = aa[selectedRow].groupRef.getSize();
739 for (SequenceI sgs : aa[selectedRow].groupRef.getSequences())
741 if (jalview.util.Platform.isControlDown(evt))
743 sg.addOrRemove(sgs, --remainToAdd == 0);
747 // notionally, we should also add intermediate sequences from
748 // last added sequence ?
749 sg.addSequence(sgs, --remainToAdd == 0);
753 ap.paintAlignment(false);
754 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
755 ap.av.sendSelection();
759 ap.seqPanel.ap.idPanel
760 .highlightSearchResults(aa[selectedRow].groupRef
761 .getSequences(null));
765 else if (aa[selectedRow].sequenceRef != null)
767 if (evt.getClickCount() == 1)
769 ap.seqPanel.ap.idPanel
770 .highlightSearchResults(Arrays
771 .asList(new SequenceI[] { aa[selectedRow].sequenceRef }));
773 else if (evt.getClickCount() >= 2)
775 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
776 SequenceGroup sg = ap.av.getSelectionGroup();
779 // we make a copy rather than edit the current selection if no
781 // see Enhancement JAL-1557
782 if (!(jalview.util.Platform.isControlDown(evt) || evt
785 sg = new SequenceGroup(sg);
787 sg.addSequence(aa[selectedRow].sequenceRef, false);
791 if (jalview.util.Platform.isControlDown(evt))
793 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
797 // notionally, we should also add intermediate sequences from
798 // last added sequence ?
799 sg.addSequence(aa[selectedRow].sequenceRef, true);
805 sg = new SequenceGroup();
807 sg.setEndRes(ap.av.getAlignment().getWidth() - 1);
808 sg.addSequence(aa[selectedRow].sequenceRef, false);
810 ap.av.setSelectionGroup(sg);
811 ap.paintAlignment(false);
812 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
813 ap.av.sendSelection();
828 protected void copy_annotseqtoclipboard(SequenceI sq)
830 if (sq == null || sq.getLength() < 1)
834 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
835 jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t"
836 + sq.getStart() + "\t" + sq.getEnd() + "\t"
837 + sq.getSequenceAsString() + "\n");
838 if (av.hasHiddenColumns())
840 jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector();
841 for (int[] region : av.getColumnSelection().getHiddenColumns())
843 jalview.appletgui.AlignFrame.copiedHiddenColumns
844 .addElement(new int[] { region[0], region[1] });
850 public void update(Graphics g)
856 public void paint(Graphics g)
858 int w = getSize().width;
859 int h = getSize().height;
860 if (image == null || w != image.getWidth(this)
861 || h != image.getHeight(this))
863 image = createImage(w, ap.annotationPanel.getSize().height);
866 drawComponent(image.getGraphics(), w);
867 g.drawImage(image, 0, 0, this);
870 public void drawComponent(Graphics g, int width)
872 g.setFont(av.getFont());
873 FontMetrics fm = g.getFontMetrics(av.getFont());
874 g.setColor(Color.white);
875 g.fillRect(0, 0, getSize().width, getSize().height);
877 g.translate(0, -scrollOffset);
878 g.setColor(Color.black);
880 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
881 int y = 0, fy = g.getFont().getSize();
886 hasHiddenRows = false;
887 for (int i = 0; i < aa.length; i++)
891 hasHiddenRows = true;
895 x = width - fm.stringWidth(aa[i].label) - 3;
898 offset = -(aa[i].height - fy) / 2;
900 g.drawString(aa[i].label, x, y + offset);
903 g.translate(0, +scrollOffset);
906 g.setColor(Color.red);
908 g.drawLine(2, 8, 5, 2);
909 g.drawLine(5, 2, 8, 8);
911 else if (!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,