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 = null;
218 if (aa[selectedRow].groupRef!=null)
220 cons = aa[selectedRow].groupRef.getConsensusSeq();
222 cons = av.getConsensusSeq();
226 copy_annotseqtoclipboard(cons);
231 ap.annotationPanel.adjustPanelHeight();
232 ap.annotationScroller.validate();
233 ap.paintAlignment(true);
242 boolean editLabelDescription(AlignmentAnnotation annotation)
244 EditNameDialog dialog = new EditNameDialog(annotation.label,
245 annotation.description, " Annotation Name ",
246 "Annotation Description ", "Edit Annotation Name/Description");
253 annotation.label = dialog.getName();
255 String text = dialog.getDescription();
256 if (text != null && text.length() == 0)
260 annotation.description = text;
271 public void mousePressed(MouseEvent evt)
273 getSelectedRow(evt.getY() - scrollOffset);
283 public void mouseReleased(MouseEvent evt)
285 int start = selectedRow;
286 getSelectedRow(evt.getY() - scrollOffset);
287 int end = selectedRow;
291 // Swap these annotations
292 AlignmentAnnotation startAA = ap.av.alignment
293 .getAlignmentAnnotation()[start];
294 AlignmentAnnotation endAA = ap.av.alignment.getAlignmentAnnotation()[end];
296 ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
297 ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
303 ap.annotationPanel.repaint();
312 public void mouseEntered(MouseEvent evt)
327 public void mouseExited(MouseEvent evt)
329 if (dragEvent == null)
342 public void mouseDragged(MouseEvent evt)
348 Dimension d = ap.annotationScroller.getPreferredSize();
349 int dif = evt.getY() - oldY;
351 dif /= ap.av.charHeight;
352 dif *= ap.av.charHeight;
354 if ((d.height - dif) > 20)
356 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
358 d = ap.annotationSpaceFillerHolder.getPreferredSize();
359 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
360 d.width, d.height - dif));
361 ap.paintAlignment(true);
378 public void mouseMoved(MouseEvent evt)
380 resizePanel = evt.getY() < 10;
382 getSelectedRow(evt.getY() - scrollOffset);
385 && ap.av.alignment.getAlignmentAnnotation().length > selectedRow)
387 AlignmentAnnotation aa = ap.av.alignment.getAlignmentAnnotation()[selectedRow];
389 StringBuffer desc = new StringBuffer("<html>");
391 if (aa.description != null
392 && !aa.description.equals("New description"))
394 desc.append(aa.getDescription(true));
400 desc.append("Score: " + aa.score);
403 if (desc.length() != 6)
405 desc.append("</html>");
406 this.setToolTipText(desc.toString());
409 this.setToolTipText(null);
420 public void mouseClicked(MouseEvent evt)
422 if (!SwingUtilities.isRightMouseButton(evt))
427 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
429 JPopupMenu pop = new JPopupMenu("Annotations");
430 JMenuItem item = new JMenuItem(ADDNEW);
431 item.addActionListener(this);
433 if ((aa == null) || (aa.length == 0))
435 item = new JMenuItem(SHOWALL);
436 item.addActionListener(this);
438 pop.show(this, evt.getX(), evt.getY());
443 item = new JMenuItem(EDITNAME);
444 item.addActionListener(this);
446 item = new JMenuItem(HIDE);
447 item.addActionListener(this);
449 item = new JMenuItem(DELETE);
450 item.addActionListener(this);
452 item = new JMenuItem(SHOWALL);
453 item.addActionListener(this);
455 item = new JMenuItem(OUTPUT_TEXT);
456 item.addActionListener(this);
458 // TODO: annotation object should be typed for autocalculated/derived property methods
459 if (selectedRow < aa.length && aa[selectedRow].autoCalculated && aa[selectedRow].label.indexOf("Consensus")>-1)
462 // av and sequencegroup need to implement same interface for
463 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
464 "Ignore Gaps In Consensus", ap.av.getIgnoreGapsConsensus());
465 final AlignmentAnnotation aaa = aa[selectedRow];
466 cbmi.addActionListener(new ActionListener()
468 public void actionPerformed(ActionEvent e)
470 if (aaa.groupRef!=null)
472 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
474 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
478 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
479 consclipbrd.addActionListener(this);
480 pop.add(consclipbrd);
483 pop.show(this, evt.getX(), evt.getY());
487 * do a single sequence copy to jalview and the system clipboard
490 * sequence to be copied to clipboard
492 protected void copy_annotseqtoclipboard(SequenceI sq)
494 SequenceI[] seqs = new SequenceI[]
496 String[] omitHidden = null;
497 SequenceI[] dseqs = new SequenceI[]
498 { sq.getDatasetSequence() };
499 if (dseqs[0] == null)
501 dseqs[0] = new Sequence(sq);
502 dseqs[0].setSequence(jalview.analysis.AlignSeq.extractGaps(
503 jalview.util.Comparison.GapChars, sq.getSequenceAsString()));
505 sq.setDatasetSequence(dseqs[0]);
507 Alignment ds = new Alignment(dseqs);
508 if (av.hasHiddenColumns)
510 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0,
511 sq.getLength(), seqs);
514 String output = new FormatAdapter().formatSequences("Fasta", seqs,
517 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
518 new StringSelection(output), Desktop.instance);
520 Vector hiddenColumns = null;
521 if (av.hasHiddenColumns)
523 hiddenColumns = new Vector();
524 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
526 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
529 hiddenColumns.addElement(new int[]
530 { region[0], region[1] });
534 Desktop.jalviewClipboard = new Object[]
535 { seqs, ds, // what is the dataset of a consensus sequence ? need to flag
536 // sequence as special.
546 public void paintComponent(Graphics g)
549 int width = getWidth();
552 width = ap.calculateIdWidth().width + 4;
555 Graphics2D g2 = (Graphics2D) g;
558 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
559 RenderingHints.VALUE_ANTIALIAS_ON);
562 drawComponent(g2, width);
572 public void drawComponent(Graphics g, int width)
574 if (av.getFont().getSize() < 10)
580 g.setFont(av.getFont());
583 FontMetrics fm = g.getFontMetrics(g.getFont());
584 g.setColor(Color.white);
585 g.fillRect(0, 0, getWidth(), getHeight());
587 g.translate(0, scrollOffset);
588 g.setColor(Color.black);
590 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
591 int fontHeight = g.getFont().getSize();
599 for (int i = 0; i < aa.length; i++)
601 g.setColor(Color.black);
610 offset = -aa[i].height / 2;
614 offset += fm.getHeight() / 2;
615 offset -= fm.getDescent();
618 offset += fm.getDescent();
620 x = width - fm.stringWidth(aa[i].label) - 3;
622 if (aa[i].graphGroup > -1)
625 for (int gg = 0; gg < aa.length; gg++)
627 if (aa[gg].graphGroup == aa[i].graphGroup)
633 if (groupSize * (fontHeight + 8) < aa[i].height)
635 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8))) / 2;
638 for (int gg = 0; gg < aa.length; gg++)
640 if (aa[gg].graphGroup == aa[i].graphGroup)
642 x = width - fm.stringWidth(aa[gg].label) - 3;
643 g.drawString(aa[gg].label, x, y - graphExtras);
644 if (aa[gg].annotations[0] != null)
646 g.setColor(aa[gg].annotations[0].colour);
649 g.drawLine(x, y - graphExtras - 3, x
650 + fm.stringWidth(aa[gg].label), y - graphExtras - 3);
652 g.setColor(Color.black);
653 graphExtras += fontHeight + 8;
659 g.drawString(aa[i].label, x, y + offset);
666 g.drawImage(image, 2, 0 - scrollOffset, this);
668 else if (dragEvent != null && aa != null)
670 g.setColor(Color.lightGray);
671 g.drawString(aa[selectedRow].label, dragEvent.getX(), dragEvent
676 if ((aa == null) || (aa.length < 1))
678 g.drawString("Right click", 2, 8);
679 g.drawString("to add annotation", 2, 18);