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