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