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.JSONFile;
27 import jalview.io.JalviewFileChooser;
28 import jalview.io.JalviewFileView;
29 import jalview.jbgui.GCutAndPasteTransfer;
30 import jalview.util.MessageManager;
32 import java.awt.Toolkit;
33 import java.awt.datatransfer.Clipboard;
34 import java.awt.datatransfer.DataFlavor;
35 import java.awt.datatransfer.StringSelection;
36 import java.awt.datatransfer.Transferable;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.awt.event.MouseEvent;
41 import javax.swing.JMenuItem;
42 import javax.swing.JOptionPane;
43 import javax.swing.JPopupMenu;
44 import javax.swing.SwingUtilities;
47 * Cut'n'paste files into the desktop See JAL-1105
52 public class CutAndPasteTransfer extends GCutAndPasteTransfer
55 AlignViewport viewport;
57 public CutAndPasteTransfer()
59 SwingUtilities.invokeLater(new Runnable()
63 textarea.requestFocus();
72 public void setForInput(AlignViewport viewport)
74 this.viewport = viewport;
77 ok.setText(MessageManager.getString("action.add"));
80 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
86 * @return DOCUMENT ME!
88 public String getText()
90 return textarea.getText();
99 public void setText(String text)
101 textarea.setText(text);
104 public void appendText(String text)
106 textarea.append(text);
109 public void save_actionPerformed(ActionEvent e)
111 JalviewFileChooser chooser = new JalviewFileChooser(
112 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
114 chooser.setAcceptAllFileFilterUsed(false);
115 chooser.setFileView(new JalviewFileView());
116 chooser.setDialogTitle(MessageManager.getString("label.save_text_to_file"));
117 chooser.setToolTipText(MessageManager.getString("action.save"));
119 int value = chooser.showSaveDialog(this);
121 if (value == JalviewFileChooser.APPROVE_OPTION)
125 java.io.PrintWriter out = new java.io.PrintWriter(
126 new java.io.FileWriter(chooser.getSelectedFile()));
128 out.print(getText());
130 } catch (Exception ex)
132 ex.printStackTrace();
144 public void copyItem_actionPerformed(ActionEvent e)
146 textarea.getSelectedText();
147 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
148 c.setContents(new StringSelection(textarea.getSelectedText()), null);
157 public void pasteMenu_actionPerformed(ActionEvent e)
159 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
160 Transferable contents = c.getContents(this);
162 if (contents == null)
169 textarea.append((String) contents
170 .getTransferData(DataFlavor.stringFlavor));
171 } catch (Exception ex)
182 public void ok_actionPerformed(ActionEvent e)
184 String format = new IdentifyFile().Identify(getText(), "Paste");
185 // TODO: identify feature, annotation or tree file and parse appropriately.
188 if (FormatAdapter.isValidFormat(format))
192 al = new FormatAdapter().readFile(getText(), "Paste", format);
193 } catch (java.io.IOException ex)
195 JOptionPane.showInternalMessageDialog(Desktop.desktop,
196 MessageManager.formatMessage(
197 "label.couldnt_read_pasted_text", new String[]
198 { ex.toString() }), MessageManager
199 .getString("label.error_parsing_text"),
200 JOptionPane.WARNING_MESSAGE);
206 String title = MessageManager.formatMessage(
207 "label.input_cut_paste_params", new String[]
209 if (viewport != null)
211 viewport.addAlignment(al, title);
215 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
216 AlignFrame.DEFAULT_HEIGHT);
217 af.currentFileFormat = format;
218 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
219 AlignFrame.DEFAULT_HEIGHT);
220 af.statusBar.setText(MessageManager
221 .getString("label.successfully_pasted_alignment_file"));
223 af.setShowSeqFeatures(JSONFile.isSeqFeaturesEnabled());
225 af.changeColour(JSONFile.getColourScheme());
226 af.setMenusForViewport();
230 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
232 } catch (Exception ex)
246 public void cancel_actionPerformed(ActionEvent e)
250 this.setClosed(true);
251 } catch (Exception ex)
256 public void textarea_mousePressed(MouseEvent e)
258 if (SwingUtilities.isRightMouseButton(e))
260 JPopupMenu popup = new JPopupMenu(
261 MessageManager.getString("action.edit"));
262 JMenuItem item = new JMenuItem(
263 MessageManager.getString("action.copy"));
264 item.addActionListener(new ActionListener()
266 public void actionPerformed(ActionEvent e)
268 copyItem_actionPerformed(e);
272 item = new JMenuItem(MessageManager.getString("action.paste"));
273 item.addActionListener(new ActionListener()
275 public void actionPerformed(ActionEvent e)
277 pasteMenu_actionPerformed(e);
281 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);