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.MessageManager;
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()
111 .getAlignmentAnnotation();
118 for (int i = 0; i < aa.length; i++)
125 height += aa[i].height;
136 public void actionPerformed(ActionEvent evt)
138 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
140 if (evt.getActionCommand().equals(ADDNEW))
142 AlignmentAnnotation newAnnotation = new AlignmentAnnotation("", null,
143 new Annotation[ap.av.getAlignment().getWidth()]);
145 if (!editLabelDescription(newAnnotation))
150 ap.av.getAlignment().addAnnotation(newAnnotation);
151 ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
153 else if (evt.getActionCommand().equals(EDITNAME))
155 editLabelDescription(aa[selectedRow]);
157 else if (evt.getActionCommand().equals(HIDE))
159 aa[selectedRow].visible = false;
161 else if (evt.getActionCommand().equals(SHOWALL))
163 for (int i = 0; i < aa.length; i++)
165 aa[i].visible = (aa[i].annotations == null) ? false : true;
168 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
170 CutAndPasteTransfer cap = new CutAndPasteTransfer(false,
172 Frame frame = new Frame();
174 jalview.bin.JalviewLite.addFrame(frame, ap.alignFrame.getTitle()
175 + " - " + aa[selectedRow].label, 500, 100);
176 cap.setText(aa[selectedRow].toString());
178 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
180 SequenceI cons = av.getConsensusSeq();
183 copy_annotseqtoclipboard(cons);
187 ap.annotationPanel.adjustPanelHeight();
188 setSize(getSize().width, ap.annotationPanel.getSize().height);
190 ap.paintAlignment(true);
193 boolean editLabelDescription(AlignmentAnnotation annotation)
195 Checkbox padGaps = new Checkbox("Fill Empty Gaps With \""
196 + ap.av.getGapCharacter() + "\"", annotation.padGaps);
198 EditNameDialog dialog = new EditNameDialog(annotation.label,
199 annotation.description, " Annotation Label",
200 "Annotation Description", ap.alignFrame,
201 "Edit Annotation Name / Description", 500, 180, false);
203 Panel empty = new Panel(new FlowLayout());
208 dialog.setVisible(true);
212 annotation.label = dialog.getName();
213 annotation.description = dialog.getDescription();
214 annotation.setPadGaps(padGaps.getState(), av.getGapCharacter());
223 boolean resizePanel = false;
225 public void mouseMoved(MouseEvent evt)
227 resizePanel = evt.getY() < 10 && evt.getX() < 14;
228 int row = getSelectedRow(evt.getY() + scrollOffset);
232 ParseHtmlBodyAndLinks phb = new ParseHtmlBodyAndLinks(
233 av.getAlignment().getAlignmentAnnotation()[row]
234 .getDescription(true),
238 tooltip = new Tooltip(phb.getNonHtmlContent(), this);
242 tooltip.setTip(phb.getNonHtmlContent());
245 else if (tooltip != null)
252 * curent drag position
254 MouseEvent dragEvent = null;
257 * flag to indicate drag events should be ignored
259 private boolean dragCancelled = false;
262 * clear any drag events in progress
264 public void cancelDrag()
267 dragCancelled = true;
270 public void mouseDragged(MouseEvent evt)
281 Dimension d = ap.annotationPanelHolder.getSize(), e = ap.annotationSpaceFillerHolder
282 .getSize(), f = ap.seqPanelHolder.getSize();
283 int dif = evt.getY() - oldY;
285 dif /= ap.av.charHeight;
286 dif *= ap.av.charHeight;
288 if ((d.height - dif) > 20 && (f.height + dif) > 20)
290 ap.annotationPanel.setSize(d.width, d.height - dif);
291 setSize(new Dimension(e.width, d.height - dif));
292 ap.annotationSpaceFillerHolder.setSize(new Dimension(e.width,
294 ap.annotationPanelHolder.setSize(new Dimension(d.width, d.height
296 ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height - dif, 0,
297 av.calcPanelHeight());
299 ap.seqPanelHolder.setPreferredSize(f);
300 ap.setScrollValues(av.getStartRes(), av.getStartSeq());
302 // ap.paintAlignment(true);
310 if ((diff = 6 - evt.getY()) > 0)
313 ap.apvscroll.setValue(ap.apvscroll.getValue() - diff);
314 ap.adjustmentValueChanged(null);
317 else if ((0 < (diff = 6
318 - ap.annotationSpaceFillerHolder.getSize().height
322 ap.apvscroll.setValue(ap.apvscroll.getValue() + diff);
323 ap.adjustmentValueChanged(null);
329 public void mouseClicked(MouseEvent evt)
333 public void mouseReleased(MouseEvent evt)
335 if (!resizePanel && !dragCancelled)
337 int start = selectedRow;
339 int end = getSelectedRow(evt.getY() + scrollOffset);
341 if (start > -1 && start != end)
343 // Swap these annotations
344 AlignmentAnnotation startAA = ap.av.getAlignment()
345 .getAlignmentAnnotation()[start];
348 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
350 AlignmentAnnotation endAA = ap.av.getAlignment()
351 .getAlignmentAnnotation()[end];
353 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
354 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
359 dragCancelled = false;
361 ap.annotationPanel.repaint();
364 public void mouseEntered(MouseEvent evt)
366 if (evt.getY() < 10 && evt.getX() < 14)
373 public void mouseExited(MouseEvent evt)
375 dragCancelled = false;
377 if (dragEvent == null)
391 public void mousePressed(MouseEvent evt)
398 dragCancelled = false;
399 // todo: move below to mouseClicked ?
400 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
402 AlignmentAnnotation[] aa = ap.av.getAlignment()
403 .getAlignmentAnnotation();
405 // DETECT RIGHT MOUSE BUTTON IN AWT
406 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
409 PopupMenu popup = new PopupMenu(MessageManager.getString("label.annotations"));
411 MenuItem item = new MenuItem(ADDNEW);
412 item.addActionListener(this);
416 // this never happens at moment: - see comment on JAL-563
419 item = new MenuItem(SHOWALL);
420 item.addActionListener(this);
424 popup.show(this, evt.getX(), evt.getY());
427 // add the rest if there are actually rows to show
428 item = new MenuItem(EDITNAME);
429 item.addActionListener(this);
431 item = new MenuItem(HIDE);
432 item.addActionListener(this);
436 item = new MenuItem(SHOWALL);
437 item.addActionListener(this);
441 item = new MenuItem(OUTPUT_TEXT);
442 item.addActionListener(this);
444 if (selectedRow < aa.length)
446 if (aa[selectedRow].autoCalculated)
448 if (aa[selectedRow].label.indexOf("Consensus") > -1)
450 popup.addSeparator();
451 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
452 "Ignore Gaps In Consensus",
453 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
454 .getIgnoreGapsConsensus() : ap.av
455 .getIgnoreGapsConsensus());
456 final AlignmentAnnotation aaa = aa[selectedRow];
457 cbmi.addItemListener(new ItemListener()
459 public void itemStateChanged(ItemEvent e)
461 if (aaa.groupRef != null)
463 // TODO: pass on reference to ap so the view can be updated.
464 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
468 ap.av.setIgnoreGapsConsensus(cbmi.getState());
470 ap.paintAlignment(true);
474 if (aaa.groupRef != null)
476 final CheckboxMenuItem chist = new CheckboxMenuItem(
477 "Show Group Histogram",
478 aa[selectedRow].groupRef.isShowConsensusHistogram());
479 chist.addItemListener(new ItemListener()
481 public void itemStateChanged(ItemEvent e)
483 // TODO: pass on reference
489 aaa.groupRef.setShowConsensusHistogram(chist.getState());
491 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
495 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
497 aa[selectedRow].groupRef.isShowSequenceLogo());
498 cprofl.addItemListener(new ItemListener()
500 public void itemStateChanged(ItemEvent e)
502 // TODO: pass on reference
508 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
510 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
515 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
516 "Normalise Group Logo",
517 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
518 cprofn.addItemListener(new ItemListener()
520 public void itemStateChanged(ItemEvent e)
522 // TODO: pass on reference
528 aaa.groupRef.setshowSequenceLogo(true);
529 aaa.groupRef.setNormaliseSequenceLogo(cprofn.getState());
531 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
538 final CheckboxMenuItem chist = new CheckboxMenuItem(
539 "Show Histogram", av.isShowConsensusHistogram());
540 chist.addItemListener(new ItemListener()
542 public void itemStateChanged(ItemEvent e)
544 // TODO: pass on reference
550 av.setShowConsensusHistogram(chist.getState());
551 ap.alignFrame.showConsensusHistogram.setState(chist
552 .getState()); // TODO: implement
553 // ap.updateGUI()/alignFrame.updateGUI
556 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
560 final CheckboxMenuItem cprof = new CheckboxMenuItem(
561 "Show Logo", av.isShowSequenceLogo());
562 cprof.addItemListener(new ItemListener()
564 public void itemStateChanged(ItemEvent e)
566 // TODO: pass on reference
572 av.setShowSequenceLogo(cprof.getState());
573 ap.alignFrame.showSequenceLogo.setState(cprof.getState()); // TODO:
575 // ap.updateGUI()/alignFrame.updateGUI
579 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
583 final CheckboxMenuItem cprofn = new CheckboxMenuItem(
584 "Normalise Logo", av.isNormaliseSequenceLogo());
585 cprofn.addItemListener(new ItemListener()
587 public void itemStateChanged(ItemEvent e)
589 // TODO: pass on reference
595 av.setShowSequenceLogo(true);
596 ap.alignFrame.normSequenceLogo.setState(cprofn.getState()); // TODO:
598 // ap.updateGUI()/alignFrame.updateGUI
601 av.setNormaliseSequenceLogo(cprofn.getState());
603 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
609 item = new MenuItem(COPYCONS_SEQ);
610 item.addActionListener(this);
615 popup.show(this, evt.getX(), evt.getY());
620 if (selectedRow > -1 && selectedRow < aa.length)
622 if (aa[selectedRow].groupRef != null)
624 if (evt.getClickCount() >= 2)
626 // todo: make the ap scroll to the selection - not necessary, first
627 // click highlights/scrolls, second selects
628 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
629 ap.av.setSelectionGroup(// new SequenceGroup(
630 aa[selectedRow].groupRef); // );
631 ap.av.sendSelection();
632 ap.paintAlignment(false);
633 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
637 ap.seqPanel.ap.idPanel
638 .highlightSearchResults(aa[selectedRow].groupRef
639 .getSequences(null));
643 else if (aa[selectedRow].sequenceRef != null)
645 Vector sr = new Vector();
646 sr.addElement(aa[selectedRow].sequenceRef);
647 if (evt.getClickCount() == 1)
649 ap.seqPanel.ap.idPanel.highlightSearchResults(sr);
651 else if (evt.getClickCount() >= 2)
653 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
654 SequenceGroup sg = new SequenceGroup();
655 sg.addSequence(aa[selectedRow].sequenceRef, false);
656 ap.av.setSelectionGroup(sg);
657 ap.paintAlignment(false);
658 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
659 ap.av.sendSelection();
674 protected void copy_annotseqtoclipboard(SequenceI sq)
676 if (sq == null || sq.getLength() < 1)
680 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
681 jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t"
682 + sq.getStart() + "\t" + sq.getEnd() + "\t"
683 + sq.getSequenceAsString() + "\n");
684 if (av.hasHiddenColumns())
686 jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector();
687 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
689 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
692 jalview.appletgui.AlignFrame.copiedHiddenColumns
693 .addElement(new int[]
694 { region[0], region[1] });
699 public void update(Graphics g)
704 public void paint(Graphics g)
706 int w = getSize().width;
707 int h = getSize().height;
708 if (image == null || w != image.getWidth(this)
709 || h != image.getHeight(this))
711 image = createImage(w, ap.annotationPanel.getSize().height);
714 drawComponent(image.getGraphics(), w);
715 g.drawImage(image, 0, 0, this);
718 public void drawComponent(Graphics g, int width)
720 g.setFont(av.getFont());
721 FontMetrics fm = g.getFontMetrics(av.getFont());
722 g.setColor(Color.white);
723 g.fillRect(0, 0, getSize().width, getSize().height);
725 g.translate(0, -scrollOffset);
726 g.setColor(Color.black);
728 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
729 int y = 0, fy = g.getFont().getSize();
734 hasHiddenRows = false;
735 for (int i = 0; i < aa.length; i++)
739 hasHiddenRows = true;
743 x = width - fm.stringWidth(aa[i].label) - 3;
746 offset = -(aa[i].height - fy) / 2;
748 g.drawString(aa[i].label, x, y + offset);
751 g.translate(0, +scrollOffset);
754 g.setColor(Color.red);
756 g.drawLine(2, 8, 5, 2);
757 g.drawLine(5, 2, 8, 8);
759 else if (!dragCancelled && dragEvent != null && aa != null)
761 g.setColor(Color.lightGray);
762 g.drawString(aa[selectedRow].label, dragEvent.getX(),
766 if (!av.wrapAlignment && ((aa == null) || (aa.length < 1)))
768 g.setColor(Color.black);
769 g.drawString(MessageManager.getString("label.right_click"), 2, 8);
770 g.drawString(MessageManager.getString("label.to_add_annotation"), 2, 18);