JAL-1695 parameter controls whether SplitFrame is enabled
[jalview.git] / src / jalview / appletgui / CutAndPasteTransfer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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.BorderLayout;
24 import java.awt.Button;
25 import java.awt.Dialog;
26 import java.awt.Font;
27 import java.awt.Frame;
28 import java.awt.Label;
29 import java.awt.Panel;
30 import java.awt.TextArea;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33 import java.awt.event.MouseEvent;
34 import java.awt.event.MouseListener;
35
36 import jalview.analysis.AlignmentUtils;
37 import jalview.bin.JalviewLite;
38 import jalview.datamodel.Alignment;
39 import jalview.datamodel.AlignmentI;
40 import jalview.datamodel.PDBEntry;
41 import jalview.datamodel.SequenceI;
42 import jalview.io.AnnotationFile;
43 import jalview.io.AppletFormatAdapter;
44 import jalview.io.IdentifyFile;
45 import jalview.io.NewickFile;
46 import jalview.io.TCoffeeScoreFile;
47 import jalview.schemes.TCoffeeColourScheme;
48 import jalview.util.MessageManager;
49
50 public class CutAndPasteTransfer extends Panel implements ActionListener,
51         MouseListener
52 {
53   boolean pdbImport = false;
54
55   boolean treeImport = false;
56
57   boolean annotationImport = false;
58
59   SequenceI seq;
60
61   AlignFrame alignFrame;
62
63   public CutAndPasteTransfer(boolean forImport, AlignFrame alignFrame)
64   {
65     try
66     {
67       jbInit();
68     } catch (Exception e)
69     {
70       e.printStackTrace();
71     }
72
73     this.alignFrame = alignFrame;
74
75     if (!forImport)
76     {
77       buttonPanel.setVisible(false);
78     }
79   }
80
81   public String getText()
82   {
83     return textarea.getText();
84   }
85
86   public void setText(String text)
87   {
88     textarea.setText(text);
89   }
90
91   public void setPDBImport(SequenceI seq)
92   {
93     this.seq = seq;
94     accept.setLabel(MessageManager.getString("action.accept"));
95     addSequences.setVisible(false);
96     pdbImport = true;
97   }
98
99   public void setTreeImport()
100   {
101     treeImport = true;
102     accept.setLabel(MessageManager.getString("action.accept"));
103     addSequences.setVisible(false);
104   }
105
106   public void setAnnotationImport()
107   {
108     annotationImport = true;
109     accept.setLabel(MessageManager.getString("action.accept"));
110     addSequences.setVisible(false);
111   }
112
113   public void actionPerformed(ActionEvent evt)
114   {
115     if (evt.getSource() == accept)
116     {
117       ok(true);
118     }
119     else if (evt.getSource() == addSequences)
120     {
121       ok(false);
122     }
123     else if (evt.getSource() == cancel)
124     {
125       cancel();
126     }
127   }
128
129   protected void ok(boolean newWindow)
130   {
131     String text = getText();
132     int length = text.length();
133     textarea.append("\n");
134     if (textarea.getText().length() == length)
135     {
136       String warning = "\n\n#################################################\n"
137               + "WARNING!! THIS IS THE MAXIMUM SIZE OF TEXTAREA!!\n"
138               + "\nCAN'T INPUT FULL ALIGNMENT"
139               + "\n\nYOU MUST DELETE THIS WARNING TO CONTINUE"
140               + "\n\nMAKE SURE LAST SEQUENCE PASTED IS COMPLETE"
141               + "\n#################################################\n";
142       textarea.setText(text.substring(0, text.length() - warning.length())
143               + warning);
144
145       textarea.setCaretPosition(text.length());
146     }
147
148     if (pdbImport)
149     {
150       openPdbViewer(text);
151
152     }
153     else if (treeImport)
154     {
155       if (!loadTree())
156       {
157         return;
158       }
159     }
160     else if (annotationImport)
161     {
162       loadAnnotations();
163     }
164     else if (alignFrame != null)
165     {
166       loadAlignment(text, newWindow);
167     }
168
169     // TODO: dialog should indicate if data was parsed correctly or not - see
170     // JAL-1102
171     if (this.getParent() instanceof Frame)
172     {
173       ((Frame) this.getParent()).setVisible(false);
174     }
175     else
176     {
177       ((Dialog) this.getParent()).setVisible(false);
178     }
179   }
180
181   /**
182    * Parses text as Newick Tree format, and loads on to the alignment. Returns
183    * true if successful, else false.
184    */
185   protected boolean loadTree()
186   {
187     try
188     {
189       NewickFile fin = new NewickFile(textarea.getText(), "Paste");
190
191       fin.parse();
192       if (fin.getTree() != null)
193       {
194         alignFrame.loadTree(fin, "Pasted tree file");
195         return true;
196       }
197     } catch (Exception ex)
198     {
199       // TODO: JAL-1102 - should have a warning message in dialog, not simply
200       // overwrite the broken input data with the exception
201       textarea.setText(MessageManager.formatMessage(
202               "label.could_not_parse_newick_file", new Object[]
203               { ex.getMessage() }));
204       return false;
205     }
206     return false;
207   }
208
209   /**
210    * Parse text as an alignment file and add to the current or a new window.
211    * 
212    * @param text
213    * @param newWindow
214    */
215   protected void loadAlignment(String text, boolean newWindow)
216   {
217     Alignment al = null;
218
219     String format = new IdentifyFile().Identify(text,
220             AppletFormatAdapter.PASTE);
221     try
222     {
223       al = new AppletFormatAdapter().readFile(text,
224               AppletFormatAdapter.PASTE, format);
225     } catch (java.io.IOException ex)
226     {
227       ex.printStackTrace();
228     }
229
230     if (al != null)
231     {
232       al.setDataset(null); // set dataset on alignment/sequences
233
234       /*
235        * SplitFrame option dependent on applet parameter for now.
236        */
237       boolean allowSplitFrame = alignFrame.viewport.applet
238               .getDefaultParameter("enableSplitFrame", false);
239       if (allowSplitFrame && openSplitFrame(al, format))
240       {
241         return;
242       }
243       if (newWindow)
244       {
245         AlignFrame af = new AlignFrame(al, alignFrame.viewport.applet,
246                 "Cut & Paste input - " + format, false);
247         af.statusBar
248                 .setText(MessageManager
249                         .getString("label.successfully_pasted_annotation_to_alignment"));
250       }
251       else
252       {
253         alignFrame.addSequences(al.getSequencesArray());
254         alignFrame.statusBar.setText(MessageManager
255                 .getString("label.successfully_pasted_alignment_file"));
256       }
257     }
258   }
259
260   /**
261    * Check whether the new alignment could be mapped to the current one as
262    * cDNA/protein, if so offer the option to open as split frame view. Returns
263    * true if a split frame view is opened, false if not.
264    * 
265    * @param al
266    * @return
267    */
268   protected boolean openSplitFrame(Alignment al, String format)
269   {
270     final AlignmentI thisAlignment = this.alignFrame.getAlignViewport().getAlignment();
271     if (thisAlignment.isNucleotide() == al.isNucleotide())
272     {
273       // both nucleotide or both protein
274       return false;
275     }
276     AlignmentI protein = thisAlignment.isNucleotide() ? al : thisAlignment;
277     AlignmentI dna = thisAlignment.isNucleotide() ? thisAlignment : al;
278     boolean mapped = AlignmentUtils.mapProteinToCdna(protein, dna);
279     if (!mapped)
280     {
281       return false;
282     }
283
284     /*
285      * A mapping is possible; ask user if they want a split frame.
286      */
287     String title = MessageManager.getString("label.open_split_window");
288     final JVDialog dialog = new JVDialog((Frame) this.getParent(), title,
289             true, 100, 400);
290     dialog.ok.setLabel(MessageManager.getString("action.yes"));
291     dialog.cancel.setLabel(MessageManager.getString("action.no"));
292     Panel question = new Panel(new BorderLayout());
293     final String text = MessageManager
294             .getString("label.open_split_window?");
295     question.add(new Label(text, Label.CENTER), BorderLayout.CENTER);
296     dialog.setMainPanel(question);
297     dialog.setVisible(true);
298     dialog.toFront();
299     
300     if (!dialog.accept)
301     {
302       return false;
303     }
304
305     /*
306      * Open SplitFrame with DNA above and protein below, including the alignment
307      * from textbox and a copy of the original.
308      */
309     final JalviewLite applet = this.alignFrame.viewport.applet;
310     AlignFrame copyFrame = new AlignFrame(
311             this.alignFrame.viewport.getAlignment(), applet,
312             alignFrame.getTitle(), false, false);
313     AlignFrame newFrame = new AlignFrame(al, alignFrame.viewport.applet,
314             "Cut & Paste input - " + format, false, false);
315     AlignFrame dnaFrame = al.isNucleotide() ? newFrame : copyFrame;
316     AlignFrame proteinFrame = al.isNucleotide() ? copyFrame
317             : newFrame;
318     SplitFrame sf = new SplitFrame(dnaFrame, proteinFrame);
319     sf.addToDisplay(false, applet);
320     return true;
321   }
322
323   /**
324    * Parse the text as a TCoffee score file, if successful add scores as
325    * alignment annotations.
326    */
327   protected void loadAnnotations()
328   {
329     TCoffeeScoreFile tcf = null;
330     try
331     {
332       tcf = new TCoffeeScoreFile(textarea.getText(),
333               jalview.io.AppletFormatAdapter.PASTE);
334       if (tcf.isValid())
335       {
336         if (tcf.annotateAlignment(alignFrame.viewport.getAlignment(),
337                 true))
338         {
339           alignFrame.tcoffeeColour.setEnabled(true);
340           alignFrame.alignPanel.fontChanged();
341           alignFrame.changeColour(new TCoffeeColourScheme(
342                   alignFrame.viewport.getAlignment()));
343           alignFrame.statusBar
344                   .setText(MessageManager
345                           .getString("label.successfully_pasted_tcoffee_scores_to_alignment"));
346         }
347         else
348         {
349           // file valid but didn't get added to alignment for some reason
350           alignFrame.statusBar.setText(MessageManager.formatMessage(
351                   "label.failed_add_tcoffee_scores",
352                   new Object[]
353                   { (tcf.getWarningMessage() != null ? tcf
354                           .getWarningMessage() : "") }));
355         }
356       }
357       else
358       {
359         tcf = null;
360       }
361     } catch (Exception x)
362     {
363       tcf = null;
364     }
365     if (tcf == null)
366     {
367       if (new AnnotationFile().annotateAlignmentView(alignFrame.viewport,
368               textarea.getText(),
369               jalview.io.AppletFormatAdapter.PASTE))
370       {
371         alignFrame.alignPanel.fontChanged();
372         alignFrame.alignPanel.setScrollValues(0, 0);
373         alignFrame.statusBar
374                 .setText(MessageManager
375                         .getString("label.successfully_pasted_annotation_to_alignment"));
376
377       }
378       else
379       {
380         if (!alignFrame.parseFeaturesFile(textarea.getText(),
381                 jalview.io.AppletFormatAdapter.PASTE))
382         {
383           alignFrame.statusBar
384                   .setText(MessageManager
385                           .getString("label.couldnt_parse_pasted_text_as_valid_annotation_feature_GFF_tcoffee_file"));
386         }
387       }
388     }
389   }
390
391   /**
392    * Open a Jmol viewer (if available), failing that the built-in PDB viewer,
393    * passing the input text as the PDB file data.
394    * 
395    * @param text
396    */
397   protected void openPdbViewer(String text)
398   {
399     PDBEntry pdb = new PDBEntry();
400     pdb.setFile(text);
401
402     if (alignFrame.alignPanel.av.applet.jmolAvailable)
403     {
404       new jalview.appletgui.AppletJmol(pdb, new SequenceI[]
405       { seq }, null, alignFrame.alignPanel, AppletFormatAdapter.PASTE);
406     }
407     else
408     {
409       new MCview.AppletPDBViewer(pdb, new SequenceI[]
410       { seq }, null, alignFrame.alignPanel, AppletFormatAdapter.PASTE);
411     }
412   }
413
414   protected void cancel()
415   {
416     textarea.setText("");
417     if (this.getParent() instanceof Frame)
418     {
419       ((Frame) this.getParent()).setVisible(false);
420     }
421     else
422     {
423       ((Dialog) this.getParent()).setVisible(false);
424     }
425   }
426
427   protected TextArea textarea = new TextArea();
428
429   Button accept = new Button("New Window");
430
431   Button addSequences = new Button("Add to Current Alignment");
432
433   Button cancel = new Button("Close");
434
435   protected Panel buttonPanel = new Panel();
436
437   BorderLayout borderLayout1 = new BorderLayout();
438
439   private void jbInit() throws Exception
440   {
441     textarea.setFont(new java.awt.Font("Monospaced", Font.PLAIN, 10));
442     textarea.setText(MessageManager
443             .getString("label.paste_your_alignment_file"));
444     textarea.addMouseListener(this);
445     this.setLayout(borderLayout1);
446     accept.addActionListener(this);
447     addSequences.addActionListener(this);
448     cancel.addActionListener(this);
449     this.add(buttonPanel, BorderLayout.SOUTH);
450     buttonPanel.add(accept, null);
451     buttonPanel.add(addSequences);
452     buttonPanel.add(cancel, null);
453     this.add(textarea, java.awt.BorderLayout.CENTER);
454   }
455
456   public void mousePressed(MouseEvent evt)
457   {
458     if (textarea.getText().startsWith(
459             MessageManager.getString("label.paste_your")))
460     {
461       textarea.setText("");
462     }
463   }
464
465   public void mouseReleased(MouseEvent evt)
466   {
467   }
468
469   public void mouseClicked(MouseEvent evt)
470   {
471   }
472
473   public void mouseEntered(MouseEvent evt)
474   {
475   }
476
477   public void mouseExited(MouseEvent evt)
478   {
479   }
480 }