7c9d421481f1c6680ef9e913da6aa4b6cacffcb2
[jalview.git] / src / jalview / appletgui / CutAndPasteTransfer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 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 package jalview.appletgui;
20
21 import java.awt.*;
22 import java.awt.event.*;
23
24 import jalview.datamodel.*;
25 import jalview.io.*;
26
27 public class CutAndPasteTransfer
28     extends Panel implements ActionListener, MouseListener
29 {
30   boolean pdbImport = false;
31   boolean treeImport = false;
32   boolean annotationImport = false;
33   Sequence  seq;
34   AlignFrame alignFrame;
35
36   public CutAndPasteTransfer(boolean forImport, AlignFrame alignFrame)
37   {
38     try
39     {
40       jbInit();
41     }
42     catch (Exception e)
43     {
44       e.printStackTrace();
45     }
46
47     this.alignFrame = alignFrame;
48
49     if (!forImport)
50     {
51       buttonPanel.setVisible(false);
52     }
53   }
54
55   public String getText()
56   {
57     return textarea.getText();
58   }
59
60   public void setText(String text)
61   {
62     textarea.setText(text);
63   }
64
65   public void setPDBImport(Sequence seq)
66   {
67     this.seq = seq;
68     accept.setLabel("Accept");
69     addSequences.setVisible(false);
70     pdbImport = true;
71   }
72
73   public void setTreeImport()
74   {
75     treeImport = true;
76     accept.setLabel("Accept");
77     addSequences.setVisible(false);
78   }
79
80   public void setAnnotationImport()
81   {
82     annotationImport = true;
83     accept.setLabel("Accept");
84     addSequences.setVisible(false);
85   }
86
87   public void actionPerformed(ActionEvent evt)
88   {
89     if (evt.getSource() == accept)
90     {
91       ok(true);
92     }
93     else if (evt.getSource() == addSequences)
94     {
95       ok(false);
96     }
97     else if (evt.getSource() == cancel)
98     {
99       cancel();
100     }
101   }
102
103   protected void ok(boolean newWindow)
104   {
105     String text = getText();
106     int length = text.length();
107     textarea.append("\n");
108     if (textarea.getText().length() == length)
109     {
110       String warning =
111           "\n\n#################################################\n"
112           + "WARNING!! THIS IS THE MAXIMUM SIZE OF TEXTAREA!!\n"
113           + "\nCAN'T INPUT FULL ALIGNMENT"
114           + "\n\nYOU MUST DELETE THIS WARNING TO CONTINUE"
115           + "\n\nMAKE SURE LAST SEQUENCE PASTED IS COMPLETE"
116           + "\n#################################################\n";
117       textarea.setText(text.substring(0, text.length() - warning.length())
118                        + warning);
119
120       textarea.setCaretPosition(text.length());
121     }
122
123     if (pdbImport)
124     {
125       PDBEntry pdb = new PDBEntry();
126       pdb.setFile(text);
127
128       if ( alignFrame.alignPanel.av.applet.jmolAvailable )
129         new jalview.appletgui.AppletJmol(pdb,
130                                          new Sequence[]{seq},
131                                          null,
132                                          alignFrame.alignPanel,
133                                          AppletFormatAdapter.PASTE);
134       else
135
136         new MCview.AppletPDBViewer(pdb,
137                                    new Sequence[]{seq},
138                                    null,
139                                    alignFrame.alignPanel,
140                                    AppletFormatAdapter.PASTE);
141
142     }
143     else if (treeImport)
144     {
145       try
146       {
147         jalview.io.NewickFile fin = new jalview.io.NewickFile(textarea.getText(),
148             "Paste");
149
150         fin.parse();
151         if (fin.getTree() != null)
152         {
153           alignFrame.loadTree(fin, "Pasted tree file");
154         }
155
156       }
157       catch (Exception ex)
158       {
159         textarea.setText("Could not parse Newick file!\n" + ex);
160         return;
161       }
162     }
163     else if (annotationImport)
164     {
165       if (new AnnotationFile().readAnnotationFile(
166           alignFrame.viewport.alignment, textarea.getText(),
167           jalview.io.AppletFormatAdapter.PASTE))
168       {
169         alignFrame.alignPanel.fontChanged();
170         alignFrame.alignPanel.setScrollValues(0, 0);
171
172       }
173       else
174       {
175         alignFrame.parseFeaturesFile(textarea.getText(),
176                                      jalview.io.AppletFormatAdapter.PASTE);
177       }
178     }
179     else if (alignFrame != null)
180     {
181       Alignment al = null;
182
183       String format = new IdentifyFile().Identify(text,
184                                                   AppletFormatAdapter.PASTE);
185       try
186       {
187         al = new AppletFormatAdapter().readFile(text, AppletFormatAdapter.PASTE,
188                                                 format);
189       }
190       catch (java.io.IOException ex)
191       {
192         ex.printStackTrace();
193       }
194
195       if (al != null)
196       {
197         if (newWindow)
198         {
199           AlignFrame af = new AlignFrame(al, alignFrame.viewport.applet,
200                                          "Cut & Paste input - " + format,
201                                          false);
202           af.statusBar.setText("Successfully pasted alignment file");
203         }
204         else
205         {
206           alignFrame.addSequences(al.getSequencesArray());
207         }
208       }
209     }
210
211     if (this.getParent() instanceof Frame)
212     {
213       ( (Frame)this.getParent()).setVisible(false);
214     }
215     else
216     {
217       ( (Dialog)this.getParent()).setVisible(false);
218     }
219   }
220
221   protected void cancel()
222   {
223     textarea.setText("");
224     if (this.getParent() instanceof Frame)
225     {
226       ( (Frame)this.getParent()).setVisible(false);
227     }
228     else
229     {
230       ( (Dialog)this.getParent()).setVisible(false);
231     }
232   }
233
234   protected TextArea textarea = new TextArea();
235   Button accept = new Button("New Window");
236   Button addSequences = new Button("Add to Current Alignment");
237   Button cancel = new Button("Close");
238
239   protected Panel buttonPanel = new Panel();
240   BorderLayout borderLayout1 = new BorderLayout();
241
242   private void jbInit()
243       throws Exception
244   {
245     textarea.setFont(new java.awt.Font("Monospaced", Font.PLAIN, 10));
246     textarea.setText("Paste your alignment file here");
247     textarea.addMouseListener(this);
248     this.setLayout(borderLayout1);
249     accept.addActionListener(this);
250     addSequences.addActionListener(this);
251     cancel.addActionListener(this);
252     this.add(buttonPanel, BorderLayout.SOUTH);
253     buttonPanel.add(accept, null);
254     buttonPanel.add(addSequences);
255     buttonPanel.add(cancel, null);
256     this.add(textarea, java.awt.BorderLayout.CENTER);
257   }
258
259   public void mousePressed(MouseEvent evt)
260   {
261     if (textarea.getText().startsWith("Paste your"))
262     {
263       textarea.setText("");
264     }
265   }
266
267   public void mouseReleased(MouseEvent evt)
268   {}
269
270   public void mouseClicked(MouseEvent evt)
271   {}
272
273   public void mouseEntered(MouseEvent evt)
274   {}
275
276   public void mouseExited(MouseEvent evt)
277   {}
278 }