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.FileWriter;
32 import java.io.IOException;
33 import java.io.PrintWriter;
35 import javax.swing.JMenuItem;
36 import javax.swing.JPopupMenu;
37 import javax.swing.SwingUtilities;
39 import jalview.api.AlignViewportI;
40 import jalview.api.AlignmentViewPanel;
41 import jalview.api.ComplexAlignFile;
42 import jalview.api.FeatureSettingsModelI;
43 import jalview.api.FeaturesDisplayedI;
44 import jalview.api.FeaturesSourceI;
45 import jalview.bin.Cache;
46 import jalview.bin.Jalview;
47 import jalview.datamodel.AlignmentI;
48 import jalview.datamodel.HiddenColumns;
49 import jalview.datamodel.SequenceI;
50 import jalview.io.AlignmentFileReaderI;
51 import jalview.io.AppletFormatAdapter;
52 import jalview.io.DataSourceType;
53 import jalview.io.FileFormatException;
54 import jalview.io.FileFormatI;
55 import jalview.io.FormatAdapter;
56 import jalview.io.IdentifyFile;
57 import jalview.io.JalviewFileChooser;
58 import jalview.io.JalviewFileView;
59 import jalview.jbgui.GCutAndPasteTransfer;
60 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
61 import jalview.schemes.ColourSchemeI;
62 import jalview.util.MessageManager;
65 * Cut'n'paste files into the desktop See JAL-1105
70 public class CutAndPasteTransfer extends GCutAndPasteTransfer
73 AlignmentViewPanel alignpanel;
75 AlignViewportI viewport;
77 AlignmentFileReaderI source = null;
79 public CutAndPasteTransfer()
81 this.setFrameIcon(null);
82 SwingUtilities.invokeLater(new Runnable()
87 textarea.requestFocus();
96 public void setForInput(AlignmentViewPanel viewpanel)
98 this.alignpanel = viewpanel;
99 if (alignpanel != null)
101 this.viewport = alignpanel.getAlignViewport();
103 if (viewport != null)
105 ok.setText(MessageManager.getString("action.add"));
108 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
114 * @return DOCUMENT ME!
116 public String getText()
118 return textarea.getText();
127 public void setText(String text)
129 textarea.setText(text);
132 public void appendText(String text)
134 textarea.append(text);
138 public void save_actionPerformed(ActionEvent e)
140 // TODO: JAL-3048 JalviewFileChooser - Save option
142 JalviewFileChooser chooser = new JalviewFileChooser(
143 Cache.getProperty("LAST_DIRECTORY"));
145 chooser.setAcceptAllFileFilterUsed(false);
146 chooser.setFileView(new JalviewFileView());
147 chooser.setDialogTitle(
148 MessageManager.getString("label.save_text_to_file"));
149 chooser.setToolTipText(MessageManager.getString("action.save"));
151 int value = chooser.showSaveDialog(this);
153 if (value == JalviewFileChooser.APPROVE_OPTION)
157 PrintWriter out = new PrintWriter(
158 new FileWriter(chooser.getSelectedFile()));
160 out.print(getText());
162 } catch (Exception ex)
164 ex.printStackTrace();
177 public void copyItem_actionPerformed(ActionEvent e)
179 textarea.getSelectedText();
180 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
181 c.setContents(new StringSelection(textarea.getSelectedText()), null);
191 public void pasteMenu_actionPerformed(ActionEvent e)
193 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
194 Transferable contents = c.getContents(this);
196 if (contents == null)
204 (String) contents.getTransferData(DataFlavor.stringFlavor));
205 } catch (Exception ex)
217 public void ok_actionPerformed(ActionEvent e)
219 String text = getText();
220 if (text.trim().length() < 1)
225 FileFormatI format = null;
228 format = new IdentifyFile().identify(text, DataSourceType.PASTE);
229 } catch (FileFormatException e1)
236 .println(MessageManager.getString("label.couldnt_read_data"));
237 if (!Jalview.isHeadlessMode())
239 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
240 AppletFormatAdapter.getSupportedFormats(),
241 MessageManager.getString("label.couldnt_read_data"),
242 JvOptionPane.WARNING_MESSAGE);
247 // TODO: identify feature, annotation or tree file and parse appropriately.
248 AlignmentI al = null;
252 FormatAdapter fa = new FormatAdapter(alignpanel);
253 al = fa.readFile(getText(), DataSourceType.PASTE, format);
254 source = fa.getAlignFile();
256 } catch (IOException ex)
258 JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
259 .formatMessage("label.couldnt_read_pasted_text", new String[]
261 MessageManager.getString("label.error_parsing_text"),
262 JvOptionPane.WARNING_MESSAGE);
265 if (al != null && al.hasValidSequence())
267 String title = MessageManager
268 .formatMessage("label.input_cut_paste_params", new String[]
269 { format.getName() });
270 FeatureSettingsModelI proxyColourScheme = source
271 .getFeatureColourScheme();
274 * if the view panel was closed its alignment is nulled
275 * and this is an orphaned cut and paste window
277 if (viewport != null && viewport.getAlignment() != null)
279 ((AlignViewport) viewport).addAlignment(al, title);
280 viewport.applyFeaturesStyle(proxyColourScheme);
286 if (source instanceof ComplexAlignFile)
288 HiddenColumns hidden = ((ComplexAlignFile) source)
290 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
291 .getHiddenSequences();
292 boolean showSeqFeatures = ((ComplexAlignFile) source)
293 .isShowSeqFeatures();
294 String colourSchemeName = ((ComplexAlignFile) source)
295 .getGlobalColourScheme();
296 FeaturesDisplayedI fd = ((ComplexAlignFile) source)
297 .getDisplayedFeatures();
298 af = new AlignFrame(al, hiddenSeqs, hidden,
299 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
300 af.getViewport().setShowSequenceFeatures(showSeqFeatures);
301 af.getViewport().setFeaturesDisplayed(fd);
302 af.setMenusForViewport();
303 ColourSchemeI cs = ColourSchemeMapper
304 .getJalviewColourScheme(colourSchemeName, al);
312 af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
313 AlignFrame.DEFAULT_HEIGHT);
314 if (source instanceof FeaturesSourceI)
316 af.getViewport().setShowSequenceFeatures(true);
319 if (proxyColourScheme != null)
321 af.getViewport().applyFeaturesStyle(proxyColourScheme);
323 af.currentFileFormat = format;
324 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
325 AlignFrame.DEFAULT_HEIGHT);
326 af.setStatus(MessageManager
327 .getString("label.successfully_pasted_alignment_file"));
331 af.setMaximum(Cache.getDefault("SHOW_FULLSCREEN", false));
332 } catch (Exception ex)
340 .println(MessageManager.getString("label.couldnt_read_data"));
341 if (!Jalview.isHeadlessMode())
343 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
344 AppletFormatAdapter.getSupportedFormats(),
345 MessageManager.getString("label.couldnt_read_data"),
346 JvOptionPane.WARNING_MESSAGE);
358 public void cancel_actionPerformed(ActionEvent e)
362 this.setClosed(true);
363 } catch (Exception ex)
369 public void textarea_mousePressed(MouseEvent e)
372 * isPopupTrigger is checked in mousePressed on Mac,
373 * in mouseReleased on Windows
375 if (e.isPopupTrigger())
377 JPopupMenu popup = new JPopupMenu(
378 MessageManager.getString("action.edit"));
379 JMenuItem item = new JMenuItem(
380 MessageManager.getString("action.copy"));
381 item.addActionListener(new ActionListener()
384 public void actionPerformed(ActionEvent e)
386 copyItem_actionPerformed(e);
390 item = new JMenuItem(MessageManager.getString("action.paste"));
391 item.addActionListener(new ActionListener()
394 public void actionPerformed(ActionEvent e)
396 pasteMenu_actionPerformed(e);
400 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);