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 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.AnnotationFile;
31 import jalview.io.AppletFormatAdapter;
32 import jalview.io.FileParse;
33 import jalview.io.IdentifyFile;
34 import jalview.io.NewickFile;
35 import jalview.io.TCoffeeScoreFile;
36 import jalview.schemes.ColourSchemeI;
37 import jalview.schemes.TCoffeeColourScheme;
38 import jalview.util.MessageManager;
40 import java.awt.BorderLayout;
41 import java.awt.Button;
42 import java.awt.Dialog;
44 import java.awt.Frame;
45 import java.awt.Label;
46 import java.awt.Panel;
47 import java.awt.TextArea;
48 import java.awt.event.ActionEvent;
49 import java.awt.event.ActionListener;
50 import java.awt.event.MouseEvent;
51 import java.awt.event.MouseListener;
53 public class CutAndPasteTransfer extends Panel implements ActionListener,
56 boolean pdbImport = false;
58 boolean treeImport = false;
60 boolean annotationImport = false;
64 AlignFrame alignFrame;
66 FileParse source = null;
68 public CutAndPasteTransfer(boolean forImport, AlignFrame alignFrame)
78 this.alignFrame = alignFrame;
82 buttonPanel.setVisible(false);
86 public String getText()
88 return textarea.getText();
91 public void setText(String text)
93 textarea.setText(text);
96 public void setPDBImport(SequenceI seq)
99 accept.setLabel(MessageManager.getString("action.accept"));
100 addSequences.setVisible(false);
104 public void setTreeImport()
107 accept.setLabel(MessageManager.getString("action.accept"));
108 addSequences.setVisible(false);
111 public void setAnnotationImport()
113 annotationImport = true;
114 accept.setLabel(MessageManager.getString("action.accept"));
115 addSequences.setVisible(false);
118 public void actionPerformed(ActionEvent evt)
120 if (evt.getSource() == accept)
124 else if (evt.getSource() == addSequences)
128 else if (evt.getSource() == cancel)
134 protected void ok(boolean newWindow)
136 String text = getText();
137 int length = text.length();
138 textarea.append("\n");
139 if (textarea.getText().length() == length)
141 String warning = "\n\n#################################################\n"
142 + "WARNING!! THIS IS THE MAXIMUM SIZE OF TEXTAREA!!\n"
143 + "\nCAN'T INPUT FULL ALIGNMENT"
144 + "\n\nYOU MUST DELETE THIS WARNING TO CONTINUE"
145 + "\n\nMAKE SURE LAST SEQUENCE PASTED IS COMPLETE"
146 + "\n#################################################\n";
147 textarea.setText(text.substring(0, text.length() - warning.length())
150 textarea.setCaretPosition(text.length());
165 else if (annotationImport)
169 else if (alignFrame != null)
171 loadAlignment(text, newWindow, alignFrame.getAlignViewport());
174 // TODO: dialog should indicate if data was parsed correctly or not - see
176 if (this.getParent() instanceof Frame)
178 ((Frame) this.getParent()).setVisible(false);
182 ((Dialog) this.getParent()).setVisible(false);
187 * Parses text as Newick Tree format, and loads on to the alignment. Returns
188 * true if successful, else false.
190 protected boolean loadTree()
194 NewickFile fin = new NewickFile(textarea.getText(), "Paste");
197 if (fin.getTree() != null)
199 alignFrame.loadTree(fin, "Pasted tree file");
202 } catch (Exception ex)
204 // TODO: JAL-1102 - should have a warning message in dialog, not simply
205 // overwrite the broken input data with the exception
206 textarea.setText(MessageManager.formatMessage(
207 "label.could_not_parse_newick_file",
208 new Object[] { ex.getMessage() }));
215 * Parse text as an alignment file and add to the current or a new window.
220 protected void loadAlignment(String text, boolean newWindow,
221 AlignViewport viewport)
223 AlignmentI al = null;
225 String format = new IdentifyFile().Identify(text,
226 AppletFormatAdapter.PASTE);
227 AppletFormatAdapter afa = new AppletFormatAdapter(alignFrame.alignPanel);
230 al = afa.readFile(text, AppletFormatAdapter.PASTE, format);
231 source = afa.getAlignFile();
232 } catch (java.io.IOException ex)
234 ex.printStackTrace();
239 al.setDataset(null); // set dataset on alignment/sequences
242 * SplitFrame option dependent on applet parameter for now.
244 boolean allowSplitFrame = alignFrame.viewport.applet
245 .getDefaultParameter("enableSplitFrame", false);
246 if (allowSplitFrame && openSplitFrame(al, format))
254 if (source instanceof ComplexAlignFile)
256 ColumnSelection colSel = ((ComplexAlignFile) source)
257 .getColumnSelection();
258 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
259 .getHiddenSequences();
260 boolean showSeqFeatures = ((ComplexAlignFile) source)
261 .isShowSeqFeatures();
262 ColourSchemeI cs = ((ComplexAlignFile) source).getColourScheme();
263 af = new AlignFrame(al, hiddenSeqs, colSel,
264 alignFrame.viewport.applet, "Cut & Paste input - "
266 af.getAlignViewport().setShowSequenceFeatures(showSeqFeatures);
271 af = new AlignFrame(al, alignFrame.viewport.applet,
272 "Cut & Paste input - " + format, false);
276 .setText(MessageManager
277 .getString("label.successfully_pasted_annotation_to_alignment"));
281 alignFrame.addSequences(al.getSequencesArray());
282 alignFrame.statusBar.setText(MessageManager
283 .getString("label.successfully_pasted_alignment_file"));
289 * Check whether the new alignment could be mapped to the current one as
290 * cDNA/protein, if so offer the option to open as split frame view. Returns
291 * true if a split frame view is opened, false if not.
296 protected boolean openSplitFrame(AlignmentI al, String format)
298 final AlignmentI thisAlignment = this.alignFrame.getAlignViewport()
300 if (thisAlignment.isNucleotide() == al.isNucleotide())
302 // both nucleotide or both protein
305 AlignmentI protein = thisAlignment.isNucleotide() ? al : thisAlignment;
306 AlignmentI dna = thisAlignment.isNucleotide() ? thisAlignment : al;
307 boolean mapped = AlignmentUtils.mapProteinAlignmentToCdna(protein, dna);
314 * A mapping is possible; ask user if they want a split frame.
316 String title = MessageManager.getString("label.open_split_window");
317 final JVDialog dialog = new JVDialog((Frame) this.getParent(), title,
319 dialog.ok.setLabel(MessageManager.getString("action.yes"));
320 dialog.cancel.setLabel(MessageManager.getString("action.no"));
321 Panel question = new Panel(new BorderLayout());
322 final String text = MessageManager
323 .getString("label.open_split_window?");
324 question.add(new Label(text, Label.CENTER), BorderLayout.CENTER);
325 dialog.setMainPanel(question);
326 dialog.setVisible(true);
335 * 'align' the added alignment to match the current one
337 al.alignAs(thisAlignment);
340 * Open SplitFrame with DNA above and protein below, including the alignment
341 * from textbox and a copy of the original.
343 final JalviewLite applet = this.alignFrame.viewport.applet;
344 AlignFrame copyFrame = new AlignFrame(
345 this.alignFrame.viewport.getAlignment(), applet,
346 alignFrame.getTitle(), false, false);
347 AlignFrame newFrame = new AlignFrame(al, alignFrame.viewport.applet,
348 "Cut & Paste input - " + format, false, false);
349 AlignFrame dnaFrame = al.isNucleotide() ? newFrame : copyFrame;
350 AlignFrame proteinFrame = al.isNucleotide() ? copyFrame : newFrame;
351 SplitFrame sf = new SplitFrame(dnaFrame, proteinFrame);
352 sf.addToDisplay(false, applet);
357 * Parse the text as a TCoffee score file, if successful add scores as
358 * alignment annotations.
360 protected void loadAnnotations()
362 TCoffeeScoreFile tcf = null;
365 tcf = new TCoffeeScoreFile(textarea.getText(),
366 jalview.io.AppletFormatAdapter.PASTE);
369 if (tcf.annotateAlignment(alignFrame.viewport.getAlignment(), true))
371 alignFrame.tcoffeeColour.setEnabled(true);
372 alignFrame.alignPanel.fontChanged();
373 alignFrame.changeColour(new TCoffeeColourScheme(
374 alignFrame.viewport.getAlignment()));
376 .setText(MessageManager
377 .getString("label.successfully_pasted_tcoffee_scores_to_alignment"));
381 // file valid but didn't get added to alignment for some reason
382 alignFrame.statusBar.setText(MessageManager.formatMessage(
383 "label.failed_add_tcoffee_scores",
384 new Object[] { (tcf.getWarningMessage() != null ? tcf
385 .getWarningMessage() : "") }));
392 } catch (Exception x)
398 if (new AnnotationFile().annotateAlignmentView(alignFrame.viewport,
399 textarea.getText(), jalview.io.AppletFormatAdapter.PASTE))
401 alignFrame.alignPanel.fontChanged();
402 alignFrame.alignPanel.setScrollValues(0, 0);
404 .setText(MessageManager
405 .getString("label.successfully_pasted_annotation_to_alignment"));
410 if (!alignFrame.parseFeaturesFile(textarea.getText(),
411 jalview.io.AppletFormatAdapter.PASTE))
414 .setText(MessageManager
415 .getString("label.couldnt_parse_pasted_text_as_valid_annotation_feature_GFF_tcoffee_file"));
422 * Open a Jmol viewer (if available), failing that the built-in PDB viewer,
423 * passing the input text as the PDB file data.
427 protected void openPdbViewer(String text)
429 PDBEntry pdb = new PDBEntry();
432 if (alignFrame.alignPanel.av.applet.jmolAvailable)
434 new jalview.appletgui.AppletJmol(pdb, new SequenceI[] { seq }, null,
435 alignFrame.alignPanel, AppletFormatAdapter.PASTE);
439 new MCview.AppletPDBViewer(pdb, new SequenceI[] { seq }, null,
440 alignFrame.alignPanel, AppletFormatAdapter.PASTE);
444 protected void cancel()
446 textarea.setText("");
447 if (this.getParent() instanceof Frame)
449 ((Frame) this.getParent()).setVisible(false);
453 ((Dialog) this.getParent()).setVisible(false);
457 protected TextArea textarea = new TextArea();
459 Button accept = new Button("New Window");
461 Button addSequences = new Button("Add to Current Alignment");
463 Button cancel = new Button("Close");
465 protected Panel buttonPanel = new Panel();
467 BorderLayout borderLayout1 = new BorderLayout();
469 private void jbInit() throws Exception
471 textarea.setFont(new java.awt.Font("Monospaced", Font.PLAIN, 10));
472 textarea.setText(MessageManager
473 .getString("label.paste_your_alignment_file"));
474 textarea.addMouseListener(this);
475 this.setLayout(borderLayout1);
476 accept.addActionListener(this);
477 addSequences.addActionListener(this);
478 cancel.addActionListener(this);
479 this.add(buttonPanel, BorderLayout.SOUTH);
480 buttonPanel.add(accept, null);
481 buttonPanel.add(addSequences);
482 buttonPanel.add(cancel, null);
483 this.add(textarea, java.awt.BorderLayout.CENTER);
486 public void mousePressed(MouseEvent evt)
488 if (textarea.getText().startsWith(
489 MessageManager.getString("label.paste_your")))
491 textarea.setText("");
495 public void mouseReleased(MouseEvent evt)
499 public void mouseClicked(MouseEvent evt)
503 public void mouseEntered(MouseEvent evt)
507 public void mouseExited(MouseEvent evt)