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.
23 import jalview.api.AlignViewportI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.api.ComplexAlignFile;
26 import jalview.api.FeaturesDisplayedI;
27 import jalview.bin.Jalview;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.ColumnSelection;
30 import jalview.datamodel.SequenceI;
31 import jalview.io.AppletFormatAdapter;
32 import jalview.io.FileParse;
33 import jalview.io.FormatAdapter;
34 import jalview.io.IdentifyFile;
35 import jalview.io.JalviewFileChooser;
36 import jalview.io.JalviewFileView;
37 import jalview.jbgui.GCutAndPasteTransfer;
38 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
39 import jalview.schemes.ColourSchemeI;
40 import jalview.util.MessageManager;
42 import java.awt.Toolkit;
43 import java.awt.datatransfer.Clipboard;
44 import java.awt.datatransfer.DataFlavor;
45 import java.awt.datatransfer.StringSelection;
46 import java.awt.datatransfer.Transferable;
47 import java.awt.event.ActionEvent;
48 import java.awt.event.ActionListener;
49 import java.awt.event.MouseEvent;
51 import javax.swing.JMenuItem;
52 import javax.swing.JOptionPane;
53 import javax.swing.JPopupMenu;
54 import javax.swing.SwingUtilities;
57 * Cut'n'paste files into the desktop See JAL-1105
62 public class CutAndPasteTransfer extends GCutAndPasteTransfer
65 AlignmentViewPanel alignpanel;
67 AlignViewportI viewport;
69 FileParse source = null;
71 public CutAndPasteTransfer()
73 SwingUtilities.invokeLater(new Runnable()
78 textarea.requestFocus();
87 public void setForInput(AlignmentViewPanel viewpanel)
89 this.alignpanel = viewpanel;
90 if (alignpanel != null)
92 this.viewport = alignpanel.getAlignViewport();
96 ok.setText(MessageManager.getString("action.add"));
99 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
105 * @return DOCUMENT ME!
107 public String getText()
109 return textarea.getText();
118 public void setText(String text)
120 textarea.setText(text);
123 public void appendText(String text)
125 textarea.append(text);
129 public void save_actionPerformed(ActionEvent e)
131 JalviewFileChooser chooser = new JalviewFileChooser(
132 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
134 chooser.setAcceptAllFileFilterUsed(false);
135 chooser.setFileView(new JalviewFileView());
136 chooser.setDialogTitle(MessageManager
137 .getString("label.save_text_to_file"));
138 chooser.setToolTipText(MessageManager.getString("action.save"));
140 int value = chooser.showSaveDialog(this);
142 if (value == JalviewFileChooser.APPROVE_OPTION)
146 java.io.PrintWriter out = new java.io.PrintWriter(
147 new java.io.FileWriter(chooser.getSelectedFile()));
149 out.print(getText());
151 } catch (Exception ex)
153 ex.printStackTrace();
166 public void copyItem_actionPerformed(ActionEvent e)
168 textarea.getSelectedText();
169 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
170 c.setContents(new StringSelection(textarea.getSelectedText()), null);
180 public void pasteMenu_actionPerformed(ActionEvent e)
182 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
183 Transferable contents = c.getContents(this);
185 if (contents == null)
192 textarea.append((String) contents
193 .getTransferData(DataFlavor.stringFlavor));
194 } catch (Exception ex)
206 public void ok_actionPerformed(ActionEvent e)
208 String text = getText();
209 if (text.trim().length() < 1)
214 String format = new IdentifyFile().identify(text, "Paste");
215 if (format == null || format.equalsIgnoreCase("EMPTY DATA FILE"))
217 System.err.println(MessageManager
218 .getString("label.couldnt_read_data"));
219 if (!Jalview.isHeadlessMode())
221 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
222 AppletFormatAdapter.SUPPORTED_FORMATS,
223 MessageManager.getString("label.couldnt_read_data"),
224 JOptionPane.WARNING_MESSAGE);
229 // TODO: identify feature, annotation or tree file and parse appropriately.
230 AlignmentI al = null;
232 if (FormatAdapter.isValidFormat(format))
236 FormatAdapter fa = new FormatAdapter(alignpanel);
237 al = fa.readFile(getText(), "Paste", format);
238 source = fa.getAlignFile();
240 } catch (java.io.IOException ex)
242 JOptionPane.showInternalMessageDialog(Desktop.desktop,
243 MessageManager.formatMessage(
244 "label.couldnt_read_pasted_text",
245 new String[] { ex.toString() }), MessageManager
246 .getString("label.error_parsing_text"),
247 JOptionPane.WARNING_MESSAGE);
251 if (al != null && al.hasValidSequence())
253 String title = MessageManager.formatMessage(
254 "label.input_cut_paste_params", new String[] { format });
256 * if the view panel was closed its alignment is nulled
257 * and this is an orphaned cut and paste window
259 if (viewport != null && viewport.getAlignment() != null)
261 ((AlignViewport) viewport).addAlignment(al, title);
267 if (source instanceof ComplexAlignFile)
269 ColumnSelection colSel = ((ComplexAlignFile) source)
270 .getColumnSelection();
271 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
272 .getHiddenSequences();
273 boolean showSeqFeatures = ((ComplexAlignFile) source)
274 .isShowSeqFeatures();
275 String colourSchemeName = ((ComplexAlignFile) source)
276 .getGlobalColourScheme();
277 FeaturesDisplayedI fd = ((ComplexAlignFile) source)
278 .getDisplayedFeatures();
279 af = new AlignFrame(al, hiddenSeqs, colSel,
280 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
281 af.getViewport().setShowSequenceFeatures(showSeqFeatures);
282 af.getViewport().setFeaturesDisplayed(fd);
283 ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme(
284 colourSchemeName, al);
292 af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
293 AlignFrame.DEFAULT_HEIGHT);
296 af.currentFileFormat = format;
297 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
298 AlignFrame.DEFAULT_HEIGHT);
299 af.statusBar.setText(MessageManager
300 .getString("label.successfully_pasted_alignment_file"));
304 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
306 } catch (Exception ex)
313 System.err.println(MessageManager
314 .getString("label.couldnt_read_data"));
315 if (!Jalview.isHeadlessMode())
317 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
318 AppletFormatAdapter.SUPPORTED_FORMATS,
319 MessageManager.getString("label.couldnt_read_data"),
320 JOptionPane.WARNING_MESSAGE);
332 public void cancel_actionPerformed(ActionEvent e)
336 this.setClosed(true);
337 } catch (Exception ex)
343 public void textarea_mousePressed(MouseEvent e)
345 if (SwingUtilities.isRightMouseButton(e))
347 JPopupMenu popup = new JPopupMenu(
348 MessageManager.getString("action.edit"));
349 JMenuItem item = new JMenuItem(
350 MessageManager.getString("action.copy"));
351 item.addActionListener(new ActionListener()
354 public void actionPerformed(ActionEvent e)
356 copyItem_actionPerformed(e);
360 item = new JMenuItem(MessageManager.getString("action.paste"));
361 item.addActionListener(new ActionListener()
364 public void actionPerformed(ActionEvent e)
366 pasteMenu_actionPerformed(e);
370 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);