One class for edit name/description
[jalview.git] / src / jalview / appletgui / AnnotationLabels.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 \r
20 package jalview.appletgui;\r
21 \r
22 import java.awt.*;\r
23 import java.awt.event.*;\r
24 import java.util.Vector;\r
25 \r
26 import jalview.datamodel.*;\r
27 \r
28 public class AnnotationLabels\r
29     extends Panel implements ActionListener, MouseListener, MouseMotionListener\r
30 {\r
31   Image image;\r
32   boolean active = false;\r
33   AlignmentPanel ap;\r
34   AlignViewport av;\r
35   boolean resizing = false;\r
36   int oldY, mouseX;\r
37   static String EDITNAME = "Edit label/description";\r
38   static String HIDE = "Hide this row";\r
39   static String DELETE = "Delete this row";\r
40   static String SHOWALL = "Show all hidden rows";\r
41   static String OUTPUT_TEXT = "Show Values In Textbox";\r
42   static String COPYCONS_SEQ = "Copy Consensus Sequence";\r
43 \r
44   int selectedRow = 0;\r
45   int scrollOffset = 0;\r
46 \r
47   Tooltip tooltip;\r
48 \r
49   public AnnotationLabels(AlignmentPanel ap)\r
50   {\r
51     this.ap = ap;\r
52     this.av = ap.av;\r
53     setLayout(null);\r
54     addMouseListener(this);\r
55     addMouseMotionListener(this);\r
56   }\r
57 \r
58   public AnnotationLabels(AlignViewport av)\r
59 {\r
60   this.av = av;\r
61 }\r
62 \r
63 \r
64   public void setScrollOffset(int y)\r
65   {\r
66     scrollOffset = y;\r
67     repaint();\r
68   }\r
69 \r
70   void getSelectedRow(int y)\r
71   {\r
72     selectedRow = -1;\r
73     AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();\r
74 \r
75     if(aa==null)\r
76       return;\r
77 \r
78     int height = 0;\r
79     for (int i = 0; i < aa.length; i++)\r
80     {\r
81       if (!aa[i].visible)\r
82       {\r
83         continue;\r
84       }\r
85 \r
86       height += aa[i].height;\r
87       if (y < height)\r
88       {\r
89         selectedRow = i;\r
90         break;\r
91       }\r
92     }\r
93   }\r
94 \r
95   public void actionPerformed(ActionEvent evt)\r
96   {\r
97     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();\r
98 \r
99     if(evt.getActionCommand().equals(EDITNAME))\r
100     {\r
101       EditNameDialog dialog = new EditNameDialog(\r
102           aa[selectedRow].label,\r
103           aa[selectedRow].description,\r
104           "       Annotation Label",\r
105           "Annotation Description",\r
106           ap,\r
107           "Edit Annotation Name / Description");\r
108 \r
109       if (dialog.accept)\r
110       {\r
111         aa[selectedRow].label = dialog.getName();\r
112         aa[selectedRow].description = dialog.getDescription();\r
113         repaint();\r
114       }\r
115     }\r
116     if (evt.getActionCommand().equals(HIDE))\r
117     {\r
118       aa[selectedRow].visible = false;\r
119     }\r
120     else if (evt.getActionCommand().equals(SHOWALL))\r
121     {\r
122       for (int i = 0; i < aa.length; i++)\r
123       {\r
124         aa[i].visible = true;\r
125       }\r
126     }\r
127     else if (evt.getActionCommand().equals(OUTPUT_TEXT))\r
128     {\r
129       CutAndPasteTransfer cap = new CutAndPasteTransfer(false, ap.alignFrame);\r
130       Frame frame = new Frame();\r
131       frame.add(cap);\r
132       jalview.bin.JalviewLite.addFrame(frame,\r
133                                        ap.alignFrame.getTitle() + " - " +\r
134                                        aa[selectedRow].label, 500, 100);\r
135       cap.setText(aa[selectedRow].toString());\r
136     }\r
137     else if (evt.getActionCommand().equals(COPYCONS_SEQ))\r
138     {\r
139       SequenceI cons=av.getConsensusSeq();\r
140       if (cons!=null)\r
141         copy_annotseqtoclipboard(cons);\r
142 \r
143     }\r
144     ap.annotationPanel.adjustPanelHeight();\r
145     setSize(getSize().width, ap.annotationPanel.getSize().height);\r
146     ap.validate();\r
147     ap.repaint();\r
148   }\r
149 \r
150   public void mouseMoved(MouseEvent evt)\r
151   {\r
152     getSelectedRow(evt.getY() - scrollOffset);\r
153 \r
154     if (selectedRow > -1)\r
155     {\r
156       if (tooltip == null)\r
157         tooltip = new Tooltip(ap.av.alignment.\r
158                                           getAlignmentAnnotation()[selectedRow].\r
159                                           description,\r
160                                           this);\r
161       else\r
162         tooltip.setTip(ap.av.alignment.\r
163                      getAlignmentAnnotation()[selectedRow].description);\r
164     }\r
165     else if (tooltip != null)\r
166     {\r
167       tooltip.setTip("");\r
168     }\r
169 \r
170   }\r
171 \r
172   public void mouseDragged(MouseEvent evt)\r
173   {}\r
174   public void mouseClicked(MouseEvent evt)\r
175   {}\r
176   public void mouseReleased(MouseEvent evt)\r
177   {}\r
178   public void mouseEntered(MouseEvent evt)\r
179   {}\r
180   public void mouseExited(MouseEvent evt)\r
181   {}\r
182   public void mousePressed(MouseEvent evt)\r
183   {\r
184     AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();\r
185 \r
186     PopupMenu pop = new PopupMenu("Annotations");\r
187 \r
188     MenuItem item = new MenuItem(EDITNAME);\r
189     item.addActionListener(this);\r
190     pop.add(item);\r
191     item = new MenuItem(HIDE);\r
192     item.addActionListener(this);\r
193     pop.add(item);\r
194     item = new MenuItem(SHOWALL);\r
195     item.addActionListener(this);\r
196     pop.add(item);\r
197     this.add(pop);\r
198     item = new MenuItem(OUTPUT_TEXT);\r
199     item.addActionListener(this);\r
200     pop.add(item);\r
201 \r
202     if (aa[selectedRow]==ap.av.consensus)\r
203     {\r
204       pop.addSeparator();\r
205       final CheckboxMenuItem cbmi = new CheckboxMenuItem(\r
206           "Ignore Gaps In Consensus",\r
207           ap.av.getIgnoreGapsConsensus());\r
208 \r
209       cbmi.addItemListener(new ItemListener()\r
210       {\r
211         public void itemStateChanged(ItemEvent e)\r
212         {\r
213           ap.av.setIgnoreGapsConsensus(cbmi.getState());\r
214           ap.repaint();\r
215         }\r
216       });\r
217       pop.add(cbmi);\r
218       final MenuItem cpcons=new MenuItem(COPYCONS_SEQ);\r
219       cpcons.addActionListener(this);\r
220       pop.add(cpcons);\r
221     }\r
222 \r
223      pop.show(this, evt.getX(), evt.getY());\r
224 \r
225   }\r
226 /**\r
227      * DOCUMENT ME!\r
228      *\r
229      * @param e DOCUMENT ME!\r
230      */\r
231     protected void copy_annotseqtoclipboard(SequenceI sq)\r
232     {\r
233       if (sq==null || sq.getLength()<1)\r
234         return;\r
235       jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();\r
236       jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t" +\r
237           sq.getStart() + "\t" +\r
238           sq.getEnd() + "\t" +\r
239           sq.getSequenceAsString() + "\n");\r
240       if (av.hasHiddenColumns)\r
241       {\r
242         jalview.appletgui.AlignFrame.copiedHiddenColumns=new Vector();\r
243         for(int i=0; i<av.getColumnSelection().getHiddenColumns().size(); i++)\r
244         {\r
245           int[] region = (int[])\r
246               av.getColumnSelection().getHiddenColumns().elementAt(i);\r
247 \r
248           jalview.appletgui.AlignFrame.copiedHiddenColumns.addElement(new int[]{region[0],\r
249                             region[1]});\r
250         }\r
251       }\r
252     }\r
253 \r
254     public void update(Graphics g)\r
255     {\r
256       paint(g);\r
257     }\r
258 \r
259   public void paint(Graphics g)\r
260   {\r
261     int w = getSize().width;\r
262     if (image == null || w != image.getWidth(this))\r
263     {\r
264       image = createImage(w, ap.annotationPanel.getSize().height);\r
265     }\r
266 \r
267     drawComponent(image.getGraphics(), w);\r
268     g.drawImage(image,0,0,this);\r
269   }\r
270 \r
271   public void drawComponent(Graphics g, int width)\r
272   {\r
273     g.setFont(av.getFont());\r
274     FontMetrics fm = g.getFontMetrics(av.getFont());\r
275     g.setColor(Color.white);\r
276     g.fillRect(0, 0, getSize().width, getSize().height);\r
277 \r
278     g.translate(0, scrollOffset);\r
279     g.setColor(Color.black);\r
280 \r
281     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();\r
282     int y = g.getFont().getSize();\r
283     int x = 0;\r
284 \r
285     if (aa != null)\r
286     {\r
287       for (int i = 0; i < aa.length; i++)\r
288       {\r
289         if (!aa[i].visible)\r
290         {\r
291           continue;\r
292         }\r
293 \r
294         x = width - fm.stringWidth(aa[i].label) - 3;\r
295 \r
296         if (aa[i].graph>0)\r
297         {\r
298           y += (aa[i].height / 3);\r
299         }\r
300 \r
301         g.drawString(aa[i].label, x, y);\r
302 \r
303         if (aa[i].graph>0)\r
304         {\r
305           y += (2 * aa[i].height / 3);\r
306         }\r
307         else\r
308         {\r
309           y += aa[i].height;\r
310         }\r
311       }\r
312     }\r
313   }\r
314 \r
315 }\r