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.AppletFormatAdapter;
34 import jalview.io.DataSourceType;
35 import jalview.io.FileFormatI;
36 import jalview.io.FileParse;
37 import jalview.io.FormatAdapter;
38 import jalview.io.IdentifyFile;
39 import jalview.io.JalviewFileChooser;
40 import jalview.io.JalviewFileView;
41 import jalview.jbgui.GCutAndPasteTransfer;
42 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
43 import jalview.schemes.ColourSchemeI;
44 import jalview.util.MessageManager;
46 import java.awt.Toolkit;
47 import java.awt.datatransfer.Clipboard;
48 import java.awt.datatransfer.DataFlavor;
49 import java.awt.datatransfer.StringSelection;
50 import java.awt.datatransfer.Transferable;
51 import java.awt.event.ActionEvent;
52 import java.awt.event.ActionListener;
53 import java.awt.event.MouseEvent;
55 import javax.swing.JMenuItem;
56 import javax.swing.JOptionPane;
57 import javax.swing.JPopupMenu;
58 import javax.swing.SwingUtilities;
61 * Cut'n'paste files into the desktop See JAL-1105
66 public class CutAndPasteTransfer extends GCutAndPasteTransfer
69 AlignmentViewPanel alignpanel;
71 AlignViewportI viewport;
73 FileParse source = null;
75 public CutAndPasteTransfer()
77 SwingUtilities.invokeLater(new Runnable()
82 textarea.requestFocus();
91 public void setForInput(AlignmentViewPanel viewpanel)
93 this.alignpanel = viewpanel;
94 if (alignpanel != null)
96 this.viewport = alignpanel.getAlignViewport();
100 ok.setText(MessageManager.getString("action.add"));
103 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
109 * @return DOCUMENT ME!
111 public String getText()
113 return textarea.getText();
122 public void setText(String text)
124 textarea.setText(text);
127 public void appendText(String text)
129 textarea.append(text);
133 public void save_actionPerformed(ActionEvent e)
135 JalviewFileChooser chooser = new JalviewFileChooser(
136 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
138 chooser.setAcceptAllFileFilterUsed(false);
139 chooser.setFileView(new JalviewFileView());
140 chooser.setDialogTitle(MessageManager
141 .getString("label.save_text_to_file"));
142 chooser.setToolTipText(MessageManager.getString("action.save"));
144 int value = chooser.showSaveDialog(this);
146 if (value == JalviewFileChooser.APPROVE_OPTION)
150 java.io.PrintWriter out = new java.io.PrintWriter(
151 new java.io.FileWriter(chooser.getSelectedFile()));
153 out.print(getText());
155 } catch (Exception ex)
157 ex.printStackTrace();
170 public void copyItem_actionPerformed(ActionEvent e)
172 textarea.getSelectedText();
173 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
174 c.setContents(new StringSelection(textarea.getSelectedText()), null);
184 public void pasteMenu_actionPerformed(ActionEvent e)
186 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
187 Transferable contents = c.getContents(this);
189 if (contents == null)
196 textarea.append((String) contents
197 .getTransferData(DataFlavor.stringFlavor));
198 } catch (Exception ex)
210 public void ok_actionPerformed(ActionEvent e)
212 String text = getText();
213 if (text.trim().length() < 1)
218 FileFormatI format = new IdentifyFile().identify(text,
219 DataSourceType.PASTE);
222 System.err.println(MessageManager
223 .getString("label.couldnt_read_data"));
224 if (!Jalview.isHeadlessMode())
226 JOptionPane.showInternalMessageDialog(Desktop.desktop,
227 AppletFormatAdapter.SUPPORTED_FORMATS,
228 MessageManager.getString("label.couldnt_read_data"),
229 JOptionPane.WARNING_MESSAGE);
234 // TODO: identify feature, annotation or tree file and parse appropriately.
235 AlignmentI al = null;
237 if (FormatAdapter.isValidFormat(format))
241 FormatAdapter fa = new FormatAdapter(alignpanel);
242 al = fa.readFile(getText(), DataSourceType.PASTE, format);
243 source = fa.getAlignFile();
245 } catch (java.io.IOException ex)
247 JOptionPane.showInternalMessageDialog(Desktop.desktop,
248 MessageManager.formatMessage(
249 "label.couldnt_read_pasted_text",
250 new String[] { ex.toString() }), MessageManager
251 .getString("label.error_parsing_text"),
252 JOptionPane.WARNING_MESSAGE);
256 if (al != null && al.hasValidSequence())
258 String title = MessageManager.formatMessage(
259 "label.input_cut_paste_params", new String[] { format });
260 FeatureSettingsModelI proxyColourScheme = source
261 .getFeatureColourScheme();
264 * if the view panel was closed its alignment is nulled
265 * and this is an orphaned cut and paste window
267 if (viewport != null && viewport.getAlignment() != null)
269 if (proxyColourScheme != null)
271 viewport.applyFeaturesStyle(proxyColourScheme);
273 ((AlignViewport) viewport).addAlignment(al, title);
279 if (source instanceof ComplexAlignFile)
281 ColumnSelection colSel = ((ComplexAlignFile) source)
282 .getColumnSelection();
283 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
284 .getHiddenSequences();
285 boolean showSeqFeatures = ((ComplexAlignFile) source)
286 .isShowSeqFeatures();
287 String colourSchemeName = ((ComplexAlignFile) source)
288 .getGlobalColourScheme();
289 FeaturesDisplayedI fd = ((ComplexAlignFile) source)
290 .getDisplayedFeatures();
291 af = new AlignFrame(al, hiddenSeqs, colSel,
292 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
293 af.getViewport().setShowSequenceFeatures(showSeqFeatures);
294 af.getViewport().setFeaturesDisplayed(fd);
295 ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme(
296 colourSchemeName, al);
304 af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
305 AlignFrame.DEFAULT_HEIGHT);
306 if (source instanceof FeaturesSourceI)
308 af.getViewport().setShowSequenceFeatures(true);
311 if (proxyColourScheme != null)
313 af.getViewport().applyFeaturesStyle(proxyColourScheme);
315 af.currentFileFormat = format;
316 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
317 AlignFrame.DEFAULT_HEIGHT);
318 af.statusBar.setText(MessageManager
319 .getString("label.successfully_pasted_alignment_file"));
323 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
325 } catch (Exception ex)
332 System.err.println(MessageManager
333 .getString("label.couldnt_read_data"));
334 if (!Jalview.isHeadlessMode())
336 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
337 AppletFormatAdapter.SUPPORTED_FORMATS,
338 MessageManager.getString("label.couldnt_read_data"),
339 JOptionPane.WARNING_MESSAGE);
351 public void cancel_actionPerformed(ActionEvent e)
355 this.setClosed(true);
356 } catch (Exception ex)
362 public void textarea_mousePressed(MouseEvent e)
364 if (e.isPopupTrigger())
366 JPopupMenu popup = new JPopupMenu(
367 MessageManager.getString("action.edit"));
368 JMenuItem item = new JMenuItem(
369 MessageManager.getString("action.copy"));
370 item.addActionListener(new ActionListener()
373 public void actionPerformed(ActionEvent e)
375 copyItem_actionPerformed(e);
379 item = new JMenuItem(MessageManager.getString("action.paste"));
380 item.addActionListener(new ActionListener()
383 public void actionPerformed(ActionEvent e)
385 pasteMenu_actionPerformed(e);
389 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);