2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.*;
24 import java.awt.image.BufferedImage;
26 import jalview.datamodel.*;
27 import jalview.util.ParseHtmlBodyAndLinks;
29 public class AnnotationLabels extends Panel implements ActionListener,
30 MouseListener, MouseMotionListener
34 boolean active = false;
40 boolean resizing = false;
44 static String ADDNEW = "Add New Row";
46 static String EDITNAME = "Edit Label/Description";
48 static String HIDE = "Hide This Row";
50 static String SHOWALL = "Show All Hidden Rows";
52 static String OUTPUT_TEXT = "Show Values In Textbox";
54 static String COPYCONS_SEQ = "Copy Consensus Sequence";
62 private boolean hasHiddenRows;
64 public AnnotationLabels(AlignmentPanel ap)
71 * this retrieves the adjustable height glyph from resources. we don't use
72 * it at the moment. java.net.URL url =
73 * getClass().getResource("/images/idwidth.gif"); Image temp = null;
75 * if (url != null) { temp =
76 * java.awt.Toolkit.getDefaultToolkit().createImage(url); }
78 * try { MediaTracker mt = new MediaTracker(this); mt.addImage(temp, 0);
79 * mt.waitForID(0); } catch (Exception ex) { }
81 * BufferedImage bi = new BufferedImage(temp.getHeight(this),
82 * temp.getWidth(this), BufferedImage.TYPE_INT_RGB); Graphics2D g =
83 * (Graphics2D) bi.getGraphics(); g.rotate(Math.toRadians(90));
84 * g.drawImage(temp, 0, -bi.getWidth(this), this); image = (Image) bi;
86 addMouseListener(this);
87 addMouseMotionListener(this);
90 public AnnotationLabels(AlignViewport av)
95 public void setScrollOffset(int y)
104 * @return -2 if no rows are visible at all, -1 if no visible rows were
107 int getSelectedRow(int y)
110 AlignmentAnnotation[] aa = ap.av.getAlignment().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(av.getAlignment().getAlignmentAnnotation()[row].getDescription(true), true, "\n");
234 tooltip = new Tooltip(phb.getNonHtmlContent(), this);
238 tooltip.setTip(phb.getNonHtmlContent());
241 else if (tooltip != null)
248 * curent drag position
250 MouseEvent dragEvent = null;
253 * flag to indicate drag events should be ignored
255 private boolean dragCancelled = false;
258 * clear any drag events in progress
260 public void cancelDrag()
263 dragCancelled = true;
266 public void mouseDragged(MouseEvent evt)
277 Dimension d = ap.annotationPanelHolder.getSize(), e = ap.annotationSpaceFillerHolder
278 .getSize(), f = ap.seqPanelHolder.getSize();
279 int dif = evt.getY() - oldY;
281 dif /= ap.av.charHeight;
282 dif *= ap.av.charHeight;
284 if ((d.height - dif) > 20 && (f.height + dif) > 20)
286 ap.annotationPanel.setSize(d.width, d.height - dif);
287 setSize(new Dimension(e.width, d.height - dif));
288 ap.annotationSpaceFillerHolder.setSize(new Dimension(e.width,
290 ap.annotationPanelHolder.setSize(new Dimension(d.width, d.height
292 ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height - dif, 0,
293 ap.annotationPanel.calcPanelHeight());
295 ap.seqPanelHolder.setPreferredSize(f);
296 ap.setScrollValues(av.getStartRes(), av.getStartSeq());
298 // ap.paintAlignment(true);
306 if ((diff = 6 - evt.getY()) > 0)
309 ap.apvscroll.setValue(ap.apvscroll.getValue() - diff);
310 ap.adjustmentValueChanged(null);
313 else if ((0 < (diff = 6
314 - ap.annotationSpaceFillerHolder.getSize().height
318 ap.apvscroll.setValue(ap.apvscroll.getValue() + diff);
319 ap.adjustmentValueChanged(null);
325 public void mouseClicked(MouseEvent evt)
329 public void mouseReleased(MouseEvent evt)
331 if (!resizePanel && !dragCancelled)
333 int start = selectedRow;
335 int end = getSelectedRow(evt.getY() + scrollOffset);
337 if (start>-1 && start != end)
339 // Swap these annotations
340 AlignmentAnnotation startAA = ap.av.getAlignment()
341 .getAlignmentAnnotation()[start];
344 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
346 AlignmentAnnotation endAA = ap.av.getAlignment()
347 .getAlignmentAnnotation()[end];
349 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
350 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
355 dragCancelled = false;
357 ap.annotationPanel.repaint();
360 public void mouseEntered(MouseEvent evt)
362 if (evt.getY() < 10 && evt.getX() < 14)
369 public void mouseExited(MouseEvent evt)
371 dragCancelled = false;
373 if (dragEvent == null)
387 public void mousePressed(MouseEvent evt)
395 // todo: move below to mouseClicked ?
396 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
398 AlignmentAnnotation[] aa = ap.av.getAlignment().getAlignmentAnnotation();
400 // DETECT RIGHT MOUSE BUTTON IN AWT
401 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
404 PopupMenu popup = new PopupMenu("Annotations");
406 MenuItem item = new MenuItem(ADDNEW);
407 item.addActionListener(this);
411 // this never happens at moment: - see comment on JAL-563
414 item = new MenuItem(SHOWALL);
415 item.addActionListener(this);
419 popup.show(this, evt.getX(), evt.getY());
422 // add the rest if there are actually rows to show
423 item = new MenuItem(EDITNAME);
424 item.addActionListener(this);
426 item = new MenuItem(HIDE);
427 item.addActionListener(this);
431 item = new MenuItem(SHOWALL);
432 item.addActionListener(this);
436 item = new MenuItem(OUTPUT_TEXT);
437 item.addActionListener(this);
439 if (selectedRow < aa.length)
441 if (aa[selectedRow].autoCalculated)
443 if (aa[selectedRow].label.indexOf("Consensus") > -1)
445 popup.addSeparator();
446 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
447 "Ignore Gaps In Consensus",
448 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
449 .getIgnoreGapsConsensus() : ap.av
450 .getIgnoreGapsConsensus());
451 final AlignmentAnnotation aaa = aa[selectedRow];
452 cbmi.addItemListener(new ItemListener()
454 public void itemStateChanged(ItemEvent e)
456 if (aaa.groupRef != null)
458 // TODO: pass on reference to ap so the view can be updated.
459 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
463 ap.av.setIgnoreGapsConsensus(cbmi.getState());
465 ap.paintAlignment(true);
469 if (aaa.groupRef != null)
471 final CheckboxMenuItem chist = new CheckboxMenuItem(
472 "Show Group Histogram",
473 aa[selectedRow].groupRef.isShowConsensusHistogram());
474 chist.addItemListener(new ItemListener()
476 public void itemStateChanged(ItemEvent e)
478 // TODO: pass on reference
484 aaa.groupRef.setShowConsensusHistogram(chist.getState());
486 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
490 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
492 aa[selectedRow].groupRef.isShowSequenceLogo());
493 cprofl.addItemListener(new ItemListener()
495 public void itemStateChanged(ItemEvent e)
497 // TODO: pass on reference
503 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
505 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
512 final CheckboxMenuItem chist = new CheckboxMenuItem(
513 "Show Histogram", av.isShowConsensusHistogram());
514 chist.addItemListener(new ItemListener()
516 public void itemStateChanged(ItemEvent e)
518 // TODO: pass on reference
524 av.setShowConsensusHistogram(chist.getState());
526 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
530 final CheckboxMenuItem cprof = new CheckboxMenuItem(
531 "Show Logo", av.isShowSequenceLogo());
532 cprof.addItemListener(new ItemListener()
534 public void itemStateChanged(ItemEvent e)
536 // TODO: pass on reference
542 av.setShowSequenceLogo(cprof.getState());
544 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
550 item = new MenuItem(COPYCONS_SEQ);
551 item.addActionListener(this);
556 popup.show(this, evt.getX(), evt.getY());
561 if (selectedRow > -1 && selectedRow < aa.length)
563 if (aa[selectedRow].groupRef != null)
565 if (evt.getClickCount() >= 2)
567 // todo: make the ap scroll to the selection - not necessary, first
568 // click highlights/scrolls, second selects
569 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
570 ap.av.setSelectionGroup(// new SequenceGroup(
571 aa[selectedRow].groupRef); // );
572 ap.av.sendSelection();
573 ap.paintAlignment(false);
574 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
578 ap.seqPanel.ap.idPanel
579 .highlightSearchResults(aa[selectedRow].groupRef
580 .getSequences(null));
584 else if (aa[selectedRow].sequenceRef != null)
586 Vector sr = new Vector();
587 sr.addElement(aa[selectedRow].sequenceRef);
588 if (evt.getClickCount() == 1)
590 ap.seqPanel.ap.idPanel.highlightSearchResults(sr);
592 else if (evt.getClickCount() >= 2)
594 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
595 SequenceGroup sg = new SequenceGroup();
596 sg.addSequence(aa[selectedRow].sequenceRef, false);
597 ap.av.setSelectionGroup(sg);
598 ap.paintAlignment(false);
599 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
600 ap.av.sendSelection();
615 protected void copy_annotseqtoclipboard(SequenceI sq)
617 if (sq == null || sq.getLength() < 1)
621 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
622 jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t"
623 + sq.getStart() + "\t" + sq.getEnd() + "\t"
624 + sq.getSequenceAsString() + "\n");
625 if (av.hasHiddenColumns())
627 jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector();
628 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
630 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
633 jalview.appletgui.AlignFrame.copiedHiddenColumns
634 .addElement(new int[]
635 { region[0], region[1] });
640 public void update(Graphics g)
645 public void paint(Graphics g)
647 int w = getSize().width;
648 int h = getSize().height;
649 if (image == null || w != image.getWidth(this) || h!=image.getHeight(this) )
651 image = createImage(w, ap.annotationPanel.getSize().height);
654 drawComponent(image.getGraphics(), w);
655 g.drawImage(image, 0, 0, this);
658 public void drawComponent(Graphics g, int width)
660 g.setFont(av.getFont());
661 FontMetrics fm = g.getFontMetrics(av.getFont());
662 g.setColor(Color.white);
663 g.fillRect(0, 0, getSize().width, getSize().height);
665 g.translate(0, -scrollOffset);
666 g.setColor(Color.black);
668 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
669 int y = 0, fy = g.getFont().getSize();
674 hasHiddenRows = false;
675 for (int i = 0; i < aa.length; i++)
679 hasHiddenRows = true;
683 x = width - fm.stringWidth(aa[i].label) - 3;
686 offset = -(aa[i].height - fy) / 2;
688 g.drawString(aa[i].label, x, y + offset);
691 g.translate(0, +scrollOffset);
694 g.setColor(Color.red);
696 g.drawLine(2, 8, 5, 2);
697 g.drawLine(5, 2, 8, 8);
699 else if (!dragCancelled && dragEvent != null && aa != null)
701 g.setColor(Color.lightGray);
702 g.drawString(aa[selectedRow].label, dragEvent.getX(),
706 if ((aa == null) || (aa.length < 1))
708 g.setColor(Color.black);
709 g.drawString("Right click", 2, 8);
710 g.drawString("to add annotation", 2, 18);