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.api.AlignViewportI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.api.ComplexAlignFile;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.datamodel.SequenceI;
29 import jalview.io.FileParse;
30 import jalview.io.FormatAdapter;
31 import jalview.io.IdentifyFile;
32 import jalview.io.JalviewFileChooser;
33 import jalview.io.JalviewFileView;
34 import jalview.jbgui.GCutAndPasteTransfer;
35 import jalview.schemes.ColourSchemeI;
36 import jalview.util.MessageManager;
38 import java.awt.Toolkit;
39 import java.awt.datatransfer.Clipboard;
40 import java.awt.datatransfer.DataFlavor;
41 import java.awt.datatransfer.StringSelection;
42 import java.awt.datatransfer.Transferable;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.ActionListener;
45 import java.awt.event.MouseEvent;
47 import javax.swing.JMenuItem;
48 import javax.swing.JOptionPane;
49 import javax.swing.JPopupMenu;
50 import javax.swing.SwingUtilities;
53 * Cut'n'paste files into the desktop See JAL-1105
58 public class CutAndPasteTransfer extends GCutAndPasteTransfer
61 AlignmentViewPanel alignpanel;
63 AlignViewportI viewport;
65 FileParse source = null;
66 public CutAndPasteTransfer()
68 SwingUtilities.invokeLater(new Runnable()
72 textarea.requestFocus();
81 public void setForInput(AlignmentViewPanel viewpanel)
83 this.alignpanel = viewpanel;
84 if (alignpanel != null)
86 this.viewport = alignpanel.getAlignViewport();
90 ok.setText(MessageManager.getString("action.add"));
93 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
99 * @return DOCUMENT ME!
101 public String getText()
103 return textarea.getText();
112 public void setText(String text)
114 textarea.setText(text);
117 public void appendText(String text)
119 textarea.append(text);
122 public void save_actionPerformed(ActionEvent e)
124 JalviewFileChooser chooser = new JalviewFileChooser(
125 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
127 chooser.setAcceptAllFileFilterUsed(false);
128 chooser.setFileView(new JalviewFileView());
129 chooser.setDialogTitle(MessageManager.getString("label.save_text_to_file"));
130 chooser.setToolTipText(MessageManager.getString("action.save"));
132 int value = chooser.showSaveDialog(this);
134 if (value == JalviewFileChooser.APPROVE_OPTION)
138 java.io.PrintWriter out = new java.io.PrintWriter(
139 new java.io.FileWriter(chooser.getSelectedFile()));
141 out.print(getText());
143 } catch (Exception ex)
145 ex.printStackTrace();
157 public void copyItem_actionPerformed(ActionEvent e)
159 textarea.getSelectedText();
160 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
161 c.setContents(new StringSelection(textarea.getSelectedText()), null);
170 public void pasteMenu_actionPerformed(ActionEvent e)
172 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
173 Transferable contents = c.getContents(this);
175 if (contents == null)
182 textarea.append((String) contents
183 .getTransferData(DataFlavor.stringFlavor));
184 } catch (Exception ex)
195 public void ok_actionPerformed(ActionEvent e)
197 String format = new IdentifyFile().Identify(getText(), "Paste");
198 // TODO: identify feature, annotation or tree file and parse appropriately.
199 AlignmentI al = null;
201 if (FormatAdapter.isValidFormat(format))
205 FormatAdapter fa = new FormatAdapter(alignpanel);
206 al = fa.readFile(getText(), "Paste", format);
207 source = fa.getAlignFile();
209 } catch (java.io.IOException ex)
211 JOptionPane.showInternalMessageDialog(Desktop.desktop,
212 MessageManager.formatMessage(
213 "label.couldnt_read_pasted_text", new String[]
214 { ex.toString() }), MessageManager
215 .getString("label.error_parsing_text"),
216 JOptionPane.WARNING_MESSAGE);
222 String title = MessageManager.formatMessage(
223 "label.input_cut_paste_params", new String[]
225 if (viewport != null)
227 ((AlignViewport) viewport).addAlignment(al, title);
233 if (source instanceof ComplexAlignFile)
235 ColumnSelection colSel = ((ComplexAlignFile) source)
236 .getColumnSelection();
237 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
238 .getHiddenSequences();
239 boolean showSeqFeatures = ((ComplexAlignFile) source)
240 .isShowSeqFeatures();
241 ColourSchemeI cs = ((ComplexAlignFile) source).getColourScheme();
242 af = new AlignFrame(al, hiddenSeqs, colSel,
243 AlignFrame.DEFAULT_WIDTH,
244 AlignFrame.DEFAULT_HEIGHT);
246 af.getViewport().setShowSequenceFeatures(showSeqFeatures);
251 af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
252 AlignFrame.DEFAULT_HEIGHT);
255 af.currentFileFormat = format;
256 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
257 AlignFrame.DEFAULT_HEIGHT);
258 af.statusBar.setText(MessageManager
259 .getString("label.successfully_pasted_alignment_file"));
263 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
265 } catch (Exception ex)
279 public void cancel_actionPerformed(ActionEvent e)
283 this.setClosed(true);
284 } catch (Exception ex)
289 public void textarea_mousePressed(MouseEvent e)
291 if (SwingUtilities.isRightMouseButton(e))
293 JPopupMenu popup = new JPopupMenu(
294 MessageManager.getString("action.edit"));
295 JMenuItem item = new JMenuItem(
296 MessageManager.getString("action.copy"));
297 item.addActionListener(new ActionListener()
299 public void actionPerformed(ActionEvent e)
301 copyItem_actionPerformed(e);
305 item = new JMenuItem(MessageManager.getString("action.paste"));
306 item.addActionListener(new ActionListener()
308 public void actionPerformed(ActionEvent e)
310 pasteMenu_actionPerformed(e);
314 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);