2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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.*;
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.alignment.getAlignmentAnnotation();
116 for (int i = 0; i < aa.length; i++)
123 height += aa[i].height;
134 public void actionPerformed(ActionEvent evt)
136 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
138 if (evt.getActionCommand().equals(ADDNEW))
140 AlignmentAnnotation newAnnotation = new AlignmentAnnotation("", null,
141 new Annotation[ap.av.alignment.getWidth()]);
143 if (!editLabelDescription(newAnnotation))
148 ap.av.alignment.addAnnotation(newAnnotation);
149 ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
151 else if (evt.getActionCommand().equals(EDITNAME))
153 editLabelDescription(aa[selectedRow]);
155 else if (evt.getActionCommand().equals(HIDE))
157 aa[selectedRow].visible = false;
159 else if (evt.getActionCommand().equals(SHOWALL))
161 for (int i = 0; i < aa.length; i++)
163 aa[i].visible = (aa[i].annotations == null) ? false : true;
166 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
168 CutAndPasteTransfer cap = new CutAndPasteTransfer(false,
170 Frame frame = new Frame();
172 jalview.bin.JalviewLite.addFrame(frame, ap.alignFrame.getTitle()
173 + " - " + aa[selectedRow].label, 500, 100);
174 cap.setText(aa[selectedRow].toString());
176 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
178 SequenceI cons = av.getConsensusSeq();
181 copy_annotseqtoclipboard(cons);
185 ap.annotationPanel.adjustPanelHeight();
186 setSize(getSize().width, ap.annotationPanel.getSize().height);
188 ap.paintAlignment(true);
191 boolean editLabelDescription(AlignmentAnnotation annotation)
193 Checkbox padGaps = new Checkbox("Fill Empty Gaps With \""
194 + ap.av.getGapCharacter() + "\"", annotation.padGaps);
196 EditNameDialog dialog = new EditNameDialog(annotation.label,
197 annotation.description, " Annotation Label",
198 "Annotation Description", ap.alignFrame,
199 "Edit Annotation Name / Description", 500, 180, false);
201 Panel empty = new Panel(new FlowLayout());
206 dialog.setVisible(true);
210 annotation.label = dialog.getName();
211 annotation.description = dialog.getDescription();
212 annotation.setPadGaps(padGaps.getState(), av.getGapCharacter());
221 boolean resizePanel = false;
223 public void mouseMoved(MouseEvent evt)
225 resizePanel = evt.getY() < 10 && evt.getX() < 14;
226 int row = getSelectedRow(evt.getY() + scrollOffset);
232 tooltip = new Tooltip(
233 ap.av.alignment.getAlignmentAnnotation()[row]
234 .getDescription(true),
239 tooltip.setTip(ap.av.alignment.getAlignmentAnnotation()[row]
240 .getDescription(true));
243 else if (tooltip != null)
250 * curent drag position
252 MouseEvent dragEvent = null;
255 * flag to indicate drag events should be ignored
257 private boolean dragCancelled = false;
260 * clear any drag events in progress
262 public void cancelDrag()
265 dragCancelled = true;
268 public void mouseDragged(MouseEvent evt)
279 Dimension d = ap.annotationPanelHolder.getSize(), e = ap.annotationSpaceFillerHolder
280 .getSize(), f = ap.seqPanelHolder.getSize();
281 int dif = evt.getY() - oldY;
283 dif /= ap.av.charHeight;
284 dif *= ap.av.charHeight;
286 if ((d.height - dif) > 20 && (f.height + dif) > 20)
288 ap.annotationPanel.setSize(d.width, d.height - dif);
289 setSize(new Dimension(e.width, d.height - dif));
290 ap.annotationSpaceFillerHolder.setSize(new Dimension(e.width,
292 ap.annotationPanelHolder.setSize(new Dimension(d.width, d.height
294 ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height - dif, 0,
295 ap.annotationPanel.calcPanelHeight());
297 ap.seqPanelHolder.setPreferredSize(f);
298 ap.setScrollValues(av.getStartRes(), av.getStartSeq());
300 // ap.paintAlignment(true);
308 if ((diff = 6 - evt.getY()) > 0)
311 ap.apvscroll.setValue(ap.apvscroll.getValue() - diff);
312 ap.adjustmentValueChanged(null);
315 else if ((0 < (diff = 6
316 - ap.annotationSpaceFillerHolder.getSize().height
320 ap.apvscroll.setValue(ap.apvscroll.getValue() + diff);
321 ap.adjustmentValueChanged(null);
327 public void mouseClicked(MouseEvent evt)
331 public void mouseReleased(MouseEvent evt)
333 if (!resizePanel && !dragCancelled)
335 int start = selectedRow;
337 int end = getSelectedRow(evt.getY() + scrollOffset);
339 if (start>-1 && start != end)
341 // Swap these annotations
342 AlignmentAnnotation startAA = ap.av.alignment
343 .getAlignmentAnnotation()[start];
346 end = ap.av.alignment.getAlignmentAnnotation().length - 1;
348 AlignmentAnnotation endAA = ap.av.alignment
349 .getAlignmentAnnotation()[end];
351 ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
352 ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
357 dragCancelled = false;
359 ap.annotationPanel.repaint();
362 public void mouseEntered(MouseEvent evt)
364 if (evt.getY() < 10 && evt.getX() < 14)
371 public void mouseExited(MouseEvent evt)
373 dragCancelled = false;
375 if (dragEvent == null)
389 public void mousePressed(MouseEvent evt)
397 // todo: move below to mouseClicked ?
398 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
400 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
402 // DETECT RIGHT MOUSE BUTTON IN AWT
403 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
406 PopupMenu popup = new PopupMenu("Annotations");
408 MenuItem item = new MenuItem(ADDNEW);
409 item.addActionListener(this);
413 // this never happens at moment: - see comment on JAL-563
416 item = new MenuItem(SHOWALL);
417 item.addActionListener(this);
421 popup.show(this, evt.getX(), evt.getY());
424 // add the rest if there are actually rows to show
425 item = new MenuItem(EDITNAME);
426 item.addActionListener(this);
428 item = new MenuItem(HIDE);
429 item.addActionListener(this);
433 item = new MenuItem(SHOWALL);
434 item.addActionListener(this);
438 item = new MenuItem(OUTPUT_TEXT);
439 item.addActionListener(this);
441 if (selectedRow < aa.length)
443 if (aa[selectedRow].autoCalculated)
445 if (aa[selectedRow].label.indexOf("Consensus") > -1)
447 popup.addSeparator();
448 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
449 "Ignore Gaps In Consensus",
450 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
451 .getIgnoreGapsConsensus() : ap.av
452 .getIgnoreGapsConsensus());
453 final AlignmentAnnotation aaa = aa[selectedRow];
454 cbmi.addItemListener(new ItemListener()
456 public void itemStateChanged(ItemEvent e)
458 if (aaa.groupRef != null)
460 // TODO: pass on reference to ap so the view can be updated.
461 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
465 ap.av.setIgnoreGapsConsensus(cbmi.getState());
467 ap.paintAlignment(true);
471 if (aaa.groupRef != null)
473 final CheckboxMenuItem chist = new CheckboxMenuItem(
474 "Show Group Histogram",
475 aa[selectedRow].groupRef.isShowConsensusHistogram());
476 chist.addItemListener(new ItemListener()
478 public void itemStateChanged(ItemEvent e)
480 // TODO: pass on reference
486 aaa.groupRef.setShowConsensusHistogram(chist.getState());
488 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
492 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
494 aa[selectedRow].groupRef.isShowSequenceLogo());
495 cprofl.addItemListener(new ItemListener()
497 public void itemStateChanged(ItemEvent e)
499 // TODO: pass on reference
505 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
507 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
514 final CheckboxMenuItem chist = new CheckboxMenuItem(
515 "Show Histogram", av.isShowConsensusHistogram());
516 chist.addItemListener(new ItemListener()
518 public void itemStateChanged(ItemEvent e)
520 // TODO: pass on reference
526 av.setShowConsensusHistogram(chist.getState());
528 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
532 final CheckboxMenuItem cprof = new CheckboxMenuItem(
533 "Show Logo", av.isShowSequenceLogo());
534 cprof.addItemListener(new ItemListener()
536 public void itemStateChanged(ItemEvent e)
538 // TODO: pass on reference
544 av.setShowSequenceLogo(cprof.getState());
546 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
552 item = new MenuItem(COPYCONS_SEQ);
553 item.addActionListener(this);
558 popup.show(this, evt.getX(), evt.getY());
563 if (selectedRow > -1 && selectedRow < aa.length)
565 if (aa[selectedRow].groupRef != null)
567 if (evt.getClickCount() >= 2)
569 // todo: make the ap scroll to the selection - not necessary, first
570 // click highlights/scrolls, second selects
571 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
572 ap.av.setSelectionGroup(// new SequenceGroup(
573 aa[selectedRow].groupRef); // );
574 ap.av.sendSelection();
575 ap.paintAlignment(false);
576 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
580 ap.seqPanel.ap.idPanel
581 .highlightSearchResults(aa[selectedRow].groupRef
582 .getSequences(null));
586 else if (aa[selectedRow].sequenceRef != null)
588 Vector sr = new Vector();
589 sr.addElement(aa[selectedRow].sequenceRef);
590 if (evt.getClickCount() == 1)
592 ap.seqPanel.ap.idPanel.highlightSearchResults(sr);
594 else if (evt.getClickCount() >= 2)
596 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
597 SequenceGroup sg = new SequenceGroup();
598 sg.addSequence(aa[selectedRow].sequenceRef, false);
599 ap.av.setSelectionGroup(sg);
600 ap.paintAlignment(false);
601 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
602 ap.av.sendSelection();
617 protected void copy_annotseqtoclipboard(SequenceI sq)
619 if (sq == null || sq.getLength() < 1)
623 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
624 jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t"
625 + sq.getStart() + "\t" + sq.getEnd() + "\t"
626 + sq.getSequenceAsString() + "\n");
627 if (av.hasHiddenColumns)
629 jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector();
630 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
632 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
635 jalview.appletgui.AlignFrame.copiedHiddenColumns
636 .addElement(new int[]
637 { region[0], region[1] });
642 public void update(Graphics g)
647 public void paint(Graphics g)
649 int w = getSize().width;
650 int h = getSize().height;
651 if (image == null || w != image.getWidth(this) || h!=image.getHeight(this) )
653 image = createImage(w, ap.annotationPanel.getSize().height);
656 drawComponent(image.getGraphics(), w);
657 g.drawImage(image, 0, 0, this);
660 public void drawComponent(Graphics g, int width)
662 g.setFont(av.getFont());
663 FontMetrics fm = g.getFontMetrics(av.getFont());
664 g.setColor(Color.white);
665 g.fillRect(0, 0, getSize().width, getSize().height);
667 g.translate(0, -scrollOffset);
668 g.setColor(Color.black);
670 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
671 int y = 0, fy = g.getFont().getSize();
676 hasHiddenRows = false;
677 for (int i = 0; i < aa.length; i++)
681 hasHiddenRows = true;
685 x = width - fm.stringWidth(aa[i].label) - 3;
688 offset = -(aa[i].height - fy) / 2;
690 g.drawString(aa[i].label, x, y + offset);
693 g.translate(0, +scrollOffset);
696 g.setColor(Color.red);
698 g.drawLine(2, 8, 5, 2);
699 g.drawLine(5, 2, 8, 8);
701 else if (!dragCancelled && dragEvent != null && aa != null)
703 g.setColor(Color.lightGray);
704 g.drawString(aa[selectedRow].label, dragEvent.getX(),
708 if ((aa == null) || (aa.length < 1))
710 g.setColor(Color.black);
711 g.drawString("Right click", 2, 8);
712 g.drawString("to add annotation", 2, 18);