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;
34 import java.util.HashMap;
37 import javax.swing.JMenuItem;
38 import javax.swing.JPopupMenu;
39 import javax.swing.SwingUtilities;
41 import jalview.api.AlignViewportI;
42 import jalview.api.AlignmentViewPanel;
43 import jalview.api.ComplexAlignFile;
44 import jalview.api.FeatureSettingsModelI;
45 import jalview.api.FeaturesDisplayedI;
46 import jalview.api.FeaturesSourceI;
47 import jalview.bin.Cache;
48 import jalview.bin.Jalview;
49 import jalview.datamodel.AlignmentI;
50 import jalview.datamodel.HiddenColumns;
51 import jalview.datamodel.SequenceI;
52 import jalview.io.AlignmentFileReaderI;
53 import jalview.io.AppletFormatAdapter;
54 import jalview.io.DataSourceType;
55 import jalview.io.FileFormatException;
56 import jalview.io.FileFormatI;
57 import jalview.io.FormatAdapter;
58 import jalview.io.IdentifyFile;
59 import jalview.io.JalviewFileChooser;
60 import jalview.io.JalviewFileView;
61 import jalview.jbgui.GCutAndPasteTransfer;
62 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
63 import jalview.schemes.ColourSchemeI;
64 import jalview.util.MessageManager;
67 * Cut'n'paste files into the desktop See JAL-1105
72 public class CutAndPasteTransfer extends GCutAndPasteTransfer
75 AlignmentViewPanel alignpanel;
77 AlignViewportI viewport;
79 AlignmentFileReaderI source = null;
81 public CutAndPasteTransfer()
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 ok_actionPerformed(e, new HashMap<String, String>());
223 public void ok_actionPerformed(ActionEvent e, Map<String, String> options)
225 String text = getText();
226 if (text.trim().length() < 1)
231 FileFormatI format = null;
234 format = new IdentifyFile().identify(text, DataSourceType.PASTE);
235 } catch (FileFormatException e1)
242 .println(MessageManager.getString("label.couldnt_read_data"));
243 if (!Jalview.isHeadlessMode())
245 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
246 AppletFormatAdapter.getSupportedFormats(),
247 MessageManager.getString("label.couldnt_read_data"),
248 JvOptionPane.WARNING_MESSAGE);
253 // TODO: identify feature, annotation or tree file and parse appropriately.
254 AlignmentI al = null;
258 FormatAdapter fa = new FormatAdapter(alignpanel);
259 al = fa.readFile(getText(), DataSourceType.PASTE, format);
260 source = fa.getAlignFile();
261 } catch (IOException ex)
263 JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
264 .formatMessage("label.couldnt_read_pasted_text", new String[]
266 MessageManager.getString("label.error_parsing_text"),
267 JvOptionPane.WARNING_MESSAGE);
270 if (al != null && al.hasValidSequence())
272 String title = options.containsKey("title") ? options.get("title")
273 : MessageManager.formatMessage("label.input_cut_paste_params",
275 { format.getName() });
276 FeatureSettingsModelI proxyColourScheme = source
277 .getFeatureColourScheme();
280 * if the view panel was closed its alignment is nulled
281 * and this is an orphaned cut and paste window
283 if (viewport != null && viewport.getAlignment() != null)
285 ((AlignViewport) viewport).addAlignment(al, title);
286 viewport.applyFeaturesStyle(proxyColourScheme);
292 if (source instanceof ComplexAlignFile)
294 HiddenColumns hidden = ((ComplexAlignFile) source)
296 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
297 .getHiddenSequences();
298 boolean showSeqFeatures = ((ComplexAlignFile) source)
299 .isShowSeqFeatures();
300 String colourSchemeName = ((ComplexAlignFile) source)
301 .getGlobalColourScheme();
302 FeaturesDisplayedI fd = ((ComplexAlignFile) source)
303 .getDisplayedFeatures();
304 af = new AlignFrame(al, hiddenSeqs, hidden,
305 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
306 af.getViewport().setShowSequenceFeatures(showSeqFeatures);
307 af.getViewport().setFeaturesDisplayed(fd);
308 af.setMenusForViewport();
309 ColourSchemeI cs = ColourSchemeMapper
310 .getJalviewColourScheme(colourSchemeName, al);
318 af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
319 AlignFrame.DEFAULT_HEIGHT);
320 if (source instanceof FeaturesSourceI)
322 af.getViewport().setShowSequenceFeatures(true);
325 if (proxyColourScheme != null)
327 af.getViewport().applyFeaturesStyle(proxyColourScheme);
329 af.currentFileFormat = format;
330 if (options.containsKey("wrap"))
332 af.wrapMenuItem_actionPerformed(e,
333 Boolean.parseBoolean(options.get("wrap")));
335 if (options.containsKey("showannotations"))
337 af.annotationPanelMenuItem_actionPerformed(e,
338 Boolean.parseBoolean(options.get("showannotations")));
340 if (options.containsKey("colourscheme"))
342 af.changeColour_actionPerformed(options.get("colourscheme"));
344 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
345 AlignFrame.DEFAULT_HEIGHT);
346 af.setStatus(MessageManager
347 .getString("label.successfully_pasted_alignment_file"));
351 af.setMaximum(Cache.getDefault("SHOW_FULLSCREEN", false));
352 } catch (Exception ex)
360 .println(MessageManager.getString("label.couldnt_read_data"));
361 if (!Jalview.isHeadlessMode())
363 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
364 AppletFormatAdapter.getSupportedFormats(),
365 MessageManager.getString("label.couldnt_read_data"),
366 JvOptionPane.WARNING_MESSAGE);
378 public void cancel_actionPerformed(ActionEvent e)
382 this.setClosed(true);
383 } catch (Exception ex)
389 public void textarea_mousePressed(MouseEvent e)
392 * isPopupTrigger is checked in mousePressed on Mac,
393 * in mouseReleased on Windows
395 if (e.isPopupTrigger())
397 JPopupMenu popup = new JPopupMenu(
398 MessageManager.getString("action.edit"));
399 JMenuItem item = new JMenuItem(
400 MessageManager.getString("action.copy"));
401 item.addActionListener(new ActionListener()
404 public void actionPerformed(ActionEvent e)
406 copyItem_actionPerformed(e);
410 item = new JMenuItem(MessageManager.getString("action.paste"));
411 item.addActionListener(new ActionListener()
414 public void actionPerformed(ActionEvent e)
416 pasteMenu_actionPerformed(e);
420 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);