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[]
207 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
209 SequenceI cons=av.getConsensusSeq();
211 copy_annotseqtoclipboard(cons);
215 ap.annotationPanel.adjustPanelHeight();
216 ap.annotationScroller.validate();
223 * @param evt DOCUMENT ME!
225 public void mousePressed(MouseEvent evt)
227 getSelectedRow(evt.getY() - scrollOffset);
234 * @param evt DOCUMENT ME!
236 public void mouseReleased(MouseEvent evt)
238 int start = selectedRow;
239 getSelectedRow(evt.getY() - scrollOffset);
240 int end = selectedRow;
244 //Swap these annotations
245 AlignmentAnnotation startAA = ap.av.alignment.getAlignmentAnnotation()[start];
246 AlignmentAnnotation endAA = ap.av.alignment.getAlignmentAnnotation()[end];
249 ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
250 ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
256 ap.annotationPanel.repaint();
262 * @param evt DOCUMENT ME!
264 public void mouseEntered(MouseEvent evt)
276 * @param evt DOCUMENT ME!
278 public void mouseExited(MouseEvent evt)
280 if(dragEvent == null)
290 * @param evt DOCUMENT ME!
292 public void mouseDragged(MouseEvent evt)
298 Dimension d = ap.annotationScroller.getPreferredSize();
299 int dif = evt.getY() - oldY;
301 dif /= ap.av.charHeight;
302 dif *= ap.av.charHeight;
304 if ( (d.height - dif) > 20)
306 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
308 d = ap.annotationSpaceFillerHolder.getPreferredSize();
309 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
310 d.width, d.height - dif));
323 * @param evt DOCUMENT ME!
325 public void mouseMoved(MouseEvent evt)
327 resizePanel = evt.getY()<10;
329 getSelectedRow(evt.getY() - scrollOffset);
333 setToolTipText(ap.av.alignment.
334 getAlignmentAnnotation()[selectedRow].description);
343 * @param evt DOCUMENT ME!
345 public void mouseClicked(MouseEvent evt)
347 if(!SwingUtilities.isRightMouseButton(evt))
350 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
352 if ((aa == null) || (aa.length == 0))
354 JPopupMenu pop = new JPopupMenu("Annotations");
355 JMenuItem item = new JMenuItem(ADDNEW);
356 item.addActionListener(this);
358 pop.show(this, evt.getX(), evt.getY());
364 JPopupMenu pop = new JPopupMenu("Annotations");
365 JMenuItem item = new JMenuItem(ADDNEW);
366 item.addActionListener(this);
368 item = new JMenuItem(HIDE);
369 item.addActionListener(this);
371 item = new JMenuItem(DELETE);
372 item.addActionListener(this);
374 item = new JMenuItem(SHOWALL);
375 item.addActionListener(this);
377 item = new JMenuItem(OUTPUT_TEXT);
378 item.addActionListener(this);
380 // annotation object should be typed
381 if (aa[selectedRow].label.equals("Consensus"))
384 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
385 "Ignore Gaps In Consensus",
386 ap.av.getIgnoreGapsConsensus());
387 cbmi.addActionListener(new ActionListener()
388 {public void actionPerformed(ActionEvent e)
390 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
394 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
395 consclipbrd.addActionListener(this);
396 pop.add(consclipbrd);
399 pop.show(this, evt.getX(), evt.getY());
402 * do a single sequence copy to jalview and the system clipboard
404 * @param sq sequence to be copied to clipboard
406 protected void copy_annotseqtoclipboard(SequenceI sq)
408 SequenceI [] seqs = new SequenceI[] { sq };
409 String[] omitHidden = null;
410 SequenceI [] dseqs=new SequenceI[] { sq.getDatasetSequence()};
411 if (dseqs[0]==null) {
412 dseqs[0] = new Sequence(sq);
413 dseqs[0].setSequence(
414 jalview.analysis.AlignSeq.extractGaps(
415 jalview.util.Comparison.GapChars,
416 sq.getSequenceAsString()));
418 sq.setDatasetSequence(dseqs[0]);
420 Alignment ds=new Alignment(dseqs);
421 if (av.hasHiddenColumns)
423 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0, sq.getLength(), seqs);
426 String output = new FormatAdapter().formatSequences(
432 Toolkit.getDefaultToolkit().getSystemClipboard()
433 .setContents(new StringSelection(output), Desktop.instance);
435 Vector hiddenColumns = null;
436 if(av.hasHiddenColumns)
438 hiddenColumns =new Vector();
439 for(int i=0; i<av.getColumnSelection().getHiddenColumns().size(); i++)
441 int[] region = (int[])
442 av.getColumnSelection().getHiddenColumns().elementAt(i);
444 hiddenColumns.addElement(new int[]{region[0],
449 Desktop.jalviewClipboard = new Object[]{ seqs,
450 ds, // what is the dataset of a consensus sequence ? need to flag sequence as special.
456 * @param g1 DOCUMENT ME!
458 public void paintComponent(Graphics g)
461 int width = getWidth();
463 width = ap.calculateIdWidth().width + 4;
465 Graphics2D g2 = (Graphics2D) g;
467 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
468 RenderingHints.VALUE_ANTIALIAS_ON);
470 drawComponent(g2, width);
477 * @param g DOCUMENT ME!
479 public void drawComponent(Graphics g, int width)
481 if(av.getFont().getSize()<10)
484 g.setFont(av.getFont());
486 FontMetrics fm = g.getFontMetrics(g.getFont());
487 g.setColor(Color.white);
488 g.fillRect(0, 0, getWidth(), getHeight());
490 g.translate(0, scrollOffset);
491 g.setColor(Color.black);
493 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
494 int fontHeight = g.getFont().getSize();
503 for (int i = 0; i < aa.length; i++)
510 x = width - fm.stringWidth(aa[i].label) - 3;
512 if (aa[i].graph>0 && aa[i].graphHeight>0)
516 y += (aa[i].height / 3);
518 if(aa[i].graphGroup<0)
519 graphExtras = y + fontHeight;
522 if(aa[i].graphGroup>-1)
525 for (int gg = 0; gg < aa.length; gg++)
527 if (aa[gg].graphGroup == aa[i].graphGroup)
531 if(groupSize * (fontHeight+8) < aa[i].height)
532 graphExtras += (aa[i].height -( groupSize * (fontHeight+8)) )/2;
534 for(int gg=0; gg<aa.length; gg++)
536 if(aa[gg].graphGroup==aa[i].graphGroup)
538 x = width - fm.stringWidth(aa[gg].label) - 3;
539 g.drawString(aa[gg].label, x, graphExtras );
540 if(aa[gg].annotations[0]!=null)
541 g.setColor(aa[gg].annotations[0].colour);
543 g.drawLine( x, graphExtras+3,
544 x+fm.stringWidth(aa[gg].label),
547 g.setColor(Color.black);
548 graphExtras += fontHeight+8;
553 g.drawString(aa[i].label, x, y);
557 /* if (aa[i].graphLines != null)
559 for (int gl = 0; gl < aa[i].graphLines.size(); gl++)
561 x = width - fm.stringWidth(aa[i].getGraphLine(gl).label) - 3;
562 g.drawString(aa[i].getGraphLine(gl).label, x, graphExtras);
563 g.setColor(aa[i].getGraphLine(gl).colour);
564 Graphics2D g2 = (Graphics2D) g;
565 g2.setStroke(new BasicStroke(1,
566 BasicStroke.CAP_SQUARE,
567 BasicStroke.JOIN_ROUND, 3f,
573 g.drawLine(x, graphExtras,
574 x+fm.stringWidth(aa[i].label),
576 g2.setStroke(new BasicStroke());
579 y += ((2 * aa[i].height) / 3);
590 g.drawImage(image, 2, 0 - scrollOffset, this);
592 else if (dragEvent != null && aa!=null)
594 g.setColor(Color.lightGray);
595 g.drawString(aa[selectedRow].label, dragEvent.getX(), dragEvent.getY() - scrollOffset);
599 if ((aa == null) || (aa.length < 1))
601 g.drawString("Right click", 2, 8);
602 g.drawString("to add annotation", 2, 18);