2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3 * Copyright (C) 2015 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.api.FeaturesDisplayedI;
27 import jalview.bin.Jalview;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.ColumnSelection;
30 import jalview.datamodel.SequenceI;
31 import jalview.io.AppletFormatAdapter;
32 import jalview.io.FileParse;
33 import jalview.io.FormatAdapter;
34 import jalview.io.IdentifyFile;
35 import jalview.io.JalviewFileChooser;
36 import jalview.io.JalviewFileView;
37 import jalview.jbgui.GCutAndPasteTransfer;
38 import jalview.schemes.ColourSchemeI;
39 import jalview.util.MessageManager;
41 import java.awt.Toolkit;
42 import java.awt.datatransfer.Clipboard;
43 import java.awt.datatransfer.DataFlavor;
44 import java.awt.datatransfer.StringSelection;
45 import java.awt.datatransfer.Transferable;
46 import java.awt.event.ActionEvent;
47 import java.awt.event.ActionListener;
48 import java.awt.event.MouseEvent;
50 import javax.swing.JMenuItem;
51 import javax.swing.JOptionPane;
52 import javax.swing.JPopupMenu;
53 import javax.swing.SwingUtilities;
56 * Cut'n'paste files into the desktop See JAL-1105
61 public class CutAndPasteTransfer extends GCutAndPasteTransfer
64 AlignmentViewPanel alignpanel;
66 AlignViewportI viewport;
68 FileParse source = null;
70 public CutAndPasteTransfer()
72 SwingUtilities.invokeLater(new Runnable()
76 textarea.requestFocus();
85 public void setForInput(AlignmentViewPanel viewpanel)
87 this.alignpanel = viewpanel;
88 if (alignpanel != null)
90 this.viewport = alignpanel.getAlignViewport();
94 ok.setText(MessageManager.getString("action.add"));
97 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
103 * @return DOCUMENT ME!
105 public String getText()
107 return textarea.getText();
116 public void setText(String text)
118 textarea.setText(text);
121 public void appendText(String text)
123 textarea.append(text);
126 public void save_actionPerformed(ActionEvent e)
128 JalviewFileChooser chooser = new JalviewFileChooser(
129 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
131 chooser.setAcceptAllFileFilterUsed(false);
132 chooser.setFileView(new JalviewFileView());
133 chooser.setDialogTitle(MessageManager
134 .getString("label.save_text_to_file"));
135 chooser.setToolTipText(MessageManager.getString("action.save"));
137 int value = chooser.showSaveDialog(this);
139 if (value == JalviewFileChooser.APPROVE_OPTION)
143 java.io.PrintWriter out = new java.io.PrintWriter(
144 new java.io.FileWriter(chooser.getSelectedFile()));
146 out.print(getText());
148 } catch (Exception ex)
150 ex.printStackTrace();
162 public void copyItem_actionPerformed(ActionEvent e)
164 textarea.getSelectedText();
165 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
166 c.setContents(new StringSelection(textarea.getSelectedText()), null);
175 public void pasteMenu_actionPerformed(ActionEvent e)
177 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
178 Transferable contents = c.getContents(this);
180 if (contents == null)
187 textarea.append((String) contents
188 .getTransferData(DataFlavor.stringFlavor));
189 } catch (Exception ex)
200 public void ok_actionPerformed(ActionEvent e)
202 String text = getText();
203 if (text.trim().length() < 1)
208 String format = new IdentifyFile().Identify(text, "Paste");
209 if (format == null || format.equalsIgnoreCase("EMPTY DATA FILE"))
211 System.err.println(MessageManager
212 .getString("label.couldnt_read_data"));
213 if (!Jalview.isHeadlessMode())
215 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
216 AppletFormatAdapter.SUPPORTED_FORMATS,
217 MessageManager.getString("label.couldnt_read_data"),
218 JOptionPane.WARNING_MESSAGE);
223 // TODO: identify feature, annotation or tree file and parse appropriately.
224 AlignmentI al = null;
226 if (FormatAdapter.isValidFormat(format))
230 FormatAdapter fa = new FormatAdapter(alignpanel);
231 al = fa.readFile(getText(), "Paste", format);
232 source = fa.getAlignFile();
234 } catch (java.io.IOException ex)
236 JOptionPane.showInternalMessageDialog(Desktop.desktop,
237 MessageManager.formatMessage(
238 "label.couldnt_read_pasted_text",
239 new String[] { ex.toString() }), MessageManager
240 .getString("label.error_parsing_text"),
241 JOptionPane.WARNING_MESSAGE);
245 if (al != null && al.hasValidSequence())
247 String title = MessageManager.formatMessage(
248 "label.input_cut_paste_params", new String[] { format });
249 if (viewport != null)
251 ((AlignViewport) viewport).addAlignment(al, title);
257 if (source instanceof ComplexAlignFile)
259 ColumnSelection colSel = ((ComplexAlignFile) source)
260 .getColumnSelection();
261 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
262 .getHiddenSequences();
263 boolean showSeqFeatures = ((ComplexAlignFile) source)
264 .isShowSeqFeatures();
265 ColourSchemeI cs = ((ComplexAlignFile) source).getColourScheme();
266 FeaturesDisplayedI fd = ((ComplexAlignFile) source)
267 .getDisplayedFeatures();
268 af = new AlignFrame(al, hiddenSeqs, colSel,
269 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
270 af.getViewport().setShowSequenceFeatures(showSeqFeatures);
271 af.getViewport().setFeaturesDisplayed(fd);
276 af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
277 AlignFrame.DEFAULT_HEIGHT);
280 af.currentFileFormat = format;
281 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
282 AlignFrame.DEFAULT_HEIGHT);
283 af.statusBar.setText(MessageManager
284 .getString("label.successfully_pasted_alignment_file"));
288 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
290 } catch (Exception ex)
297 System.err.println(MessageManager
298 .getString("label.couldnt_read_data"));
299 if (!Jalview.isHeadlessMode())
301 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
302 AppletFormatAdapter.SUPPORTED_FORMATS,
303 MessageManager.getString("label.couldnt_read_data"),
304 JOptionPane.WARNING_MESSAGE);
315 public void cancel_actionPerformed(ActionEvent e)
319 this.setClosed(true);
320 } catch (Exception ex)
325 public void textarea_mousePressed(MouseEvent e)
327 if (SwingUtilities.isRightMouseButton(e))
329 JPopupMenu popup = new JPopupMenu(
330 MessageManager.getString("action.edit"));
331 JMenuItem item = new JMenuItem(
332 MessageManager.getString("action.copy"));
333 item.addActionListener(new ActionListener()
335 public void actionPerformed(ActionEvent e)
337 copyItem_actionPerformed(e);
341 item = new JMenuItem(MessageManager.getString("action.paste"));
342 item.addActionListener(new ActionListener()
344 public void actionPerformed(ActionEvent e)
346 pasteMenu_actionPerformed(e);
350 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);