2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.appletgui;
23 import java.awt.BorderLayout;
24 import java.awt.Button;
25 import java.awt.Dialog;
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;
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;
50 public class CutAndPasteTransfer extends Panel implements ActionListener,
53 boolean pdbImport = false;
55 boolean treeImport = false;
57 boolean annotationImport = false;
61 AlignFrame alignFrame;
63 public CutAndPasteTransfer(boolean forImport, AlignFrame alignFrame)
73 this.alignFrame = alignFrame;
77 buttonPanel.setVisible(false);
81 public String getText()
83 return textarea.getText();
86 public void setText(String text)
88 textarea.setText(text);
91 public void setPDBImport(SequenceI seq)
94 accept.setLabel(MessageManager.getString("action.accept"));
95 addSequences.setVisible(false);
99 public void setTreeImport()
102 accept.setLabel(MessageManager.getString("action.accept"));
103 addSequences.setVisible(false);
106 public void setAnnotationImport()
108 annotationImport = true;
109 accept.setLabel(MessageManager.getString("action.accept"));
110 addSequences.setVisible(false);
113 public void actionPerformed(ActionEvent evt)
115 if (evt.getSource() == accept)
119 else if (evt.getSource() == addSequences)
123 else if (evt.getSource() == cancel)
129 protected void ok(boolean newWindow)
131 String text = getText();
132 int length = text.length();
133 textarea.append("\n");
134 if (textarea.getText().length() == length)
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())
145 textarea.setCaretPosition(text.length());
160 else if (annotationImport)
164 else if (alignFrame != null)
166 loadAlignment(text, newWindow);
169 // TODO: dialog should indicate if data was parsed correctly or not - see
171 if (this.getParent() instanceof Frame)
173 ((Frame) this.getParent()).setVisible(false);
177 ((Dialog) this.getParent()).setVisible(false);
182 * Parses text as Newick Tree format, and loads on to the alignment. Returns
183 * true if successful, else false.
185 protected boolean loadTree()
189 NewickFile fin = new NewickFile(textarea.getText(), "Paste");
192 if (fin.getTree() != null)
194 alignFrame.loadTree(fin, "Pasted tree file");
197 } catch (Exception ex)
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() }));
210 * Parse text as an alignment file and add to the current or a new window.
215 protected void loadAlignment(String text, boolean newWindow)
219 String format = new IdentifyFile().Identify(text,
220 AppletFormatAdapter.PASTE);
223 al = new AppletFormatAdapter().readFile(text,
224 AppletFormatAdapter.PASTE, format);
225 } catch (java.io.IOException ex)
227 ex.printStackTrace();
232 al.setDataset(null); // set dataset on alignment/sequences
235 * SplitFrame option dependent on applet parameter for now.
237 boolean allowSplitFrame = alignFrame.viewport.applet
238 .getDefaultParameter("enableSplitFrame", false);
239 if (allowSplitFrame && openSplitFrame(al, format))
245 AlignFrame af = new AlignFrame(al, alignFrame.viewport.applet,
246 "Cut & Paste input - " + format, false);
248 .setText(MessageManager
249 .getString("label.successfully_pasted_annotation_to_alignment"));
253 alignFrame.addSequences(al.getSequencesArray());
254 alignFrame.statusBar.setText(MessageManager
255 .getString("label.successfully_pasted_alignment_file"));
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.
268 protected boolean openSplitFrame(Alignment al, String format)
270 final AlignmentI thisAlignment = this.alignFrame.getAlignViewport().getAlignment();
271 if (thisAlignment.isNucleotide() == al.isNucleotide())
273 // both nucleotide or both protein
276 AlignmentI protein = thisAlignment.isNucleotide() ? al : thisAlignment;
277 AlignmentI dna = thisAlignment.isNucleotide() ? thisAlignment : al;
278 boolean mapped = AlignmentUtils.mapProteinToCdna(protein, dna);
285 * A mapping is possible; ask user if they want a split frame.
287 String title = MessageManager.getString("label.open_split_window");
288 final JVDialog dialog = new JVDialog((Frame) this.getParent(), title,
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);
306 * Open SplitFrame with DNA above and protein below, including the alignment
307 * from textbox and a copy of the original.
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
318 SplitFrame sf = new SplitFrame(dnaFrame, proteinFrame);
319 sf.addToDisplay(false, applet);
324 * Parse the text as a TCoffee score file, if successful add scores as
325 * alignment annotations.
327 protected void loadAnnotations()
329 TCoffeeScoreFile tcf = null;
332 tcf = new TCoffeeScoreFile(textarea.getText(),
333 jalview.io.AppletFormatAdapter.PASTE);
336 if (tcf.annotateAlignment(alignFrame.viewport.getAlignment(),
339 alignFrame.tcoffeeColour.setEnabled(true);
340 alignFrame.alignPanel.fontChanged();
341 alignFrame.changeColour(new TCoffeeColourScheme(
342 alignFrame.viewport.getAlignment()));
344 .setText(MessageManager
345 .getString("label.successfully_pasted_tcoffee_scores_to_alignment"));
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",
353 { (tcf.getWarningMessage() != null ? tcf
354 .getWarningMessage() : "") }));
361 } catch (Exception x)
367 if (new AnnotationFile().annotateAlignmentView(alignFrame.viewport,
369 jalview.io.AppletFormatAdapter.PASTE))
371 alignFrame.alignPanel.fontChanged();
372 alignFrame.alignPanel.setScrollValues(0, 0);
374 .setText(MessageManager
375 .getString("label.successfully_pasted_annotation_to_alignment"));
380 if (!alignFrame.parseFeaturesFile(textarea.getText(),
381 jalview.io.AppletFormatAdapter.PASTE))
384 .setText(MessageManager
385 .getString("label.couldnt_parse_pasted_text_as_valid_annotation_feature_GFF_tcoffee_file"));
392 * Open a Jmol viewer (if available), failing that the built-in PDB viewer,
393 * passing the input text as the PDB file data.
397 protected void openPdbViewer(String text)
399 PDBEntry pdb = new PDBEntry();
402 if (alignFrame.alignPanel.av.applet.jmolAvailable)
404 new jalview.appletgui.AppletJmol(pdb, new SequenceI[]
405 { seq }, null, alignFrame.alignPanel, AppletFormatAdapter.PASTE);
409 new MCview.AppletPDBViewer(pdb, new SequenceI[]
410 { seq }, null, alignFrame.alignPanel, AppletFormatAdapter.PASTE);
414 protected void cancel()
416 textarea.setText("");
417 if (this.getParent() instanceof Frame)
419 ((Frame) this.getParent()).setVisible(false);
423 ((Dialog) this.getParent()).setVisible(false);
427 protected TextArea textarea = new TextArea();
429 Button accept = new Button("New Window");
431 Button addSequences = new Button("Add to Current Alignment");
433 Button cancel = new Button("Close");
435 protected Panel buttonPanel = new Panel();
437 BorderLayout borderLayout1 = new BorderLayout();
439 private void jbInit() throws Exception
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);
456 public void mousePressed(MouseEvent evt)
458 if (textarea.getText().startsWith(
459 MessageManager.getString("label.paste_your")))
461 textarea.setText("");
465 public void mouseReleased(MouseEvent evt)
469 public void mouseClicked(MouseEvent evt)
473 public void mouseEntered(MouseEvent evt)
477 public void mouseExited(MouseEvent evt)