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