JAL-1517 fix copyright for 2.8.2
[jalview.git] / src / jalview / appletgui / CutAndPasteTransfer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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("label.could_not_parse_newick_file", new String[]{ex.getMessage()}));
161         return;
162       }
163     }
164     else if (annotationImport)
165     {
166       TCoffeeScoreFile tcf = null;
167       try
168       {
169         tcf = new TCoffeeScoreFile(textarea.getText(),
170                 jalview.io.AppletFormatAdapter.PASTE);
171         if (tcf.isValid())
172         {
173           if (tcf.annotateAlignment(alignFrame.viewport.getAlignment(),
174                   true))
175           {
176             alignFrame.tcoffeeColour.setEnabled(true);
177             alignFrame.alignPanel.fontChanged();
178             alignFrame.changeColour(new TCoffeeColourScheme(
179                     alignFrame.viewport.getAlignment()));
180             alignFrame.statusBar
181                     .setText(MessageManager.getString("label.successfully_pasted_tcoffee_scores_to_alignment"));
182           }
183           else
184           {
185             // file valid but didn't get added to alignment for some reason
186             alignFrame.statusBar.setText(MessageManager.formatMessage("label.failed_add_tcoffee_scores", new String[]{(tcf.getWarningMessage() != null ? tcf.getWarningMessage() : "")}));
187           }
188         }
189         else
190         {
191           tcf = null;
192         }
193       } catch (Exception x)
194       {
195         tcf = null;
196       }
197       if (tcf == null)
198       {
199         if (new AnnotationFile().readAnnotationFile(
200                 alignFrame.viewport.getAlignment(), textarea.getText(),
201                 jalview.io.AppletFormatAdapter.PASTE))
202         {
203           alignFrame.alignPanel.fontChanged();
204           alignFrame.alignPanel.setScrollValues(0, 0);
205           alignFrame.statusBar
206                   .setText(MessageManager.getString("label.successfully_pasted_annotation_to_alignment"));
207
208         }
209         else
210         {
211           if (!alignFrame.parseFeaturesFile(textarea.getText(),
212                   jalview.io.AppletFormatAdapter.PASTE))
213           {
214             alignFrame.statusBar
215                     .setText(MessageManager.getString("label.couldnt_parse_pasted_text_as_valid_annotation_feature_GFF_tcoffee_file"));
216           }
217         }
218       }
219     }
220     else if (alignFrame != null)
221     {
222       Alignment al = null;
223
224       String format = new IdentifyFile().Identify(text,
225               AppletFormatAdapter.PASTE);
226       try
227       {
228         al = new AppletFormatAdapter().readFile(text,
229                 AppletFormatAdapter.PASTE, format);
230       } catch (java.io.IOException ex)
231       {
232         ex.printStackTrace();
233       }
234
235       if (al != null)
236       {
237         if (newWindow)
238         {
239           AlignFrame af = new AlignFrame(al, alignFrame.viewport.applet,
240                   "Cut & Paste input - " + format, false);
241           af.statusBar.setText(MessageManager.getString("label.successfully_pasted_annotation_to_alignment"));
242         }
243         else
244         {
245           alignFrame.addSequences(al.getSequencesArray());
246           alignFrame.statusBar
247                   .setText(MessageManager.getString("label.successfully_pasted_alignment_file"));
248         }
249       }
250     }
251     // TODO: dialog should indicate if data was parsed correctly or not - see
252     // JAL-1102
253     if (this.getParent() instanceof Frame)
254     {
255       ((Frame) this.getParent()).setVisible(false);
256     }
257     else
258     {
259       ((Dialog) this.getParent()).setVisible(false);
260     }
261   }
262
263   protected void cancel()
264   {
265     textarea.setText("");
266     if (this.getParent() instanceof Frame)
267     {
268       ((Frame) this.getParent()).setVisible(false);
269     }
270     else
271     {
272       ((Dialog) this.getParent()).setVisible(false);
273     }
274   }
275
276   protected TextArea textarea = new TextArea();
277
278   Button accept = new Button("New Window");
279
280   Button addSequences = new Button("Add to Current Alignment");
281
282   Button cancel = new Button("Close");
283
284   protected Panel buttonPanel = new Panel();
285
286   BorderLayout borderLayout1 = new BorderLayout();
287
288   private void jbInit() throws Exception
289   {
290     textarea.setFont(new java.awt.Font("Monospaced", Font.PLAIN, 10));
291     textarea.setText(MessageManager.getString("label.paste_your_alignment_file"));
292     textarea.addMouseListener(this);
293     this.setLayout(borderLayout1);
294     accept.addActionListener(this);
295     addSequences.addActionListener(this);
296     cancel.addActionListener(this);
297     this.add(buttonPanel, BorderLayout.SOUTH);
298     buttonPanel.add(accept, null);
299     buttonPanel.add(addSequences);
300     buttonPanel.add(cancel, null);
301     this.add(textarea, java.awt.BorderLayout.CENTER);
302   }
303
304   public void mousePressed(MouseEvent evt)
305   {
306     if (textarea.getText().startsWith(MessageManager.getString("label.paste_your")))
307     {
308       textarea.setText("");
309     }
310   }
311
312   public void mouseReleased(MouseEvent evt)
313   {
314   }
315
316   public void mouseClicked(MouseEvent evt)
317   {
318   }
319
320   public void mouseEntered(MouseEvent evt)
321   {
322   }
323
324   public void mouseExited(MouseEvent evt)
325   {
326   }
327 }