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 java.awt.Toolkit;
24 import java.awt.datatransfer.Clipboard;
25 import java.awt.datatransfer.DataFlavor;
26 import java.awt.datatransfer.StringSelection;
27 import java.awt.datatransfer.Transferable;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.MouseEvent;
32 import javax.swing.JMenuItem;
33 import javax.swing.JOptionPane;
34 import javax.swing.JPopupMenu;
35 import javax.swing.SwingUtilities;
37 import jalview.api.ComplexAlignFile;
38 import jalview.datamodel.Alignment;
39 import jalview.datamodel.ColumnSelection;
40 import jalview.datamodel.SequenceI;
41 import jalview.io.FileParse;
42 import jalview.io.FormatAdapter;
43 import jalview.io.IdentifyFile;
44 import jalview.io.JalviewFileChooser;
45 import jalview.io.JalviewFileView;
46 import jalview.jbgui.GCutAndPasteTransfer;
47 import jalview.schemes.ColourSchemeI;
48 import jalview.util.MessageManager;
51 * Cut'n'paste files into the desktop See JAL-1105
56 public class CutAndPasteTransfer extends GCutAndPasteTransfer
59 AlignViewport viewport;
61 FileParse source = null;
62 public CutAndPasteTransfer()
64 SwingUtilities.invokeLater(new Runnable()
68 textarea.requestFocus();
77 public void setForInput(AlignViewport viewport)
79 this.viewport = viewport;
82 ok.setText(MessageManager.getString("action.add"));
85 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
91 * @return DOCUMENT ME!
93 public String getText()
95 return textarea.getText();
104 public void setText(String text)
106 textarea.setText(text);
109 public void appendText(String text)
111 textarea.append(text);
114 public void save_actionPerformed(ActionEvent e)
116 JalviewFileChooser chooser = new JalviewFileChooser(
117 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
119 chooser.setAcceptAllFileFilterUsed(false);
120 chooser.setFileView(new JalviewFileView());
121 chooser.setDialogTitle(MessageManager.getString("label.save_text_to_file"));
122 chooser.setToolTipText(MessageManager.getString("action.save"));
124 int value = chooser.showSaveDialog(this);
126 if (value == JalviewFileChooser.APPROVE_OPTION)
130 java.io.PrintWriter out = new java.io.PrintWriter(
131 new java.io.FileWriter(chooser.getSelectedFile()));
133 out.print(getText());
135 } catch (Exception ex)
137 ex.printStackTrace();
149 public void copyItem_actionPerformed(ActionEvent e)
151 textarea.getSelectedText();
152 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
153 c.setContents(new StringSelection(textarea.getSelectedText()), null);
162 public void pasteMenu_actionPerformed(ActionEvent e)
164 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
165 Transferable contents = c.getContents(this);
167 if (contents == null)
174 textarea.append((String) contents
175 .getTransferData(DataFlavor.stringFlavor));
176 } catch (Exception ex)
187 public void ok_actionPerformed(ActionEvent e)
189 String format = new IdentifyFile().Identify(getText(), "Paste");
190 // TODO: identify feature, annotation or tree file and parse appropriately.
193 if (FormatAdapter.isValidFormat(format))
197 FormatAdapter fa = new FormatAdapter(viewport);
198 al = fa.readFile(getText(), "Paste", format);
199 source = fa.getAlignFile();
201 } catch (java.io.IOException ex)
203 JOptionPane.showInternalMessageDialog(Desktop.desktop,
204 MessageManager.formatMessage(
205 "label.couldnt_read_pasted_text", new String[]
206 { ex.toString() }), MessageManager
207 .getString("label.error_parsing_text"),
208 JOptionPane.WARNING_MESSAGE);
214 String title = MessageManager.formatMessage(
215 "label.input_cut_paste_params", new String[]
217 if (viewport != null)
219 viewport.addAlignment(al, title);
225 if (source instanceof ComplexAlignFile)
227 ColumnSelection colSel = ((ComplexAlignFile) source)
228 .getColumnSelection();
229 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
230 .getHiddenSequences();
231 boolean showSeqFeatures = ((ComplexAlignFile) source)
232 .isShowSeqFeatures();
233 ColourSchemeI cs = ((ComplexAlignFile) source).getColourScheme();
234 af = new AlignFrame(al, hiddenSeqs, colSel,
235 AlignFrame.DEFAULT_WIDTH,
236 AlignFrame.DEFAULT_HEIGHT);
238 af.getViewport().setShowSequenceFeatures(showSeqFeatures);
243 af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
244 AlignFrame.DEFAULT_HEIGHT);
247 af.currentFileFormat = format;
248 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
249 AlignFrame.DEFAULT_HEIGHT);
250 af.statusBar.setText(MessageManager
251 .getString("label.successfully_pasted_alignment_file"));
255 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
257 } catch (Exception ex)
271 public void cancel_actionPerformed(ActionEvent e)
275 this.setClosed(true);
276 } catch (Exception ex)
281 public void textarea_mousePressed(MouseEvent e)
283 if (SwingUtilities.isRightMouseButton(e))
285 JPopupMenu popup = new JPopupMenu(
286 MessageManager.getString("action.edit"));
287 JMenuItem item = new JMenuItem(
288 MessageManager.getString("action.copy"));
289 item.addActionListener(new ActionListener()
291 public void actionPerformed(ActionEvent e)
293 copyItem_actionPerformed(e);
297 item = new JMenuItem(MessageManager.getString("action.paste"));
298 item.addActionListener(new ActionListener()
300 public void actionPerformed(ActionEvent e)
302 pasteMenu_actionPerformed(e);
306 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);