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("Conservation"))
176 ap.av.showConservation = false;
179 if (aa[selectedRow].label.equals("Quality"))
181 ap.av.showQuality = false;
184 if (aa[selectedRow].label.equals("Consensus"))
186 ap.av.showIdentity = false;
189 dif = aa[selectedRow].height * -1;
191 else if (evt.getActionCommand().equals(DELETE))
193 ap.av.alignment.deleteAnnotation(aa[selectedRow]);
194 dif = aa[selectedRow].height * -1;
196 else if (evt.getActionCommand().equals(SHOWALL))
198 for (int i = 0; i < aa.length; i++)
203 aa[i].visible = true;
207 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
209 new AnnotationExporter().exportAnnotations(
211 new AlignmentAnnotation[]
215 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
217 SequenceI cons=av.getConsensusSeq();
219 copy_annotseqtoclipboard(cons);
223 ap.annotationPanel.adjustPanelHeight();
224 ap.annotationScroller.validate();
231 * @param evt DOCUMENT ME!
233 public void mousePressed(MouseEvent evt)
235 getSelectedRow(evt.getY() - scrollOffset);
242 * @param evt DOCUMENT ME!
244 public void mouseReleased(MouseEvent evt)
246 int start = selectedRow;
247 getSelectedRow(evt.getY() - scrollOffset);
248 int end = selectedRow;
252 //Swap these annotations
253 AlignmentAnnotation startAA = ap.av.alignment.getAlignmentAnnotation()[start];
254 AlignmentAnnotation endAA = ap.av.alignment.getAlignmentAnnotation()[end];
257 ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
258 ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
264 ap.annotationPanel.repaint();
270 * @param evt DOCUMENT ME!
272 public void mouseEntered(MouseEvent evt)
284 * @param evt DOCUMENT ME!
286 public void mouseExited(MouseEvent evt)
288 if(dragEvent == null)
298 * @param evt DOCUMENT ME!
300 public void mouseDragged(MouseEvent evt)
306 Dimension d = ap.annotationScroller.getPreferredSize();
307 int dif = evt.getY() - oldY;
309 dif /= ap.av.charHeight;
310 dif *= ap.av.charHeight;
312 if ( (d.height - dif) > 20)
314 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
316 d = ap.annotationSpaceFillerHolder.getPreferredSize();
317 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
318 d.width, d.height - dif));
331 * @param evt DOCUMENT ME!
333 public void mouseMoved(MouseEvent evt)
335 resizePanel = evt.getY()<10;
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(jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.GapChars, sq.getSequence()));
414 sq.setDatasetSequence(dseqs[0]);
416 Alignment ds=new Alignment(dseqs);
417 if (av.hasHiddenColumns)
419 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0, sq.getLength(), seqs);
422 String output = new FormatAdapter().formatSequences(
428 Toolkit.getDefaultToolkit().getSystemClipboard()
429 .setContents(new StringSelection(output), Desktop.instance);
431 Vector hiddenColumns = null;
432 if(av.hasHiddenColumns)
434 hiddenColumns =new Vector();
435 for(int i=0; i<av.getColumnSelection().getHiddenColumns().size(); i++)
437 int[] region = (int[])
438 av.getColumnSelection().getHiddenColumns().elementAt(i);
440 hiddenColumns.addElement(new int[]{region[0],
445 Desktop.jalviewClipboard = new Object[]{ seqs,
446 ds, // what is the dataset of a consensus sequence ? need to flag sequence as special.
452 * @param g1 DOCUMENT ME!
454 public void paintComponent(Graphics g)
457 int width = getWidth();
459 width = ap.calculateIdWidth().width + 4;
461 Graphics2D g2 = (Graphics2D) g;
463 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
464 RenderingHints.VALUE_ANTIALIAS_ON);
466 drawComponent(g2, width);
473 * @param g DOCUMENT ME!
475 public void drawComponent(Graphics g, int width)
477 if(av.getFont().getSize()<10)
480 g.setFont(av.getFont());
482 FontMetrics fm = g.getFontMetrics(g.getFont());
483 g.setColor(Color.white);
484 g.fillRect(0, 0, getWidth(), getHeight());
486 g.translate(0, scrollOffset);
487 g.setColor(Color.black);
489 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
490 int fontHeight = g.getFont().getSize();
499 for (int i = 0; i < aa.length; i++)
506 x = width - fm.stringWidth(aa[i].label) - 3;
508 if (aa[i].graph>0 && aa[i].graphHeight>0)
512 y += (aa[i].height / 3);
514 if(aa[i].graphGroup<0)
515 graphExtras = y + fontHeight;
518 if(aa[i].graphGroup>-1)
521 for (int gg = 0; gg < aa.length; gg++)
523 if (aa[gg].graphGroup == aa[i].graphGroup)
527 if(groupSize * (fontHeight+8) < aa[i].height)
528 graphExtras += (aa[i].height -( groupSize * (fontHeight+8)) )/2;
530 for(int gg=0; gg<aa.length; gg++)
532 if(aa[gg].graphGroup==aa[i].graphGroup)
534 x = width - fm.stringWidth(aa[gg].label) - 3;
535 g.drawString(aa[gg].label, x, graphExtras );
536 if(aa[gg].annotations[0]!=null)
537 g.setColor(aa[gg].annotations[0].colour);
539 g.drawLine( x, graphExtras+3,
540 x+fm.stringWidth(aa[gg].label),
543 g.setColor(Color.black);
544 graphExtras += fontHeight+8;
549 g.drawString(aa[i].label, x, y);
553 /* if (aa[i].graphLines != null)
555 for (int gl = 0; gl < aa[i].graphLines.size(); gl++)
557 x = width - fm.stringWidth(aa[i].getGraphLine(gl).label) - 3;
558 g.drawString(aa[i].getGraphLine(gl).label, x, graphExtras);
559 g.setColor(aa[i].getGraphLine(gl).colour);
560 Graphics2D g2 = (Graphics2D) g;
561 g2.setStroke(new BasicStroke(1,
562 BasicStroke.CAP_SQUARE,
563 BasicStroke.JOIN_ROUND, 3f,
569 g.drawLine(x, graphExtras,
570 x+fm.stringWidth(aa[i].label),
572 g2.setStroke(new BasicStroke());
575 y += ((2 * aa[i].height) / 3);
586 g.drawImage(image, 2, 0 - scrollOffset, this);
588 else if (dragEvent != null && aa!=null)
590 g.setColor(Color.lightGray);
591 g.drawString(aa[selectedRow].label, dragEvent.getX(), dragEvent.getY() - scrollOffset);
595 if ((aa == null) || (aa.length < 1))
597 g.drawString("Right click", 2, 8);
598 g.drawString("to add annotation", 2, 18);