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