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.FileParse;
35 import jalview.io.FormatAdapter;
36 import jalview.io.IdentifyFile;
37 import jalview.io.JalviewFileChooser;
38 import jalview.io.JalviewFileView;
39 import jalview.jbgui.GCutAndPasteTransfer;
40 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
41 import jalview.schemes.ColourSchemeI;
42 import jalview.util.MessageManager;
44 import java.awt.Toolkit;
45 import java.awt.datatransfer.Clipboard;
46 import java.awt.datatransfer.DataFlavor;
47 import java.awt.datatransfer.StringSelection;
48 import java.awt.datatransfer.Transferable;
49 import java.awt.event.ActionEvent;
50 import java.awt.event.ActionListener;
51 import java.awt.event.MouseEvent;
53 import javax.swing.JMenuItem;
54 import javax.swing.JOptionPane;
55 import javax.swing.JPopupMenu;
56 import javax.swing.SwingUtilities;
59 * Cut'n'paste files into the desktop See JAL-1105
64 public class CutAndPasteTransfer extends GCutAndPasteTransfer
67 AlignmentViewPanel alignpanel;
69 AlignViewportI viewport;
71 FileParse source = null;
73 public CutAndPasteTransfer()
75 SwingUtilities.invokeLater(new Runnable()
80 textarea.requestFocus();
89 public void setForInput(AlignmentViewPanel viewpanel)
91 this.alignpanel = viewpanel;
92 if (alignpanel != null)
94 this.viewport = alignpanel.getAlignViewport();
98 ok.setText(MessageManager.getString("action.add"));
101 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
107 * @return DOCUMENT ME!
109 public String getText()
111 return textarea.getText();
120 public void setText(String text)
122 textarea.setText(text);
125 public void appendText(String text)
127 textarea.append(text);
131 public void save_actionPerformed(ActionEvent e)
133 JalviewFileChooser chooser = new JalviewFileChooser(
134 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
136 chooser.setAcceptAllFileFilterUsed(false);
137 chooser.setFileView(new JalviewFileView());
138 chooser.setDialogTitle(MessageManager
139 .getString("label.save_text_to_file"));
140 chooser.setToolTipText(MessageManager.getString("action.save"));
142 int value = chooser.showSaveDialog(this);
144 if (value == JalviewFileChooser.APPROVE_OPTION)
148 java.io.PrintWriter out = new java.io.PrintWriter(
149 new java.io.FileWriter(chooser.getSelectedFile()));
151 out.print(getText());
153 } catch (Exception ex)
155 ex.printStackTrace();
168 public void copyItem_actionPerformed(ActionEvent e)
170 textarea.getSelectedText();
171 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
172 c.setContents(new StringSelection(textarea.getSelectedText()), null);
182 public void pasteMenu_actionPerformed(ActionEvent e)
184 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
185 Transferable contents = c.getContents(this);
187 if (contents == null)
194 textarea.append((String) contents
195 .getTransferData(DataFlavor.stringFlavor));
196 } catch (Exception ex)
208 public void ok_actionPerformed(ActionEvent e)
210 String text = getText();
211 if (text.trim().length() < 1)
216 String format = new IdentifyFile().identify(text, "Paste");
217 if (format == null || format.equalsIgnoreCase("EMPTY DATA FILE"))
219 System.err.println(MessageManager
220 .getString("label.couldnt_read_data"));
221 if (!Jalview.isHeadlessMode())
223 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
224 AppletFormatAdapter.SUPPORTED_FORMATS,
225 MessageManager.getString("label.couldnt_read_data"),
226 JOptionPane.WARNING_MESSAGE);
231 // TODO: identify feature, annotation or tree file and parse appropriately.
232 AlignmentI al = null;
234 if (FormatAdapter.isValidFormat(format))
238 FormatAdapter fa = new FormatAdapter(alignpanel);
239 al = fa.readFile(getText(), "Paste", format);
240 source = fa.getAlignFile();
242 } catch (java.io.IOException ex)
244 JOptionPane.showInternalMessageDialog(Desktop.desktop,
245 MessageManager.formatMessage(
246 "label.couldnt_read_pasted_text",
247 new String[] { ex.toString() }), MessageManager
248 .getString("label.error_parsing_text"),
249 JOptionPane.WARNING_MESSAGE);
253 if (al != null && al.hasValidSequence())
255 String title = MessageManager.formatMessage(
256 "label.input_cut_paste_params", new String[] { format });
257 FeatureSettingsModelI proxyColourScheme = source
258 .getFeatureColourScheme();
261 * if the view panel was closed its alignment is nulled
262 * and this is an orphaned cut and paste window
264 if (viewport != null && viewport.getAlignment() != null)
266 if (proxyColourScheme != null)
268 viewport.applyFeaturesStyle(proxyColourScheme);
270 ((AlignViewport) viewport).addAlignment(al, title);
276 if (source instanceof ComplexAlignFile)
278 ColumnSelection colSel = ((ComplexAlignFile) source)
279 .getColumnSelection();
280 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
281 .getHiddenSequences();
282 boolean showSeqFeatures = ((ComplexAlignFile) source)
283 .isShowSeqFeatures();
284 String colourSchemeName = ((ComplexAlignFile) source)
285 .getGlobalColourScheme();
286 FeaturesDisplayedI fd = ((ComplexAlignFile) source)
287 .getDisplayedFeatures();
288 af = new AlignFrame(al, hiddenSeqs, colSel,
289 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
290 af.getViewport().setShowSequenceFeatures(showSeqFeatures);
291 af.getViewport().setFeaturesDisplayed(fd);
292 ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme(
293 colourSchemeName, al);
301 af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
302 AlignFrame.DEFAULT_HEIGHT);
303 if (source instanceof FeaturesSourceI)
305 af.getViewport().setShowSequenceFeatures(true);
308 if (proxyColourScheme != null)
310 af.getViewport().applyFeaturesStyle(proxyColourScheme);
312 af.currentFileFormat = format;
313 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
314 AlignFrame.DEFAULT_HEIGHT);
315 af.statusBar.setText(MessageManager
316 .getString("label.successfully_pasted_alignment_file"));
320 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
322 } catch (Exception ex)
329 System.err.println(MessageManager
330 .getString("label.couldnt_read_data"));
331 if (!Jalview.isHeadlessMode())
333 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
334 AppletFormatAdapter.SUPPORTED_FORMATS,
335 MessageManager.getString("label.couldnt_read_data"),
336 JOptionPane.WARNING_MESSAGE);
348 public void cancel_actionPerformed(ActionEvent e)
352 this.setClosed(true);
353 } catch (Exception ex)
359 public void textarea_mousePressed(MouseEvent e)
361 if (SwingUtilities.isRightMouseButton(e))
363 JPopupMenu popup = new JPopupMenu(
364 MessageManager.getString("action.edit"));
365 JMenuItem item = new JMenuItem(
366 MessageManager.getString("action.copy"));
367 item.addActionListener(new ActionListener()
370 public void actionPerformed(ActionEvent e)
372 copyItem_actionPerformed(e);
376 item = new JMenuItem(MessageManager.getString("action.paste"));
377 item.addActionListener(new ActionListener()
380 public void actionPerformed(ActionEvent e)
382 pasteMenu_actionPerformed(e);
386 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);