2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b2)
3 * Copyright (C) 2015 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()
77 textarea.requestFocus();
86 public void setForInput(AlignmentViewPanel viewpanel)
88 this.alignpanel = viewpanel;
89 if (alignpanel != null)
91 this.viewport = alignpanel.getAlignViewport();
95 ok.setText(MessageManager.getString("action.add"));
98 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
104 * @return DOCUMENT ME!
106 public String getText()
108 return textarea.getText();
117 public void setText(String text)
119 textarea.setText(text);
122 public void appendText(String text)
124 textarea.append(text);
127 public void save_actionPerformed(ActionEvent e)
129 JalviewFileChooser chooser = new JalviewFileChooser(
130 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
132 chooser.setAcceptAllFileFilterUsed(false);
133 chooser.setFileView(new JalviewFileView());
134 chooser.setDialogTitle(MessageManager
135 .getString("label.save_text_to_file"));
136 chooser.setToolTipText(MessageManager.getString("action.save"));
138 int value = chooser.showSaveDialog(this);
140 if (value == JalviewFileChooser.APPROVE_OPTION)
144 java.io.PrintWriter out = new java.io.PrintWriter(
145 new java.io.FileWriter(chooser.getSelectedFile()));
147 out.print(getText());
149 } catch (Exception ex)
151 ex.printStackTrace();
163 public void copyItem_actionPerformed(ActionEvent e)
165 textarea.getSelectedText();
166 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
167 c.setContents(new StringSelection(textarea.getSelectedText()), null);
176 public void pasteMenu_actionPerformed(ActionEvent e)
178 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
179 Transferable contents = c.getContents(this);
181 if (contents == null)
188 textarea.append((String) contents
189 .getTransferData(DataFlavor.stringFlavor));
190 } catch (Exception ex)
201 public void ok_actionPerformed(ActionEvent e)
203 String text = getText();
204 if (text.trim().length() < 1)
209 String format = new IdentifyFile().Identify(text, "Paste");
210 if (format == null || format.equalsIgnoreCase("EMPTY DATA FILE"))
212 System.err.println(MessageManager
213 .getString("label.couldnt_read_data"));
214 if (!Jalview.isHeadlessMode())
216 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
217 AppletFormatAdapter.SUPPORTED_FORMATS,
218 MessageManager.getString("label.couldnt_read_data"),
219 JOptionPane.WARNING_MESSAGE);
224 // TODO: identify feature, annotation or tree file and parse appropriately.
225 AlignmentI al = null;
227 if (FormatAdapter.isValidFormat(format))
231 FormatAdapter fa = new FormatAdapter(alignpanel);
232 al = fa.readFile(getText(), "Paste", format);
233 source = fa.getAlignFile();
235 } catch (java.io.IOException ex)
237 JOptionPane.showInternalMessageDialog(Desktop.desktop,
238 MessageManager.formatMessage(
239 "label.couldnt_read_pasted_text",
240 new String[] { ex.toString() }), MessageManager
241 .getString("label.error_parsing_text"),
242 JOptionPane.WARNING_MESSAGE);
246 if (al != null && al.hasValidSequence())
248 String title = MessageManager.formatMessage(
249 "label.input_cut_paste_params", new String[] { format });
250 if (viewport != null)
252 ((AlignViewport) viewport).addAlignment(al, title);
258 if (source instanceof ComplexAlignFile)
260 ColumnSelection colSel = ((ComplexAlignFile) source)
261 .getColumnSelection();
262 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
263 .getHiddenSequences();
264 boolean showSeqFeatures = ((ComplexAlignFile) source)
265 .isShowSeqFeatures();
266 String colourSchemeName = ((ComplexAlignFile) source)
267 .getGlobalColourScheme();
268 FeaturesDisplayedI fd = ((ComplexAlignFile) source)
269 .getDisplayedFeatures();
270 af = new AlignFrame(al, hiddenSeqs, colSel,
271 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
272 af.getViewport().setShowSequenceFeatures(showSeqFeatures);
273 af.getViewport().setFeaturesDisplayed(fd);
274 ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme(
275 colourSchemeName, al);
283 af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
284 AlignFrame.DEFAULT_HEIGHT);
287 af.currentFileFormat = format;
288 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
289 AlignFrame.DEFAULT_HEIGHT);
290 af.statusBar.setText(MessageManager
291 .getString("label.successfully_pasted_alignment_file"));
295 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
297 } catch (Exception ex)
304 System.err.println(MessageManager
305 .getString("label.couldnt_read_data"));
306 if (!Jalview.isHeadlessMode())
308 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
309 AppletFormatAdapter.SUPPORTED_FORMATS,
310 MessageManager.getString("label.couldnt_read_data"),
311 JOptionPane.WARNING_MESSAGE);
322 public void cancel_actionPerformed(ActionEvent e)
326 this.setClosed(true);
327 } catch (Exception ex)
332 public void textarea_mousePressed(MouseEvent e)
334 if (SwingUtilities.isRightMouseButton(e))
336 JPopupMenu popup = new JPopupMenu(
337 MessageManager.getString("action.edit"));
338 JMenuItem item = new JMenuItem(
339 MessageManager.getString("action.copy"));
340 item.addActionListener(new ActionListener()
342 public void actionPerformed(ActionEvent e)
344 copyItem_actionPerformed(e);
348 item = new JMenuItem(MessageManager.getString("action.paste"));
349 item.addActionListener(new ActionListener()
351 public void actionPerformed(ActionEvent e)
353 pasteMenu_actionPerformed(e);
357 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);