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