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.datamodel.Alignment;
24 import jalview.io.FormatAdapter;
25 import jalview.io.IdentifyFile;
26 import jalview.io.JalviewFileChooser;
27 import jalview.io.JalviewFileView;
28 import jalview.jbgui.GCutAndPasteTransfer;
29 import jalview.util.MessageManager;
31 import java.awt.Toolkit;
32 import java.awt.datatransfer.Clipboard;
33 import java.awt.datatransfer.DataFlavor;
34 import java.awt.datatransfer.StringSelection;
35 import java.awt.datatransfer.Transferable;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.awt.event.MouseEvent;
40 import javax.swing.JMenuItem;
41 import javax.swing.JOptionPane;
42 import javax.swing.JPopupMenu;
43 import javax.swing.SwingUtilities;
46 * Cut'n'paste files into the desktop See JAL-1105
51 public class CutAndPasteTransfer extends GCutAndPasteTransfer
54 AlignViewport viewport;
56 public CutAndPasteTransfer()
58 SwingUtilities.invokeLater(new Runnable()
62 textarea.requestFocus();
71 public void setForInput(AlignViewport viewport)
73 this.viewport = viewport;
76 ok.setText(MessageManager.getString("action.add"));
79 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
85 * @return DOCUMENT ME!
87 public String getText()
89 return textarea.getText();
98 public void setText(String text)
100 textarea.setText(text);
103 public void appendText(String text)
105 textarea.append(text);
108 public void save_actionPerformed(ActionEvent e)
110 JalviewFileChooser chooser = new JalviewFileChooser(
111 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
113 chooser.setAcceptAllFileFilterUsed(false);
114 chooser.setFileView(new JalviewFileView());
115 chooser.setDialogTitle(MessageManager.getString("label.save_text_to_file"));
116 chooser.setToolTipText(MessageManager.getString("action.save"));
118 int value = chooser.showSaveDialog(this);
120 if (value == JalviewFileChooser.APPROVE_OPTION)
124 java.io.PrintWriter out = new java.io.PrintWriter(
125 new java.io.FileWriter(chooser.getSelectedFile()));
127 out.print(getText());
129 } catch (Exception ex)
131 ex.printStackTrace();
143 public void copyItem_actionPerformed(ActionEvent e)
145 textarea.getSelectedText();
146 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
147 c.setContents(new StringSelection(textarea.getSelectedText()), null);
156 public void pasteMenu_actionPerformed(ActionEvent e)
158 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
159 Transferable contents = c.getContents(this);
161 if (contents == null)
168 textarea.append((String) contents
169 .getTransferData(DataFlavor.stringFlavor));
170 } catch (Exception ex)
181 public void ok_actionPerformed(ActionEvent e)
183 String format = new IdentifyFile().Identify(getText(), "Paste");
184 // TODO: identify feature, annotation or tree file and parse appropriately.
187 if (FormatAdapter.isValidFormat(format))
191 al = new FormatAdapter().readFile(getText(), "Paste", format);
192 } catch (java.io.IOException ex)
194 JOptionPane.showInternalMessageDialog(Desktop.desktop,
195 MessageManager.formatMessage(
196 "label.couldnt_read_pasted_text", new String[]
197 { ex.toString() }), MessageManager
198 .getString("label.error_parsing_text"),
199 JOptionPane.WARNING_MESSAGE);
205 String title = MessageManager.formatMessage(
206 "label.input_cut_paste_params", new String[]
208 if (viewport != null)
210 viewport.addAlignment(al, title);
214 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
215 AlignFrame.DEFAULT_HEIGHT);
216 af.currentFileFormat = format;
217 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
218 AlignFrame.DEFAULT_HEIGHT);
219 af.statusBar.setText(MessageManager
220 .getString("label.successfully_pasted_alignment_file"));
224 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
226 } catch (Exception ex)
239 public void cancel_actionPerformed(ActionEvent e)
243 this.setClosed(true);
244 } catch (Exception ex)
249 public void textarea_mousePressed(MouseEvent e)
251 if (SwingUtilities.isRightMouseButton(e))
253 JPopupMenu popup = new JPopupMenu(
254 MessageManager.getString("action.edit"));
255 JMenuItem item = new JMenuItem(
256 MessageManager.getString("action.copy"));
257 item.addActionListener(new ActionListener()
259 public void actionPerformed(ActionEvent e)
261 copyItem_actionPerformed(e);
265 item = new JMenuItem(MessageManager.getString("action.paste"));
266 item.addActionListener(new ActionListener()
268 public void actionPerformed(ActionEvent e)
270 pasteMenu_actionPerformed(e);
274 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);