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;
26 import java.awt.event.*;
28 import jalview.datamodel.*;
29 import jalview.util.MessageManager;
30 import jalview.util.ParseHtmlBodyAndLinks;
32 public class AnnotationLabels extends Panel implements ActionListener,
33 MouseListener, MouseMotionListener
37 boolean active = false;
43 boolean resizing = false;
47 static String ADDNEW = "Add New Row";
49 static String EDITNAME = "Edit Label/Description";
51 static String HIDE = "Hide This Row";
53 static String SHOWALL = "Show All Hidden Rows";
55 static String OUTPUT_TEXT = "Show Values In Textbox";
57 static String COPYCONS_SEQ = "Copy Consensus Sequence";
65 private boolean hasHiddenRows;
67 public AnnotationLabels(AlignmentPanel ap)
74 * this retrieves the adjustable height glyph from resources. we don't use
75 * it at the moment. java.net.URL url =
76 * getClass().getResource("/images/idwidth.gif"); Image temp = null;
78 * if (url != null) { temp =
79 * java.awt.Toolkit.getDefaultToolkit().createImage(url); }
81 * try { MediaTracker mt = new MediaTracker(this); mt.addImage(temp, 0);
82 * mt.waitForID(0); } catch (Exception ex) { }
84 * BufferedImage bi = new BufferedImage(temp.getHeight(this),
85 * temp.getWidth(this), BufferedImage.TYPE_INT_RGB); Graphics2D g =
86 * (Graphics2D) bi.getGraphics(); g.rotate(Math.toRadians(90));
87 * g.drawImage(temp, 0, -bi.getWidth(this), this); image = (Image) bi;
89 addMouseListener(this);
90 addMouseMotionListener(this);
93 public AnnotationLabels(AlignViewport av)
98 public void setScrollOffset(int y, boolean repaint)
110 * @return -2 if no rows are visible at all, -1 if no visible rows were
113 int getSelectedRow(int y)
116 AlignmentAnnotation[] aa = ap.av.getAlignment()
117 .getAlignmentAnnotation();
124 for (int i = 0; i < aa.length; i++)
131 height += aa[i].height;
142 public void actionPerformed(ActionEvent evt)
144 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
146 if (evt.getActionCommand().equals(ADDNEW))
148 AlignmentAnnotation newAnnotation = new AlignmentAnnotation("", null,
149 new Annotation[ap.av.getAlignment().getWidth()]);
151 if (!editLabelDescription(newAnnotation))
156 ap.av.getAlignment().addAnnotation(newAnnotation);
157 ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
159 else if (evt.getActionCommand().equals(EDITNAME))
161 editLabelDescription(aa[selectedRow]);
163 else if (evt.getActionCommand().equals(HIDE))
165 aa[selectedRow].visible = false;
167 else if (evt.getActionCommand().equals(SHOWALL))
169 for (int i = 0; i < aa.length; i++)
171 aa[i].visible = (aa[i].annotations == null) ? false : true;
174 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
176 CutAndPasteTransfer cap = new CutAndPasteTransfer(false,
178 Frame frame = new Frame();
180 jalview.bin.JalviewLite.addFrame(frame, ap.alignFrame.getTitle()
181 + " - " + aa[selectedRow].label, 500, 100);
182 cap.setText(aa[selectedRow].toString());
184 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
186 SequenceI cons = av.getConsensusSeq();
189 copy_annotseqtoclipboard(cons);
193 ap.annotationPanel.adjustPanelHeight();
194 setSize(getSize().width, ap.annotationPanel.getSize().height);
196 ap.paintAlignment(true);
199 boolean editLabelDescription(AlignmentAnnotation annotation)
201 Checkbox padGaps = new Checkbox("Fill Empty Gaps With \""
202 + ap.av.getGapCharacter() + "\"", annotation.padGaps);
204 EditNameDialog dialog = new EditNameDialog(annotation.label,
205 annotation.description, " Annotation Label",
206 "Annotation Description", ap.alignFrame,
207 "Edit Annotation Name / Description", 500, 180, false);
209 Panel empty = new Panel(new FlowLayout());
214 dialog.setVisible(true);
218 annotation.label = dialog.getName();
219 annotation.description = dialog.getDescription();
220 annotation.setPadGaps(padGaps.getState(), av.getGapCharacter());
229 boolean resizePanel = false;
231 public void mouseMoved(MouseEvent evt)
233 resizePanel = evt.getY() < 10 && evt.getX() < 14;
234 int row = getSelectedRow(evt.getY() + scrollOffset);
238 ParseHtmlBodyAndLinks phb = new ParseHtmlBodyAndLinks(
239 av.getAlignment().getAlignmentAnnotation()[row]
240 .getDescription(true),
244 tooltip = new Tooltip(phb.getNonHtmlContent(), this);
248 tooltip.setTip(phb.getNonHtmlContent());
251 else if (tooltip != null)
258 * curent drag position
260 MouseEvent dragEvent = null;
263 * flag to indicate drag events should be ignored
265 private boolean dragCancelled = false;
268 * clear any drag events in progress
270 public void cancelDrag()
273 dragCancelled = true;
276 public void mouseDragged(MouseEvent evt)
287 Dimension d = ap.annotationPanelHolder.getSize(), e = ap.annotationSpaceFillerHolder
288 .getSize(), f = ap.seqPanelHolder.getSize();
289 int dif = evt.getY() - oldY;
291 dif /= ap.av.charHeight;
292 dif *= ap.av.charHeight;
294 if ((d.height - dif) > 20 && (f.height + dif) > 20)
296 ap.annotationPanel.setSize(d.width, d.height - dif);
297 setSize(new Dimension(e.width, d.height - dif));
298 ap.annotationSpaceFillerHolder.setSize(new Dimension(e.width,
300 ap.annotationPanelHolder.setSize(new Dimension(d.width, d.height
302 ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height - dif, 0,
303 av.calcPanelHeight());
305 ap.seqPanelHolder.setPreferredSize(f);
306 ap.setScrollValues(av.getStartRes(), av.getStartSeq());
308 // ap.paintAlignment(true);
316 if ((diff = 6 - evt.getY()) > 0)
319 ap.apvscroll.setValue(ap.apvscroll.getValue() - diff);
320 ap.adjustmentValueChanged(null);
323 else if ((0 < (diff = 6
324 - ap.annotationSpaceFillerHolder.getSize().height
328 ap.apvscroll.setValue(ap.apvscroll.getValue() + diff);
329 ap.adjustmentValueChanged(null);
335 public void mouseClicked(MouseEvent evt)
339 public void mouseReleased(MouseEvent evt)
341 if (!resizePanel && !dragCancelled)
343 int start = selectedRow;
345 int end = getSelectedRow(evt.getY() + scrollOffset);
347 if (start > -1 && start != end)
349 // Swap these annotations
350 AlignmentAnnotation startAA = ap.av.getAlignment()
351 .getAlignmentAnnotation()[start];
354 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
356 AlignmentAnnotation endAA = ap.av.getAlignment()
357 .getAlignmentAnnotation()[end];
359 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
360 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
365 dragCancelled = false;
367 ap.annotationPanel.repaint();
370 public void mouseEntered(MouseEvent evt)
372 if (evt.getY() < 10 && evt.getX() < 14)
379 public void mouseExited(MouseEvent evt)
381 dragCancelled = false;
383 if (dragEvent == null)
397 public void mousePressed(MouseEvent evt)
404 dragCancelled = false;
405 // todo: move below to mouseClicked ?
406 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
408 AlignmentAnnotation[] aa = ap.av.getAlignment()
409 .getAlignmentAnnotation();
411 // DETECT RIGHT MOUSE BUTTON IN AWT
412 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
415 PopupMenu popup = new PopupMenu(
416 MessageManager.getString("label.annotations"));
418 MenuItem item = new MenuItem(ADDNEW);
419 item.addActionListener(this);
423 // this never happens at moment: - see comment on JAL-563
426 item = new MenuItem(SHOWALL);
427 item.addActionListener(this);
431 popup.show(this, evt.getX(), evt.getY());
434 // add the rest if there are actually rows to show
435 item = new MenuItem(EDITNAME);
436 item.addActionListener(this);
438 item = new MenuItem(HIDE);
439 item.addActionListener(this);
443 item = new MenuItem(SHOWALL);
444 item.addActionListener(this);
448 item = new MenuItem(OUTPUT_TEXT);
449 item.addActionListener(this);
451 if (selectedRow < aa.length)
453 if (aa[selectedRow].autoCalculated)
455 if (aa[selectedRow].label.indexOf("Consensus") > -1)
457 popup.addSeparator();
458 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
459 "Ignore Gaps In Consensus",
460 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
461 .getIgnoreGapsConsensus() : ap.av
462 .getIgnoreGapsConsensus());
463 final AlignmentAnnotation aaa = aa[selectedRow];
464 cbmi.addItemListener(new ItemListener()
466 public void itemStateChanged(ItemEvent e)
468 if (aaa.groupRef != null)
470 // TODO: pass on reference to ap so the view can be updated.
471 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
475 ap.av.setIgnoreGapsConsensus(cbmi.getState());
477 ap.paintAlignment(true);
481 if (aaa.groupRef != null)
483 final CheckboxMenuItem chist = new CheckboxMenuItem(
484 "Show Group Histogram",
485 aa[selectedRow].groupRef.isShowConsensusHistogram());
486 chist.addItemListener(new ItemListener()
488 public void itemStateChanged(ItemEvent e)
490 // TODO: pass on reference
496 aaa.groupRef.setShowConsensusHistogram(chist.getState());
498 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
502 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
504 aa[selectedRow].groupRef.isShowSequenceLogo());
505 cprofl.addItemListener(new ItemListener()
507 public void itemStateChanged(ItemEvent e)
509 // TODO: pass on reference
515 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
517 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
522 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
523 "Normalise Group Logo",
524 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
525 cprofn.addItemListener(new ItemListener()
527 public void itemStateChanged(ItemEvent e)
529 // TODO: pass on reference
535 aaa.groupRef.setshowSequenceLogo(true);
536 aaa.groupRef.setNormaliseSequenceLogo(cprofn.getState());
538 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
545 final CheckboxMenuItem chist = new CheckboxMenuItem(
546 "Show Histogram", av.isShowConsensusHistogram());
547 chist.addItemListener(new ItemListener()
549 public void itemStateChanged(ItemEvent e)
551 // TODO: pass on reference
557 av.setShowConsensusHistogram(chist.getState());
558 ap.alignFrame.showConsensusHistogram.setState(chist
559 .getState()); // TODO: implement
560 // ap.updateGUI()/alignFrame.updateGUI
563 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
567 final CheckboxMenuItem cprof = new CheckboxMenuItem(
568 "Show Logo", av.isShowSequenceLogo());
569 cprof.addItemListener(new ItemListener()
571 public void itemStateChanged(ItemEvent e)
573 // TODO: pass on reference
579 av.setShowSequenceLogo(cprof.getState());
580 ap.alignFrame.showSequenceLogo.setState(cprof.getState()); // TODO:
582 // ap.updateGUI()/alignFrame.updateGUI
586 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
590 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
591 "Normalise Logo", av.isNormaliseSequenceLogo());
592 cprofn.addItemListener(new ItemListener()
594 public void itemStateChanged(ItemEvent e)
596 // TODO: pass on reference
602 av.setShowSequenceLogo(true);
603 ap.alignFrame.normSequenceLogo.setState(cprofn.getState()); // TODO:
605 // ap.updateGUI()/alignFrame.updateGUI
608 av.setNormaliseSequenceLogo(cprofn.getState());
610 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
616 item = new MenuItem(COPYCONS_SEQ);
617 item.addActionListener(this);
622 popup.show(this, evt.getX(), evt.getY());
627 if (selectedRow > -1 && selectedRow < aa.length)
629 if (aa[selectedRow].groupRef != null)
631 if (evt.getClickCount() >= 2)
633 // todo: make the ap scroll to the selection - not necessary, first
634 // click highlights/scrolls, second selects
635 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
636 ap.av.setSelectionGroup(// new SequenceGroup(
637 aa[selectedRow].groupRef); // );
638 ap.av.sendSelection();
639 ap.paintAlignment(false);
640 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
644 ap.seqPanel.ap.idPanel
645 .highlightSearchResults(aa[selectedRow].groupRef
646 .getSequences(null));
650 else if (aa[selectedRow].sequenceRef != null)
652 Vector sr = new Vector();
653 sr.addElement(aa[selectedRow].sequenceRef);
654 if (evt.getClickCount() == 1)
656 ap.seqPanel.ap.idPanel.highlightSearchResults(sr);
658 else if (evt.getClickCount() >= 2)
660 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
661 SequenceGroup sg = new SequenceGroup();
662 sg.addSequence(aa[selectedRow].sequenceRef, false);
663 ap.av.setSelectionGroup(sg);
664 ap.paintAlignment(false);
665 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
666 ap.av.sendSelection();
681 protected void copy_annotseqtoclipboard(SequenceI sq)
683 if (sq == null || sq.getLength() < 1)
687 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
688 jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t"
689 + sq.getStart() + "\t" + sq.getEnd() + "\t"
690 + sq.getSequenceAsString() + "\n");
691 if (av.hasHiddenColumns())
693 jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector();
694 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
696 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
699 jalview.appletgui.AlignFrame.copiedHiddenColumns
700 .addElement(new int[]
701 { region[0], region[1] });
706 public void update(Graphics g)
711 public void paint(Graphics g)
713 int w = getSize().width;
714 int h = getSize().height;
715 if (image == null || w != image.getWidth(this)
716 || h != image.getHeight(this))
718 image = createImage(w, ap.annotationPanel.getSize().height);
721 drawComponent(image.getGraphics(), w);
722 g.drawImage(image, 0, 0, this);
725 public void drawComponent(Graphics g, int width)
727 g.setFont(av.getFont());
728 FontMetrics fm = g.getFontMetrics(av.getFont());
729 g.setColor(Color.white);
730 g.fillRect(0, 0, getSize().width, getSize().height);
732 g.translate(0, -scrollOffset);
733 g.setColor(Color.black);
735 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
736 int y = 0, fy = g.getFont().getSize();
741 hasHiddenRows = false;
742 for (int i = 0; i < aa.length; i++)
746 hasHiddenRows = true;
750 x = width - fm.stringWidth(aa[i].label) - 3;
753 offset = -(aa[i].height - fy) / 2;
755 g.drawString(aa[i].label, x, y + offset);
758 g.translate(0, +scrollOffset);
761 g.setColor(Color.red);
763 g.drawLine(2, 8, 5, 2);
764 g.drawLine(5, 2, 8, 8);
766 else if (!dragCancelled && dragEvent != null && aa != null)
768 g.setColor(Color.lightGray);
769 g.drawString(aa[selectedRow].label, dragEvent.getX(),
773 if (!av.wrapAlignment && ((aa == null) || (aa.length < 1)))
775 g.setColor(Color.black);
776 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
777 g.drawString(MessageManager.getString("label.to_add_annotation"), 2,