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.AlignmentFileI;
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;
56 import javax.swing.JMenuItem;
57 import javax.swing.JOptionPane;
58 import javax.swing.JPopupMenu;
59 import javax.swing.SwingUtilities;
62 * Cut'n'paste files into the desktop See JAL-1105
67 public class CutAndPasteTransfer extends GCutAndPasteTransfer
70 AlignmentViewPanel alignpanel;
72 AlignViewportI viewport;
74 AlignmentFileI source = null;
76 public CutAndPasteTransfer()
78 SwingUtilities.invokeLater(new Runnable()
83 textarea.requestFocus();
92 public void setForInput(AlignmentViewPanel viewpanel)
94 this.alignpanel = viewpanel;
95 if (alignpanel != null)
97 this.viewport = alignpanel.getAlignViewport();
101 ok.setText(MessageManager.getString("action.add"));
104 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
110 * @return DOCUMENT ME!
112 public String getText()
114 return textarea.getText();
123 public void setText(String text)
125 textarea.setText(text);
128 public void appendText(String text)
130 textarea.append(text);
134 public void save_actionPerformed(ActionEvent e)
136 JalviewFileChooser chooser = new JalviewFileChooser(
137 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
139 chooser.setAcceptAllFileFilterUsed(false);
140 chooser.setFileView(new JalviewFileView());
141 chooser.setDialogTitle(MessageManager
142 .getString("label.save_text_to_file"));
143 chooser.setToolTipText(MessageManager.getString("action.save"));
145 int value = chooser.showSaveDialog(this);
147 if (value == JalviewFileChooser.APPROVE_OPTION)
151 java.io.PrintWriter out = new java.io.PrintWriter(
152 new java.io.FileWriter(chooser.getSelectedFile()));
154 out.print(getText());
156 } catch (Exception ex)
158 ex.printStackTrace();
171 public void copyItem_actionPerformed(ActionEvent e)
173 textarea.getSelectedText();
174 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
175 c.setContents(new StringSelection(textarea.getSelectedText()), null);
185 public void pasteMenu_actionPerformed(ActionEvent e)
187 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
188 Transferable contents = c.getContents(this);
190 if (contents == null)
197 textarea.append((String) contents
198 .getTransferData(DataFlavor.stringFlavor));
199 } catch (Exception ex)
211 public void ok_actionPerformed(ActionEvent e)
213 String text = getText();
214 if (text.trim().length() < 1)
219 FileFormatI format = null;
222 format = new IdentifyFile().identify(text, DataSourceType.PASTE);
223 } catch (FileFormatException e1)
229 System.err.println(MessageManager
230 .getString("label.couldnt_read_data"));
231 if (!Jalview.isHeadlessMode())
233 JOptionPane.showInternalMessageDialog(Desktop.desktop,
234 AppletFormatAdapter.SUPPORTED_FORMATS,
235 MessageManager.getString("label.couldnt_read_data"),
236 JOptionPane.WARNING_MESSAGE);
241 // TODO: identify feature, annotation or tree file and parse appropriately.
242 AlignmentI al = null;
246 FormatAdapter fa = new FormatAdapter(alignpanel);
247 al = fa.readFile(getText(), DataSourceType.PASTE, format);
248 source = fa.getAlignFile();
250 } catch (java.io.IOException ex)
252 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
253 .formatMessage("label.couldnt_read_pasted_text", new String[]
254 { ex.toString() }), MessageManager
255 .getString("label.error_parsing_text"),
256 JOptionPane.WARNING_MESSAGE);
259 if (al != null && al.hasValidSequence())
261 String title = MessageManager.formatMessage(
262 "label.input_cut_paste_params",
263 new String[] { format.toString() });
264 FeatureSettingsModelI proxyColourScheme = source
265 .getFeatureColourScheme();
268 * if the view panel was closed its alignment is nulled
269 * and this is an orphaned cut and paste window
271 if (viewport != null && viewport.getAlignment() != null)
273 if (proxyColourScheme != null)
275 viewport.applyFeaturesStyle(proxyColourScheme);
277 ((AlignViewport) viewport).addAlignment(al, title);
283 if (source instanceof ComplexAlignFile)
285 ColumnSelection colSel = ((ComplexAlignFile) source)
286 .getColumnSelection();
287 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
288 .getHiddenSequences();
289 boolean showSeqFeatures = ((ComplexAlignFile) source)
290 .isShowSeqFeatures();
291 String colourSchemeName = ((ComplexAlignFile) source)
292 .getGlobalColourScheme();
293 FeaturesDisplayedI fd = ((ComplexAlignFile) source)
294 .getDisplayedFeatures();
295 af = new AlignFrame(al, hiddenSeqs, colSel,
296 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
297 af.getViewport().setShowSequenceFeatures(showSeqFeatures);
298 af.getViewport().setFeaturesDisplayed(fd);
299 ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme(
300 colourSchemeName, al);
308 af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
309 AlignFrame.DEFAULT_HEIGHT);
310 if (source instanceof FeaturesSourceI)
312 af.getViewport().setShowSequenceFeatures(true);
315 if (proxyColourScheme != null)
317 af.getViewport().applyFeaturesStyle(proxyColourScheme);
319 af.currentFileFormat = format;
320 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
321 AlignFrame.DEFAULT_HEIGHT);
322 af.statusBar.setText(MessageManager
323 .getString("label.successfully_pasted_alignment_file"));
327 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
329 } catch (Exception ex)
336 System.err.println(MessageManager
337 .getString("label.couldnt_read_data"));
338 if (!Jalview.isHeadlessMode())
340 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
341 AppletFormatAdapter.SUPPORTED_FORMATS,
342 MessageManager.getString("label.couldnt_read_data"),
343 JOptionPane.WARNING_MESSAGE);
355 public void cancel_actionPerformed(ActionEvent e)
359 this.setClosed(true);
360 } catch (Exception ex)
366 public void textarea_mousePressed(MouseEvent e)
368 if (e.isPopupTrigger())
370 JPopupMenu popup = new JPopupMenu(
371 MessageManager.getString("action.edit"));
372 JMenuItem item = new JMenuItem(
373 MessageManager.getString("action.copy"));
374 item.addActionListener(new ActionListener()
377 public void actionPerformed(ActionEvent e)
379 copyItem_actionPerformed(e);
383 item = new JMenuItem(MessageManager.getString("action.paste"));
384 item.addActionListener(new ActionListener()
387 public void actionPerformed(ActionEvent e)
389 pasteMenu_actionPerformed(e);
393 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);