2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.appletgui;
24 import java.awt.event.*;
26 import jalview.datamodel.*;
27 import jalview.util.MessageManager;
28 import jalview.util.ParseHtmlBodyAndLinks;
30 public class AnnotationLabels extends Panel implements ActionListener,
31 MouseListener, MouseMotionListener
35 boolean active = false;
41 boolean resizing = false;
45 static String ADDNEW = "Add New Row";
47 static String EDITNAME = "Edit Label/Description";
49 static String HIDE = "Hide This Row";
51 static String SHOWALL = "Show All Hidden Rows";
53 static String OUTPUT_TEXT = "Show Values In Textbox";
55 static String COPYCONS_SEQ = "Copy Consensus Sequence";
63 private boolean hasHiddenRows;
65 public AnnotationLabels(AlignmentPanel ap)
72 * this retrieves the adjustable height glyph from resources. we don't use
73 * it at the moment. java.net.URL url =
74 * getClass().getResource("/images/idwidth.gif"); Image temp = null;
76 * if (url != null) { temp =
77 * java.awt.Toolkit.getDefaultToolkit().createImage(url); }
79 * try { MediaTracker mt = new MediaTracker(this); mt.addImage(temp, 0);
80 * mt.waitForID(0); } catch (Exception ex) { }
82 * BufferedImage bi = new BufferedImage(temp.getHeight(this),
83 * temp.getWidth(this), BufferedImage.TYPE_INT_RGB); Graphics2D g =
84 * (Graphics2D) bi.getGraphics(); g.rotate(Math.toRadians(90));
85 * g.drawImage(temp, 0, -bi.getWidth(this), this); image = (Image) bi;
87 addMouseListener(this);
88 addMouseMotionListener(this);
91 public AnnotationLabels(AlignViewport av)
96 public void setScrollOffset(int y, boolean repaint)
108 * @return -2 if no rows are visible at all, -1 if no visible rows were
111 int getSelectedRow(int y)
114 AlignmentAnnotation[] aa = ap.av.getAlignment()
115 .getAlignmentAnnotation();
122 for (int i = 0; i < aa.length; i++)
129 height += aa[i].height;
140 public void actionPerformed(ActionEvent evt)
142 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
144 if (evt.getActionCommand().equals(ADDNEW))
146 AlignmentAnnotation newAnnotation = new AlignmentAnnotation("", null,
147 new Annotation[ap.av.getAlignment().getWidth()]);
149 if (!editLabelDescription(newAnnotation))
154 ap.av.getAlignment().addAnnotation(newAnnotation);
155 ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
157 else if (evt.getActionCommand().equals(EDITNAME))
159 editLabelDescription(aa[selectedRow]);
161 else if (evt.getActionCommand().equals(HIDE))
163 aa[selectedRow].visible = false;
165 else if (evt.getActionCommand().equals(SHOWALL))
167 for (int i = 0; i < aa.length; i++)
169 aa[i].visible = (aa[i].annotations == null) ? false : true;
172 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
174 CutAndPasteTransfer cap = new CutAndPasteTransfer(false,
176 Frame frame = new Frame();
178 jalview.bin.JalviewLite.addFrame(frame, ap.alignFrame.getTitle()
179 + " - " + aa[selectedRow].label, 500, 100);
180 cap.setText(aa[selectedRow].toString());
182 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
184 SequenceI cons = av.getConsensusSeq();
187 copy_annotseqtoclipboard(cons);
191 ap.annotationPanel.adjustPanelHeight();
192 setSize(getSize().width, ap.annotationPanel.getSize().height);
194 ap.paintAlignment(true);
197 boolean editLabelDescription(AlignmentAnnotation annotation)
199 Checkbox padGaps = new Checkbox("Fill Empty Gaps With \""
200 + ap.av.getGapCharacter() + "\"", annotation.padGaps);
202 EditNameDialog dialog = new EditNameDialog(annotation.label,
203 annotation.description, " Annotation Label",
204 "Annotation Description", ap.alignFrame,
205 "Edit Annotation Name / Description", 500, 180, false);
207 Panel empty = new Panel(new FlowLayout());
212 dialog.setVisible(true);
216 annotation.label = dialog.getName();
217 annotation.description = dialog.getDescription();
218 annotation.setPadGaps(padGaps.getState(), av.getGapCharacter());
227 boolean resizePanel = false;
229 public void mouseMoved(MouseEvent evt)
231 resizePanel = evt.getY() < 10 && evt.getX() < 14;
232 int row = getSelectedRow(evt.getY() + scrollOffset);
236 ParseHtmlBodyAndLinks phb = new ParseHtmlBodyAndLinks(
237 av.getAlignment().getAlignmentAnnotation()[row]
238 .getDescription(true),
242 tooltip = new Tooltip(phb.getNonHtmlContent(), this);
246 tooltip.setTip(phb.getNonHtmlContent());
249 else if (tooltip != null)
256 * curent drag position
258 MouseEvent dragEvent = null;
261 * flag to indicate drag events should be ignored
263 private boolean dragCancelled = false;
266 * clear any drag events in progress
268 public void cancelDrag()
271 dragCancelled = true;
274 public void mouseDragged(MouseEvent evt)
285 Dimension d = ap.annotationPanelHolder.getSize(), e = ap.annotationSpaceFillerHolder
286 .getSize(), f = ap.seqPanelHolder.getSize();
287 int dif = evt.getY() - oldY;
289 dif /= ap.av.charHeight;
290 dif *= ap.av.charHeight;
292 if ((d.height - dif) > 20 && (f.height + dif) > 20)
294 ap.annotationPanel.setSize(d.width, d.height - dif);
295 setSize(new Dimension(e.width, d.height - dif));
296 ap.annotationSpaceFillerHolder.setSize(new Dimension(e.width,
298 ap.annotationPanelHolder.setSize(new Dimension(d.width, d.height
300 ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height - dif, 0,
301 av.calcPanelHeight());
303 ap.seqPanelHolder.setPreferredSize(f);
304 ap.setScrollValues(av.getStartRes(), av.getStartSeq());
306 // ap.paintAlignment(true);
314 if ((diff = 6 - evt.getY()) > 0)
317 ap.apvscroll.setValue(ap.apvscroll.getValue() - diff);
318 ap.adjustmentValueChanged(null);
321 else if ((0 < (diff = 6
322 - ap.annotationSpaceFillerHolder.getSize().height
326 ap.apvscroll.setValue(ap.apvscroll.getValue() + diff);
327 ap.adjustmentValueChanged(null);
333 public void mouseClicked(MouseEvent evt)
337 public void mouseReleased(MouseEvent evt)
339 if (!resizePanel && !dragCancelled)
341 int start = selectedRow;
343 int end = getSelectedRow(evt.getY() + scrollOffset);
345 if (start > -1 && start != end)
347 // Swap these annotations
348 AlignmentAnnotation startAA = ap.av.getAlignment()
349 .getAlignmentAnnotation()[start];
352 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
354 AlignmentAnnotation endAA = ap.av.getAlignment()
355 .getAlignmentAnnotation()[end];
357 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
358 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
363 dragCancelled = false;
365 ap.annotationPanel.repaint();
368 public void mouseEntered(MouseEvent evt)
370 if (evt.getY() < 10 && evt.getX() < 14)
377 public void mouseExited(MouseEvent evt)
379 dragCancelled = false;
381 if (dragEvent == null)
395 public void mousePressed(MouseEvent evt)
402 dragCancelled = false;
403 // todo: move below to mouseClicked ?
404 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
406 AlignmentAnnotation[] aa = ap.av.getAlignment()
407 .getAlignmentAnnotation();
409 // DETECT RIGHT MOUSE BUTTON IN AWT
410 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
413 PopupMenu popup = new PopupMenu(MessageManager.getString("label.annotations"));
415 MenuItem item = new MenuItem(ADDNEW);
416 item.addActionListener(this);
420 // this never happens at moment: - see comment on JAL-563
423 item = new MenuItem(SHOWALL);
424 item.addActionListener(this);
428 popup.show(this, evt.getX(), evt.getY());
431 // add the rest if there are actually rows to show
432 item = new MenuItem(EDITNAME);
433 item.addActionListener(this);
435 item = new MenuItem(HIDE);
436 item.addActionListener(this);
440 item = new MenuItem(SHOWALL);
441 item.addActionListener(this);
445 item = new MenuItem(OUTPUT_TEXT);
446 item.addActionListener(this);
448 if (selectedRow < aa.length)
450 if (aa[selectedRow].autoCalculated)
452 if (aa[selectedRow].label.indexOf("Consensus") > -1)
454 popup.addSeparator();
455 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
456 "Ignore Gaps In Consensus",
457 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
458 .getIgnoreGapsConsensus() : ap.av
459 .getIgnoreGapsConsensus());
460 final AlignmentAnnotation aaa = aa[selectedRow];
461 cbmi.addItemListener(new ItemListener()
463 public void itemStateChanged(ItemEvent e)
465 if (aaa.groupRef != null)
467 // TODO: pass on reference to ap so the view can be updated.
468 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
472 ap.av.setIgnoreGapsConsensus(cbmi.getState());
474 ap.paintAlignment(true);
478 if (aaa.groupRef != null)
480 final CheckboxMenuItem chist = new CheckboxMenuItem(
481 "Show Group Histogram",
482 aa[selectedRow].groupRef.isShowConsensusHistogram());
483 chist.addItemListener(new ItemListener()
485 public void itemStateChanged(ItemEvent e)
487 // TODO: pass on reference
493 aaa.groupRef.setShowConsensusHistogram(chist.getState());
495 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
499 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
501 aa[selectedRow].groupRef.isShowSequenceLogo());
502 cprofl.addItemListener(new ItemListener()
504 public void itemStateChanged(ItemEvent e)
506 // TODO: pass on reference
512 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
514 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
519 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
520 "Normalise Group Logo",
521 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
522 cprofn.addItemListener(new ItemListener()
524 public void itemStateChanged(ItemEvent e)
526 // TODO: pass on reference
532 aaa.groupRef.setshowSequenceLogo(true);
533 aaa.groupRef.setNormaliseSequenceLogo(cprofn.getState());
535 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
542 final CheckboxMenuItem chist = new CheckboxMenuItem(
543 "Show Histogram", av.isShowConsensusHistogram());
544 chist.addItemListener(new ItemListener()
546 public void itemStateChanged(ItemEvent e)
548 // TODO: pass on reference
554 av.setShowConsensusHistogram(chist.getState());
555 ap.alignFrame.showConsensusHistogram.setState(chist
556 .getState()); // TODO: implement
557 // ap.updateGUI()/alignFrame.updateGUI
560 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
564 final CheckboxMenuItem cprof = new CheckboxMenuItem(
565 "Show Logo", av.isShowSequenceLogo());
566 cprof.addItemListener(new ItemListener()
568 public void itemStateChanged(ItemEvent e)
570 // TODO: pass on reference
576 av.setShowSequenceLogo(cprof.getState());
577 ap.alignFrame.showSequenceLogo.setState(cprof.getState()); // TODO:
579 // ap.updateGUI()/alignFrame.updateGUI
583 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
587 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
588 "Normalise Logo", av.isNormaliseSequenceLogo());
589 cprofn.addItemListener(new ItemListener()
591 public void itemStateChanged(ItemEvent e)
593 // TODO: pass on reference
599 av.setShowSequenceLogo(true);
600 ap.alignFrame.normSequenceLogo.setState(cprofn.getState()); // TODO:
602 // ap.updateGUI()/alignFrame.updateGUI
605 av.setNormaliseSequenceLogo(cprofn.getState());
607 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
613 item = new MenuItem(COPYCONS_SEQ);
614 item.addActionListener(this);
619 popup.show(this, evt.getX(), evt.getY());
624 if (selectedRow > -1 && selectedRow < aa.length)
626 if (aa[selectedRow].groupRef != null)
628 if (evt.getClickCount() >= 2)
630 // todo: make the ap scroll to the selection - not necessary, first
631 // click highlights/scrolls, second selects
632 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
633 ap.av.setSelectionGroup(// new SequenceGroup(
634 aa[selectedRow].groupRef); // );
635 ap.av.sendSelection();
636 ap.paintAlignment(false);
637 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
641 ap.seqPanel.ap.idPanel
642 .highlightSearchResults(aa[selectedRow].groupRef
643 .getSequences(null));
647 else if (aa[selectedRow].sequenceRef != null)
649 Vector sr = new Vector();
650 sr.addElement(aa[selectedRow].sequenceRef);
651 if (evt.getClickCount() == 1)
653 ap.seqPanel.ap.idPanel.highlightSearchResults(sr);
655 else if (evt.getClickCount() >= 2)
657 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
658 SequenceGroup sg = new SequenceGroup();
659 sg.addSequence(aa[selectedRow].sequenceRef, false);
660 ap.av.setSelectionGroup(sg);
661 ap.paintAlignment(false);
662 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
663 ap.av.sendSelection();
678 protected void copy_annotseqtoclipboard(SequenceI sq)
680 if (sq == null || sq.getLength() < 1)
684 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
685 jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t"
686 + sq.getStart() + "\t" + sq.getEnd() + "\t"
687 + sq.getSequenceAsString() + "\n");
688 if (av.hasHiddenColumns())
690 jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector();
691 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
693 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
696 jalview.appletgui.AlignFrame.copiedHiddenColumns
697 .addElement(new int[]
698 { region[0], region[1] });
703 public void update(Graphics g)
708 public void paint(Graphics g)
710 int w = getSize().width;
711 int h = getSize().height;
712 if (image == null || w != image.getWidth(this)
713 || h != image.getHeight(this))
715 image = createImage(w, ap.annotationPanel.getSize().height);
718 drawComponent(image.getGraphics(), w);
719 g.drawImage(image, 0, 0, this);
722 public void drawComponent(Graphics g, int width)
724 g.setFont(av.getFont());
725 FontMetrics fm = g.getFontMetrics(av.getFont());
726 g.setColor(Color.white);
727 g.fillRect(0, 0, getSize().width, getSize().height);
729 g.translate(0, -scrollOffset);
730 g.setColor(Color.black);
732 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
733 int y = 0, fy = g.getFont().getSize();
738 hasHiddenRows = false;
739 for (int i = 0; i < aa.length; i++)
743 hasHiddenRows = true;
747 x = width - fm.stringWidth(aa[i].label) - 3;
750 offset = -(aa[i].height - fy) / 2;
752 g.drawString(aa[i].label, x, y + offset);
755 g.translate(0, +scrollOffset);
758 g.setColor(Color.red);
760 g.drawLine(2, 8, 5, 2);
761 g.drawLine(5, 2, 8, 8);
763 else if (!dragCancelled && dragEvent != null && aa != null)
765 g.setColor(Color.lightGray);
766 g.drawString(aa[selectedRow].label, dragEvent.getX(),
770 if (!av.wrapAlignment && ((aa == null) || (aa.length < 1)))
772 g.setColor(Color.black);
773 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
774 g.drawString(MessageManager.getString("label.to_add_annotation"), 2, 18);