added copy consensus sequence to consensus annotation popup menu
[jalview.git] / src / jalview / appletgui / AnnotationLabels.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 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.awt.*;
23 import java.awt.event.*;
24 import java.util.Vector;
25
26 import jalview.datamodel.*;
27
28 public class AnnotationLabels
29     extends Panel implements ActionListener
30 {
31   boolean active = false;
32   AlignmentPanel ap;
33   AlignViewport av;
34   boolean resizing = false;
35   int oldY, mouseX;
36   static String ADDNEW = "Add new row";
37   static String HIDE = "Hide this row";
38   static String DELETE = "Delete this row";
39   static String SHOWALL = "Show all hidden rows";
40   static String OUTPUT_TEXT = "Show Values In Textbox";
41   static String COPYCONS_SEQ = "Copy Consensus Sequence";
42   
43   int selectedRow = 0;
44   int scrollOffset = 0;
45
46   public AnnotationLabels(AlignmentPanel ap)
47   {
48     this.ap = ap;
49     this.av = ap.av;
50     setLayout(null);
51     addMouseListener(new MouseAdapter()
52     {
53       public void mousePressed(MouseEvent evt)
54       {
55         doMousePressed(evt);
56       }
57     });
58   }
59
60   public AnnotationLabels(AlignViewport av)
61 {
62   this.av = av;
63 }
64
65
66   public void setScrollOffset(int y)
67   {
68     scrollOffset = y;
69     repaint();
70   }
71
72   public void actionPerformed(ActionEvent evt)
73   {
74     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
75
76     if (evt.getActionCommand().equals(HIDE))
77     {
78       aa[selectedRow].visible = false;
79     }
80     else if (evt.getActionCommand().equals(SHOWALL))
81     {
82       for (int i = 0; i < aa.length; i++)
83       {
84         aa[i].visible = true;
85       }
86     }
87     else if (evt.getActionCommand().equals(OUTPUT_TEXT))
88     {
89       CutAndPasteTransfer cap = new CutAndPasteTransfer(false, ap.alignFrame);
90       Frame frame = new Frame();
91       frame.add(cap);
92       jalview.bin.JalviewLite.addFrame(frame,
93                                        ap.alignFrame.getTitle() + " - " +
94                                        aa[selectedRow].label, 500, 100);
95       cap.setText(aa[selectedRow].toString());
96     }
97     else if (evt.getActionCommand().equals(COPYCONS_SEQ))
98     {
99       SequenceI cons=av.getConsensusSeq();
100       if (cons!=null)
101         copy_annotseqtoclipboard(cons);
102       
103     }
104     ap.annotationPanel.adjustPanelHeight();
105     setSize(getSize().width, ap.annotationPanel.getSize().height);
106     ap.validate();
107     ap.repaint();
108   }
109
110   public void doMousePressed(MouseEvent evt)
111   {
112     int y = evt.getY() - scrollOffset;
113     AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
114     int height = 0;
115     for (int i = 0; i < aa.length; i++)
116     {
117       if (!aa[i].visible)
118       {
119         continue;
120       }
121
122       height += aa[i].height;
123       if (y < height)
124       {
125         selectedRow = i;
126         break;
127       }
128     }
129
130     PopupMenu pop = new PopupMenu("Annotations");
131     MenuItem item = new MenuItem(HIDE);
132     item.addActionListener(this);
133     pop.add(item);
134     item = new MenuItem(SHOWALL);
135     item.addActionListener(this);
136     pop.add(item);
137     this.add(pop);
138     item = new MenuItem(OUTPUT_TEXT);
139     item.addActionListener(this);
140     pop.add(item);
141
142     if (aa[selectedRow].label.equals("Consensus"))
143     {
144       pop.addSeparator();
145       final CheckboxMenuItem cbmi = new CheckboxMenuItem(
146           "Ignore Gaps In Consensus",
147           ap.av.getIgnoreGapsConsensus());
148
149       cbmi.addItemListener(new ItemListener()
150       {
151         public void itemStateChanged(ItemEvent e)
152         {
153           ap.av.setIgnoreGapsConsensus(cbmi.getState());
154           ap.repaint();
155         }
156       });
157       pop.add(cbmi);
158       final MenuItem cpcons=new MenuItem(COPYCONS_SEQ);
159       cpcons.addActionListener(this);
160       pop.add(cpcons);
161     }
162
163      pop.show(this, evt.getX(), evt.getY());
164
165   }
166 /**
167      * DOCUMENT ME!
168      *
169      * @param e DOCUMENT ME!
170      */
171     protected void copy_annotseqtoclipboard(SequenceI sq)
172     {
173       if (sq==null || sq.getLength()<1)
174         return;
175       jalview.appletgui.AlignFrame.copiedSequences = new StringBuffer();
176       jalview.appletgui.AlignFrame.copiedSequences.append(sq.getName() + "\t" +
177           sq.getStart() + "\t" +
178           sq.getEnd() + "\t" +
179           sq.getSequence() + "\n");
180       if (av.hasHiddenColumns)
181       {
182         jalview.appletgui.AlignFrame.copiedHiddenColumns=new Vector();
183         for(int i=0; i<av.getColumnSelection().getHiddenColumns().size(); i++)
184         {
185           int[] region = (int[])
186               av.getColumnSelection().getHiddenColumns().elementAt(i);
187
188           jalview.appletgui.AlignFrame.copiedHiddenColumns.addElement(new int[]{region[0],
189                             region[1]});
190         }
191       }
192     }
193
194   public void paint(Graphics g)
195   {
196     drawComponent(g, getSize().width);
197   }
198
199   public void drawComponent(Graphics g, int width)
200   {
201     g.setFont(av.getFont());
202     FontMetrics fm = g.getFontMetrics(av.getFont());
203     g.setColor(Color.white);
204     g.fillRect(0, 0, getSize().width, getSize().height);
205
206     g.translate(0, scrollOffset);
207     g.setColor(Color.black);
208
209     AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
210     int y = g.getFont().getSize();
211     int x = 0;
212
213     if (aa != null)
214     {
215       for (int i = 0; i < aa.length; i++)
216       {
217         if (!aa[i].visible)
218         {
219           continue;
220         }
221
222         x = width - fm.stringWidth(aa[i].label) - 3;
223
224         if (aa[i].graph>0)
225         {
226           y += (aa[i].height / 3);
227         }
228
229         g.drawString(aa[i].label, x, y);
230
231         if (aa[i].graph>0)
232         {
233           y += (2 * aa[i].height / 3);
234         }
235         else
236         {
237           y += aa[i].height;
238         }
239       }
240     }
241   }
242
243 }