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.FeaturesDisplayedI;
27 import jalview.api.FeaturesSourceI;
28 import jalview.bin.Jalview;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.ColumnSelection;
31 import jalview.datamodel.SequenceI;
32 import jalview.io.AppletFormatAdapter;
33 import jalview.io.FileParse;
34 import jalview.io.FormatAdapter;
35 import jalview.io.IdentifyFile;
36 import jalview.io.JalviewFileChooser;
37 import jalview.io.JalviewFileView;
38 import jalview.jbgui.GCutAndPasteTransfer;
39 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
40 import jalview.schemes.ColourSchemeI;
41 import jalview.util.MessageManager;
43 import java.awt.Toolkit;
44 import java.awt.datatransfer.Clipboard;
45 import java.awt.datatransfer.DataFlavor;
46 import java.awt.datatransfer.StringSelection;
47 import java.awt.datatransfer.Transferable;
48 import java.awt.event.ActionEvent;
49 import java.awt.event.ActionListener;
50 import java.awt.event.MouseEvent;
52 import javax.swing.JMenuItem;
53 import javax.swing.JOptionPane;
54 import javax.swing.JPopupMenu;
55 import javax.swing.SwingUtilities;
58 * Cut'n'paste files into the desktop See JAL-1105
63 public class CutAndPasteTransfer extends GCutAndPasteTransfer
66 AlignmentViewPanel alignpanel;
68 AlignViewportI viewport;
70 FileParse source = null;
72 public CutAndPasteTransfer()
74 SwingUtilities.invokeLater(new Runnable()
79 textarea.requestFocus();
88 public void setForInput(AlignmentViewPanel viewpanel)
90 this.alignpanel = viewpanel;
91 if (alignpanel != null)
93 this.viewport = alignpanel.getAlignViewport();
97 ok.setText(MessageManager.getString("action.add"));
100 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
106 * @return DOCUMENT ME!
108 public String getText()
110 return textarea.getText();
119 public void setText(String text)
121 textarea.setText(text);
124 public void appendText(String text)
126 textarea.append(text);
130 public void save_actionPerformed(ActionEvent e)
132 JalviewFileChooser chooser = new JalviewFileChooser(
133 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
135 chooser.setAcceptAllFileFilterUsed(false);
136 chooser.setFileView(new JalviewFileView());
137 chooser.setDialogTitle(MessageManager
138 .getString("label.save_text_to_file"));
139 chooser.setToolTipText(MessageManager.getString("action.save"));
141 int value = chooser.showSaveDialog(this);
143 if (value == JalviewFileChooser.APPROVE_OPTION)
147 java.io.PrintWriter out = new java.io.PrintWriter(
148 new java.io.FileWriter(chooser.getSelectedFile()));
150 out.print(getText());
152 } catch (Exception ex)
154 ex.printStackTrace();
167 public void copyItem_actionPerformed(ActionEvent e)
169 textarea.getSelectedText();
170 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
171 c.setContents(new StringSelection(textarea.getSelectedText()), null);
181 public void pasteMenu_actionPerformed(ActionEvent e)
183 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
184 Transferable contents = c.getContents(this);
186 if (contents == null)
193 textarea.append((String) contents
194 .getTransferData(DataFlavor.stringFlavor));
195 } catch (Exception ex)
207 public void ok_actionPerformed(ActionEvent e)
209 String text = getText();
210 if (text.trim().length() < 1)
215 String format = new IdentifyFile().identify(text, "Paste");
216 if (format == null || format.equalsIgnoreCase("EMPTY DATA FILE"))
218 System.err.println(MessageManager
219 .getString("label.couldnt_read_data"));
220 if (!Jalview.isHeadlessMode())
222 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
223 AppletFormatAdapter.SUPPORTED_FORMATS,
224 MessageManager.getString("label.couldnt_read_data"),
225 JOptionPane.WARNING_MESSAGE);
230 // TODO: identify feature, annotation or tree file and parse appropriately.
231 AlignmentI al = null;
233 if (FormatAdapter.isValidFormat(format))
237 FormatAdapter fa = new FormatAdapter(alignpanel);
238 al = fa.readFile(getText(), "Paste", format);
239 source = fa.getAlignFile();
241 } catch (java.io.IOException ex)
243 JOptionPane.showInternalMessageDialog(Desktop.desktop,
244 MessageManager.formatMessage(
245 "label.couldnt_read_pasted_text",
246 new String[] { ex.toString() }), MessageManager
247 .getString("label.error_parsing_text"),
248 JOptionPane.WARNING_MESSAGE);
252 if (al != null && al.hasValidSequence())
254 String title = MessageManager.formatMessage(
255 "label.input_cut_paste_params", new String[] { format });
257 * if the view panel was closed its alignment is nulled
258 * and this is an orphaned cut and paste window
260 if (viewport != null && viewport.getAlignment() != null)
262 ((AlignViewport) viewport).addAlignment(al, title);
268 if (source instanceof ComplexAlignFile)
270 ColumnSelection colSel = ((ComplexAlignFile) source)
271 .getColumnSelection();
272 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
273 .getHiddenSequences();
274 boolean showSeqFeatures = ((ComplexAlignFile) source)
275 .isShowSeqFeatures();
276 String colourSchemeName = ((ComplexAlignFile) source)
277 .getGlobalColourScheme();
278 FeaturesDisplayedI fd = ((ComplexAlignFile) source)
279 .getDisplayedFeatures();
280 af = new AlignFrame(al, hiddenSeqs, colSel,
281 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
282 af.getViewport().setShowSequenceFeatures(showSeqFeatures);
283 af.getViewport().setFeaturesDisplayed(fd);
284 ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme(
285 colourSchemeName, al);
293 af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
294 AlignFrame.DEFAULT_HEIGHT);
295 if (source instanceof FeaturesSourceI)
297 af.getViewport().setShowSequenceFeatures(true);
301 af.currentFileFormat = format;
302 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
303 AlignFrame.DEFAULT_HEIGHT);
304 af.statusBar.setText(MessageManager
305 .getString("label.successfully_pasted_alignment_file"));
309 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
311 } catch (Exception ex)
318 System.err.println(MessageManager
319 .getString("label.couldnt_read_data"));
320 if (!Jalview.isHeadlessMode())
322 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
323 AppletFormatAdapter.SUPPORTED_FORMATS,
324 MessageManager.getString("label.couldnt_read_data"),
325 JOptionPane.WARNING_MESSAGE);
337 public void cancel_actionPerformed(ActionEvent e)
341 this.setClosed(true);
342 } catch (Exception ex)
348 public void textarea_mousePressed(MouseEvent e)
350 if (SwingUtilities.isRightMouseButton(e))
352 JPopupMenu popup = new JPopupMenu(
353 MessageManager.getString("action.edit"));
354 JMenuItem item = new JMenuItem(
355 MessageManager.getString("action.copy"));
356 item.addActionListener(new ActionListener()
359 public void actionPerformed(ActionEvent e)
361 copyItem_actionPerformed(e);
365 item = new JMenuItem(MessageManager.getString("action.paste"));
366 item.addActionListener(new ActionListener()
369 public void actionPerformed(ActionEvent e)
371 pasteMenu_actionPerformed(e);
375 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);