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