2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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)
105 * @return -2 if no rows are visible at all, -1 if no visible rows were
108 int getSelectedRow(int y)
111 AlignmentAnnotation[] aa = ap.av.getAlignment()
112 .getAlignmentAnnotation();
119 for (int i = 0; i < aa.length; i++)
126 height += aa[i].height;
137 public void actionPerformed(ActionEvent evt)
139 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
141 if (evt.getActionCommand().equals(ADDNEW))
143 AlignmentAnnotation newAnnotation = new AlignmentAnnotation("", null,
144 new Annotation[ap.av.getAlignment().getWidth()]);
146 if (!editLabelDescription(newAnnotation))
151 ap.av.getAlignment().addAnnotation(newAnnotation);
152 ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
154 else if (evt.getActionCommand().equals(EDITNAME))
156 editLabelDescription(aa[selectedRow]);
158 else if (evt.getActionCommand().equals(HIDE))
160 aa[selectedRow].visible = false;
162 else if (evt.getActionCommand().equals(SHOWALL))
164 for (int i = 0; i < aa.length; i++)
166 aa[i].visible = (aa[i].annotations == null) ? false : true;
169 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
171 CutAndPasteTransfer cap = new CutAndPasteTransfer(false,
173 Frame frame = new Frame();
175 jalview.bin.JalviewLite.addFrame(frame, ap.alignFrame.getTitle()
176 + " - " + aa[selectedRow].label, 500, 100);
177 cap.setText(aa[selectedRow].toString());
179 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
181 SequenceI cons = av.getConsensusSeq();
184 copy_annotseqtoclipboard(cons);
188 ap.annotationPanel.adjustPanelHeight();
189 setSize(getSize().width, ap.annotationPanel.getSize().height);
191 ap.paintAlignment(true);
194 boolean editLabelDescription(AlignmentAnnotation annotation)
196 Checkbox padGaps = new Checkbox("Fill Empty Gaps With \""
197 + ap.av.getGapCharacter() + "\"", annotation.padGaps);
199 EditNameDialog dialog = new EditNameDialog(annotation.label,
200 annotation.description, " Annotation Label",
201 "Annotation Description", ap.alignFrame,
202 "Edit Annotation Name / Description", 500, 180, false);
204 Panel empty = new Panel(new FlowLayout());
209 dialog.setVisible(true);
213 annotation.label = dialog.getName();
214 annotation.description = dialog.getDescription();
215 annotation.setPadGaps(padGaps.getState(), av.getGapCharacter());
224 boolean resizePanel = false;
226 public void mouseMoved(MouseEvent evt)
228 resizePanel = evt.getY() < 10 && evt.getX() < 14;
229 int row = getSelectedRow(evt.getY() + scrollOffset);
233 ParseHtmlBodyAndLinks phb = new ParseHtmlBodyAndLinks(
234 av.getAlignment().getAlignmentAnnotation()[row]
235 .getDescription(true),
239 tooltip = new Tooltip(phb.getNonHtmlContent(), this);
243 tooltip.setTip(phb.getNonHtmlContent());
246 else if (tooltip != null)
253 * curent drag position
255 MouseEvent dragEvent = null;
258 * flag to indicate drag events should be ignored
260 private boolean dragCancelled = false;
263 * clear any drag events in progress
265 public void cancelDrag()
268 dragCancelled = true;
271 public void mouseDragged(MouseEvent evt)
282 Dimension d = ap.annotationPanelHolder.getSize(), e = ap.annotationSpaceFillerHolder
283 .getSize(), f = ap.seqPanelHolder.getSize();
284 int dif = evt.getY() - oldY;
286 dif /= ap.av.charHeight;
287 dif *= ap.av.charHeight;
289 if ((d.height - dif) > 20 && (f.height + dif) > 20)
291 ap.annotationPanel.setSize(d.width, d.height - dif);
292 setSize(new Dimension(e.width, d.height - dif));
293 ap.annotationSpaceFillerHolder.setSize(new Dimension(e.width,
295 ap.annotationPanelHolder.setSize(new Dimension(d.width, d.height
297 ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height - dif, 0,
298 av.calcPanelHeight());
300 ap.seqPanelHolder.setPreferredSize(f);
301 ap.setScrollValues(av.getStartRes(), av.getStartSeq());
303 // ap.paintAlignment(true);
311 if ((diff = 6 - evt.getY()) > 0)
314 ap.apvscroll.setValue(ap.apvscroll.getValue() - diff);
315 ap.adjustmentValueChanged(null);
318 else if ((0 < (diff = 6
319 - ap.annotationSpaceFillerHolder.getSize().height
323 ap.apvscroll.setValue(ap.apvscroll.getValue() + diff);
324 ap.adjustmentValueChanged(null);
330 public void mouseClicked(MouseEvent evt)
334 public void mouseReleased(MouseEvent evt)
336 if (!resizePanel && !dragCancelled)
338 int start = selectedRow;
340 int end = getSelectedRow(evt.getY() + scrollOffset);
342 if (start > -1 && start != end)
344 // Swap these annotations
345 AlignmentAnnotation startAA = ap.av.getAlignment()
346 .getAlignmentAnnotation()[start];
349 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
351 AlignmentAnnotation endAA = ap.av.getAlignment()
352 .getAlignmentAnnotation()[end];
354 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
355 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
360 dragCancelled = false;
362 ap.annotationPanel.repaint();
365 public void mouseEntered(MouseEvent evt)
367 if (evt.getY() < 10 && evt.getX() < 14)
374 public void mouseExited(MouseEvent evt)
376 dragCancelled = false;
378 if (dragEvent == null)
392 public void mousePressed(MouseEvent evt)
399 dragCancelled = false;
400 // todo: move below to mouseClicked ?
401 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
403 AlignmentAnnotation[] aa = ap.av.getAlignment()
404 .getAlignmentAnnotation();
406 // DETECT RIGHT MOUSE BUTTON IN AWT
407 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
410 PopupMenu popup = new PopupMenu(MessageManager.getString("label.annotations"));
412 MenuItem item = new MenuItem(ADDNEW);
413 item.addActionListener(this);
417 // this never happens at moment: - see comment on JAL-563
420 item = new MenuItem(SHOWALL);
421 item.addActionListener(this);
425 popup.show(this, evt.getX(), evt.getY());
428 // add the rest if there are actually rows to show
429 item = new MenuItem(EDITNAME);
430 item.addActionListener(this);
432 item = new MenuItem(HIDE);
433 item.addActionListener(this);
437 item = new MenuItem(SHOWALL);
438 item.addActionListener(this);
442 item = new MenuItem(OUTPUT_TEXT);
443 item.addActionListener(this);
445 if (selectedRow < aa.length)
447 if (aa[selectedRow].autoCalculated)
449 if (aa[selectedRow].label.indexOf("Consensus") > -1)
451 popup.addSeparator();
452 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
453 "Ignore Gaps In Consensus",
454 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
455 .getIgnoreGapsConsensus() : ap.av
456 .getIgnoreGapsConsensus());
457 final AlignmentAnnotation aaa = aa[selectedRow];
458 cbmi.addItemListener(new ItemListener()
460 public void itemStateChanged(ItemEvent e)
462 if (aaa.groupRef != null)
464 // TODO: pass on reference to ap so the view can be updated.
465 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
469 ap.av.setIgnoreGapsConsensus(cbmi.getState());
471 ap.paintAlignment(true);
475 if (aaa.groupRef != null)
477 final CheckboxMenuItem chist = new CheckboxMenuItem(
478 "Show Group Histogram",
479 aa[selectedRow].groupRef.isShowConsensusHistogram());
480 chist.addItemListener(new ItemListener()
482 public void itemStateChanged(ItemEvent e)
484 // TODO: pass on reference
490 aaa.groupRef.setShowConsensusHistogram(chist.getState());
492 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
496 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
498 aa[selectedRow].groupRef.isShowSequenceLogo());
499 cprofl.addItemListener(new ItemListener()
501 public void itemStateChanged(ItemEvent e)
503 // TODO: pass on reference
509 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
511 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
516 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
517 "Normalise Group Logo",
518 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
519 cprofn.addItemListener(new ItemListener()
521 public void itemStateChanged(ItemEvent e)
523 // TODO: pass on reference
529 aaa.groupRef.setshowSequenceLogo(true);
530 aaa.groupRef.setNormaliseSequenceLogo(cprofn.getState());
532 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
539 final CheckboxMenuItem chist = new CheckboxMenuItem(
540 "Show Histogram", av.isShowConsensusHistogram());
541 chist.addItemListener(new ItemListener()
543 public void itemStateChanged(ItemEvent e)
545 // TODO: pass on reference
551 av.setShowConsensusHistogram(chist.getState());
552 ap.alignFrame.showConsensusHistogram.setState(chist
553 .getState()); // TODO: implement
554 // ap.updateGUI()/alignFrame.updateGUI
557 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
561 final CheckboxMenuItem cprof = new CheckboxMenuItem(
562 "Show Logo", av.isShowSequenceLogo());
563 cprof.addItemListener(new ItemListener()
565 public void itemStateChanged(ItemEvent e)
567 // TODO: pass on reference
573 av.setShowSequenceLogo(cprof.getState());
574 ap.alignFrame.showSequenceLogo.setState(cprof.getState()); // TODO:
576 // ap.updateGUI()/alignFrame.updateGUI
580 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
584 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
585 "Normalise Logo", av.isNormaliseSequenceLogo());
586 cprofn.addItemListener(new ItemListener()
588 public void itemStateChanged(ItemEvent e)
590 // TODO: pass on reference
596 av.setShowSequenceLogo(true);
597 ap.alignFrame.normSequenceLogo.setState(cprofn.getState()); // TODO:
599 // ap.updateGUI()/alignFrame.updateGUI
602 av.setNormaliseSequenceLogo(cprofn.getState());
604 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
610 item = new MenuItem(COPYCONS_SEQ);
611 item.addActionListener(this);
616 popup.show(this, evt.getX(), evt.getY());
621 if (selectedRow > -1 && selectedRow < aa.length)
623 if (aa[selectedRow].groupRef != null)
625 if (evt.getClickCount() >= 2)
627 // todo: make the ap scroll to the selection - not necessary, first
628 // click highlights/scrolls, second selects
629 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
630 ap.av.setSelectionGroup(// new SequenceGroup(
631 aa[selectedRow].groupRef); // );
632 ap.av.sendSelection();
633 ap.paintAlignment(false);
634 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
638 ap.seqPanel.ap.idPanel
639 .highlightSearchResults(aa[selectedRow].groupRef
640 .getSequences(null));
644 else if (aa[selectedRow].sequenceRef != null)
646 Vector sr = new Vector();
647 sr.addElement(aa[selectedRow].sequenceRef);
648 if (evt.getClickCount() == 1)
650 ap.seqPanel.ap.idPanel.highlightSearchResults(sr);
652 else if (evt.getClickCount() >= 2)
654 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
655 SequenceGroup sg = new SequenceGroup();
656 sg.addSequence(aa[selectedRow].sequenceRef, false);
657 ap.av.setSelectionGroup(sg);
658 ap.paintAlignment(false);
659 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
660 ap.av.sendSelection();
675 protected void copy_annotseqtoclipboard(SequenceI sq)
677 if (sq == null || sq.getLength() < 1)
681 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
682 jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t"
683 + sq.getStart() + "\t" + sq.getEnd() + "\t"
684 + sq.getSequenceAsString() + "\n");
685 if (av.hasHiddenColumns())
687 jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector();
688 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
690 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
693 jalview.appletgui.AlignFrame.copiedHiddenColumns
694 .addElement(new int[]
695 { region[0], region[1] });
700 public void update(Graphics g)
705 public void paint(Graphics g)
707 int w = getSize().width;
708 int h = getSize().height;
709 if (image == null || w != image.getWidth(this)
710 || h != image.getHeight(this))
712 image = createImage(w, ap.annotationPanel.getSize().height);
715 drawComponent(image.getGraphics(), w);
716 g.drawImage(image, 0, 0, this);
719 public void drawComponent(Graphics g, int width)
721 g.setFont(av.getFont());
722 FontMetrics fm = g.getFontMetrics(av.getFont());
723 g.setColor(Color.white);
724 g.fillRect(0, 0, getSize().width, getSize().height);
726 g.translate(0, -scrollOffset);
727 g.setColor(Color.black);
729 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
730 int y = 0, fy = g.getFont().getSize();
735 hasHiddenRows = false;
736 for (int i = 0; i < aa.length; i++)
740 hasHiddenRows = true;
744 x = width - fm.stringWidth(aa[i].label) - 3;
747 offset = -(aa[i].height - fy) / 2;
749 g.drawString(aa[i].label, x, y + offset);
752 g.translate(0, +scrollOffset);
755 g.setColor(Color.red);
757 g.drawLine(2, 8, 5, 2);
758 g.drawLine(5, 2, 8, 8);
760 else if (!dragCancelled && dragEvent != null && aa != null)
762 g.setColor(Color.lightGray);
763 g.drawString(aa[selectedRow].label, dragEvent.getX(),
767 if (!av.wrapAlignment && ((aa == null) || (aa.length < 1)))
769 g.setColor(Color.black);
770 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
771 g.drawString(MessageManager.getString("label.to_add_annotation"), 2, 18);