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.api.FeatureSettingsModelI;
27 import jalview.api.FeaturesDisplayedI;
28 import jalview.api.FeaturesSourceI;
29 import jalview.bin.Jalview;
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.ColumnSelection;
32 import jalview.datamodel.SequenceI;
33 import jalview.io.AlignmentFileReaderI;
34 import jalview.io.AppletFormatAdapter;
35 import jalview.io.DataSourceType;
36 import jalview.io.FileFormatException;
37 import jalview.io.FileFormatI;
38 import jalview.io.FormatAdapter;
39 import jalview.io.IdentifyFile;
40 import jalview.io.JalviewFileChooser;
41 import jalview.io.JalviewFileView;
42 import jalview.jbgui.GCutAndPasteTransfer;
43 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
44 import jalview.schemes.ColourSchemeI;
45 import jalview.util.MessageManager;
47 import java.awt.Toolkit;
48 import java.awt.datatransfer.Clipboard;
49 import java.awt.datatransfer.DataFlavor;
50 import java.awt.datatransfer.StringSelection;
51 import java.awt.datatransfer.Transferable;
52 import java.awt.event.ActionEvent;
53 import java.awt.event.ActionListener;
54 import java.awt.event.MouseEvent;
55 import java.io.FileWriter;
56 import java.io.IOException;
57 import java.io.PrintWriter;
59 import javax.swing.JMenuItem;
60 import javax.swing.JPopupMenu;
61 import javax.swing.SwingUtilities;
64 * Cut'n'paste files into the desktop See JAL-1105
69 public class CutAndPasteTransfer extends GCutAndPasteTransfer
72 AlignmentViewPanel alignpanel;
74 AlignViewportI viewport;
76 AlignmentFileReaderI source = null;
78 public CutAndPasteTransfer()
80 SwingUtilities.invokeLater(new Runnable()
85 textarea.requestFocus();
94 public void setForInput(AlignmentViewPanel viewpanel)
96 this.alignpanel = viewpanel;
97 if (alignpanel != null)
99 this.viewport = alignpanel.getAlignViewport();
101 if (viewport != null)
103 ok.setText(MessageManager.getString("action.add"));
106 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
112 * @return DOCUMENT ME!
114 public String getText()
116 return textarea.getText();
125 public void setText(String text)
127 textarea.setText(text);
130 public void appendText(String text)
132 textarea.append(text);
136 public void save_actionPerformed(ActionEvent e)
138 JalviewFileChooser chooser = new JalviewFileChooser(
139 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
141 chooser.setAcceptAllFileFilterUsed(false);
142 chooser.setFileView(new JalviewFileView());
143 chooser.setDialogTitle(MessageManager
144 .getString("label.save_text_to_file"));
145 chooser.setToolTipText(MessageManager.getString("action.save"));
147 int value = chooser.showSaveDialog(this);
149 if (value == JalviewFileChooser.APPROVE_OPTION)
153 PrintWriter out = new PrintWriter(new FileWriter(
154 chooser.getSelectedFile()));
156 out.print(getText());
158 } catch (Exception ex)
160 ex.printStackTrace();
173 public void copyItem_actionPerformed(ActionEvent e)
175 textarea.getSelectedText();
176 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
177 c.setContents(new StringSelection(textarea.getSelectedText()), null);
187 public void pasteMenu_actionPerformed(ActionEvent e)
189 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
190 Transferable contents = c.getContents(this);
192 if (contents == null)
199 textarea.append((String) contents
200 .getTransferData(DataFlavor.stringFlavor));
201 } catch (Exception ex)
213 public void ok_actionPerformed(ActionEvent e)
215 String text = getText();
216 if (text.trim().length() < 1)
221 FileFormatI format = null;
224 format = new IdentifyFile().identify(text, DataSourceType.PASTE);
225 } catch (FileFormatException e1)
231 System.err.println(MessageManager
232 .getString("label.couldnt_read_data"));
233 if (!Jalview.isHeadlessMode())
235 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
236 AppletFormatAdapter.getSupportedFormats(),
237 MessageManager.getString("label.couldnt_read_data"),
238 JvOptionPane.WARNING_MESSAGE);
243 // TODO: identify feature, annotation or tree file and parse appropriately.
244 AlignmentI al = null;
248 FormatAdapter fa = new FormatAdapter(alignpanel);
249 al = fa.readFile(getText(), DataSourceType.PASTE, format);
250 source = fa.getAlignFile();
252 } catch (IOException ex)
254 JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
255 .formatMessage("label.couldnt_read_pasted_text", new String[]
256 { ex.toString() }), MessageManager
257 .getString("label.error_parsing_text"),
258 JvOptionPane.WARNING_MESSAGE);
261 if (al != null && al.hasValidSequence())
263 String title = MessageManager.formatMessage(
264 "label.input_cut_paste_params",
265 new String[] { format.getName() });
266 FeatureSettingsModelI proxyColourScheme = source
267 .getFeatureColourScheme();
270 * if the view panel was closed its alignment is nulled
271 * and this is an orphaned cut and paste window
273 if (viewport != null && viewport.getAlignment() != null)
275 if (proxyColourScheme != null)
277 viewport.applyFeaturesStyle(proxyColourScheme);
279 ((AlignViewport) viewport).addAlignment(al, title);
285 if (source instanceof ComplexAlignFile)
287 ColumnSelection colSel = ((ComplexAlignFile) source)
288 .getColumnSelection();
289 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
290 .getHiddenSequences();
291 boolean showSeqFeatures = ((ComplexAlignFile) source)
292 .isShowSeqFeatures();
293 String colourSchemeName = ((ComplexAlignFile) source)
294 .getGlobalColourScheme();
295 FeaturesDisplayedI fd = ((ComplexAlignFile) source)
296 .getDisplayedFeatures();
297 af = new AlignFrame(al, hiddenSeqs, colSel,
298 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
299 af.getViewport().setShowSequenceFeatures(showSeqFeatures);
300 af.getViewport().setFeaturesDisplayed(fd);
301 ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme(
302 colourSchemeName, al);
310 af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
311 AlignFrame.DEFAULT_HEIGHT);
312 if (source instanceof FeaturesSourceI)
314 af.getViewport().setShowSequenceFeatures(true);
317 if (proxyColourScheme != null)
319 af.getViewport().applyFeaturesStyle(proxyColourScheme);
321 af.currentFileFormat = format;
322 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
323 AlignFrame.DEFAULT_HEIGHT);
324 af.statusBar.setText(MessageManager
325 .getString("label.successfully_pasted_alignment_file"));
329 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
331 } catch (Exception ex)
338 System.err.println(MessageManager
339 .getString("label.couldnt_read_data"));
340 if (!Jalview.isHeadlessMode())
342 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
343 AppletFormatAdapter.getSupportedFormats(),
344 MessageManager.getString("label.couldnt_read_data"),
345 JvOptionPane.WARNING_MESSAGE);
357 public void cancel_actionPerformed(ActionEvent e)
361 this.setClosed(true);
362 } catch (Exception ex)
368 public void textarea_mousePressed(MouseEvent e)
371 * isPopupTrigger is checked in mousePressed on Mac,
372 * in mouseReleased on Windows
374 if (e.isPopupTrigger())
376 JPopupMenu popup = new JPopupMenu(
377 MessageManager.getString("action.edit"));
378 JMenuItem item = new JMenuItem(
379 MessageManager.getString("action.copy"));
380 item.addActionListener(new ActionListener()
383 public void actionPerformed(ActionEvent e)
385 copyItem_actionPerformed(e);
389 item = new JMenuItem(MessageManager.getString("action.paste"));
390 item.addActionListener(new ActionListener()
393 public void actionPerformed(ActionEvent e)
395 pasteMenu_actionPerformed(e);
399 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);