AlignmentAnnotation: added annotation score attribute and allowed for annotation...
[jalview.git] / src / jalview / appletgui / AnnotationLabels.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  *
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.
9  *
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.
14  *
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
18  */
19
20 package jalview.appletgui;
21
22 import java.util.*;
23
24 import java.awt.*;
25 import java.awt.event.*;
26
27 import jalview.datamodel.*;
28
29 public class AnnotationLabels
30     extends Panel implements ActionListener, MouseListener, MouseMotionListener
31 {
32   Image image;
33   boolean active = false;
34   AlignmentPanel ap;
35   AlignViewport av;
36   boolean resizing = false;
37   int oldY, mouseX;
38
39   static String ADDNEW = "Add New Row";
40   static String EDITNAME = "Edit Label/Description";
41   static String HIDE = "Hide This Row";
42   static String SHOWALL = "Show All Hidden Rows";
43   static String OUTPUT_TEXT = "Show Values In Textbox";
44   static String COPYCONS_SEQ = "Copy Consensus Sequence";
45
46   int scrollOffset = 0;
47   int selectedRow = -1;
48
49   Tooltip tooltip;
50
51   public AnnotationLabels(AlignmentPanel ap)
52   {
53     this.ap = ap;
54     this.av = ap.av;
55     setLayout(null);
56     addMouseListener(this);
57     addMouseMotionListener(this);
58   }
59
60   public AnnotationLabels(AlignViewport av)
61   {
62     this.av = av;
63   }
64
65   public void setScrollOffset(int y)
66   {
67     scrollOffset = y;
68     repaint();
69   }
70
71   int getSelectedRow(int y)
72   {
73     int row = -1;
74     AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
75
76     if (aa == null)
77     {
78       return row;
79     }
80
81     int height = 0;
82     for (int i = 0; i < aa.length; i++)
83     {
84       if (!aa[i].visible)
85       {
86         continue;
87       }
88
89       height += aa[i].height;
90       if (y < height)
91       {
92         row = i;
93         break;
94       }
95     }
96
97     return row;
98   }
99
100   public void actionPerformed(ActionEvent evt)
101   {
102     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
103
104     if (evt.getActionCommand().equals(ADDNEW))
105     {
106       AlignmentAnnotation newAnnotation = new AlignmentAnnotation("",
107           null,
108           new Annotation[ap.av.alignment.getWidth()]);
109
110       if (!editLabelDescription(newAnnotation))
111       {
112         return;
113       }
114
115       ap.av.alignment.addAnnotation(newAnnotation);
116       ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
117     }
118     else if (evt.getActionCommand().equals(EDITNAME))
119     {
120       editLabelDescription(aa[selectedRow]);
121     }
122     else if (evt.getActionCommand().equals(HIDE))
123     {
124       aa[selectedRow].visible = false;
125     }
126     else if (evt.getActionCommand().equals(SHOWALL))
127     {
128       for (int i = 0; i < aa.length; i++)
129       {
130         aa[i].visible = (aa[i].annotations==null) ? false : true;
131       }
132     }
133     else if (evt.getActionCommand().equals(OUTPUT_TEXT))
134     {
135       CutAndPasteTransfer cap = new CutAndPasteTransfer(false, ap.alignFrame);
136       Frame frame = new Frame();
137       frame.add(cap);
138       jalview.bin.JalviewLite.addFrame(frame,
139                                        ap.alignFrame.getTitle() + " - " +
140                                        aa[selectedRow].label, 500, 100);
141       cap.setText(aa[selectedRow].toString());
142     }
143     else if (evt.getActionCommand().equals(COPYCONS_SEQ))
144     {
145       SequenceI cons = av.getConsensusSeq();
146       if (cons != null)
147       {
148         copy_annotseqtoclipboard(cons);
149       }
150
151     }
152     ap.annotationPanel.adjustPanelHeight();
153     setSize(getSize().width, ap.annotationPanel.getSize().height);
154     ap.validate();
155     ap.paintAlignment(true);
156   }
157
158   boolean editLabelDescription(AlignmentAnnotation annotation)
159   {
160     EditNameDialog dialog = new EditNameDialog(
161         annotation.label,
162         annotation.description,
163         "       Annotation Label",
164         "Annotation Description",
165         ap.alignFrame,
166         "Edit Annotation Name / Description",
167         500, 100);
168
169     if (dialog.accept)
170     {
171       annotation.label = dialog.getName();
172       annotation.description = dialog.getDescription();
173       repaint();
174       return true;
175     }
176     else
177       return false;
178
179   }
180
181   public void mouseMoved(MouseEvent evt)
182   {
183     int row = getSelectedRow(evt.getY() - scrollOffset);
184
185     if (row > -1)
186     {
187       if (tooltip == null)
188       {
189         tooltip = new Tooltip(ap.av.alignment.
190                               getAlignmentAnnotation()[row].
191                               description,
192                               this);
193       }
194       else
195       {
196         tooltip.setTip(ap.av.alignment.
197                        getAlignmentAnnotation()[row].description);
198       }
199     }
200     else if (tooltip != null)
201     {
202       tooltip.setTip("");
203     }
204
205   }
206
207   public void mouseDragged(MouseEvent evt)
208   {}
209
210   public void mouseClicked(MouseEvent evt)
211   {}
212
213   public void mouseReleased(MouseEvent evt)
214   {}
215
216   public void mouseEntered(MouseEvent evt)
217   {}
218
219   public void mouseExited(MouseEvent evt)
220   {}
221
222   public void mousePressed(MouseEvent evt)
223   {
224     selectedRow = getSelectedRow(evt.getY() - scrollOffset);
225
226     AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
227
228     PopupMenu popup = new PopupMenu("Annotations");
229
230     MenuItem item = new MenuItem(ADDNEW);
231     item.addActionListener(this);
232     popup.add(item);
233     item = new MenuItem(EDITNAME);
234     item.addActionListener(this);
235     popup.add(item);
236     item = new MenuItem(HIDE);
237     item.addActionListener(this);
238     popup.add(item);
239     item = new MenuItem(SHOWALL);
240     item.addActionListener(this);
241     popup.add(item);
242     this.add(popup);
243     item = new MenuItem(OUTPUT_TEXT);
244     item.addActionListener(this);
245     popup.add(item);
246
247     if (aa[selectedRow] == ap.av.consensus)
248     {
249       popup.addSeparator();
250       final CheckboxMenuItem cbmi = new CheckboxMenuItem(
251           "Ignore Gaps In Consensus",
252           ap.av.getIgnoreGapsConsensus());
253
254       cbmi.addItemListener(new ItemListener()
255       {
256         public void itemStateChanged(ItemEvent e)
257         {
258           ap.av.setIgnoreGapsConsensus(cbmi.getState());
259           ap.paintAlignment(true);
260         }
261       });
262       popup.add(cbmi);
263       item = new MenuItem(COPYCONS_SEQ);
264       item.addActionListener(this);
265       popup.add(item);
266     }
267
268     popup.show(this, evt.getX(), evt.getY());
269
270   }
271
272   /**
273    * DOCUMENT ME!
274    *
275    * @param e DOCUMENT ME!
276    */
277   protected void copy_annotseqtoclipboard(SequenceI sq)
278   {
279     if (sq == null || sq.getLength() < 1)
280     {
281       return;
282     }
283     jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
284     jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t" +
285         sq.getStart() + "\t" +
286         sq.getEnd() + "\t" +
287         sq.getSequenceAsString() + "\n");
288     if (av.hasHiddenColumns)
289     {
290       jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector();
291       for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
292       {
293         int[] region = (int[])
294             av.getColumnSelection().getHiddenColumns().elementAt(i);
295
296         jalview.appletgui.AlignFrame.copiedHiddenColumns.addElement(new int[]
297             {region[0],
298             region[1]});
299       }
300     }
301   }
302
303   public void update(Graphics g)
304   {
305     paint(g);
306   }
307
308   public void paint(Graphics g)
309   {
310     int w = getSize().width;
311     if (image == null || w != image.getWidth(this))
312     {
313       image = createImage(w, ap.annotationPanel.getSize().height);
314     }
315
316     drawComponent(image.getGraphics(), w);
317     g.drawImage(image, 0, 0, this);
318   }
319
320   public void drawComponent(Graphics g, int width)
321   {
322     g.setFont(av.getFont());
323     FontMetrics fm = g.getFontMetrics(av.getFont());
324     g.setColor(Color.white);
325     g.fillRect(0, 0, getSize().width, getSize().height);
326
327     g.translate(0, scrollOffset);
328     g.setColor(Color.black);
329
330     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
331     int y = g.getFont().getSize();
332     int x = 0;
333
334     if (aa != null)
335     {
336       for (int i = 0; i < aa.length; i++)
337       {
338         if (!aa[i].visible)
339         {
340           continue;
341         }
342
343         x = width - fm.stringWidth(aa[i].label) - 3;
344
345         if (aa[i].graph > 0)
346         {
347           y += (aa[i].height / 3);
348         }
349
350         g.drawString(aa[i].label, x, y);
351
352         if (aa[i].graph > 0)
353         {
354           y += (2 * aa[i].height / 3);
355         }
356         else
357         {
358           y += aa[i].height;
359         }
360       }
361     }
362   }
363
364 }