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;
25 import java.awt.event.*;
27 import jalview.datamodel.*;
28 import jalview.util.MessageManager;
29 import jalview.util.ParseHtmlBodyAndLinks;
31 public class AnnotationLabels extends Panel implements ActionListener,
32 MouseListener, MouseMotionListener
36 boolean active = false;
42 boolean resizing = false;
46 static String ADDNEW = "Add New Row";
48 static String EDITNAME = "Edit Label/Description";
50 static String HIDE = "Hide This Row";
52 static String SHOWALL = "Show All Hidden Rows";
54 static String OUTPUT_TEXT = "Show Values In Textbox";
56 static String COPYCONS_SEQ = "Copy Consensus Sequence";
64 private boolean hasHiddenRows;
66 public AnnotationLabels(AlignmentPanel ap)
73 * this retrieves the adjustable height glyph from resources. we don't use
74 * it at the moment. java.net.URL url =
75 * getClass().getResource("/images/idwidth.gif"); Image temp = null;
77 * if (url != null) { temp =
78 * java.awt.Toolkit.getDefaultToolkit().createImage(url); }
80 * try { MediaTracker mt = new MediaTracker(this); mt.addImage(temp, 0);
81 * mt.waitForID(0); } catch (Exception ex) { }
83 * BufferedImage bi = new BufferedImage(temp.getHeight(this),
84 * temp.getWidth(this), BufferedImage.TYPE_INT_RGB); Graphics2D g =
85 * (Graphics2D) bi.getGraphics(); g.rotate(Math.toRadians(90));
86 * g.drawImage(temp, 0, -bi.getWidth(this), this); image = (Image) bi;
88 addMouseListener(this);
89 addMouseMotionListener(this);
92 public AnnotationLabels(AlignViewport av)
97 public void setScrollOffset(int y, boolean repaint)
109 * @return -2 if no rows are visible at all, -1 if no visible rows were
112 int getSelectedRow(int y)
115 AlignmentAnnotation[] aa = ap.av.getAlignment()
116 .getAlignmentAnnotation();
123 for (int i = 0; i < aa.length; i++)
130 height += aa[i].height;
141 public void actionPerformed(ActionEvent evt)
143 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
145 if (evt.getActionCommand().equals(ADDNEW))
147 AlignmentAnnotation newAnnotation = new AlignmentAnnotation("", null,
148 new Annotation[ap.av.getAlignment().getWidth()]);
150 if (!editLabelDescription(newAnnotation))
155 ap.av.getAlignment().addAnnotation(newAnnotation);
156 ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
158 else if (evt.getActionCommand().equals(EDITNAME))
160 editLabelDescription(aa[selectedRow]);
162 else if (evt.getActionCommand().equals(HIDE))
164 aa[selectedRow].visible = false;
166 else if (evt.getActionCommand().equals(SHOWALL))
168 for (int i = 0; i < aa.length; i++)
170 aa[i].visible = (aa[i].annotations == null) ? false : true;
173 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
175 CutAndPasteTransfer cap = new CutAndPasteTransfer(false,
177 Frame frame = new Frame();
179 jalview.bin.JalviewLite.addFrame(frame, ap.alignFrame.getTitle()
180 + " - " + aa[selectedRow].label, 500, 100);
181 cap.setText(aa[selectedRow].toString());
183 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
185 SequenceI cons = av.getConsensusSeq();
188 copy_annotseqtoclipboard(cons);
192 ap.annotationPanel.adjustPanelHeight();
193 setSize(getSize().width, ap.annotationPanel.getSize().height);
195 ap.paintAlignment(true);
198 boolean editLabelDescription(AlignmentAnnotation annotation)
200 Checkbox padGaps = new Checkbox("Fill Empty Gaps With \""
201 + ap.av.getGapCharacter() + "\"", annotation.padGaps);
203 EditNameDialog dialog = new EditNameDialog(annotation.label,
204 annotation.description, " Annotation Label",
205 "Annotation Description", ap.alignFrame,
206 "Edit Annotation Name / Description", 500, 180, false);
208 Panel empty = new Panel(new FlowLayout());
213 dialog.setVisible(true);
217 annotation.label = dialog.getName();
218 annotation.description = dialog.getDescription();
219 annotation.setPadGaps(padGaps.getState(), av.getGapCharacter());
228 boolean resizePanel = false;
230 public void mouseMoved(MouseEvent evt)
232 resizePanel = evt.getY() < 10 && evt.getX() < 14;
233 int row = getSelectedRow(evt.getY() + scrollOffset);
237 ParseHtmlBodyAndLinks phb = new ParseHtmlBodyAndLinks(
238 av.getAlignment().getAlignmentAnnotation()[row]
239 .getDescription(true),
243 tooltip = new Tooltip(phb.getNonHtmlContent(), this);
247 tooltip.setTip(phb.getNonHtmlContent());
250 else if (tooltip != null)
257 * curent drag position
259 MouseEvent dragEvent = null;
262 * flag to indicate drag events should be ignored
264 private boolean dragCancelled = false;
267 * clear any drag events in progress
269 public void cancelDrag()
272 dragCancelled = true;
275 public void mouseDragged(MouseEvent evt)
286 Dimension d = ap.annotationPanelHolder.getSize(), e = ap.annotationSpaceFillerHolder
287 .getSize(), f = ap.seqPanelHolder.getSize();
288 int dif = evt.getY() - oldY;
290 dif /= ap.av.charHeight;
291 dif *= ap.av.charHeight;
293 if ((d.height - dif) > 20 && (f.height + dif) > 20)
295 ap.annotationPanel.setSize(d.width, d.height - dif);
296 setSize(new Dimension(e.width, d.height - dif));
297 ap.annotationSpaceFillerHolder.setSize(new Dimension(e.width,
299 ap.annotationPanelHolder.setSize(new Dimension(d.width, d.height
301 ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height - dif, 0,
302 av.calcPanelHeight());
304 ap.seqPanelHolder.setPreferredSize(f);
305 ap.setScrollValues(av.getStartRes(), av.getStartSeq());
307 // ap.paintAlignment(true);
315 if ((diff = 6 - evt.getY()) > 0)
318 ap.apvscroll.setValue(ap.apvscroll.getValue() - diff);
319 ap.adjustmentValueChanged(null);
322 else if ((0 < (diff = 6
323 - ap.annotationSpaceFillerHolder.getSize().height
327 ap.apvscroll.setValue(ap.apvscroll.getValue() + diff);
328 ap.adjustmentValueChanged(null);
334 public void mouseClicked(MouseEvent evt)
338 public void mouseReleased(MouseEvent evt)
340 if (!resizePanel && !dragCancelled)
342 int start = selectedRow;
344 int end = getSelectedRow(evt.getY() + scrollOffset);
346 if (start > -1 && start != end)
348 // Swap these annotations
349 AlignmentAnnotation startAA = ap.av.getAlignment()
350 .getAlignmentAnnotation()[start];
353 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
355 AlignmentAnnotation endAA = ap.av.getAlignment()
356 .getAlignmentAnnotation()[end];
358 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
359 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
364 dragCancelled = false;
366 ap.annotationPanel.repaint();
369 public void mouseEntered(MouseEvent evt)
371 if (evt.getY() < 10 && evt.getX() < 14)
378 public void mouseExited(MouseEvent evt)
380 dragCancelled = false;
382 if (dragEvent == null)
396 public void mousePressed(MouseEvent evt)
403 dragCancelled = false;
404 // todo: move below to mouseClicked ?
405 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
407 AlignmentAnnotation[] aa = ap.av.getAlignment()
408 .getAlignmentAnnotation();
410 // DETECT RIGHT MOUSE BUTTON IN AWT
411 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
414 PopupMenu popup = new PopupMenu(
415 MessageManager.getString("label.annotations"));
417 MenuItem item = new MenuItem(ADDNEW);
418 item.addActionListener(this);
422 // this never happens at moment: - see comment on JAL-563
425 item = new MenuItem(SHOWALL);
426 item.addActionListener(this);
430 popup.show(this, evt.getX(), evt.getY());
433 // add the rest if there are actually rows to show
434 item = new MenuItem(EDITNAME);
435 item.addActionListener(this);
437 item = new MenuItem(HIDE);
438 item.addActionListener(this);
442 item = new MenuItem(SHOWALL);
443 item.addActionListener(this);
447 item = new MenuItem(OUTPUT_TEXT);
448 item.addActionListener(this);
450 if (selectedRow < aa.length)
452 if (aa[selectedRow].autoCalculated)
454 if (aa[selectedRow].label.indexOf("Consensus") > -1)
456 popup.addSeparator();
457 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
458 "Ignore Gaps In Consensus",
459 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
460 .getIgnoreGapsConsensus() : ap.av
461 .getIgnoreGapsConsensus());
462 final AlignmentAnnotation aaa = aa[selectedRow];
463 cbmi.addItemListener(new ItemListener()
465 public void itemStateChanged(ItemEvent e)
467 if (aaa.groupRef != null)
469 // TODO: pass on reference to ap so the view can be updated.
470 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
474 ap.av.setIgnoreGapsConsensus(cbmi.getState());
476 ap.paintAlignment(true);
480 if (aaa.groupRef != null)
482 final CheckboxMenuItem chist = new CheckboxMenuItem(
483 "Show Group Histogram",
484 aa[selectedRow].groupRef.isShowConsensusHistogram());
485 chist.addItemListener(new ItemListener()
487 public void itemStateChanged(ItemEvent e)
489 // TODO: pass on reference
495 aaa.groupRef.setShowConsensusHistogram(chist.getState());
497 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
501 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
503 aa[selectedRow].groupRef.isShowSequenceLogo());
504 cprofl.addItemListener(new ItemListener()
506 public void itemStateChanged(ItemEvent e)
508 // TODO: pass on reference
514 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
516 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
521 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
522 "Normalise Group Logo",
523 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
524 cprofn.addItemListener(new ItemListener()
526 public void itemStateChanged(ItemEvent e)
528 // TODO: pass on reference
534 aaa.groupRef.setshowSequenceLogo(true);
535 aaa.groupRef.setNormaliseSequenceLogo(cprofn.getState());
537 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
544 final CheckboxMenuItem chist = new CheckboxMenuItem(
545 "Show Histogram", av.isShowConsensusHistogram());
546 chist.addItemListener(new ItemListener()
548 public void itemStateChanged(ItemEvent e)
550 // TODO: pass on reference
556 av.setShowConsensusHistogram(chist.getState());
557 ap.alignFrame.showConsensusHistogram.setState(chist
558 .getState()); // TODO: implement
559 // ap.updateGUI()/alignFrame.updateGUI
562 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
566 final CheckboxMenuItem cprof = new CheckboxMenuItem(
567 "Show Logo", av.isShowSequenceLogo());
568 cprof.addItemListener(new ItemListener()
570 public void itemStateChanged(ItemEvent e)
572 // TODO: pass on reference
578 av.setShowSequenceLogo(cprof.getState());
579 ap.alignFrame.showSequenceLogo.setState(cprof.getState()); // TODO:
581 // ap.updateGUI()/alignFrame.updateGUI
585 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
589 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
590 "Normalise Logo", av.isNormaliseSequenceLogo());
591 cprofn.addItemListener(new ItemListener()
593 public void itemStateChanged(ItemEvent e)
595 // TODO: pass on reference
601 av.setShowSequenceLogo(true);
602 ap.alignFrame.normSequenceLogo.setState(cprofn.getState()); // TODO:
604 // ap.updateGUI()/alignFrame.updateGUI
607 av.setNormaliseSequenceLogo(cprofn.getState());
609 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
615 item = new MenuItem(COPYCONS_SEQ);
616 item.addActionListener(this);
621 popup.show(this, evt.getX(), evt.getY());
626 if (selectedRow > -1 && selectedRow < aa.length)
628 if (aa[selectedRow].groupRef != null)
630 if (evt.getClickCount() >= 2)
632 // todo: make the ap scroll to the selection - not necessary, first
633 // click highlights/scrolls, second selects
634 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
635 ap.av.setSelectionGroup(// new SequenceGroup(
636 aa[selectedRow].groupRef); // );
637 ap.av.sendSelection();
638 ap.paintAlignment(false);
639 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
643 ap.seqPanel.ap.idPanel
644 .highlightSearchResults(aa[selectedRow].groupRef
645 .getSequences(null));
649 else if (aa[selectedRow].sequenceRef != null)
651 if (evt.getClickCount() == 1)
653 ap.seqPanel.ap.idPanel.highlightSearchResults(Arrays
654 .asList(new SequenceI[]
655 { aa[selectedRow].sequenceRef }));
657 else if (evt.getClickCount() >= 2)
659 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
660 SequenceGroup sg = ap.av.getSelectionGroup();
663 // we make a copy rather than edit the current selection if no modifiers pressed
664 // see Enhancement JAL-1557
665 if (!(evt.isControlDown() || evt.isShiftDown()))
667 sg = new SequenceGroup(sg);
669 sg.addSequence(aa[selectedRow].sequenceRef, false);
671 if (evt.isControlDown())
673 sg.addOrRemove(aa[selectedRow].sequenceRef, true);
675 // notionally, we should also add intermediate sequences from last added sequence ?
676 sg.addSequence(aa[selectedRow].sequenceRef, true);
680 sg = new SequenceGroup();
682 sg.setEndRes(ap.av.getAlignment().getWidth()-1);
683 sg.addSequence(aa[selectedRow].sequenceRef, false);
685 ap.av.setSelectionGroup(sg);
686 ap.paintAlignment(false);
687 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
688 ap.av.sendSelection();
703 protected void copy_annotseqtoclipboard(SequenceI sq)
705 if (sq == null || sq.getLength() < 1)
709 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
710 jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t"
711 + sq.getStart() + "\t" + sq.getEnd() + "\t"
712 + sq.getSequenceAsString() + "\n");
713 if (av.hasHiddenColumns())
715 jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector();
716 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
718 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
721 jalview.appletgui.AlignFrame.copiedHiddenColumns
722 .addElement(new int[]
723 { region[0], region[1] });
728 public void update(Graphics g)
733 public void paint(Graphics g)
735 int w = getSize().width;
736 int h = getSize().height;
737 if (image == null || w != image.getWidth(this)
738 || h != image.getHeight(this))
740 image = createImage(w, ap.annotationPanel.getSize().height);
743 drawComponent(image.getGraphics(), w);
744 g.drawImage(image, 0, 0, this);
747 public void drawComponent(Graphics g, int width)
749 g.setFont(av.getFont());
750 FontMetrics fm = g.getFontMetrics(av.getFont());
751 g.setColor(Color.white);
752 g.fillRect(0, 0, getSize().width, getSize().height);
754 g.translate(0, -scrollOffset);
755 g.setColor(Color.black);
757 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
758 int y = 0, fy = g.getFont().getSize();
763 hasHiddenRows = false;
764 for (int i = 0; i < aa.length; i++)
768 hasHiddenRows = true;
772 x = width - fm.stringWidth(aa[i].label) - 3;
775 offset = -(aa[i].height - fy) / 2;
777 g.drawString(aa[i].label, x, y + offset);
780 g.translate(0, +scrollOffset);
783 g.setColor(Color.red);
785 g.drawLine(2, 8, 5, 2);
786 g.drawLine(5, 2, 8, 8);
788 else if (!dragCancelled && dragEvent != null && aa != null)
790 g.setColor(Color.lightGray);
791 g.drawString(aa[selectedRow].label, dragEvent.getX(),
795 if (!av.wrapAlignment && ((aa == null) || (aa.length < 1)))
797 g.setColor(Color.black);
798 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
799 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,