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 java.awt.Toolkit;
24 import java.awt.datatransfer.Clipboard;
25 import java.awt.datatransfer.DataFlavor;
26 import java.awt.datatransfer.StringSelection;
27 import java.awt.datatransfer.Transferable;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.MouseEvent;
31 import java.io.FileNotFoundException;
32 import java.io.FileWriter;
33 import java.io.IOException;
34 import java.io.PrintWriter;
36 import javax.swing.JMenuItem;
37 import javax.swing.JPopupMenu;
38 import javax.swing.SwingUtilities;
40 import jalview.api.AlignViewportI;
41 import jalview.api.AlignmentViewPanel;
42 import jalview.api.ComplexAlignFile;
43 import jalview.api.FeatureSettingsModelI;
44 import jalview.api.FeaturesDisplayedI;
45 import jalview.api.FeaturesSourceI;
46 import jalview.bin.Cache;
47 import jalview.bin.Jalview;
48 import jalview.datamodel.AlignmentI;
49 import jalview.datamodel.HiddenColumns;
50 import jalview.datamodel.SequenceI;
51 import jalview.io.AlignmentFileReaderI;
52 import jalview.io.AppletFormatAdapter;
53 import jalview.io.DataSourceType;
54 import jalview.io.FileFormatException;
55 import jalview.io.FileFormatI;
56 import jalview.io.FormatAdapter;
57 import jalview.io.IdentifyFile;
58 import jalview.io.JalviewFileChooser;
59 import jalview.io.JalviewFileView;
60 import jalview.jbgui.GCutAndPasteTransfer;
61 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
62 import jalview.schemes.ColourSchemeI;
63 import jalview.util.MessageManager;
66 * Cut'n'paste files into the desktop See JAL-1105
71 public class CutAndPasteTransfer extends GCutAndPasteTransfer
74 AlignmentViewPanel alignpanel;
76 AlignViewportI viewport;
78 AlignmentFileReaderI source = null;
80 public CutAndPasteTransfer()
82 this.setFrameIcon(null);
83 SwingUtilities.invokeLater(new Runnable()
88 textarea.requestFocus();
97 public void setForInput(AlignmentViewPanel viewpanel)
99 this.alignpanel = viewpanel;
100 if (alignpanel != null)
102 this.viewport = alignpanel.getAlignViewport();
104 if (viewport != null)
106 ok.setText(MessageManager.getString("action.add"));
109 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
115 * @return DOCUMENT ME!
117 public String getText()
119 return textarea.getText();
128 public void setText(String text)
130 textarea.setText(text);
133 public void appendText(String text)
135 textarea.append(text);
139 public void save_actionPerformed(ActionEvent e)
141 // TODO: JAL-3048 JalviewFileChooser - Save option
143 JalviewFileChooser chooser = new JalviewFileChooser(
144 Cache.getProperty("LAST_DIRECTORY"));
146 chooser.setAcceptAllFileFilterUsed(false);
147 chooser.setFileView(new JalviewFileView());
148 chooser.setDialogTitle(
149 MessageManager.getString("label.save_text_to_file"));
150 chooser.setToolTipText(MessageManager.getString("action.save"));
152 int value = chooser.showSaveDialog(this);
154 if (value == JalviewFileChooser.APPROVE_OPTION)
158 PrintWriter out = new PrintWriter(
159 new FileWriter(chooser.getSelectedFile()));
161 out.print(getText());
163 } catch (Exception ex)
165 ex.printStackTrace();
178 public void copyItem_actionPerformed(ActionEvent e)
180 textarea.getSelectedText();
181 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
182 c.setContents(new StringSelection(textarea.getSelectedText()), null);
192 public void pasteMenu_actionPerformed(ActionEvent e)
194 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
195 Transferable contents = c.getContents(this);
197 if (contents == null)
205 (String) contents.getTransferData(DataFlavor.stringFlavor));
206 } catch (Exception ex)
218 public void ok_actionPerformed(ActionEvent e)
220 String text = getText();
221 if (text.trim().length() < 1)
226 FileFormatI format = null;
229 format = new IdentifyFile().identify(text, DataSourceType.PASTE);
230 } catch (FileNotFoundException e0)
233 } catch (FileFormatException e1)
240 .println(MessageManager.getString("label.couldnt_read_data"));
241 if (!Jalview.isHeadlessMode())
243 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
244 AppletFormatAdapter.getSupportedFormats(),
245 MessageManager.getString("label.couldnt_read_data"),
246 JvOptionPane.WARNING_MESSAGE);
251 // TODO: identify feature, annotation or tree file and parse appropriately.
252 AlignmentI al = null;
256 FormatAdapter fa = new FormatAdapter(alignpanel);
257 al = fa.readFile(getText(), DataSourceType.PASTE, format);
258 source = fa.getAlignFile();
260 } catch (IOException ex)
262 JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
263 .formatMessage("label.couldnt_read_pasted_text", new String[]
265 MessageManager.getString("label.error_parsing_text"),
266 JvOptionPane.WARNING_MESSAGE);
269 if (al != null && al.hasValidSequence())
271 String title = MessageManager
272 .formatMessage("label.input_cut_paste_params", new String[]
273 { format.getName() });
274 FeatureSettingsModelI proxyColourScheme = source
275 .getFeatureColourScheme();
278 * if the view panel was closed its alignment is nulled
279 * and this is an orphaned cut and paste window
281 if (viewport != null && viewport.getAlignment() != null)
283 ((AlignViewport) viewport).addAlignment(al, title);
284 viewport.applyFeaturesStyle(proxyColourScheme);
290 if (source instanceof ComplexAlignFile)
292 HiddenColumns hidden = ((ComplexAlignFile) source)
294 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
295 .getHiddenSequences();
296 boolean showSeqFeatures = ((ComplexAlignFile) source)
297 .isShowSeqFeatures();
298 String colourSchemeName = ((ComplexAlignFile) source)
299 .getGlobalColourScheme();
300 FeaturesDisplayedI fd = ((ComplexAlignFile) source)
301 .getDisplayedFeatures();
302 af = new AlignFrame(al, hiddenSeqs, hidden,
303 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
304 af.getViewport().setShowSequenceFeatures(showSeqFeatures);
305 af.getViewport().setFeaturesDisplayed(fd);
306 af.setMenusForViewport();
307 ColourSchemeI cs = ColourSchemeMapper
308 .getJalviewColourScheme(colourSchemeName, al);
316 af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
317 AlignFrame.DEFAULT_HEIGHT);
318 if (source instanceof FeaturesSourceI)
320 af.getViewport().setShowSequenceFeatures(true);
323 if (proxyColourScheme != null)
325 af.getViewport().applyFeaturesStyle(proxyColourScheme);
327 af.currentFileFormat = format;
328 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
329 AlignFrame.DEFAULT_HEIGHT);
330 af.setStatus(MessageManager
331 .getString("label.successfully_pasted_alignment_file"));
335 af.setMaximum(Cache.getDefault("SHOW_FULLSCREEN", false));
336 } catch (Exception ex)
344 .println(MessageManager.getString("label.couldnt_read_data"));
345 if (!Jalview.isHeadlessMode())
347 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
348 AppletFormatAdapter.getSupportedFormats(),
349 MessageManager.getString("label.couldnt_read_data"),
350 JvOptionPane.WARNING_MESSAGE);
362 public void cancel_actionPerformed(ActionEvent e)
366 this.setClosed(true);
367 } catch (Exception ex)
373 public void textarea_mousePressed(MouseEvent e)
376 * isPopupTrigger is checked in mousePressed on Mac,
377 * in mouseReleased on Windows
379 if (e.isPopupTrigger())
381 JPopupMenu popup = new JPopupMenu(
382 MessageManager.getString("action.edit"));
383 JMenuItem item = new JMenuItem(
384 MessageManager.getString("action.copy"));
385 item.addActionListener(new ActionListener()
388 public void actionPerformed(ActionEvent e)
390 copyItem_actionPerformed(e);
394 item = new JMenuItem(MessageManager.getString("action.paste"));
395 item.addActionListener(new ActionListener()
398 public void actionPerformed(ActionEvent e)
400 pasteMenu_actionPerformed(e);
404 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);