setVisible flag sent to EditNameDialog
[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     Checkbox padGaps = new Checkbox("Fill Empty Gaps With \""
161                                     +ap.av.getGapCharacter()+"\"",
162                                     annotation.padGaps);
163
164     EditNameDialog dialog = new EditNameDialog(
165         annotation.label,
166         annotation.description,
167         "      Annotation Label",
168         "Annotation Description",
169         ap.alignFrame,
170         "Edit Annotation Name / Description",
171         500, 180, false);
172
173     Panel empty = new Panel(new FlowLayout());
174     empty.add(padGaps);
175     dialog.add(empty);
176     dialog.pack();
177
178     dialog.setVisible(true);
179
180     if (dialog.accept)
181     {
182       annotation.label = dialog.getName();
183       annotation.description = dialog.getDescription();
184       annotation.setPadGaps(padGaps.getState(), av.getGapCharacter());
185       repaint();
186       return true;
187     }
188     else
189       return false;
190
191   }
192
193   public void mouseMoved(MouseEvent evt)
194   {
195     int row = getSelectedRow(evt.getY() - scrollOffset);
196
197     if (row > -1)
198     {
199       if (tooltip == null)
200       {
201         tooltip = new Tooltip(ap.av.alignment.
202                               getAlignmentAnnotation()[row].
203                               description,
204                               this);
205       }
206       else
207       {
208         tooltip.setTip(ap.av.alignment.
209                        getAlignmentAnnotation()[row].description);
210       }
211     }
212     else if (tooltip != null)
213     {
214       tooltip.setTip("");
215     }
216
217   }
218
219   public void mouseDragged(MouseEvent evt)
220   {}
221
222   public void mouseClicked(MouseEvent evt)
223   {}
224
225   public void mouseReleased(MouseEvent evt)
226   {}
227
228   public void mouseEntered(MouseEvent evt)
229   {}
230
231   public void mouseExited(MouseEvent evt)
232   {}
233
234   public void mousePressed(MouseEvent evt)
235   {
236     selectedRow = getSelectedRow(evt.getY() - scrollOffset);
237
238     AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
239
240     PopupMenu popup = new PopupMenu("Annotations");
241
242     MenuItem item = new MenuItem(ADDNEW);
243     item.addActionListener(this);
244     popup.add(item);
245     item = new MenuItem(EDITNAME);
246     item.addActionListener(this);
247     popup.add(item);
248     item = new MenuItem(HIDE);
249     item.addActionListener(this);
250     popup.add(item);
251     item = new MenuItem(SHOWALL);
252     item.addActionListener(this);
253     popup.add(item);
254     this.add(popup);
255     item = new MenuItem(OUTPUT_TEXT);
256     item.addActionListener(this);
257     popup.add(item);
258
259     if (aa[selectedRow] == ap.av.consensus)
260     {
261       popup.addSeparator();
262       final CheckboxMenuItem cbmi = new CheckboxMenuItem(
263           "Ignore Gaps In Consensus",
264           ap.av.getIgnoreGapsConsensus());
265
266       cbmi.addItemListener(new ItemListener()
267       {
268         public void itemStateChanged(ItemEvent e)
269         {
270           ap.av.setIgnoreGapsConsensus(cbmi.getState());
271           ap.paintAlignment(true);
272         }
273       });
274       popup.add(cbmi);
275       item = new MenuItem(COPYCONS_SEQ);
276       item.addActionListener(this);
277       popup.add(item);
278     }
279
280     popup.show(this, evt.getX(), evt.getY());
281
282   }
283
284   /**
285    * DOCUMENT ME!
286    *
287    * @param e DOCUMENT ME!
288    */
289   protected void copy_annotseqtoclipboard(SequenceI sq)
290   {
291     if (sq == null || sq.getLength() < 1)
292     {
293       return;
294     }
295     jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
296     jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t" +
297         sq.getStart() + "\t" +
298         sq.getEnd() + "\t" +
299         sq.getSequenceAsString() + "\n");
300     if (av.hasHiddenColumns)
301     {
302       jalview.appletgui.AlignFrame.copiedHiddenColumns = new Vector();
303       for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
304       {
305         int[] region = (int[])
306             av.getColumnSelection().getHiddenColumns().elementAt(i);
307
308         jalview.appletgui.AlignFrame.copiedHiddenColumns.addElement(new int[]
309             {region[0],
310             region[1]});
311       }
312     }
313   }
314
315   public void update(Graphics g)
316   {
317     paint(g);
318   }
319
320   public void paint(Graphics g)
321   {
322     int w = getSize().width;
323     if (image == null || w != image.getWidth(this))
324     {
325       image = createImage(w, ap.annotationPanel.getSize().height);
326     }
327
328     drawComponent(image.getGraphics(), w);
329     g.drawImage(image, 0, 0, this);
330   }
331
332   public void drawComponent(Graphics g, int width)
333   {
334     g.setFont(av.getFont());
335     FontMetrics fm = g.getFontMetrics(av.getFont());
336     g.setColor(Color.white);
337     g.fillRect(0, 0, getSize().width, getSize().height);
338
339     g.translate(0, scrollOffset);
340     g.setColor(Color.black);
341
342     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
343     int y = g.getFont().getSize();
344     int x = 0;
345
346     if (aa != null)
347     {
348       for (int i = 0; i < aa.length; i++)
349       {
350         if (!aa[i].visible)
351         {
352           continue;
353         }
354
355         x = width - fm.stringWidth(aa[i].label) - 3;
356
357         if (aa[i].graph > 0)
358         {
359           y += (aa[i].height / 3);
360         }
361
362         g.drawString(aa[i].label, x, y);
363
364         if (aa[i].graph > 0)
365         {
366           y += (2 * aa[i].height / 3);
367         }
368         else
369         {
370           y += aa[i].height;
371         }
372       }
373     }
374   }
375
376 }