2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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
21 import jalview.datamodel.*;
22 import jalview.io.FormatAdapter;
25 import java.awt.datatransfer.StringSelection;
26 import java.awt.event.*;
27 import java.awt.image.*;
28 import java.util.Vector;
39 public class AnnotationLabels extends JPanel implements MouseListener,
40 MouseMotionListener, ActionListener
42 static String ADDNEW = "Add New Row";
43 static String HIDE = "Hide This Row";
44 static String DELETE = "Delete This Row";
45 static String SHOWALL = "Show All Hidden Rows";
46 static String OUTPUT_TEXT = "Export Annotation";
47 static String COPYCONS_SEQ = "Copy Consensus Sequence";
48 boolean resizePanel = false;
52 boolean resizing = false;
57 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),
90 temp.getWidth(this), BufferedImage.TYPE_INT_RGB);
91 Graphics2D g = (Graphics2D) bi.getGraphics();
92 g.rotate(Math.toRadians(90));
93 g.drawImage(temp, 0, -bi.getWidth(this), this);
96 addMouseListener(this);
97 addMouseMotionListener(this);
100 public AnnotationLabels(AlignViewport av)
108 * @param y DOCUMENT ME!
110 public void setScrollOffset(int y)
116 void getSelectedRow(int y)
119 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
123 for (int i = 0; i < aa.length; i++)
130 height += aa[i].height;
145 * @param evt DOCUMENT ME!
147 public void actionPerformed(ActionEvent evt)
150 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
152 if (evt.getActionCommand().equals(ADDNEW))
154 String label = JOptionPane.showInputDialog(this,
155 "Label for annotation");
162 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(label,
164 new Annotation[ap.av.alignment.getWidth()]);
166 ap.av.alignment.addAnnotation(newAnnotation);
167 ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
169 dif = aa[aa.length - 1].height;
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 dif = aa[selectedRow].height * -1;
182 else if (evt.getActionCommand().equals(DELETE))
184 ap.av.alignment.deleteAnnotation(aa[selectedRow]);
185 dif = aa[selectedRow].height * -1;
187 else if (evt.getActionCommand().equals(SHOWALL))
189 for (int i = 0; i < aa.length; i++)
194 aa[i].visible = true;
198 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
200 new AnnotationExporter().exportAnnotations(
202 new AlignmentAnnotation[]
206 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
208 SequenceI cons=av.getConsensusSeq();
210 copy_annotseqtoclipboard(cons);
214 ap.annotationPanel.adjustPanelHeight();
215 ap.annotationScroller.validate();
222 * @param evt DOCUMENT ME!
224 public void mousePressed(MouseEvent evt)
226 getSelectedRow(evt.getY() - scrollOffset);
233 * @param evt DOCUMENT ME!
235 public void mouseReleased(MouseEvent evt)
237 int start = selectedRow;
238 getSelectedRow(evt.getY() - scrollOffset);
239 int end = selectedRow;
243 //Swap these annotations
244 AlignmentAnnotation startAA = ap.av.alignment.getAlignmentAnnotation()[start];
245 AlignmentAnnotation endAA = ap.av.alignment.getAlignmentAnnotation()[end];
248 ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
249 ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
255 ap.annotationPanel.repaint();
261 * @param evt DOCUMENT ME!
263 public void mouseEntered(MouseEvent evt)
275 * @param evt DOCUMENT ME!
277 public void mouseExited(MouseEvent evt)
279 if(dragEvent == null)
289 * @param evt DOCUMENT ME!
291 public void mouseDragged(MouseEvent evt)
297 Dimension d = ap.annotationScroller.getPreferredSize();
298 int dif = evt.getY() - oldY;
300 dif /= ap.av.charHeight;
301 dif *= ap.av.charHeight;
303 if ( (d.height - dif) > 20)
305 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
307 d = ap.annotationSpaceFillerHolder.getPreferredSize();
308 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
309 d.width, d.height - dif));
322 * @param evt DOCUMENT ME!
324 public void mouseMoved(MouseEvent evt)
326 resizePanel = evt.getY()<10;
328 getSelectedRow(evt.getY() - scrollOffset);
332 setToolTipText(ap.av.alignment.
333 getAlignmentAnnotation()[selectedRow].description);
342 * @param evt DOCUMENT ME!
344 public void mouseClicked(MouseEvent evt)
346 if(!SwingUtilities.isRightMouseButton(evt))
349 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
351 if ((aa == null) || (aa.length == 0))
353 JPopupMenu pop = new JPopupMenu("Annotations");
354 JMenuItem item = new JMenuItem(ADDNEW);
355 item.addActionListener(this);
357 pop.show(this, evt.getX(), evt.getY());
363 JPopupMenu pop = new JPopupMenu("Annotations");
364 JMenuItem item = new JMenuItem(ADDNEW);
365 item.addActionListener(this);
367 item = new JMenuItem(HIDE);
368 item.addActionListener(this);
370 item = new JMenuItem(DELETE);
371 item.addActionListener(this);
373 item = new JMenuItem(SHOWALL);
374 item.addActionListener(this);
376 item = new JMenuItem(OUTPUT_TEXT);
377 item.addActionListener(this);
379 // annotation object should be typed
380 if (aa[selectedRow].label.equals("Consensus"))
383 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
384 "Ignore Gaps In Consensus",
385 ap.av.getIgnoreGapsConsensus());
386 cbmi.addActionListener(new ActionListener()
387 {public void actionPerformed(ActionEvent e)
389 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
393 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
394 consclipbrd.addActionListener(this);
395 pop.add(consclipbrd);
398 pop.show(this, evt.getX(), evt.getY());
401 * do a single sequence copy to jalview and the system clipboard
403 * @param sq sequence to be copied to clipboard
405 protected void copy_annotseqtoclipboard(SequenceI sq)
407 SequenceI [] seqs = new SequenceI[] { sq };
408 String[] omitHidden = null;
409 SequenceI [] dseqs=new SequenceI[] { sq.getDatasetSequence()};
410 if (dseqs[0]==null) {
411 dseqs[0] = new Sequence(sq);
412 dseqs[0].setSequence(
413 jalview.analysis.AlignSeq.extractGaps(
414 jalview.util.Comparison.GapChars,
415 sq.getSequenceAsString()));
417 sq.setDatasetSequence(dseqs[0]);
419 Alignment ds=new Alignment(dseqs);
420 if (av.hasHiddenColumns)
422 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0, sq.getLength(), seqs);
425 String output = new FormatAdapter().formatSequences(
431 Toolkit.getDefaultToolkit().getSystemClipboard()
432 .setContents(new StringSelection(output), Desktop.instance);
434 Vector hiddenColumns = null;
435 if(av.hasHiddenColumns)
437 hiddenColumns =new Vector();
438 for(int i=0; i<av.getColumnSelection().getHiddenColumns().size(); i++)
440 int[] region = (int[])
441 av.getColumnSelection().getHiddenColumns().elementAt(i);
443 hiddenColumns.addElement(new int[]{region[0],
448 Desktop.jalviewClipboard = new Object[]{ seqs,
449 ds, // what is the dataset of a consensus sequence ? need to flag sequence as special.
455 * @param g1 DOCUMENT ME!
457 public void paintComponent(Graphics g)
460 int width = getWidth();
462 width = ap.calculateIdWidth().width + 4;
464 Graphics2D g2 = (Graphics2D) g;
466 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
467 RenderingHints.VALUE_ANTIALIAS_ON);
469 drawComponent(g2, width);
476 * @param g DOCUMENT ME!
478 public void drawComponent(Graphics g, int width)
480 if(av.getFont().getSize()<10)
483 g.setFont(av.getFont());
485 FontMetrics fm = g.getFontMetrics(g.getFont());
486 g.setColor(Color.white);
487 g.fillRect(0, 0, getWidth(), getHeight());
489 g.translate(0, scrollOffset);
490 g.setColor(Color.black);
492 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
493 int fontHeight = g.getFont().getSize();
502 for (int i = 0; i < aa.length; i++)
509 x = width - fm.stringWidth(aa[i].label) - 3;
511 if (aa[i].graph>0 && aa[i].graphHeight>0)
515 y += (aa[i].height / 3);
517 if(aa[i].graphGroup<0)
518 graphExtras = y + fontHeight;
521 if(aa[i].graphGroup>-1)
524 for (int gg = 0; gg < aa.length; gg++)
526 if (aa[gg].graphGroup == aa[i].graphGroup)
530 if(groupSize * (fontHeight+8) < aa[i].height)
531 graphExtras += (aa[i].height -( groupSize * (fontHeight+8)) )/2;
533 for(int gg=0; gg<aa.length; gg++)
535 if(aa[gg].graphGroup==aa[i].graphGroup)
537 x = width - fm.stringWidth(aa[gg].label) - 3;
538 g.drawString(aa[gg].label, x, graphExtras );
539 if(aa[gg].annotations[0]!=null)
540 g.setColor(aa[gg].annotations[0].colour);
542 g.drawLine( x, graphExtras+3,
543 x+fm.stringWidth(aa[gg].label),
546 g.setColor(Color.black);
547 graphExtras += fontHeight+8;
552 g.drawString(aa[i].label, x, y);
556 /* if (aa[i].graphLines != null)
558 for (int gl = 0; gl < aa[i].graphLines.size(); gl++)
560 x = width - fm.stringWidth(aa[i].getGraphLine(gl).label) - 3;
561 g.drawString(aa[i].getGraphLine(gl).label, x, graphExtras);
562 g.setColor(aa[i].getGraphLine(gl).colour);
563 Graphics2D g2 = (Graphics2D) g;
564 g2.setStroke(new BasicStroke(1,
565 BasicStroke.CAP_SQUARE,
566 BasicStroke.JOIN_ROUND, 3f,
572 g.drawLine(x, graphExtras,
573 x+fm.stringWidth(aa[i].label),
575 g2.setStroke(new BasicStroke());
578 y += ((2 * aa[i].height) / 3);
589 g.drawImage(image, 2, 0 - scrollOffset, this);
591 else if (dragEvent != null && aa!=null)
593 g.setColor(Color.lightGray);
594 g.drawString(aa[selectedRow].label, dragEvent.getX(), dragEvent.getY() - scrollOffset);
598 if ((aa == null) || (aa.length < 1))
600 g.drawString("Right click", 2, 8);
601 g.drawString("to add annotation", 2, 18);