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)
70 java.net.URL url = getClass().getResource("/images/idwidth.gif");
75 temp = java.awt.Toolkit.getDefaultToolkit().createImage(url);
80 MediaTracker mt = new MediaTracker(this);
88 BufferedImage bi = new BufferedImage(temp.getHeight(this),
89 temp.getWidth(this), BufferedImage.TYPE_INT_RGB);
90 Graphics2D g = (Graphics2D) bi.getGraphics();
91 g.rotate(Math.toRadians(90));
92 g.drawImage(temp, 0, -bi.getWidth(this), this);
95 addMouseListener(this);
96 addMouseMotionListener(this);
99 public AnnotationLabels(AlignViewport av)
107 * @param y DOCUMENT ME!
109 public void setScrollOffset(int y)
115 void getSelectedRow(int y)
118 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
122 for (int i = 0; i < aa.length; i++)
129 height += aa[i].height;
144 * @param evt DOCUMENT ME!
146 public void actionPerformed(ActionEvent evt)
149 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
151 if (evt.getActionCommand().equals(ADDNEW))
153 String label = JOptionPane.showInputDialog(this,
154 "Label for annotation");
161 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(label,
163 new Annotation[ap.av.alignment.getWidth()]);
165 ap.av.alignment.addAnnotation(newAnnotation);
166 ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
168 dif = aa[aa.length - 1].height;
170 else if (evt.getActionCommand().equals(HIDE))
172 aa[selectedRow].visible = false;
174 if (aa[selectedRow].label.equals("Quality"))
176 ap.av.quality = null;
179 dif = aa[selectedRow].height * -1;
181 else if (evt.getActionCommand().equals(DELETE))
183 ap.av.alignment.deleteAnnotation(aa[selectedRow]);
184 dif = aa[selectedRow].height * -1;
186 else if (evt.getActionCommand().equals(SHOWALL))
188 for (int i = 0; i < aa.length; i++)
193 aa[i].visible = true;
197 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
199 new AnnotationExporter().exportAnnotations(
201 new AlignmentAnnotation[]
205 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
207 SequenceI cons=av.getConsensusSeq();
209 copy_annotseqtoclipboard(cons);
213 ap.annotationPanel.adjustPanelHeight();
214 ap.annotationScroller.validate();
221 * @param evt DOCUMENT ME!
223 public void mousePressed(MouseEvent evt)
225 getSelectedRow(evt.getY() - scrollOffset);
232 * @param evt DOCUMENT ME!
234 public void mouseReleased(MouseEvent evt)
236 int start = selectedRow;
237 getSelectedRow(evt.getY() - scrollOffset);
238 int end = selectedRow;
242 //Swap these annotations
243 AlignmentAnnotation startAA = ap.av.alignment.getAlignmentAnnotation()[start];
244 AlignmentAnnotation endAA = ap.av.alignment.getAlignmentAnnotation()[end];
247 ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
248 ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
254 ap.annotationPanel.repaint();
260 * @param evt DOCUMENT ME!
262 public void mouseEntered(MouseEvent evt)
274 * @param evt DOCUMENT ME!
276 public void mouseExited(MouseEvent evt)
278 if(dragEvent == null)
288 * @param evt DOCUMENT ME!
290 public void mouseDragged(MouseEvent evt)
296 Dimension d = ap.annotationScroller.getPreferredSize();
297 int dif = evt.getY() - oldY;
299 dif /= ap.av.charHeight;
300 dif *= ap.av.charHeight;
302 if ( (d.height - dif) > 20)
304 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
306 d = ap.annotationSpaceFillerHolder.getPreferredSize();
307 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
308 d.width, d.height - dif));
321 * @param evt DOCUMENT ME!
323 public void mouseMoved(MouseEvent evt)
325 resizePanel = evt.getY()<10;
333 * @param evt DOCUMENT ME!
335 public void mouseClicked(MouseEvent evt)
337 if(!SwingUtilities.isRightMouseButton(evt))
340 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
342 if ((aa == null) || (aa.length == 0))
344 JPopupMenu pop = new JPopupMenu("Annotations");
345 JMenuItem item = new JMenuItem(ADDNEW);
346 item.addActionListener(this);
348 pop.show(this, evt.getX(), evt.getY());
354 JPopupMenu pop = new JPopupMenu("Annotations");
355 JMenuItem item = new JMenuItem(ADDNEW);
356 item.addActionListener(this);
358 item = new JMenuItem(HIDE);
359 item.addActionListener(this);
361 item = new JMenuItem(DELETE);
362 item.addActionListener(this);
364 item = new JMenuItem(SHOWALL);
365 item.addActionListener(this);
367 item = new JMenuItem(OUTPUT_TEXT);
368 item.addActionListener(this);
370 // annotation object should be typed
371 if (aa[selectedRow].label.equals("Consensus"))
374 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
375 "Ignore Gaps In Consensus",
376 ap.av.getIgnoreGapsConsensus());
377 cbmi.addActionListener(new ActionListener()
378 {public void actionPerformed(ActionEvent e)
380 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
384 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
385 consclipbrd.addActionListener(this);
386 pop.add(consclipbrd);
389 pop.show(this, evt.getX(), evt.getY());
392 * do a single sequence copy to jalview and the system clipboard
394 * @param sq sequence to be copied to clipboard
396 protected void copy_annotseqtoclipboard(SequenceI sq)
398 SequenceI [] seqs = new SequenceI[] { sq };
399 String[] omitHidden = null;
400 SequenceI [] dseqs=new SequenceI[] { sq.getDatasetSequence()};
401 if (dseqs[0]==null) {
402 dseqs[0] = new Sequence(sq);
403 dseqs[0].setSequence(
404 jalview.analysis.AlignSeq.extractGaps(
405 jalview.util.Comparison.GapChars,
406 sq.getSequenceAsString()));
408 sq.setDatasetSequence(dseqs[0]);
410 Alignment ds=new Alignment(dseqs);
411 if (av.hasHiddenColumns)
413 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0, sq.getLength(), seqs);
416 String output = new FormatAdapter().formatSequences(
422 Toolkit.getDefaultToolkit().getSystemClipboard()
423 .setContents(new StringSelection(output), Desktop.instance);
425 Vector hiddenColumns = null;
426 if(av.hasHiddenColumns)
428 hiddenColumns =new Vector();
429 for(int i=0; i<av.getColumnSelection().getHiddenColumns().size(); i++)
431 int[] region = (int[])
432 av.getColumnSelection().getHiddenColumns().elementAt(i);
434 hiddenColumns.addElement(new int[]{region[0],
439 Desktop.jalviewClipboard = new Object[]{ seqs,
440 ds, // what is the dataset of a consensus sequence ? need to flag sequence as special.
446 * @param g1 DOCUMENT ME!
448 public void paintComponent(Graphics g)
451 int width = getWidth();
453 width = ap.calculateIdWidth().width + 4;
455 Graphics2D g2 = (Graphics2D) g;
457 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
458 RenderingHints.VALUE_ANTIALIAS_ON);
460 drawComponent(g2, width);
467 * @param g DOCUMENT ME!
469 public void drawComponent(Graphics g, int width)
471 if(av.getFont().getSize()<10)
474 g.setFont(av.getFont());
476 FontMetrics fm = g.getFontMetrics(g.getFont());
477 g.setColor(Color.white);
478 g.fillRect(0, 0, getWidth(), getHeight());
480 g.translate(0, scrollOffset);
481 g.setColor(Color.black);
483 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
484 int fontHeight = g.getFont().getSize();
493 for (int i = 0; i < aa.length; i++)
500 x = width - fm.stringWidth(aa[i].label) - 3;
502 if (aa[i].graph>0 && aa[i].graphHeight>0)
506 y += (aa[i].height / 3);
508 if(aa[i].graphGroup<0)
509 graphExtras = y + fontHeight;
512 if(aa[i].graphGroup>-1)
515 for (int gg = 0; gg < aa.length; gg++)
517 if (aa[gg].graphGroup == aa[i].graphGroup)
521 if(groupSize * (fontHeight+8) < aa[i].height)
522 graphExtras += (aa[i].height -( groupSize * (fontHeight+8)) )/2;
524 for(int gg=0; gg<aa.length; gg++)
526 if(aa[gg].graphGroup==aa[i].graphGroup)
528 x = width - fm.stringWidth(aa[gg].label) - 3;
529 g.drawString(aa[gg].label, x, graphExtras );
530 if(aa[gg].annotations[0]!=null)
531 g.setColor(aa[gg].annotations[0].colour);
533 g.drawLine( x, graphExtras+3,
534 x+fm.stringWidth(aa[gg].label),
537 g.setColor(Color.black);
538 graphExtras += fontHeight+8;
543 g.drawString(aa[i].label, x, y);
547 /* if (aa[i].graphLines != null)
549 for (int gl = 0; gl < aa[i].graphLines.size(); gl++)
551 x = width - fm.stringWidth(aa[i].getGraphLine(gl).label) - 3;
552 g.drawString(aa[i].getGraphLine(gl).label, x, graphExtras);
553 g.setColor(aa[i].getGraphLine(gl).colour);
554 Graphics2D g2 = (Graphics2D) g;
555 g2.setStroke(new BasicStroke(1,
556 BasicStroke.CAP_SQUARE,
557 BasicStroke.JOIN_ROUND, 3f,
563 g.drawLine(x, graphExtras,
564 x+fm.stringWidth(aa[i].label),
566 g2.setStroke(new BasicStroke());
569 y += ((2 * aa[i].height) / 3);
580 g.drawImage(image, 2, 0 - scrollOffset, this);
582 else if (dragEvent != null && aa!=null)
584 g.setColor(Color.lightGray);
585 g.drawString(aa[selectedRow].label, dragEvent.getX(), dragEvent.getY() - scrollOffset);
589 if ((aa == null) || (aa.length < 1))
591 g.drawString("Right click", 2, 8);
592 g.drawString("to add annotation", 2, 18);