2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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/>.
21 import java.awt.datatransfer.*;
22 import java.awt.event.*;
25 import jalview.datamodel.*;
27 import jalview.jbgui.*;
35 public class CutAndPasteTransfer extends GCutAndPasteTransfer
38 AlignViewport viewport;
40 public CutAndPasteTransfer()
42 SwingUtilities.invokeLater(new Runnable()
46 textarea.requestFocus();
55 public void setForInput(AlignViewport viewport)
57 this.viewport = viewport;
63 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
69 * @return DOCUMENT ME!
71 public String getText()
73 return textarea.getText();
82 public void setText(String text)
84 textarea.setText(text);
87 public void appendText(String text)
89 textarea.append(text);
92 public void save_actionPerformed(ActionEvent e)
94 JalviewFileChooser chooser = new JalviewFileChooser(
95 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
97 chooser.setAcceptAllFileFilterUsed(false);
98 chooser.setFileView(new JalviewFileView());
99 chooser.setDialogTitle("Save Text to File");
100 chooser.setToolTipText("Save");
102 int value = chooser.showSaveDialog(this);
104 if (value == JalviewFileChooser.APPROVE_OPTION)
108 java.io.PrintWriter out = new java.io.PrintWriter(
109 new java.io.FileWriter(chooser.getSelectedFile()));
111 out.print(getText());
113 } catch (Exception ex)
115 ex.printStackTrace();
127 public void copyItem_actionPerformed(ActionEvent e)
129 textarea.getSelectedText();
130 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
131 c.setContents(new StringSelection(textarea.getSelectedText()), null);
140 public void pasteMenu_actionPerformed(ActionEvent e)
142 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
143 Transferable contents = c.getContents(this);
145 if (contents == null)
152 textarea.append((String) contents
153 .getTransferData(DataFlavor.stringFlavor));
154 } catch (Exception ex)
165 public void ok_actionPerformed(ActionEvent e)
167 String format = new IdentifyFile().Identify(getText(), "Paste");
168 // TODO: identify feature, annotation or tree file and parse appropriately.
171 if (FormatAdapter.isValidFormat(format))
175 al = new FormatAdapter().readFile(getText(), "Paste", format);
176 } catch (java.io.IOException ex)
178 JOptionPane.showInternalMessageDialog(Desktop.desktop,
179 "Couldn't read the pasted text.\n" + ex.toString(),
180 "Error parsing text", JOptionPane.WARNING_MESSAGE);
186 if (viewport != null)
188 for (int i = 0; i < al.getHeight(); i++)
190 viewport.getAlignment().addSequence(al.getSequenceAt(i));
193 viewport.firePropertyChange("alignment", null, viewport
194 .getAlignment().getSequences());
198 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
199 AlignFrame.DEFAULT_HEIGHT);
200 af.currentFileFormat = format;
201 Desktop.addInternalFrame(af, "Cut & Paste input - " + format,
202 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
203 af.statusBar.setText("Successfully pasted alignment file");
207 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
209 } catch (Exception ex)
222 public void cancel_actionPerformed(ActionEvent e)
226 this.setClosed(true);
227 } catch (Exception ex)
232 public void textarea_mousePressed(MouseEvent e)
234 if (SwingUtilities.isRightMouseButton(e))
236 JPopupMenu popup = new JPopupMenu("Edit");
237 JMenuItem item = new JMenuItem("Copy");
238 item.addActionListener(new ActionListener()
240 public void actionPerformed(ActionEvent e)
242 copyItem_actionPerformed(e);
246 item = new JMenuItem("Paste");
247 item.addActionListener(new ActionListener()
249 public void actionPerformed(ActionEvent e)
251 pasteMenu_actionPerformed(e);
255 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);