2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
22 import java.awt.datatransfer.*;
23 import java.awt.event.*;
26 import jalview.datamodel.*;
28 import jalview.jbgui.*;
29 import jalview.util.MessageManager;
32 * Cut'n'paste files into the desktop See JAL-1105
37 public class CutAndPasteTransfer extends GCutAndPasteTransfer
40 AlignViewport viewport;
42 public CutAndPasteTransfer()
44 SwingUtilities.invokeLater(new Runnable()
48 textarea.requestFocus();
57 public void setForInput(AlignViewport viewport)
59 this.viewport = viewport;
62 ok.setText(MessageManager.getString("action.add"));
65 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
71 * @return DOCUMENT ME!
73 public String getText()
75 return textarea.getText();
84 public void setText(String text)
86 textarea.setText(text);
89 public void appendText(String text)
91 textarea.append(text);
94 public void save_actionPerformed(ActionEvent e)
96 JalviewFileChooser chooser = new JalviewFileChooser(
97 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
99 chooser.setAcceptAllFileFilterUsed(false);
100 chooser.setFileView(new JalviewFileView());
101 chooser.setDialogTitle("Save Text to File");
102 chooser.setToolTipText(MessageManager.getString("action.save"));
104 int value = chooser.showSaveDialog(this);
106 if (value == JalviewFileChooser.APPROVE_OPTION)
110 java.io.PrintWriter out = new java.io.PrintWriter(
111 new java.io.FileWriter(chooser.getSelectedFile()));
113 out.print(getText());
115 } catch (Exception ex)
117 ex.printStackTrace();
129 public void copyItem_actionPerformed(ActionEvent e)
131 textarea.getSelectedText();
132 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
133 c.setContents(new StringSelection(textarea.getSelectedText()), null);
142 public void pasteMenu_actionPerformed(ActionEvent e)
144 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
145 Transferable contents = c.getContents(this);
147 if (contents == null)
154 textarea.append((String) contents
155 .getTransferData(DataFlavor.stringFlavor));
156 } catch (Exception ex)
167 public void ok_actionPerformed(ActionEvent e)
169 String format = new IdentifyFile().Identify(getText(), "Paste");
170 // TODO: identify feature, annotation or tree file and parse appropriately.
173 if (FormatAdapter.isValidFormat(format))
177 al = new FormatAdapter().readFile(getText(), "Paste", format);
178 } catch (java.io.IOException ex)
180 JOptionPane.showInternalMessageDialog(Desktop.desktop,
181 MessageManager.formatMessage("label.couldnt_read_pasted_text", new String[]{ex.toString()}),
182 MessageManager.getString("label.error_parsing_text"), JOptionPane.WARNING_MESSAGE);
188 if (viewport != null)
190 for (int i = 0; i < al.getHeight(); i++)
192 viewport.getAlignment().addSequence(al.getSequenceAt(i));
195 viewport.firePropertyChange("alignment", null, viewport
196 .getAlignment().getSequences());
200 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
201 AlignFrame.DEFAULT_HEIGHT);
202 af.currentFileFormat = format;
203 Desktop.addInternalFrame(af, MessageManager.formatMessage("label.input_cut_paste_params", new String[]{format}),
204 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
205 af.statusBar.setText(MessageManager.getString("label.successfully_pasted_alignment_file"));
209 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
211 } catch (Exception ex)
224 public void cancel_actionPerformed(ActionEvent e)
228 this.setClosed(true);
229 } catch (Exception ex)
234 public void textarea_mousePressed(MouseEvent e)
236 if (SwingUtilities.isRightMouseButton(e))
238 JPopupMenu popup = new JPopupMenu(MessageManager.getString("action.edit"));
239 JMenuItem item = new JMenuItem(MessageManager.getString("action.copy"));
240 item.addActionListener(new ActionListener()
242 public void actionPerformed(ActionEvent e)
244 copyItem_actionPerformed(e);
248 item = new JMenuItem(MessageManager.getString("action.paste"));
249 item.addActionListener(new ActionListener()
251 public void actionPerformed(ActionEvent e)
253 pasteMenu_actionPerformed(e);
257 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);