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