JAL-1432 updated copyright notices
[jalview.git] / src / jalview / appletgui / CutAndPasteTransfer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
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  * The Jalview Authors are detailed in the 'AUTHORS' file.
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 import jalview.schemes.TCoffeeColourScheme;
27
28 public class CutAndPasteTransfer extends Panel implements ActionListener,
29         MouseListener
30 {
31   boolean pdbImport = false;
32
33   boolean treeImport = false;
34
35   boolean annotationImport = false;
36
37   Sequence seq;
38
39   AlignFrame alignFrame;
40
41   public CutAndPasteTransfer(boolean forImport, AlignFrame alignFrame)
42   {
43     try
44     {
45       jbInit();
46     } catch (Exception e)
47     {
48       e.printStackTrace();
49     }
50
51     this.alignFrame = alignFrame;
52
53     if (!forImport)
54     {
55       buttonPanel.setVisible(false);
56     }
57   }
58
59   public String getText()
60   {
61     return textarea.getText();
62   }
63
64   public void setText(String text)
65   {
66     textarea.setText(text);
67   }
68
69   public void setPDBImport(Sequence seq)
70   {
71     this.seq = seq;
72     accept.setLabel("Accept");
73     addSequences.setVisible(false);
74     pdbImport = true;
75   }
76
77   public void setTreeImport()
78   {
79     treeImport = true;
80     accept.setLabel("Accept");
81     addSequences.setVisible(false);
82   }
83
84   public void setAnnotationImport()
85   {
86     annotationImport = true;
87     accept.setLabel("Accept");
88     addSequences.setVisible(false);
89   }
90
91   public void actionPerformed(ActionEvent evt)
92   {
93     if (evt.getSource() == accept)
94     {
95       ok(true);
96     }
97     else if (evt.getSource() == addSequences)
98     {
99       ok(false);
100     }
101     else if (evt.getSource() == cancel)
102     {
103       cancel();
104     }
105   }
106
107   protected void ok(boolean newWindow)
108   {
109     String text = getText();
110     int length = text.length();
111     textarea.append("\n");
112     if (textarea.getText().length() == length)
113     {
114       String warning = "\n\n#################################################\n"
115               + "WARNING!! THIS IS THE MAXIMUM SIZE OF TEXTAREA!!\n"
116               + "\nCAN'T INPUT FULL ALIGNMENT"
117               + "\n\nYOU MUST DELETE THIS WARNING TO CONTINUE"
118               + "\n\nMAKE SURE LAST SEQUENCE PASTED IS COMPLETE"
119               + "\n#################################################\n";
120       textarea.setText(text.substring(0, text.length() - warning.length())
121               + warning);
122
123       textarea.setCaretPosition(text.length());
124     }
125
126     if (pdbImport)
127     {
128       PDBEntry pdb = new PDBEntry();
129       pdb.setFile(text);
130
131       if (alignFrame.alignPanel.av.applet.jmolAvailable)
132         new jalview.appletgui.AppletJmol(pdb, new Sequence[]
133         { seq }, null, alignFrame.alignPanel, AppletFormatAdapter.PASTE);
134       else
135
136         new MCview.AppletPDBViewer(pdb, new Sequence[]
137         { seq }, null, alignFrame.alignPanel, AppletFormatAdapter.PASTE);
138
139     }
140     else if (treeImport)
141     {
142       try
143       {
144         jalview.io.NewickFile fin = new jalview.io.NewickFile(
145                 textarea.getText(), "Paste");
146
147         fin.parse();
148         if (fin.getTree() != null)
149         {
150           alignFrame.loadTree(fin, "Pasted tree file");
151         }
152
153       } catch (Exception ex)
154       {
155         // TODO: JAL-1102 - should have a warning message in dialog, not simply
156         // overwrite the broken input data with the exception
157         textarea.setText("Could not parse Newick file!\n" + ex);
158         return;
159       }
160     }
161     else if (annotationImport)
162     {
163       TCoffeeScoreFile tcf = null;
164       try
165       {
166         tcf = new TCoffeeScoreFile(textarea.getText(),
167                 jalview.io.AppletFormatAdapter.PASTE);
168         if (tcf.isValid())
169         {
170           if (tcf.annotateAlignment(alignFrame.viewport.getAlignment(),
171                   true))
172           {
173             alignFrame.tcoffeeColour.setEnabled(true);
174             alignFrame.alignPanel.fontChanged();
175             alignFrame.changeColour(new TCoffeeColourScheme(
176                     alignFrame.viewport.getAlignment()));
177             alignFrame.statusBar
178                     .setText("Successfully pasted T-Coffee scores to alignment.");
179           }
180           else
181           {
182             // file valid but didn't get added to alignment for some reason
183             alignFrame.statusBar.setText("Failed to add T-Coffee scores: "
184                     + (tcf.getWarningMessage() != null ? tcf
185                             .getWarningMessage() : ""));
186           }
187         }
188         else
189         {
190           tcf = null;
191         }
192       } catch (Exception x)
193       {
194         tcf = null;
195       }
196       if (tcf == null)
197       {
198         if (new AnnotationFile().readAnnotationFile(
199                 alignFrame.viewport.getAlignment(), textarea.getText(),
200                 jalview.io.AppletFormatAdapter.PASTE))
201         {
202           alignFrame.alignPanel.fontChanged();
203           alignFrame.alignPanel.setScrollValues(0, 0);
204           alignFrame.statusBar
205                   .setText("Successfully pasted annotation to alignment.");
206
207         }
208         else
209         {
210           if (!alignFrame.parseFeaturesFile(textarea.getText(),
211                   jalview.io.AppletFormatAdapter.PASTE))
212           {
213             alignFrame.statusBar
214                     .setText("Couldn't parse pasted text as a valid annotation, feature, GFF, or T-Coffee score file.");
215           }
216         }
217       }
218     }
219     else if (alignFrame != null)
220     {
221       Alignment al = null;
222
223       String format = new IdentifyFile().Identify(text,
224               AppletFormatAdapter.PASTE);
225       try
226       {
227         al = new AppletFormatAdapter().readFile(text,
228                 AppletFormatAdapter.PASTE, format);
229       } catch (java.io.IOException ex)
230       {
231         ex.printStackTrace();
232       }
233
234       if (al != null)
235       {
236         if (newWindow)
237         {
238           AlignFrame af = new AlignFrame(al, alignFrame.viewport.applet,
239                   "Cut & Paste input - " + format, false);
240           af.statusBar.setText("Successfully pasted alignment file");
241         }
242         else
243         {
244           alignFrame.addSequences(al.getSequencesArray());
245           alignFrame.statusBar
246                   .setText("Successfully pasted alignment file");
247         }
248       }
249     }
250     // TODO: dialog should indicate if data was parsed correctly or not - see
251     // JAL-1102
252     if (this.getParent() instanceof Frame)
253     {
254       ((Frame) this.getParent()).setVisible(false);
255     }
256     else
257     {
258       ((Dialog) this.getParent()).setVisible(false);
259     }
260   }
261
262   protected void cancel()
263   {
264     textarea.setText("");
265     if (this.getParent() instanceof Frame)
266     {
267       ((Frame) this.getParent()).setVisible(false);
268     }
269     else
270     {
271       ((Dialog) this.getParent()).setVisible(false);
272     }
273   }
274
275   protected TextArea textarea = new TextArea();
276
277   Button accept = new Button("New Window");
278
279   Button addSequences = new Button("Add to Current Alignment");
280
281   Button cancel = new Button("Close");
282
283   protected Panel buttonPanel = new Panel();
284
285   BorderLayout borderLayout1 = new BorderLayout();
286
287   private void jbInit() throws Exception
288   {
289     textarea.setFont(new java.awt.Font("Monospaced", Font.PLAIN, 10));
290     textarea.setText("Paste your alignment file here");
291     textarea.addMouseListener(this);
292     this.setLayout(borderLayout1);
293     accept.addActionListener(this);
294     addSequences.addActionListener(this);
295     cancel.addActionListener(this);
296     this.add(buttonPanel, BorderLayout.SOUTH);
297     buttonPanel.add(accept, null);
298     buttonPanel.add(addSequences);
299     buttonPanel.add(cancel, null);
300     this.add(textarea, java.awt.BorderLayout.CENTER);
301   }
302
303   public void mousePressed(MouseEvent evt)
304   {
305     if (textarea.getText().startsWith("Paste your"))
306     {
307       textarea.setText("");
308     }
309   }
310
311   public void mouseReleased(MouseEvent evt)
312   {
313   }
314
315   public void mouseClicked(MouseEvent evt)
316   {
317   }
318
319   public void mouseEntered(MouseEvent evt)
320   {
321   }
322
323   public void mouseExited(MouseEvent evt)
324   {
325   }
326 }