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
233 if (openSplitFrame(al, format))
239 AlignFrame af = new AlignFrame(al, alignFrame.viewport.applet,
240 "Cut & Paste input - " + format, false);
242 .setText(MessageManager
243 .getString("label.successfully_pasted_annotation_to_alignment"));
247 alignFrame.addSequences(al.getSequencesArray());
248 alignFrame.statusBar.setText(MessageManager
249 .getString("label.successfully_pasted_alignment_file"));
255 * Check whether the new alignment could be mapped to the current one as
256 * cDNA/protein, if so offer the option to open as split frame view. Returns
257 * true if a split frame view is opened, false if not.
262 protected boolean openSplitFrame(Alignment al, String format)
264 final AlignmentI thisAlignment = this.alignFrame.getAlignViewport().getAlignment();
265 if (thisAlignment.isNucleotide() == al.isNucleotide())
267 // both nucleotide or both protein
270 AlignmentI protein = thisAlignment.isNucleotide() ? al : thisAlignment;
271 AlignmentI dna = thisAlignment.isNucleotide() ? thisAlignment : al;
272 boolean mapped = AlignmentUtils.mapProteinToCdna(protein, dna);
279 * A mapping is possible; ask user if they want a split frame.
281 String title = MessageManager.getString("label.open_split_window");
282 final JVDialog dialog = new JVDialog((Frame) this.getParent(), title,
284 dialog.ok.setLabel(MessageManager.getString("action.yes"));
285 dialog.cancel.setLabel(MessageManager.getString("action.no"));
286 Panel question = new Panel(new BorderLayout());
287 final String text = MessageManager
288 .getString("label.open_split_window?");
289 question.add(new Label(text, Label.CENTER), BorderLayout.CENTER);
290 dialog.setMainPanel(question);
291 dialog.setVisible(true);
300 * Open SplitFrame with DNA above and protein below, including the alignment
301 * from textbox and a copy of the original.
303 final JalviewLite applet = this.alignFrame.viewport.applet;
304 AlignFrame copyFrame = new AlignFrame(
305 this.alignFrame.viewport.getAlignment(), applet,
306 alignFrame.getTitle(), false, false);
307 AlignFrame newFrame = new AlignFrame(al, alignFrame.viewport.applet,
308 "Cut & Paste input - " + format, false, false);
309 AlignFrame dnaFrame = al.isNucleotide() ? newFrame : copyFrame;
310 AlignFrame proteinFrame = al.isNucleotide() ? copyFrame
312 SplitFrame sf = new SplitFrame(dnaFrame, proteinFrame);
313 sf.addToDisplay(false, applet);
318 * Parse the text as a TCoffee score file, if successful add scores as
319 * alignment annotations.
321 protected void loadAnnotations()
323 TCoffeeScoreFile tcf = null;
326 tcf = new TCoffeeScoreFile(textarea.getText(),
327 jalview.io.AppletFormatAdapter.PASTE);
330 if (tcf.annotateAlignment(alignFrame.viewport.getAlignment(),
333 alignFrame.tcoffeeColour.setEnabled(true);
334 alignFrame.alignPanel.fontChanged();
335 alignFrame.changeColour(new TCoffeeColourScheme(
336 alignFrame.viewport.getAlignment()));
338 .setText(MessageManager
339 .getString("label.successfully_pasted_tcoffee_scores_to_alignment"));
343 // file valid but didn't get added to alignment for some reason
344 alignFrame.statusBar.setText(MessageManager.formatMessage(
345 "label.failed_add_tcoffee_scores",
347 { (tcf.getWarningMessage() != null ? tcf
348 .getWarningMessage() : "") }));
355 } catch (Exception x)
361 if (new AnnotationFile().annotateAlignmentView(alignFrame.viewport,
363 jalview.io.AppletFormatAdapter.PASTE))
365 alignFrame.alignPanel.fontChanged();
366 alignFrame.alignPanel.setScrollValues(0, 0);
368 .setText(MessageManager
369 .getString("label.successfully_pasted_annotation_to_alignment"));
374 if (!alignFrame.parseFeaturesFile(textarea.getText(),
375 jalview.io.AppletFormatAdapter.PASTE))
378 .setText(MessageManager
379 .getString("label.couldnt_parse_pasted_text_as_valid_annotation_feature_GFF_tcoffee_file"));
386 * Open a Jmol viewer (if available), failing that the built-in PDB viewer,
387 * passing the input text as the PDB file data.
391 protected void openPdbViewer(String text)
393 PDBEntry pdb = new PDBEntry();
396 if (alignFrame.alignPanel.av.applet.jmolAvailable)
398 new jalview.appletgui.AppletJmol(pdb, new SequenceI[]
399 { seq }, null, alignFrame.alignPanel, AppletFormatAdapter.PASTE);
403 new MCview.AppletPDBViewer(pdb, new SequenceI[]
404 { seq }, null, alignFrame.alignPanel, AppletFormatAdapter.PASTE);
408 protected void cancel()
410 textarea.setText("");
411 if (this.getParent() instanceof Frame)
413 ((Frame) this.getParent()).setVisible(false);
417 ((Dialog) this.getParent()).setVisible(false);
421 protected TextArea textarea = new TextArea();
423 Button accept = new Button("New Window");
425 Button addSequences = new Button("Add to Current Alignment");
427 Button cancel = new Button("Close");
429 protected Panel buttonPanel = new Panel();
431 BorderLayout borderLayout1 = new BorderLayout();
433 private void jbInit() throws Exception
435 textarea.setFont(new java.awt.Font("Monospaced", Font.PLAIN, 10));
436 textarea.setText(MessageManager
437 .getString("label.paste_your_alignment_file"));
438 textarea.addMouseListener(this);
439 this.setLayout(borderLayout1);
440 accept.addActionListener(this);
441 addSequences.addActionListener(this);
442 cancel.addActionListener(this);
443 this.add(buttonPanel, BorderLayout.SOUTH);
444 buttonPanel.add(accept, null);
445 buttonPanel.add(addSequences);
446 buttonPanel.add(cancel, null);
447 this.add(textarea, java.awt.BorderLayout.CENTER);
450 public void mousePressed(MouseEvent evt)
452 if (textarea.getText().startsWith(
453 MessageManager.getString("label.paste_your")))
455 textarea.setText("");
459 public void mouseReleased(MouseEvent evt)
463 public void mouseClicked(MouseEvent evt)
467 public void mouseEntered(MouseEvent evt)
471 public void mouseExited(MouseEvent evt)