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;
27 import jalview.datamodel.*;
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.alignment.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.alignment.getAlignmentAnnotation();
139 if (evt.getActionCommand().equals(ADDNEW))
141 AlignmentAnnotation newAnnotation = new AlignmentAnnotation("", null,
142 new Annotation[ap.av.alignment.getWidth()]);
144 if (!editLabelDescription(newAnnotation))
149 ap.av.alignment.addAnnotation(newAnnotation);
150 ap.av.alignment.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;
228 int row = getSelectedRow(evt.getY() + scrollOffset);
234 tooltip = new Tooltip(
235 ap.av.alignment.getAlignmentAnnotation()[row]
236 .getDescription(true),
241 tooltip.setTip(ap.av.alignment.getAlignmentAnnotation()[row]
242 .getDescription(true));
245 else if (tooltip != null)
252 MouseEvent dragEvent = null;
254 public void mouseDragged(MouseEvent evt)
260 Dimension d = ap.annotationPanelHolder.getSize(), e = ap.annotationSpaceFillerHolder
261 .getSize(), f = ap.seqPanelHolder.getSize();
262 int dif = evt.getY() - oldY;
264 dif /= ap.av.charHeight;
265 dif *= ap.av.charHeight;
267 if ((d.height - dif) > 20 && (f.height + dif) > 20)
269 ap.annotationPanel.setSize(d.width, d.height - dif);
270 setSize(new Dimension(e.width, d.height - dif));
271 ap.annotationSpaceFillerHolder.setSize(new Dimension(e.width,
273 ap.annotationPanelHolder.setSize(new Dimension(d.width, d.height
275 ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height - dif, 0,
276 ap.annotationPanel.calcPanelHeight());
278 ap.seqPanelHolder.setPreferredSize(f);
279 ap.setScrollValues(av.getStartRes(), av.getStartSeq());
281 // ap.paintAlignment(true);
292 public void mouseClicked(MouseEvent evt)
296 public void mouseReleased(MouseEvent evt)
301 ap.annotationPanel.repaint();
304 public void mouseEntered(MouseEvent evt)
306 if (evt.getY() < 10 && evt.getX() < 14)
313 public void mouseExited(MouseEvent evt)
316 if (dragEvent == null)
330 public void mousePressed(MouseEvent evt)
337 // todo: move below to mouseClicked ?
338 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
340 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
342 // DETECT RIGHT MOUSE BUTTON IN AWT
343 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
346 PopupMenu popup = new PopupMenu("Annotations");
348 MenuItem item = new MenuItem(ADDNEW);
349 item.addActionListener(this);
353 // this never happens at moment: - see comment on JAL-563
356 item = new MenuItem(SHOWALL);
357 item.addActionListener(this);
361 popup.show(this, evt.getX(), evt.getY());
364 // add the rest if there are actually rows to show
365 item = new MenuItem(EDITNAME);
366 item.addActionListener(this);
368 item = new MenuItem(HIDE);
369 item.addActionListener(this);
373 item = new MenuItem(SHOWALL);
374 item.addActionListener(this);
378 item = new MenuItem(OUTPUT_TEXT);
379 item.addActionListener(this);
381 if (selectedRow < aa.length)
383 if (aa[selectedRow].autoCalculated)
385 if (aa[selectedRow].label.indexOf("Consensus") > -1)
387 popup.addSeparator();
388 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
389 "Ignore Gaps In Consensus",
390 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
391 .getIgnoreGapsConsensus() : ap.av
392 .getIgnoreGapsConsensus());
393 final AlignmentAnnotation aaa = aa[selectedRow];
394 cbmi.addItemListener(new ItemListener()
396 public void itemStateChanged(ItemEvent e)
398 if (aaa.groupRef != null)
400 // TODO: pass on reference to ap so the view can be updated.
401 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
405 ap.av.setIgnoreGapsConsensus(cbmi.getState());
407 ap.paintAlignment(true);
411 if (aaa.groupRef != null)
413 final CheckboxMenuItem chist = new CheckboxMenuItem(
414 "Show Group Histogram",
415 aa[selectedRow].groupRef.isShowConsensusHistogram());
416 chist.addItemListener(new ItemListener()
418 public void itemStateChanged(ItemEvent e)
420 // TODO: pass on reference
426 aaa.groupRef.setShowConsensusHistogram(chist.getState());
428 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
432 final CheckboxMenuItem cprofl = new CheckboxMenuItem(
434 aa[selectedRow].groupRef.isShowSequenceLogo());
435 cprofl.addItemListener(new ItemListener()
437 public void itemStateChanged(ItemEvent e)
439 // TODO: pass on reference
445 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
447 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
454 final CheckboxMenuItem chist = new CheckboxMenuItem(
455 "Show Histogram", av.isShowConsensusHistogram());
456 chist.addItemListener(new ItemListener()
458 public void itemStateChanged(ItemEvent e)
460 // TODO: pass on reference
466 av.setShowConsensusHistogram(chist.getState());
468 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
472 final CheckboxMenuItem cprof = new CheckboxMenuItem(
473 "Show Logo", av.isShowSequenceLogo());
474 cprof.addItemListener(new ItemListener()
476 public void itemStateChanged(ItemEvent e)
478 // TODO: pass on reference
484 av.setShowSequenceLogo(cprof.getState());
486 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
492 item = new MenuItem(COPYCONS_SEQ);
493 item.addActionListener(this);
498 popup.show(this, evt.getX(), evt.getY());
503 if (selectedRow > -1 && selectedRow < aa.length)
505 if (aa[selectedRow].groupRef != null)
507 if (evt.getClickCount() >= 2)
509 // todo: make the ap scroll to the selection - not necessary, first
510 // click highlights/scrolls, second selects
511 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
512 ap.av.setSelectionGroup(// new SequenceGroup(
513 aa[selectedRow].groupRef); // );
514 ap.av.sendSelection();
515 ap.paintAlignment(false);
516 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
520 ap.seqPanel.ap.idPanel
521 .highlightSearchResults(aa[selectedRow].groupRef
522 .getSequences(null));
526 else if (aa[selectedRow].sequenceRef != null)
528 Vector sr = new Vector();
529 sr.addElement(aa[selectedRow].sequenceRef);
530 if (evt.getClickCount() == 1)
532 ap.seqPanel.ap.idPanel.highlightSearchResults(sr);
534 else if (evt.getClickCount() >= 2)
536 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
537 SequenceGroup sg = new SequenceGroup();
538 sg.addSequence(aa[selectedRow].sequenceRef, false);
539 ap.av.setSelectionGroup(sg);
540 ap.paintAlignment(false);
541 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
542 ap.av.sendSelection();
557 protected void copy_annotseqtoclipboard(SequenceI sq)
559 if (sq == null || sq.getLength() < 1)
563 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
564 jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t"
565 + sq.getStart() + "\t" + sq.getEnd() + "\t"
566 + sq.getSequenceAsString() + "\n");
567 if (av.hasHiddenColumns)
569 jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector();
570 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
572 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
575 jalview.appletgui.AlignFrame.copiedHiddenColumns
576 .addElement(new int[]
577 { region[0], region[1] });
582 public void update(Graphics g)
587 public void paint(Graphics g)
589 int w = getSize().width;
590 if (image == null || w != image.getWidth(this))
592 image = createImage(w, ap.annotationPanel.getSize().height);
595 drawComponent(image.getGraphics(), w);
596 g.drawImage(image, 0, 0, this);
599 public void drawComponent(Graphics g, int width)
601 g.setFont(av.getFont());
602 FontMetrics fm = g.getFontMetrics(av.getFont());
603 g.setColor(Color.white);
604 g.fillRect(0, 0, getSize().width, getSize().height);
606 g.translate(0, -scrollOffset);
607 g.setColor(Color.black);
609 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
610 int y = 0, fy = g.getFont().getSize();
615 hasHiddenRows = false;
616 for (int i = 0; i < aa.length; i++)
620 hasHiddenRows = true;
624 x = width - fm.stringWidth(aa[i].label) - 3;
627 offset = -(aa[i].height - fy) / 2;
629 g.drawString(aa[i].label, x, y + offset);
632 g.translate(0, +scrollOffset);
635 g.setColor(Color.red);
637 g.drawLine(2, 8, 5, 2);
638 g.drawLine(5, 2, 8, 8);
640 else if (dragEvent != null && aa != null)
642 g.setColor(Color.lightGray);
643 g.drawString(aa[selectedRow].label, dragEvent.getX(),
647 if ((aa == null) || (aa.length < 1))
649 g.setColor(Color.black);
650 g.drawString("Right click", 2, 8);
651 g.drawString("to add annotation", 2, 18);