2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.appletgui;
23 import java.awt.event.*;
24 import java.awt.image.BufferedImage;
26 import jalview.datamodel.*;
28 public class AnnotationLabels extends Panel implements ActionListener,
29 MouseListener, MouseMotionListener
33 boolean active = false;
39 boolean resizing = false;
43 static String ADDNEW = "Add New Row";
45 static String EDITNAME = "Edit Label/Description";
47 static String HIDE = "Hide This Row";
49 static String SHOWALL = "Show All Hidden Rows";
51 static String OUTPUT_TEXT = "Show Values In Textbox";
53 static String COPYCONS_SEQ = "Copy Consensus Sequence";
61 private boolean hasHiddenRows;
63 public AnnotationLabels(AlignmentPanel ap)
69 java.net.URL url = getClass().getResource("/images/idwidth.gif");
74 temp = java.awt.Toolkit.getDefaultToolkit().createImage(url);
79 MediaTracker mt = new MediaTracker(this);
82 } catch (Exception ex)
86 BufferedImage bi = new BufferedImage(temp.getHeight(this),
87 temp.getWidth(this), BufferedImage.TYPE_INT_RGB);
88 Graphics2D g = (Graphics2D) bi.getGraphics();
89 g.rotate(Math.toRadians(90));
90 g.drawImage(temp, 0, -bi.getWidth(this), this);
93 addMouseListener(this);
94 addMouseMotionListener(this);
97 public AnnotationLabels(AlignViewport av)
102 public void setScrollOffset(int y)
111 * @return -2 if no rows are visible at all, -1 if no visible rows were
114 int getSelectedRow(int y)
117 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
124 for (int i = 0; i < aa.length; i++)
131 height += aa[i].height;
142 public void actionPerformed(ActionEvent evt)
144 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
146 if (evt.getActionCommand().equals(ADDNEW))
148 AlignmentAnnotation newAnnotation = new AlignmentAnnotation("", null,
149 new Annotation[ap.av.alignment.getWidth()]);
151 if (!editLabelDescription(newAnnotation))
156 ap.av.alignment.addAnnotation(newAnnotation);
157 ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
159 else if (evt.getActionCommand().equals(EDITNAME))
161 editLabelDescription(aa[selectedRow]);
163 else if (evt.getActionCommand().equals(HIDE))
165 aa[selectedRow].visible = false;
167 else if (evt.getActionCommand().equals(SHOWALL))
169 for (int i = 0; i < aa.length; i++)
171 aa[i].visible = (aa[i].annotations == null) ? false : true;
174 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
176 CutAndPasteTransfer cap = new CutAndPasteTransfer(false,
178 Frame frame = new Frame();
180 jalview.bin.JalviewLite.addFrame(frame, ap.alignFrame.getTitle()
181 + " - " + aa[selectedRow].label, 500, 100);
182 cap.setText(aa[selectedRow].toString());
184 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
186 SequenceI cons = av.getConsensusSeq();
189 copy_annotseqtoclipboard(cons);
193 ap.annotationPanel.adjustPanelHeight();
194 setSize(getSize().width, ap.annotationPanel.getSize().height);
196 ap.paintAlignment(true);
199 boolean editLabelDescription(AlignmentAnnotation annotation)
201 Checkbox padGaps = new Checkbox("Fill Empty Gaps With \""
202 + ap.av.getGapCharacter() + "\"", annotation.padGaps);
204 EditNameDialog dialog = new EditNameDialog(annotation.label,
205 annotation.description, " Annotation Label",
206 "Annotation Description", ap.alignFrame,
207 "Edit Annotation Name / Description", 500, 180, false);
209 Panel empty = new Panel(new FlowLayout());
214 dialog.setVisible(true);
218 annotation.label = dialog.getName();
219 annotation.description = dialog.getDescription();
220 annotation.setPadGaps(padGaps.getState(), av.getGapCharacter());
229 boolean resizePanel = false;
231 public void mouseMoved(MouseEvent evt)
233 resizePanel = evt.getY() < 10;
235 int row = getSelectedRow(evt.getY() + scrollOffset);
241 tooltip = new Tooltip(
242 ap.av.alignment.getAlignmentAnnotation()[row]
243 .getDescription(true),
248 tooltip.setTip(ap.av.alignment.getAlignmentAnnotation()[row]
249 .getDescription(true));
252 else if (tooltip != null)
259 MouseEvent dragEvent = null;
261 public void mouseDragged(MouseEvent evt)
267 Dimension d = ap.annotationPanelHolder.getSize(),e = ap.annotationSpaceFillerHolder.getSize();;
268 int dif = evt.getY() - oldY;
270 dif /= ap.av.charHeight;
271 dif *= ap.av.charHeight;
273 if ((d.height - dif) > 20)
276 setPreferredSize(new Dimension(e.width,d.height-dif));
277 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(e.width, d.height - dif));
278 ap.annotationPanelHolder.setPreferredSize(new Dimension(d.width, d.height - dif));
279 ap.apvscroll.setValues(ap.apvscroll.getValue(), d.height-dif, 0, ap.annotationPanel.adjustPanelHeight(false));
282 //ap.paintAlignment(true);
293 public void mouseClicked(MouseEvent evt)
297 public void mouseReleased(MouseEvent evt)
302 ap.annotationPanel.repaint();
305 public void mouseEntered(MouseEvent evt)
314 public void mouseExited(MouseEvent evt)
317 if (dragEvent == null)
331 public void mousePressed(MouseEvent evt)
334 // todo: move below to mouseClicked ?
335 selectedRow = getSelectedRow(evt.getY() + scrollOffset);
337 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
339 // DETECT RIGHT MOUSE BUTTON IN AWT
340 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
343 PopupMenu popup = new PopupMenu("Annotations");
345 MenuItem item = new MenuItem(ADDNEW);
346 item.addActionListener(this);
350 // this never happens at moment: - see comment on JAL-563
353 item = new MenuItem(SHOWALL);
354 item.addActionListener(this);
358 popup.show(this, evt.getX(), evt.getY());
361 // add the rest if there are actually rows to show
362 item = new MenuItem(EDITNAME);
363 item.addActionListener(this);
365 item = new MenuItem(HIDE);
366 item.addActionListener(this);
370 item = new MenuItem(SHOWALL);
371 item.addActionListener(this);
375 item = new MenuItem(OUTPUT_TEXT);
376 item.addActionListener(this);
379 if (aa[selectedRow] == ap.av.consensus)
381 popup.addSeparator();
382 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
383 "Ignore Gaps In Consensus", ap.av.getIgnoreGapsConsensus());
385 cbmi.addItemListener(new ItemListener()
387 public void itemStateChanged(ItemEvent e)
389 ap.av.setIgnoreGapsConsensus(cbmi.getState());
390 ap.paintAlignment(true);
394 item = new MenuItem(COPYCONS_SEQ);
395 item.addActionListener(this);
399 popup.show(this, evt.getX(), evt.getY());
404 if (selectedRow > -1 && selectedRow < aa.length)
406 if (aa[selectedRow].groupRef != null)
408 if (evt.getClickCount() >= 2)
410 // todo: make the ap scroll to the selection - not necessary, first
411 // click highlights/scrolls, second selects
412 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
413 ap.av.setSelectionGroup(// new SequenceGroup(
414 aa[selectedRow].groupRef); // );
415 ap.av.sendSelection();
416 ap.paintAlignment(false);
417 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
421 ap.seqPanel.ap.idPanel
422 .highlightSearchResults(aa[selectedRow].groupRef
423 .getSequences(null));
427 else if (aa[selectedRow].sequenceRef != null)
429 Vector sr = new Vector();
430 sr.addElement(aa[selectedRow].sequenceRef);
431 if (evt.getClickCount() == 1)
433 ap.seqPanel.ap.idPanel.highlightSearchResults(sr);
435 else if (evt.getClickCount() >= 2)
437 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
438 SequenceGroup sg = new SequenceGroup();
439 sg.addSequence(aa[selectedRow].sequenceRef, false);
440 ap.av.setSelectionGroup(sg);
441 ap.paintAlignment(false);
442 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
443 ap.av.sendSelection();
458 protected void copy_annotseqtoclipboard(SequenceI sq)
460 if (sq == null || sq.getLength() < 1)
464 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
465 jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t"
466 + sq.getStart() + "\t" + sq.getEnd() + "\t"
467 + sq.getSequenceAsString() + "\n");
468 if (av.hasHiddenColumns)
470 jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector();
471 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
473 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
476 jalview.appletgui.AlignFrame.copiedHiddenColumns
477 .addElement(new int[]
478 { region[0], region[1] });
483 public void update(Graphics g)
488 public void paint(Graphics g)
490 int w = getSize().width;
491 if (image == null || w != image.getWidth(this))
493 image = createImage(w, ap.annotationPanel.getSize().height);
496 drawComponent(image.getGraphics(), w);
497 g.drawImage(image, 0, 0, this);
500 public void drawComponent(Graphics g, int width)
502 g.setFont(av.getFont());
503 FontMetrics fm = g.getFontMetrics(av.getFont());
504 g.setColor(Color.white);
505 g.fillRect(0, 0, getSize().width, getSize().height);
507 g.translate(0, -scrollOffset);
508 g.setColor(Color.black);
510 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
511 int y = 0, fy = g.getFont().getSize();
516 hasHiddenRows = false;
517 for (int i = 0; i < aa.length; i++)
521 hasHiddenRows = true;
525 x = width - fm.stringWidth(aa[i].label) - 3;
528 offset = -(aa[i].height - fy) / 2;
530 g.drawString(aa[i].label, x, y + offset);
533 g.translate(0, +scrollOffset);
536 g.setColor(Color.red);
538 g.drawLine(2, 8, 5, 2);
539 g.drawLine(5, 2, 8, 8);
541 else if (dragEvent != null && aa != null)
543 g.setColor(Color.lightGray);
544 g.drawString(aa[selectedRow].label, dragEvent.getX(),
548 if ((aa == null) || (aa.length < 1))
550 g.setColor(Color.black);
551 g.drawString("Right click", 2, 8);
552 g.drawString("to add annotation", 2, 18);