2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.appletgui;
23 import java.awt.event.*;
25 import jalview.datamodel.*;
26 import jalview.util.ParseHtmlBodyAndLinks;
28 public class AnnotationLabels extends Panel implements ActionListener,
29 MouseListener, MouseMotionListener
33 boolean active = false;
39 boolean resizing = false;
43 static String ADDNEW = "Add New Row";
45 static String EDITNAME = "Edit Label/Description";
47 static String HIDE = "Hide This Row";
49 static String SHOWALL = "Show All Hidden Rows";
51 static String OUTPUT_TEXT = "Show Values In Textbox";
53 static String COPYCONS_SEQ = "Copy Consensus Sequence";
61 private boolean hasHiddenRows;
63 public AnnotationLabels(AlignmentPanel ap)
70 * this retrieves the adjustable height glyph from resources. we don't use
71 * it at the moment. java.net.URL url =
72 * getClass().getResource("/images/idwidth.gif"); Image temp = null;
74 * if (url != null) { temp =
75 * java.awt.Toolkit.getDefaultToolkit().createImage(url); }
77 * try { MediaTracker mt = new MediaTracker(this); mt.addImage(temp, 0);
78 * mt.waitForID(0); } catch (Exception ex) { }
80 * BufferedImage bi = new BufferedImage(temp.getHeight(this),
81 * temp.getWidth(this), BufferedImage.TYPE_INT_RGB); Graphics2D g =
82 * (Graphics2D) bi.getGraphics(); g.rotate(Math.toRadians(90));
83 * g.drawImage(temp, 0, -bi.getWidth(this), this); image = (Image) bi;
85 addMouseListener(this);
86 addMouseMotionListener(this);
89 public AnnotationLabels(AlignViewport av)
94 public void setScrollOffset(int y)
103 * @return -2 if no rows are visible at all, -1 if no visible rows were
106 int getSelectedRow(int y)
109 AlignmentAnnotation[] aa = ap.av.getAlignment()
110 .getAlignmentAnnotation();
117 for (int i = 0; i < aa.length; i++)
124 height += aa[i].height;
135 public void actionPerformed(ActionEvent evt)
137 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
139 if (evt.getActionCommand().equals(ADDNEW))
141 AlignmentAnnotation newAnnotation = new AlignmentAnnotation("", null,
142 new Annotation[ap.av.getAlignment().getWidth()]);
144 if (!editLabelDescription(newAnnotation))
149 ap.av.getAlignment().addAnnotation(newAnnotation);
150 ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
152 else if (evt.getActionCommand().equals(EDITNAME))
154 editLabelDescription(aa[selectedRow]);
156 else if (evt.getActionCommand().equals(HIDE))
158 aa[selectedRow].visible = false;
160 else if (evt.getActionCommand().equals(SHOWALL))
162 for (int i = 0; i < aa.length; i++)
164 aa[i].visible = (aa[i].annotations == null) ? false : true;
167 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
169 CutAndPasteTransfer cap = new CutAndPasteTransfer(false,
171 Frame frame = new Frame();
173 jalview.bin.JalviewLite.addFrame(frame, ap.alignFrame.getTitle()
174 + " - " + aa[selectedRow].label, 500, 100);
175 cap.setText(aa[selectedRow].toString());
177 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
179 SequenceI cons = av.getConsensusSeq();
182 copy_annotseqtoclipboard(cons);
186 ap.annotationPanel.adjustPanelHeight();
187 setSize(getSize().width, ap.annotationPanel.getSize().height);
189 ap.paintAlignment(true);
192 boolean editLabelDescription(AlignmentAnnotation annotation)
194 Checkbox padGaps = new Checkbox("Fill Empty Gaps With \""
195 + ap.av.getGapCharacter() + "\"", annotation.padGaps);
197 EditNameDialog dialog = new EditNameDialog(annotation.label,
198 annotation.description, " Annotation Label",
199 "Annotation Description", ap.alignFrame,
200 "Edit Annotation Name / Description", 500, 180, false);
202 Panel empty = new Panel(new FlowLayout());
207 dialog.setVisible(true);
211 annotation.label = dialog.getName();
212 annotation.description = dialog.getDescription();
213 annotation.setPadGaps(padGaps.getState(), av.getGapCharacter());
222 boolean resizePanel = false;
224 public void mouseMoved(MouseEvent evt)
226 resizePanel = evt.getY() < 10 && evt.getX() < 14;
227 int row = getSelectedRow(evt.getY() + scrollOffset);
231 ParseHtmlBodyAndLinks phb = new ParseHtmlBodyAndLinks(
232 av.getAlignment().getAlignmentAnnotation()[row]
233 .getDescription(true),
237 tooltip = new Tooltip(phb.getNonHtmlContent(), this);
241 tooltip.setTip(phb.getNonHtmlContent());
244 else if (tooltip != null)
251 * curent drag position
253 MouseEvent dragEvent = null;
256 * flag to indicate drag events should be ignored
258 private boolean dragCancelled = false;
261 * clear any drag events in progress
263 public void cancelDrag()
266 dragCancelled = true;
269 public void mouseDragged(MouseEvent evt)
280 Dimension d = ap.annotationPanelHolder.getSize(), e = ap.annotationSpaceFillerHolder
281 .getSize(), f = ap.seqPanelHolder.getSize();
282 int dif = evt.getY() - oldY;
284 dif /= ap.av.charHeight;
285 dif *= ap.av.charHeight;
287 if ((d.height - dif) > 20 && (f.height + dif) > 20)
289 ap.annotationPanel.setSize(d.width, d.height - dif);
290 setSize(new Dimension(e.width, d.height - dif));
291 ap.annotationSpaceFillerHolder.setSize(new Dimension(e.width,
293 ap.annotationPanelHolder.setSize(new Dimension(d.width, d.height
295 ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height - dif, 0,
296 av.calcPanelHeight());
298 ap.seqPanelHolder.setPreferredSize(f);
299 ap.setScrollValues(av.getStartRes(), av.getStartSeq());
301 // ap.paintAlignment(true);
309 if ((diff = 6 - evt.getY()) > 0)
312 ap.apvscroll.setValue(ap.apvscroll.getValue() - diff);
313 ap.adjustmentValueChanged(null);
316 else if ((0 < (diff = 6
317 - ap.annotationSpaceFillerHolder.getSize().height
321 ap.apvscroll.setValue(ap.apvscroll.getValue() + diff);
322 ap.adjustmentValueChanged(null);
328 public void mouseClicked(MouseEvent evt)
332 public void mouseReleased(MouseEvent evt)
334 if (!resizePanel && !dragCancelled)
336 int start = selectedRow;
338 int end = getSelectedRow(evt.getY() + scrollOffset);
340 if (start > -1 && start != end)
342 // Swap these annotations
343 AlignmentAnnotation startAA = ap.av.getAlignment()
344 .getAlignmentAnnotation()[start];
347 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
349 AlignmentAnnotation endAA = ap.av.getAlignment()
350 .getAlignmentAnnotation()[end];
352 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
353 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
358 dragCancelled = false;
360 ap.annotationPanel.repaint();
363 public void mouseEntered(MouseEvent evt)
365 if (evt.getY() < 10 && evt.getX() < 14)
372 public void mouseExited(MouseEvent evt)
374 dragCancelled = false;
376 if (dragEvent == null)
390 public void mousePressed(MouseEvent evt)
397 dragCancelled = false;
398 // todo: move below to mouseClicked ?
399 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
401 AlignmentAnnotation[] aa = ap.av.getAlignment()
402 .getAlignmentAnnotation();
404 // DETECT RIGHT MOUSE BUTTON IN AWT
405 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
408 PopupMenu popup = new PopupMenu("Annotations");
410 MenuItem item = new MenuItem(ADDNEW);
411 item.addActionListener(this);
415 // this never happens at moment: - see comment on JAL-563
418 item = new MenuItem(SHOWALL);
419 item.addActionListener(this);
423 popup.show(this, evt.getX(), evt.getY());
426 // add the rest if there are actually rows to show
427 item = new MenuItem(EDITNAME);
428 item.addActionListener(this);
430 item = new MenuItem(HIDE);
431 item.addActionListener(this);
435 item = new MenuItem(SHOWALL);
436 item.addActionListener(this);
440 item = new MenuItem(OUTPUT_TEXT);
441 item.addActionListener(this);
443 if (selectedRow < aa.length)
445 if (aa[selectedRow].autoCalculated)
447 if (aa[selectedRow].label.indexOf("Consensus") > -1)
449 popup.addSeparator();
450 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
451 "Ignore Gaps In Consensus",
452 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
453 .getIgnoreGapsConsensus() : ap.av
454 .getIgnoreGapsConsensus());
455 final AlignmentAnnotation aaa = aa[selectedRow];
456 cbmi.addItemListener(new ItemListener()
458 public void itemStateChanged(ItemEvent e)
460 if (aaa.groupRef != null)
462 // TODO: pass on reference to ap so the view can be updated.
463 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
467 ap.av.setIgnoreGapsConsensus(cbmi.getState());
469 ap.paintAlignment(true);
473 if (aaa.groupRef != null)
475 final CheckboxMenuItem chist = new CheckboxMenuItem(
476 "Show Group Histogram",
477 aa[selectedRow].groupRef.isShowConsensusHistogram());
478 chist.addItemListener(new ItemListener()
480 public void itemStateChanged(ItemEvent e)
482 // TODO: pass on reference
488 aaa.groupRef.setShowConsensusHistogram(chist.getState());
490 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
494 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
496 aa[selectedRow].groupRef.isShowSequenceLogo());
497 cprofl.addItemListener(new ItemListener()
499 public void itemStateChanged(ItemEvent e)
501 // TODO: pass on reference
507 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
509 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
514 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
515 "Normalise Group Logo",
516 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
517 cprofn.addItemListener(new ItemListener()
519 public void itemStateChanged(ItemEvent e)
521 // TODO: pass on reference
527 aaa.groupRef.setshowSequenceLogo(true);
528 aaa.groupRef.setNormaliseSequenceLogo(cprofn.getState());
530 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
537 final CheckboxMenuItem chist = new CheckboxMenuItem(
538 "Show Histogram", av.isShowConsensusHistogram());
539 chist.addItemListener(new ItemListener()
541 public void itemStateChanged(ItemEvent e)
543 // TODO: pass on reference
549 av.setShowConsensusHistogram(chist.getState());
550 ap.alignFrame.showConsensusHistogram.setState(chist
551 .getState()); // TODO: implement
552 // ap.updateGUI()/alignFrame.updateGUI
555 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
559 final CheckboxMenuItem cprof = new CheckboxMenuItem(
560 "Show Logo", av.isShowSequenceLogo());
561 cprof.addItemListener(new ItemListener()
563 public void itemStateChanged(ItemEvent e)
565 // TODO: pass on reference
571 av.setShowSequenceLogo(cprof.getState());
572 ap.alignFrame.showSequenceLogo.setState(cprof.getState()); // TODO:
574 // ap.updateGUI()/alignFrame.updateGUI
578 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
582 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
583 "Normalise Logo", av.isNormaliseSequenceLogo());
584 cprofn.addItemListener(new ItemListener()
586 public void itemStateChanged(ItemEvent e)
588 // TODO: pass on reference
594 av.setShowSequenceLogo(true);
595 ap.alignFrame.normSequenceLogo.setState(cprofn.getState()); // TODO:
597 // ap.updateGUI()/alignFrame.updateGUI
600 av.setNormaliseSequenceLogo(cprofn.getState());
602 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
608 item = new MenuItem(COPYCONS_SEQ);
609 item.addActionListener(this);
614 popup.show(this, evt.getX(), evt.getY());
619 if (selectedRow > -1 && selectedRow < aa.length)
621 if (aa[selectedRow].groupRef != null)
623 if (evt.getClickCount() >= 2)
625 // todo: make the ap scroll to the selection - not necessary, first
626 // click highlights/scrolls, second selects
627 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
628 ap.av.setSelectionGroup(// new SequenceGroup(
629 aa[selectedRow].groupRef); // );
630 ap.av.sendSelection();
631 ap.paintAlignment(false);
632 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
636 ap.seqPanel.ap.idPanel
637 .highlightSearchResults(aa[selectedRow].groupRef
638 .getSequences(null));
642 else if (aa[selectedRow].sequenceRef != null)
644 Vector sr = new Vector();
645 sr.addElement(aa[selectedRow].sequenceRef);
646 if (evt.getClickCount() == 1)
648 ap.seqPanel.ap.idPanel.highlightSearchResults(sr);
650 else if (evt.getClickCount() >= 2)
652 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
653 SequenceGroup sg = new SequenceGroup();
654 sg.addSequence(aa[selectedRow].sequenceRef, false);
655 ap.av.setSelectionGroup(sg);
656 ap.paintAlignment(false);
657 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
658 ap.av.sendSelection();
673 protected void copy_annotseqtoclipboard(SequenceI sq)
675 if (sq == null || sq.getLength() < 1)
679 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
680 jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t"
681 + sq.getStart() + "\t" + sq.getEnd() + "\t"
682 + sq.getSequenceAsString() + "\n");
683 if (av.hasHiddenColumns())
685 jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector();
686 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
688 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
691 jalview.appletgui.AlignFrame.copiedHiddenColumns
692 .addElement(new int[]
693 { region[0], region[1] });
698 public void update(Graphics g)
703 public void paint(Graphics g)
705 int w = getSize().width;
706 int h = getSize().height;
707 if (image == null || w != image.getWidth(this)
708 || h != image.getHeight(this))
710 image = createImage(w, ap.annotationPanel.getSize().height);
713 drawComponent(image.getGraphics(), w);
714 g.drawImage(image, 0, 0, this);
717 public void drawComponent(Graphics g, int width)
719 g.setFont(av.getFont());
720 FontMetrics fm = g.getFontMetrics(av.getFont());
721 g.setColor(Color.white);
722 g.fillRect(0, 0, getSize().width, getSize().height);
724 g.translate(0, -scrollOffset);
725 g.setColor(Color.black);
727 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
728 int y = 0, fy = g.getFont().getSize();
733 hasHiddenRows = false;
734 for (int i = 0; i < aa.length; i++)
738 hasHiddenRows = true;
742 x = width - fm.stringWidth(aa[i].label) - 3;
745 offset = -(aa[i].height - fy) / 2;
747 g.drawString(aa[i].label, x, y + offset);
750 g.translate(0, +scrollOffset);
753 g.setColor(Color.red);
755 g.drawLine(2, 8, 5, 2);
756 g.drawLine(5, 2, 8, 8);
758 else if (!dragCancelled && dragEvent != null && aa != null)
760 g.setColor(Color.lightGray);
761 g.drawString(aa[selectedRow].label, dragEvent.getX(),
765 if (!av.wrapAlignment && ((aa == null) || (aa.length < 1)))
767 g.setColor(Color.black);
768 g.drawString("Right click", 2, 8);
769 g.drawString("to add annotation", 2, 18);