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(jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.GapChars, sq.getSequence()));
404 sq.setDatasetSequence(dseqs[0]);
406 Alignment ds=new Alignment(dseqs);
407 if (av.hasHiddenColumns)
409 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0, sq.getLength(), seqs);
412 String output = new FormatAdapter().formatSequences(
418 Toolkit.getDefaultToolkit().getSystemClipboard()
419 .setContents(new StringSelection(output), Desktop.instance);
421 Vector hiddenColumns = null;
422 if(av.hasHiddenColumns)
424 hiddenColumns =new Vector();
425 for(int i=0; i<av.getColumnSelection().getHiddenColumns().size(); i++)
427 int[] region = (int[])
428 av.getColumnSelection().getHiddenColumns().elementAt(i);
430 hiddenColumns.addElement(new int[]{region[0],
435 Desktop.jalviewClipboard = new Object[]{ seqs,
436 ds, // what is the dataset of a consensus sequence ? need to flag sequence as special.
442 * @param g1 DOCUMENT ME!
444 public void paintComponent(Graphics g)
447 int width = getWidth();
449 width = ap.calculateIdWidth().width + 4;
451 Graphics2D g2 = (Graphics2D) g;
453 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
454 RenderingHints.VALUE_ANTIALIAS_ON);
456 drawComponent(g2, width);
463 * @param g DOCUMENT ME!
465 public void drawComponent(Graphics g, int width)
467 if(av.getFont().getSize()<10)
470 g.setFont(av.getFont());
472 FontMetrics fm = g.getFontMetrics(g.getFont());
473 g.setColor(Color.white);
474 g.fillRect(0, 0, getWidth(), getHeight());
476 g.translate(0, scrollOffset);
477 g.setColor(Color.black);
479 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
480 int fontHeight = g.getFont().getSize();
489 for (int i = 0; i < aa.length; i++)
496 x = width - fm.stringWidth(aa[i].label) - 3;
498 if (aa[i].graph>0 && aa[i].graphHeight>0)
502 y += (aa[i].height / 3);
504 if(aa[i].graphGroup<0)
505 graphExtras = y + fontHeight;
508 if(aa[i].graphGroup>-1)
511 for (int gg = 0; gg < aa.length; gg++)
513 if (aa[gg].graphGroup == aa[i].graphGroup)
517 if(groupSize * (fontHeight+8) < aa[i].height)
518 graphExtras += (aa[i].height -( groupSize * (fontHeight+8)) )/2;
520 for(int gg=0; gg<aa.length; gg++)
522 if(aa[gg].graphGroup==aa[i].graphGroup)
524 x = width - fm.stringWidth(aa[gg].label) - 3;
525 g.drawString(aa[gg].label, x, graphExtras );
526 if(aa[gg].annotations[0]!=null)
527 g.setColor(aa[gg].annotations[0].colour);
529 g.drawLine( x, graphExtras+3,
530 x+fm.stringWidth(aa[gg].label),
533 g.setColor(Color.black);
534 graphExtras += fontHeight+8;
539 g.drawString(aa[i].label, x, y);
543 /* if (aa[i].graphLines != null)
545 for (int gl = 0; gl < aa[i].graphLines.size(); gl++)
547 x = width - fm.stringWidth(aa[i].getGraphLine(gl).label) - 3;
548 g.drawString(aa[i].getGraphLine(gl).label, x, graphExtras);
549 g.setColor(aa[i].getGraphLine(gl).colour);
550 Graphics2D g2 = (Graphics2D) g;
551 g2.setStroke(new BasicStroke(1,
552 BasicStroke.CAP_SQUARE,
553 BasicStroke.JOIN_ROUND, 3f,
559 g.drawLine(x, graphExtras,
560 x+fm.stringWidth(aa[i].label),
562 g2.setStroke(new BasicStroke());
565 y += ((2 * aa[i].height) / 3);
576 g.drawImage(image, 2, 0 - scrollOffset, this);
578 else if (dragEvent != null && aa!=null)
580 g.setColor(Color.lightGray);
581 g.drawString(aa[selectedRow].label, dragEvent.getX(), dragEvent.getY() - scrollOffset);
585 if ((aa == null) || (aa.length < 1))
587 g.drawString("Right click", 2, 8);
588 g.drawString("to add annotation", 2, 18);