2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 import java.awt.datatransfer.*;
25 import java.awt.event.*;
26 import java.awt.image.*;
29 import jalview.datamodel.*;
38 public class AnnotationLabels extends JPanel implements MouseListener,
39 MouseMotionListener, ActionListener
41 static String ADDNEW = "Add New Row";
43 static String EDITNAME = "Edit Label/Description";
45 static String HIDE = "Hide This Row";
47 static String DELETE = "Delete This Row";
49 static String SHOWALL = "Show All Hidden Rows";
51 static String OUTPUT_TEXT = "Export Annotation";
53 static String COPYCONS_SEQ = "Copy Consensus Sequence";
55 boolean resizePanel = false;
63 boolean resizing = false;
73 Font font = new Font("Arial", Font.PLAIN, 11);
76 * Creates a new AnnotationLabels object.
81 public AnnotationLabels(AlignmentPanel ap)
85 ToolTipManager.sharedInstance().registerComponent(this);
87 java.net.URL url = getClass().getResource("/images/idwidth.gif");
92 temp = java.awt.Toolkit.getDefaultToolkit().createImage(url);
97 MediaTracker mt = new MediaTracker(this);
100 } catch (Exception ex)
104 BufferedImage bi = new BufferedImage(temp.getHeight(this), temp
105 .getWidth(this), BufferedImage.TYPE_INT_RGB);
106 Graphics2D g = (Graphics2D) bi.getGraphics();
107 g.rotate(Math.toRadians(90));
108 g.drawImage(temp, 0, -bi.getWidth(this), this);
111 addMouseListener(this);
112 addMouseMotionListener(this);
115 public AnnotationLabels(AlignViewport av)
126 public void setScrollOffset(int y)
132 void getSelectedRow(int y)
135 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
139 for (int i = 0; i < aa.length; i++)
146 height += aa[i].height;
164 public void actionPerformed(ActionEvent evt)
166 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
168 if (evt.getActionCommand().equals(ADDNEW))
170 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
171 null, new Annotation[ap.av.alignment.getWidth()]);
173 if (!editLabelDescription(newAnnotation))
178 ap.av.alignment.addAnnotation(newAnnotation);
179 ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
181 else if (evt.getActionCommand().equals(EDITNAME))
183 editLabelDescription(aa[selectedRow]);
186 else if (evt.getActionCommand().equals(HIDE))
188 aa[selectedRow].visible = false;
190 if (aa[selectedRow].label.equals("Quality"))
192 ap.av.quality = null;
195 else if (evt.getActionCommand().equals(DELETE))
197 ap.av.alignment.deleteAnnotation(aa[selectedRow]);
199 else if (evt.getActionCommand().equals(SHOWALL))
201 for (int i = 0; i < aa.length; i++)
203 if (!aa[i].visible && aa[i].annotations != null)
205 aa[i].visible = true;
209 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
211 new AnnotationExporter().exportAnnotations(ap,
212 new AlignmentAnnotation[]
213 { aa[selectedRow] }, null, null);
215 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
217 SequenceI cons = av.getConsensusSeq();
220 copy_annotseqtoclipboard(cons);
225 ap.annotationPanel.adjustPanelHeight();
226 ap.annotationScroller.validate();
227 ap.paintAlignment(true);
236 boolean editLabelDescription(AlignmentAnnotation annotation)
238 EditNameDialog dialog = new EditNameDialog(annotation.label,
239 annotation.description, " Annotation Name ",
240 "Annotation Description ", "Edit Annotation Name/Description");
247 annotation.label = dialog.getName();
249 String text = dialog.getDescription();
250 if (text != null && text.length() == 0)
254 annotation.description = text;
265 public void mousePressed(MouseEvent evt)
267 getSelectedRow(evt.getY() - scrollOffset);
277 public void mouseReleased(MouseEvent evt)
279 int start = selectedRow;
280 getSelectedRow(evt.getY() - scrollOffset);
281 int end = selectedRow;
285 // Swap these annotations
286 AlignmentAnnotation startAA = ap.av.alignment
287 .getAlignmentAnnotation()[start];
288 AlignmentAnnotation endAA = ap.av.alignment.getAlignmentAnnotation()[end];
290 ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
291 ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
297 ap.annotationPanel.repaint();
306 public void mouseEntered(MouseEvent evt)
321 public void mouseExited(MouseEvent evt)
323 if (dragEvent == null)
336 public void mouseDragged(MouseEvent evt)
342 Dimension d = ap.annotationScroller.getPreferredSize();
343 int dif = evt.getY() - oldY;
345 dif /= ap.av.charHeight;
346 dif *= ap.av.charHeight;
348 if ((d.height - dif) > 20)
350 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
352 d = ap.annotationSpaceFillerHolder.getPreferredSize();
353 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
354 d.width, d.height - dif));
355 ap.paintAlignment(true);
372 public void mouseMoved(MouseEvent evt)
374 resizePanel = evt.getY() < 10;
376 getSelectedRow(evt.getY() - scrollOffset);
379 && ap.av.alignment.getAlignmentAnnotation().length > selectedRow)
381 AlignmentAnnotation aa = ap.av.alignment.getAlignmentAnnotation()[selectedRow];
383 StringBuffer desc = new StringBuffer("<html>");
385 if (aa.description != null
386 && !aa.description.equals("New description"))
388 desc.append(aa.getDescription(true));
394 desc.append("Score: " + aa.score);
397 if (desc.length() != 6)
399 desc.append("</html>");
400 this.setToolTipText(desc.toString());
403 this.setToolTipText(null);
414 public void mouseClicked(MouseEvent evt)
416 if (!SwingUtilities.isRightMouseButton(evt))
421 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
423 JPopupMenu pop = new JPopupMenu("Annotations");
424 JMenuItem item = new JMenuItem(ADDNEW);
425 item.addActionListener(this);
427 if ((aa == null) || (aa.length == 0))
429 item = new JMenuItem(SHOWALL);
430 item.addActionListener(this);
432 pop.show(this, evt.getX(), evt.getY());
437 item = new JMenuItem(EDITNAME);
438 item.addActionListener(this);
440 item = new JMenuItem(HIDE);
441 item.addActionListener(this);
443 item = new JMenuItem(DELETE);
444 item.addActionListener(this);
446 item = new JMenuItem(SHOWALL);
447 item.addActionListener(this);
449 item = new JMenuItem(OUTPUT_TEXT);
450 item.addActionListener(this);
452 // annotation object should be typed
453 if (selectedRow < aa.length && aa[selectedRow] == ap.av.consensus)
456 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
457 "Ignore Gaps In Consensus", ap.av.getIgnoreGapsConsensus());
458 cbmi.addActionListener(new ActionListener()
460 public void actionPerformed(ActionEvent e)
462 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
466 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
467 consclipbrd.addActionListener(this);
468 pop.add(consclipbrd);
471 pop.show(this, evt.getX(), evt.getY());
475 * do a single sequence copy to jalview and the system clipboard
478 * sequence to be copied to clipboard
480 protected void copy_annotseqtoclipboard(SequenceI sq)
482 SequenceI[] seqs = new SequenceI[]
484 String[] omitHidden = null;
485 SequenceI[] dseqs = new SequenceI[]
486 { sq.getDatasetSequence() };
487 if (dseqs[0] == null)
489 dseqs[0] = new Sequence(sq);
490 dseqs[0].setSequence(jalview.analysis.AlignSeq.extractGaps(
491 jalview.util.Comparison.GapChars, sq.getSequenceAsString()));
493 sq.setDatasetSequence(dseqs[0]);
495 Alignment ds = new Alignment(dseqs);
496 if (av.hasHiddenColumns)
498 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0,
499 sq.getLength(), seqs);
502 String output = new FormatAdapter().formatSequences("Fasta", seqs,
505 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
506 new StringSelection(output), Desktop.instance);
508 Vector hiddenColumns = null;
509 if (av.hasHiddenColumns)
511 hiddenColumns = new Vector();
512 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
514 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
517 hiddenColumns.addElement(new int[]
518 { region[0], region[1] });
522 Desktop.jalviewClipboard = new Object[]
523 { seqs, ds, // what is the dataset of a consensus sequence ? need to flag
524 // sequence as special.
534 public void paintComponent(Graphics g)
537 int width = getWidth();
540 width = ap.calculateIdWidth().width + 4;
543 Graphics2D g2 = (Graphics2D) g;
546 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
547 RenderingHints.VALUE_ANTIALIAS_ON);
550 drawComponent(g2, width);
560 public void drawComponent(Graphics g, int width)
562 if (av.getFont().getSize() < 10)
568 g.setFont(av.getFont());
571 FontMetrics fm = g.getFontMetrics(g.getFont());
572 g.setColor(Color.white);
573 g.fillRect(0, 0, getWidth(), getHeight());
575 g.translate(0, scrollOffset);
576 g.setColor(Color.black);
578 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
579 int fontHeight = g.getFont().getSize();
587 for (int i = 0; i < aa.length; i++)
589 g.setColor(Color.black);
598 offset = -aa[i].height / 2;
602 offset += fm.getHeight() / 2;
603 offset -= fm.getDescent();
606 offset += fm.getDescent();
608 x = width - fm.stringWidth(aa[i].label) - 3;
610 if (aa[i].graphGroup > -1)
613 for (int gg = 0; gg < aa.length; gg++)
615 if (aa[gg].graphGroup == aa[i].graphGroup)
621 if (groupSize * (fontHeight + 8) < aa[i].height)
623 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8))) / 2;
626 for (int gg = 0; gg < aa.length; gg++)
628 if (aa[gg].graphGroup == aa[i].graphGroup)
630 x = width - fm.stringWidth(aa[gg].label) - 3;
631 g.drawString(aa[gg].label, x, y - graphExtras);
632 if (aa[gg].annotations[0] != null)
634 g.setColor(aa[gg].annotations[0].colour);
637 g.drawLine(x, y - graphExtras - 3, x
638 + fm.stringWidth(aa[gg].label), y - graphExtras - 3);
640 g.setColor(Color.black);
641 graphExtras += fontHeight + 8;
647 g.drawString(aa[i].label, x, y + offset);
654 g.drawImage(image, 2, 0 - scrollOffset, this);
656 else if (dragEvent != null && aa != null)
658 g.setColor(Color.lightGray);
659 g.drawString(aa[selectedRow].label, dragEvent.getX(), dragEvent
664 if ((aa == null) || (aa.length < 1))
666 g.drawString("Right click", 2, 8);
667 g.drawString("to add annotation", 2, 18);