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 java.awt.Checkbox;
24 import java.awt.CheckboxMenuItem;
25 import java.awt.Color;
26 import java.awt.Dimension;
27 import java.awt.FlowLayout;
28 import java.awt.FontMetrics;
29 import java.awt.Frame;
30 import java.awt.Graphics;
31 import java.awt.Image;
32 import java.awt.MenuItem;
33 import java.awt.Panel;
34 import java.awt.PopupMenu;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.awt.event.InputEvent;
38 import java.awt.event.ItemEvent;
39 import java.awt.event.ItemListener;
40 import java.awt.event.MouseEvent;
41 import java.awt.event.MouseListener;
42 import java.awt.event.MouseMotionListener;
43 import java.util.Arrays;
44 import java.util.Collections;
45 import java.util.Vector;
47 import jalview.analysis.AlignmentUtils;
48 import jalview.datamodel.AlignmentAnnotation;
49 import jalview.datamodel.Annotation;
50 import jalview.datamodel.SequenceGroup;
51 import jalview.datamodel.SequenceI;
52 import jalview.util.MessageManager;
53 import jalview.util.ParseHtmlBodyAndLinks;
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(
543 MessageManager.getString("label.show_group_histogram"),
544 aa[selectedRow].groupRef.isShowConsensusHistogram());
545 chist.addItemListener(new ItemListener()
547 public void itemStateChanged(ItemEvent e)
549 // TODO: pass on reference
555 aaa.groupRef.setShowConsensusHistogram(chist.getState());
557 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
561 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
562 MessageManager.getString("label.show_group_logo"),
563 aa[selectedRow].groupRef.isShowSequenceLogo());
564 cprofl.addItemListener(new ItemListener()
566 public void itemStateChanged(ItemEvent e)
568 // TODO: pass on reference
574 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
576 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
581 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
582 MessageManager.getString("label.normalise_group_logo"),
583 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
584 cprofn.addItemListener(new ItemListener()
586 public void itemStateChanged(ItemEvent e)
588 // TODO: pass on reference
594 aaa.groupRef.setshowSequenceLogo(true);
595 aaa.groupRef.setNormaliseSequenceLogo(cprofn.getState());
597 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
604 final CheckboxMenuItem chist = new CheckboxMenuItem(
605 MessageManager.getString("label.show_histogram"), av.isShowConsensusHistogram());
606 chist.addItemListener(new ItemListener()
608 public void itemStateChanged(ItemEvent e)
610 // TODO: pass on reference
616 av.setShowConsensusHistogram(chist.getState());
617 ap.alignFrame.showConsensusHistogram.setState(chist
618 .getState()); // TODO: implement
619 // ap.updateGUI()/alignFrame.updateGUI
622 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
626 final CheckboxMenuItem cprof = new CheckboxMenuItem(
627 MessageManager.getString("label.show_logo"), av.isShowSequenceLogo());
628 cprof.addItemListener(new ItemListener()
630 public void itemStateChanged(ItemEvent e)
632 // TODO: pass on reference
638 av.setShowSequenceLogo(cprof.getState());
639 ap.alignFrame.showSequenceLogo.setState(cprof.getState()); // TODO:
641 // ap.updateGUI()/alignFrame.updateGUI
645 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
649 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
650 MessageManager.getString("label.normalise_logo"), av.isNormaliseSequenceLogo());
651 cprofn.addItemListener(new ItemListener()
653 public void itemStateChanged(ItemEvent e)
655 // TODO: pass on reference
661 av.setShowSequenceLogo(true);
662 ap.alignFrame.normSequenceLogo.setState(cprofn.getState()); // TODO:
664 // ap.updateGUI()/alignFrame.updateGUI
667 av.setNormaliseSequenceLogo(cprofn.getState());
669 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
675 item = new MenuItem(COPYCONS_SEQ);
676 item.addActionListener(this);
681 popup.show(this, evt.getX(), evt.getY());
686 if (selectedRow > -1 && selectedRow < aa.length)
688 if (aa[selectedRow].groupRef != null)
690 if (evt.getClickCount() >= 2)
692 // todo: make the ap scroll to the selection - not necessary, first
693 // click highlights/scrolls, second selects
694 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
695 ap.av.setSelectionGroup(// new SequenceGroup(
696 aa[selectedRow].groupRef); // );
697 ap.av.sendSelection();
698 ap.paintAlignment(false);
699 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
703 ap.seqPanel.ap.idPanel
704 .highlightSearchResults(aa[selectedRow].groupRef
705 .getSequences(null));
709 else if (aa[selectedRow].sequenceRef != null)
711 if (evt.getClickCount() == 1)
713 ap.seqPanel.ap.idPanel.highlightSearchResults(Arrays
714 .asList(new SequenceI[]
715 { aa[selectedRow].sequenceRef }));
717 else if (evt.getClickCount() >= 2)
719 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
720 SequenceGroup sg = ap.av.getSelectionGroup();
723 // we make a copy rather than edit the current selection if no modifiers pressed
724 // see Enhancement JAL-1557
725 if (!(evt.isControlDown() || evt.isShiftDown()))
727 sg = new SequenceGroup(sg);
729 sg.addSequence(aa[selectedRow].sequenceRef, false);
731 if (evt.isControlDown())
733 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
735 // notionally, we should also add intermediate sequences from last added sequence ?
736 sg.addSequence(aa[selectedRow].sequenceRef, true);
740 sg = new SequenceGroup();
742 sg.setEndRes(ap.av.getAlignment().getWidth()-1);
743 sg.addSequence(aa[selectedRow].sequenceRef, false);
745 ap.av.setSelectionGroup(sg);
746 ap.paintAlignment(false);
747 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
748 ap.av.sendSelection();
763 protected void copy_annotseqtoclipboard(SequenceI sq)
765 if (sq == null || sq.getLength() < 1)
769 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
770 jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t"
771 + sq.getStart() + "\t" + sq.getEnd() + "\t"
772 + sq.getSequenceAsString() + "\n");
773 if (av.hasHiddenColumns())
775 jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector();
776 for (int[] region : av.getColumnSelection().getHiddenColumns())
778 jalview.appletgui.AlignFrame.copiedHiddenColumns
779 .addElement(new int[]
780 { region[0], region[1] });
785 public void update(Graphics g)
790 public void paint(Graphics g)
792 int w = getSize().width;
793 int h = getSize().height;
794 if (image == null || w != image.getWidth(this)
795 || h != image.getHeight(this))
797 image = createImage(w, ap.annotationPanel.getSize().height);
800 drawComponent(image.getGraphics(), w);
801 g.drawImage(image, 0, 0, this);
804 public void drawComponent(Graphics g, int width)
806 g.setFont(av.getFont());
807 FontMetrics fm = g.getFontMetrics(av.getFont());
808 g.setColor(Color.white);
809 g.fillRect(0, 0, getSize().width, getSize().height);
811 g.translate(0, -scrollOffset);
812 g.setColor(Color.black);
814 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
815 int y = 0, fy = g.getFont().getSize();
820 hasHiddenRows = false;
821 for (int i = 0; i < aa.length; i++)
825 hasHiddenRows = true;
829 x = width - fm.stringWidth(aa[i].label) - 3;
832 offset = -(aa[i].height - fy) / 2;
834 g.drawString(aa[i].label, x, y + offset);
837 g.translate(0, +scrollOffset);
840 g.setColor(Color.red);
842 g.drawLine(2, 8, 5, 2);
843 g.drawLine(5, 2, 8, 8);
845 else if (!dragCancelled && dragEvent != null && aa != null)
847 g.setColor(Color.lightGray);
848 g.drawString(aa[selectedRow].label, dragEvent.getX(),
852 if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
854 g.setColor(Color.black);
855 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
856 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,