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