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.FileParse;
25 import jalview.io.FormatAdapter;
26 import jalview.io.HtmlFile;
27 import jalview.io.IdentifyFile;
28 import jalview.io.JSONFile;
29 import jalview.io.JalviewFileChooser;
30 import jalview.io.JalviewFileView;
31 import jalview.jbgui.GCutAndPasteTransfer;
32 import jalview.util.MessageManager;
34 import java.awt.Toolkit;
35 import java.awt.datatransfer.Clipboard;
36 import java.awt.datatransfer.DataFlavor;
37 import java.awt.datatransfer.StringSelection;
38 import java.awt.datatransfer.Transferable;
39 import java.awt.event.ActionEvent;
40 import java.awt.event.ActionListener;
41 import java.awt.event.MouseEvent;
43 import javax.swing.JMenuItem;
44 import javax.swing.JOptionPane;
45 import javax.swing.JPopupMenu;
46 import javax.swing.SwingUtilities;
49 * Cut'n'paste files into the desktop See JAL-1105
54 public class CutAndPasteTransfer extends GCutAndPasteTransfer
57 AlignViewport viewport;
59 FileParse source = null;
60 public CutAndPasteTransfer()
62 SwingUtilities.invokeLater(new Runnable()
66 textarea.requestFocus();
75 public void setForInput(AlignViewport viewport)
77 this.viewport = viewport;
80 ok.setText(MessageManager.getString("action.add"));
83 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
89 * @return DOCUMENT ME!
91 public String getText()
93 return textarea.getText();
102 public void setText(String text)
104 textarea.setText(text);
107 public void appendText(String text)
109 textarea.append(text);
112 public void save_actionPerformed(ActionEvent e)
114 JalviewFileChooser chooser = new JalviewFileChooser(
115 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
117 chooser.setAcceptAllFileFilterUsed(false);
118 chooser.setFileView(new JalviewFileView());
119 chooser.setDialogTitle(MessageManager.getString("label.save_text_to_file"));
120 chooser.setToolTipText(MessageManager.getString("action.save"));
122 int value = chooser.showSaveDialog(this);
124 if (value == JalviewFileChooser.APPROVE_OPTION)
128 java.io.PrintWriter out = new java.io.PrintWriter(
129 new java.io.FileWriter(chooser.getSelectedFile()));
131 out.print(getText());
133 } catch (Exception ex)
135 ex.printStackTrace();
147 public void copyItem_actionPerformed(ActionEvent e)
149 textarea.getSelectedText();
150 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
151 c.setContents(new StringSelection(textarea.getSelectedText()), null);
160 public void pasteMenu_actionPerformed(ActionEvent e)
162 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
163 Transferable contents = c.getContents(this);
165 if (contents == null)
172 textarea.append((String) contents
173 .getTransferData(DataFlavor.stringFlavor));
174 } catch (Exception ex)
185 public void ok_actionPerformed(ActionEvent e)
187 String format = new IdentifyFile().Identify(getText(), "Paste");
188 // TODO: identify feature, annotation or tree file and parse appropriately.
191 if (FormatAdapter.isValidFormat(format))
195 FormatAdapter fa = new FormatAdapter(viewport);
196 al = fa.readFile(getText(), "Paste", format);
197 source = fa.getAlignFile();
199 } catch (java.io.IOException ex)
201 JOptionPane.showInternalMessageDialog(Desktop.desktop,
202 MessageManager.formatMessage(
203 "label.couldnt_read_pasted_text", new String[]
204 { ex.toString() }), MessageManager
205 .getString("label.error_parsing_text"),
206 JOptionPane.WARNING_MESSAGE);
212 String title = MessageManager.formatMessage(
213 "label.input_cut_paste_params", new String[]
215 if (viewport != null)
217 viewport.addAlignment(al, title);
221 AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
222 AlignFrame.DEFAULT_HEIGHT);
223 af.currentFileFormat = format;
224 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
225 AlignFrame.DEFAULT_HEIGHT);
226 af.statusBar.setText(MessageManager
227 .getString("label.successfully_pasted_alignment_file"));
229 if (source instanceof HtmlFile)
231 ((HtmlFile) source).applySettingsToAlignFrame(af);
233 else if (source instanceof JSONFile)
235 ((JSONFile) source).applySettingsToAlignFrame(af);
241 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
243 } catch (Exception ex)
257 public void cancel_actionPerformed(ActionEvent e)
261 this.setClosed(true);
262 } catch (Exception ex)
267 public void textarea_mousePressed(MouseEvent e)
269 if (SwingUtilities.isRightMouseButton(e))
271 JPopupMenu popup = new JPopupMenu(
272 MessageManager.getString("action.edit"));
273 JMenuItem item = new JMenuItem(
274 MessageManager.getString("action.copy"));
275 item.addActionListener(new ActionListener()
277 public void actionPerformed(ActionEvent e)
279 copyItem_actionPerformed(e);
283 item = new JMenuItem(MessageManager.getString("action.paste"));
284 item.addActionListener(new ActionListener()
286 public void actionPerformed(ActionEvent e)
288 pasteMenu_actionPerformed(e);
292 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);