2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 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
39 extends JPanel implements MouseListener,
40 MouseMotionListener, ActionListener
42 static String ADDNEW = "Add New Row";
43 static String EDITNAME = "Edit Label/Description";
44 static String HIDE = "Hide This Row";
45 static String DELETE = "Delete This Row";
46 static String SHOWALL = "Show All Hidden Rows";
47 static String OUTPUT_TEXT = "Export Annotation";
48 static String COPYCONS_SEQ = "Copy Consensus Sequence";
49 boolean resizePanel = false;
53 boolean resizing = false;
58 Font font = new Font("Arial", Font.PLAIN, 11);
61 * Creates a new AnnotationLabels object.
63 * @param ap DOCUMENT ME!
65 public AnnotationLabels(AlignmentPanel ap)
69 ToolTipManager.sharedInstance().registerComponent(this);
71 java.net.URL url = getClass().getResource("/images/idwidth.gif");
76 temp = java.awt.Toolkit.getDefaultToolkit().createImage(url);
81 MediaTracker mt = new MediaTracker(this);
89 BufferedImage bi = new BufferedImage(temp.getHeight(this),
91 BufferedImage.TYPE_INT_RGB);
92 Graphics2D g = (Graphics2D) bi.getGraphics();
93 g.rotate(Math.toRadians(90));
94 g.drawImage(temp, 0, -bi.getWidth(this), this);
97 addMouseListener(this);
98 addMouseMotionListener(this);
101 public AnnotationLabels(AlignViewport av)
109 * @param y DOCUMENT ME!
111 public void setScrollOffset(int y)
117 void getSelectedRow(int y)
120 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
124 for (int i = 0; i < aa.length; i++)
131 height += aa[i].height;
146 * @param evt DOCUMENT ME!
148 public void actionPerformed(ActionEvent evt)
150 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
152 if (evt.getActionCommand().equals(ADDNEW))
154 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
156 new Annotation[ap.av.alignment.getWidth()]);
158 if (!editLabelDescription(newAnnotation))
163 ap.av.alignment.addAnnotation(newAnnotation);
164 ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
166 else if (evt.getActionCommand().equals(EDITNAME))
168 editLabelDescription(aa[selectedRow]);
171 else if (evt.getActionCommand().equals(HIDE))
173 aa[selectedRow].visible = false;
175 if (aa[selectedRow].label.equals("Quality"))
177 ap.av.quality = null;
180 else if (evt.getActionCommand().equals(DELETE))
182 ap.av.alignment.deleteAnnotation(aa[selectedRow]);
184 else if (evt.getActionCommand().equals(SHOWALL))
186 for (int i = 0; i < aa.length; i++)
188 if (!aa[i].visible && aa[i].annotations!=null)
190 aa[i].visible = true;
194 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
196 new AnnotationExporter().exportAnnotations(
198 new AlignmentAnnotation[]
203 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
205 SequenceI cons = av.getConsensusSeq();
208 copy_annotseqtoclipboard(cons);
213 ap.annotationPanel.adjustPanelHeight();
214 ap.annotationScroller.validate();
215 ap.paintAlignment(true);
221 * @param e DOCUMENT ME!
223 boolean editLabelDescription(AlignmentAnnotation annotation)
225 EditNameDialog dialog = new EditNameDialog(annotation.label,
226 annotation.description,
228 "Annotation Description ",
229 "Edit Annotation Name/Description");
236 annotation.label = dialog.getName();
238 String text = dialog.getDescription();
239 if (text!=null && text.length() == 0)
243 annotation.description = text;
251 * @param evt DOCUMENT ME!
253 public void mousePressed(MouseEvent evt)
255 getSelectedRow(evt.getY() - scrollOffset);
262 * @param evt DOCUMENT ME!
264 public void mouseReleased(MouseEvent evt)
266 int start = selectedRow;
267 getSelectedRow(evt.getY() - scrollOffset);
268 int end = selectedRow;
272 //Swap these annotations
273 AlignmentAnnotation startAA = ap.av.alignment.getAlignmentAnnotation()[
275 AlignmentAnnotation endAA = ap.av.alignment.getAlignmentAnnotation()[end];
277 ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
278 ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
284 ap.annotationPanel.repaint();
290 * @param evt DOCUMENT ME!
292 public void mouseEntered(MouseEvent evt)
304 * @param evt DOCUMENT ME!
306 public void mouseExited(MouseEvent evt)
308 if (dragEvent == null)
318 * @param evt DOCUMENT ME!
320 public void mouseDragged(MouseEvent evt)
326 Dimension d = ap.annotationScroller.getPreferredSize();
327 int dif = evt.getY() - oldY;
329 dif /= ap.av.charHeight;
330 dif *= ap.av.charHeight;
332 if ( (d.height - dif) > 20)
334 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
336 d = ap.annotationSpaceFillerHolder.getPreferredSize();
337 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
338 d.width, d.height - dif));
339 ap.paintAlignment(true);
353 * @param evt DOCUMENT ME!
355 public void mouseMoved(MouseEvent evt)
357 resizePanel = evt.getY() < 10;
359 getSelectedRow(evt.getY() - scrollOffset);
363 && ap.av.alignment.getAlignmentAnnotation().length > selectedRow)
365 AlignmentAnnotation aa = ap.av.alignment.
366 getAlignmentAnnotation()[selectedRow];
368 StringBuffer desc = new StringBuffer("<html>");
370 if (aa.description != null && !aa.description.equals("New description"))
372 desc.append(aa.description);
378 desc.append("Score: "+aa.score);
383 desc.append("</html>");
384 this.setToolTipText(desc.toString());
387 this.setToolTipText(null);
395 * @param evt DOCUMENT ME!
397 public void mouseClicked(MouseEvent evt)
399 if (!SwingUtilities.isRightMouseButton(evt))
404 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
406 JPopupMenu pop = new JPopupMenu("Annotations");
407 JMenuItem item = new JMenuItem(ADDNEW);
408 item.addActionListener(this);
410 if ( (aa == null) || (aa.length == 0))
412 item = new JMenuItem(SHOWALL);
413 item.addActionListener(this);
415 pop.show(this, evt.getX(), evt.getY());
420 item = new JMenuItem(EDITNAME);
421 item.addActionListener(this);
423 item = new JMenuItem(HIDE);
424 item.addActionListener(this);
426 item = new JMenuItem(DELETE);
427 item.addActionListener(this);
429 item = new JMenuItem(SHOWALL);
430 item.addActionListener(this);
432 item = new JMenuItem(OUTPUT_TEXT);
433 item.addActionListener(this);
435 // annotation object should be typed
436 if (selectedRow<aa.length && aa[selectedRow] == ap.av.consensus)
439 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
440 "Ignore Gaps In Consensus",
441 ap.av.getIgnoreGapsConsensus());
442 cbmi.addActionListener(new ActionListener()
444 public void actionPerformed(ActionEvent e)
446 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
450 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
451 consclipbrd.addActionListener(this);
452 pop.add(consclipbrd);
455 pop.show(this, evt.getX(), evt.getY());
459 * do a single sequence copy to jalview and the system clipboard
461 * @param sq sequence to be copied to clipboard
463 protected void copy_annotseqtoclipboard(SequenceI sq)
465 SequenceI[] seqs = new SequenceI[]
468 String[] omitHidden = null;
469 SequenceI[] dseqs = new SequenceI[]
471 sq.getDatasetSequence()};
472 if (dseqs[0] == null)
474 dseqs[0] = new Sequence(sq);
475 dseqs[0].setSequence(
476 jalview.analysis.AlignSeq.extractGaps(
477 jalview.util.Comparison.GapChars,
478 sq.getSequenceAsString()));
480 sq.setDatasetSequence(dseqs[0]);
482 Alignment ds = new Alignment(dseqs);
483 if (av.hasHiddenColumns)
485 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0,
486 sq.getLength(), seqs);
489 String output = new FormatAdapter().formatSequences(
494 Toolkit.getDefaultToolkit().getSystemClipboard()
495 .setContents(new StringSelection(output), Desktop.instance);
497 Vector hiddenColumns = null;
498 if (av.hasHiddenColumns)
500 hiddenColumns = new Vector();
501 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
503 int[] region = (int[])
504 av.getColumnSelection().getHiddenColumns().elementAt(i);
506 hiddenColumns.addElement(new int[]
512 Desktop.jalviewClipboard = new Object[]
515 ds, // what is the dataset of a consensus sequence ? need to flag sequence as special.
522 * @param g1 DOCUMENT ME!
524 public void paintComponent(Graphics g)
527 int width = getWidth();
530 width = ap.calculateIdWidth().width + 4;
533 Graphics2D g2 = (Graphics2D) g;
536 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
537 RenderingHints.VALUE_ANTIALIAS_ON);
540 drawComponent(g2, width);
547 * @param g DOCUMENT ME!
549 public void drawComponent(Graphics g, int width)
551 if (av.getFont().getSize() < 10)
557 g.setFont(av.getFont());
560 FontMetrics fm = g.getFontMetrics(g.getFont());
561 g.setColor(Color.white);
562 g.fillRect(0, 0, getWidth(), getHeight());
564 g.translate(0, scrollOffset);
565 g.setColor(Color.black);
567 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
568 int fontHeight = g.getFont().getSize();
576 for (int i = 0; i < aa.length; i++)
578 g.setColor(Color.black);
588 offset = -aa[i].height/2;
592 offset += fm.getHeight()/2;
593 offset -= fm.getDescent();
596 offset += fm.getDescent();
598 x = width - fm.stringWidth(aa[i].label) - 3;
600 if (aa[i].graphGroup > -1)
603 for (int gg = 0; gg < aa.length; gg++)
605 if (aa[gg].graphGroup == aa[i].graphGroup)
611 if (groupSize * (fontHeight + 8) < aa[i].height)
613 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8))) / 2;
616 for (int gg = 0; gg < aa.length; gg++)
618 if (aa[gg].graphGroup == aa[i].graphGroup)
620 x = width - fm.stringWidth(aa[gg].label) - 3;
621 g.drawString(aa[gg].label, x,y - graphExtras);
622 if (aa[gg].annotations[0] != null)
624 g.setColor(aa[gg].annotations[0].colour);
627 g.drawLine(x, y - graphExtras - 3,
628 x + fm.stringWidth(aa[gg].label),
629 y - graphExtras - 3);
631 g.setColor(Color.black);
632 graphExtras += fontHeight + 8;
638 g.drawString(aa[i].label, x, y +offset);
645 g.drawImage(image, 2, 0 - scrollOffset, this);
647 else if (dragEvent != null && aa != null)
649 g.setColor(Color.lightGray);
650 g.drawString(aa[selectedRow].label, dragEvent.getX(),
651 dragEvent.getY() - scrollOffset);
654 if ( (aa == null) || (aa.length < 1))
656 g.drawString("Right click", 2, 8);
657 g.drawString("to add annotation", 2, 18);