2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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 jalview.datamodel.Alignment;
24 import jalview.datamodel.PDBEntry;
25 import jalview.datamodel.Sequence;
26 import jalview.io.AnnotationFile;
27 import jalview.io.AppletFormatAdapter;
28 import jalview.io.IdentifyFile;
29 import jalview.io.NewickFile;
30 import jalview.io.TCoffeeScoreFile;
31 import jalview.schemes.TCoffeeColourScheme;
32 import jalview.util.MessageManager;
34 import java.awt.BorderLayout;
35 import java.awt.Button;
36 import java.awt.Dialog;
38 import java.awt.Frame;
39 import java.awt.Panel;
40 import java.awt.TextArea;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.event.MouseEvent;
44 import java.awt.event.MouseListener;
46 public class CutAndPasteTransfer extends Panel implements ActionListener,
49 boolean pdbImport = false;
51 boolean treeImport = false;
53 boolean annotationImport = false;
57 AlignFrame alignFrame;
59 public CutAndPasteTransfer(boolean forImport, AlignFrame alignFrame)
69 this.alignFrame = alignFrame;
73 buttonPanel.setVisible(false);
77 public String getText()
79 return textarea.getText();
82 public void setText(String text)
84 textarea.setText(text);
87 public void setPDBImport(Sequence seq)
90 accept.setLabel(MessageManager.getString("action.accept"));
91 addSequences.setVisible(false);
95 public void setTreeImport()
98 accept.setLabel(MessageManager.getString("action.accept"));
99 addSequences.setVisible(false);
102 public void setAnnotationImport()
104 annotationImport = true;
105 accept.setLabel(MessageManager.getString("action.accept"));
106 addSequences.setVisible(false);
109 public void actionPerformed(ActionEvent evt)
111 if (evt.getSource() == accept)
115 else if (evt.getSource() == addSequences)
119 else if (evt.getSource() == cancel)
125 protected void ok(boolean newWindow)
127 String text = getText();
128 int length = text.length();
129 textarea.append("\n");
130 if (textarea.getText().length() == length)
132 String warning = "\n\n#################################################\n"
133 + "WARNING!! THIS IS THE MAXIMUM SIZE OF TEXTAREA!!\n"
134 + "\nCAN'T INPUT FULL ALIGNMENT"
135 + "\n\nYOU MUST DELETE THIS WARNING TO CONTINUE"
136 + "\n\nMAKE SURE LAST SEQUENCE PASTED IS COMPLETE"
137 + "\n#################################################\n";
138 textarea.setText(text.substring(0, text.length() - warning.length())
141 textarea.setCaretPosition(text.length());
156 else if (annotationImport)
160 else if (alignFrame != null)
162 loadAlignment(text, newWindow);
165 // TODO: dialog should indicate if data was parsed correctly or not - see
167 if (this.getParent() instanceof Frame)
169 ((Frame) this.getParent()).setVisible(false);
173 ((Dialog) this.getParent()).setVisible(false);
178 * Parses text as Newick Tree format, and loads on to the alignment. Returns
179 * true if successful, else false.
181 protected boolean loadTree()
185 NewickFile fin = new NewickFile(textarea.getText(), "Paste");
188 if (fin.getTree() != null)
190 alignFrame.loadTree(fin, "Pasted tree file");
193 } catch (Exception ex)
195 // TODO: JAL-1102 - should have a warning message in dialog, not simply
196 // overwrite the broken input data with the exception
197 textarea.setText(MessageManager.formatMessage(
198 "label.could_not_parse_newick_file", new Object[]
199 { ex.getMessage() }));
206 * Parse text as an alignment file and add to the current or a new window.
211 protected void loadAlignment(String text, boolean newWindow)
215 String format = new IdentifyFile().Identify(text,
216 AppletFormatAdapter.PASTE);
219 al = new AppletFormatAdapter().readFile(text,
220 AppletFormatAdapter.PASTE, format);
221 } catch (java.io.IOException ex)
223 ex.printStackTrace();
231 AlignFrame af = new AlignFrame(al, alignFrame.viewport.applet,
232 "Cut & Paste input - " + format, false);
234 .setText(MessageManager
235 .getString("label.successfully_pasted_annotation_to_alignment"));
239 alignFrame.addSequences(al.getSequencesArray());
240 alignFrame.statusBar.setText(MessageManager
241 .getString("label.successfully_pasted_alignment_file"));
247 * Parse the text as a TCoffee score file, if successful add scores as
248 * alignment annotations.
250 protected void loadAnnotations()
252 TCoffeeScoreFile tcf = null;
255 tcf = new TCoffeeScoreFile(textarea.getText(),
256 jalview.io.AppletFormatAdapter.PASTE);
259 if (tcf.annotateAlignment(alignFrame.viewport.getAlignment(),
262 alignFrame.tcoffeeColour.setEnabled(true);
263 alignFrame.alignPanel.fontChanged();
264 alignFrame.changeColour(new TCoffeeColourScheme(
265 alignFrame.viewport.getAlignment()));
267 .setText(MessageManager
268 .getString("label.successfully_pasted_tcoffee_scores_to_alignment"));
272 // file valid but didn't get added to alignment for some reason
273 alignFrame.statusBar.setText(MessageManager.formatMessage(
274 "label.failed_add_tcoffee_scores",
276 { (tcf.getWarningMessage() != null ? tcf
277 .getWarningMessage() : "") }));
284 } catch (Exception x)
290 if (new AnnotationFile().annotateAlignmentView(alignFrame.viewport,
292 jalview.io.AppletFormatAdapter.PASTE))
294 alignFrame.alignPanel.fontChanged();
295 alignFrame.alignPanel.setScrollValues(0, 0);
297 .setText(MessageManager
298 .getString("label.successfully_pasted_annotation_to_alignment"));
303 if (!alignFrame.parseFeaturesFile(textarea.getText(),
304 jalview.io.AppletFormatAdapter.PASTE))
307 .setText(MessageManager
308 .getString("label.couldnt_parse_pasted_text_as_valid_annotation_feature_GFF_tcoffee_file"));
315 * Open a Jmol viewer (if available), failing that the built-in PDB viewer,
316 * passing the input text as the PDB file data.
320 protected void openPdbViewer(String text)
322 PDBEntry pdb = new PDBEntry();
325 if (alignFrame.alignPanel.av.applet.jmolAvailable)
327 new jalview.appletgui.AppletJmol(pdb, new Sequence[]
328 { seq }, null, alignFrame.alignPanel, AppletFormatAdapter.PASTE);
332 new MCview.AppletPDBViewer(pdb, new Sequence[]
333 { seq }, null, alignFrame.alignPanel, AppletFormatAdapter.PASTE);
337 protected void cancel()
339 textarea.setText("");
340 if (this.getParent() instanceof Frame)
342 ((Frame) this.getParent()).setVisible(false);
346 ((Dialog) this.getParent()).setVisible(false);
350 protected TextArea textarea = new TextArea();
352 Button accept = new Button("New Window");
354 Button addSequences = new Button("Add to Current Alignment");
356 Button cancel = new Button("Close");
358 protected Panel buttonPanel = new Panel();
360 BorderLayout borderLayout1 = new BorderLayout();
362 private void jbInit() throws Exception
364 textarea.setFont(new java.awt.Font("Monospaced", Font.PLAIN, 10));
365 textarea.setText(MessageManager
366 .getString("label.paste_your_alignment_file"));
367 textarea.addMouseListener(this);
368 this.setLayout(borderLayout1);
369 accept.addActionListener(this);
370 addSequences.addActionListener(this);
371 cancel.addActionListener(this);
372 this.add(buttonPanel, BorderLayout.SOUTH);
373 buttonPanel.add(accept, null);
374 buttonPanel.add(addSequences);
375 buttonPanel.add(cancel, null);
376 this.add(textarea, java.awt.BorderLayout.CENTER);
379 public void mousePressed(MouseEvent evt)
381 if (textarea.getText().startsWith(
382 MessageManager.getString("label.paste_your")))
384 textarea.setText("");
388 public void mouseReleased(MouseEvent evt)
392 public void mouseClicked(MouseEvent evt)
396 public void mouseEntered(MouseEvent evt)
400 public void mouseExited(MouseEvent evt)