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