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.*;
25 import jalview.datamodel.*;
27 public class AnnotationLabels extends Panel implements ActionListener,
28 MouseListener, MouseMotionListener
32 boolean active = false;
38 boolean resizing = false;
42 static String ADDNEW = "Add New Row";
44 static String EDITNAME = "Edit Label/Description";
46 static String HIDE = "Hide This Row";
48 static String SHOWALL = "Show All Hidden Rows";
50 static String OUTPUT_TEXT = "Show Values In Textbox";
52 static String COPYCONS_SEQ = "Copy Consensus Sequence";
60 private boolean hasHiddenRows;
62 public AnnotationLabels(AlignmentPanel ap)
67 addMouseListener(this);
68 addMouseMotionListener(this);
71 public AnnotationLabels(AlignViewport av)
76 public void setScrollOffset(int y)
85 * @return -2 if no rows are visible at all, -1 if no visible rows were
88 int getSelectedRow(int y)
91 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
98 for (int i = 0; i < aa.length; i++)
105 height += aa[i].height;
116 public void actionPerformed(ActionEvent evt)
118 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
120 if (evt.getActionCommand().equals(ADDNEW))
122 AlignmentAnnotation newAnnotation = new AlignmentAnnotation("", null,
123 new Annotation[ap.av.alignment.getWidth()]);
125 if (!editLabelDescription(newAnnotation))
130 ap.av.alignment.addAnnotation(newAnnotation);
131 ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
133 else if (evt.getActionCommand().equals(EDITNAME))
135 editLabelDescription(aa[selectedRow]);
137 else if (evt.getActionCommand().equals(HIDE))
139 aa[selectedRow].visible = false;
141 else if (evt.getActionCommand().equals(SHOWALL))
143 for (int i = 0; i < aa.length; i++)
145 aa[i].visible = (aa[i].annotations == null) ? false : true;
148 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
150 CutAndPasteTransfer cap = new CutAndPasteTransfer(false,
152 Frame frame = new Frame();
154 jalview.bin.JalviewLite.addFrame(frame, ap.alignFrame.getTitle()
155 + " - " + aa[selectedRow].label, 500, 100);
156 cap.setText(aa[selectedRow].toString());
158 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
160 SequenceI cons = av.getConsensusSeq();
163 copy_annotseqtoclipboard(cons);
167 ap.annotationPanel.adjustPanelHeight();
168 setSize(getSize().width, ap.annotationPanel.getSize().height);
170 ap.paintAlignment(true);
173 boolean editLabelDescription(AlignmentAnnotation annotation)
175 Checkbox padGaps = new Checkbox("Fill Empty Gaps With \""
176 + ap.av.getGapCharacter() + "\"", annotation.padGaps);
178 EditNameDialog dialog = new EditNameDialog(annotation.label,
179 annotation.description, " Annotation Label",
180 "Annotation Description", ap.alignFrame,
181 "Edit Annotation Name / Description", 500, 180, false);
183 Panel empty = new Panel(new FlowLayout());
188 dialog.setVisible(true);
192 annotation.label = dialog.getName();
193 annotation.description = dialog.getDescription();
194 annotation.setPadGaps(padGaps.getState(), av.getGapCharacter());
203 public void mouseMoved(MouseEvent evt)
205 int row = getSelectedRow(evt.getY() - scrollOffset);
211 tooltip = new Tooltip(
212 ap.av.alignment.getAlignmentAnnotation()[row]
213 .getDescription(true),
218 tooltip.setTip(ap.av.alignment.getAlignmentAnnotation()[row]
219 .getDescription(true));
222 else if (tooltip != null)
229 public void mouseDragged(MouseEvent evt)
233 public void mouseClicked(MouseEvent evt)
237 public void mouseReleased(MouseEvent evt)
241 public void mouseEntered(MouseEvent evt)
245 public void mouseExited(MouseEvent evt)
249 public void mousePressed(MouseEvent evt)
251 selectedRow = getSelectedRow(evt.getY() - scrollOffset);
252 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
254 // DETECT RIGHT MOUSE BUTTON IN AWT
255 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
259 PopupMenu popup = new PopupMenu("Annotations");
261 MenuItem item = new MenuItem(ADDNEW);
262 item.addActionListener(this);
266 // this never happens at moment: - see comment on JAL-563
269 item = new MenuItem(SHOWALL);
270 item.addActionListener(this);
274 popup.show(this, evt.getX(), evt.getY());
277 // add the rest if there are actually rows to show
278 item = new MenuItem(EDITNAME);
279 item.addActionListener(this);
281 item = new MenuItem(HIDE);
282 item.addActionListener(this);
286 item = new MenuItem(SHOWALL);
287 item.addActionListener(this);
291 item = new MenuItem(OUTPUT_TEXT);
292 item.addActionListener(this);
295 if (aa[selectedRow] == ap.av.consensus)
297 popup.addSeparator();
298 final CheckboxMenuItem cbmi = new CheckboxMenuItem(
299 "Ignore Gaps In Consensus", ap.av.getIgnoreGapsConsensus());
301 cbmi.addItemListener(new ItemListener()
303 public void itemStateChanged(ItemEvent e)
305 ap.av.setIgnoreGapsConsensus(cbmi.getState());
306 ap.paintAlignment(true);
310 item = new MenuItem(COPYCONS_SEQ);
311 item.addActionListener(this);
315 popup.show(this, evt.getX(), evt.getY());
318 if (selectedRow > -1 && selectedRow < aa.length)
320 if (aa[selectedRow].groupRef != null)
322 if (evt.getClickCount() >= 2)
324 // todo: make the ap scroll to the selection - not necessary, first click highlights/scrolls, second selects
325 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
326 ap.av.setSelectionGroup(// new SequenceGroup(
327 aa[selectedRow].groupRef); // );
328 ap.av.sendSelection();
329 ap.paintAlignment(false);
330 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
334 ap.seqPanel.ap.idPanel
335 .highlightSearchResults(aa[selectedRow].groupRef
336 .getSequences(null));
340 else if (aa[selectedRow].sequenceRef != null)
342 Vector sr = new Vector();
343 sr.addElement(aa[selectedRow].sequenceRef);
344 if (evt.getClickCount() == 1)
346 ap.seqPanel.ap.idPanel.highlightSearchResults(sr);
348 else if (evt.getClickCount() >= 2)
350 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
351 SequenceGroup sg = new SequenceGroup();
352 sg.addSequence(aa[selectedRow].sequenceRef, false);
353 ap.av.setSelectionGroup(sg);
354 ap.paintAlignment(false);
355 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
356 ap.av.sendSelection();
371 protected void copy_annotseqtoclipboard(SequenceI sq)
373 if (sq == null || sq.getLength() < 1)
377 jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
378 jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t"
379 + sq.getStart() + "\t" + sq.getEnd() + "\t"
380 + sq.getSequenceAsString() + "\n");
381 if (av.hasHiddenColumns)
383 jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector();
384 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
386 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
389 jalview.appletgui.AlignFrame.copiedHiddenColumns
390 .addElement(new int[]
391 { region[0], region[1] });
396 public void update(Graphics g)
401 public void paint(Graphics g)
403 int w = getSize().width;
404 if (image == null || w != image.getWidth(this))
406 image = createImage(w, ap.annotationPanel.getSize().height);
409 drawComponent(image.getGraphics(), w);
410 g.drawImage(image, 0, 0, this);
413 public void drawComponent(Graphics g, int width)
415 g.setFont(av.getFont());
416 FontMetrics fm = g.getFontMetrics(av.getFont());
417 g.setColor(Color.white);
418 g.fillRect(0, 0, getSize().width, getSize().height);
420 g.translate(0, scrollOffset);
421 g.setColor(Color.black);
423 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
424 int y = 0, fy = g.getFont().getSize();
429 hasHiddenRows = false;
430 for (int i = 0; i < aa.length; i++)
434 hasHiddenRows = true;
438 x = width - fm.stringWidth(aa[i].label) - 3;
441 offset = -(aa[i].height - fy) / 2;
443 g.drawString(aa[i].label, x, y + offset);