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.bin.Cache;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.ColumnSelection;
30 import jalview.datamodel.SequenceI;
31 import jalview.io.FileParse;
32 import jalview.io.FormatAdapter;
33 import jalview.io.IdentifyFile;
34 import jalview.io.JalviewFileChooser;
35 import jalview.io.JalviewFileView;
36 import jalview.jbgui.GCutAndPasteTransfer;
37 import jalview.schemes.ColourSchemeI;
38 import jalview.util.MessageManager;
40 import java.awt.Toolkit;
41 import java.awt.datatransfer.Clipboard;
42 import java.awt.datatransfer.DataFlavor;
43 import java.awt.datatransfer.StringSelection;
44 import java.awt.datatransfer.Transferable;
45 import java.awt.event.ActionEvent;
46 import java.awt.event.ActionListener;
47 import java.awt.event.MouseEvent;
49 import javax.swing.JMenuItem;
50 import javax.swing.JOptionPane;
51 import javax.swing.JPopupMenu;
52 import javax.swing.SwingUtilities;
55 * Cut'n'paste files into the desktop See JAL-1105
60 public class CutAndPasteTransfer extends GCutAndPasteTransfer
63 AlignmentViewPanel alignpanel;
65 AlignViewportI viewport;
67 FileParse source = null;
68 public CutAndPasteTransfer()
70 SwingUtilities.invokeLater(new Runnable()
74 textarea.requestFocus();
83 public void setForInput(AlignmentViewPanel viewpanel)
85 this.alignpanel = viewpanel;
86 if (alignpanel != null)
88 this.viewport = alignpanel.getAlignViewport();
92 ok.setText(MessageManager.getString("action.add"));
95 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
101 * @return DOCUMENT ME!
103 public String getText()
105 return textarea.getText();
114 public void setText(String text)
116 textarea.setText(text);
119 public void appendText(String text)
121 textarea.append(text);
124 public void save_actionPerformed(ActionEvent e)
126 JalviewFileChooser chooser = new JalviewFileChooser(
127 Cache.getProperty("LAST_DIRECTORY"));
129 chooser.setAcceptAllFileFilterUsed(false);
130 chooser.setFileView(new JalviewFileView());
131 chooser.setDialogTitle(MessageManager.getString("label.save_text_to_file"));
132 chooser.setToolTipText(MessageManager.getString("action.save"));
134 int value = chooser.showSaveDialog(this);
136 if (value == JalviewFileChooser.APPROVE_OPTION)
140 java.io.PrintWriter out = new java.io.PrintWriter(
141 new java.io.FileWriter(chooser.getSelectedFile()));
143 out.print(getText());
145 } catch (Exception ex)
147 ex.printStackTrace();
159 public void copyItem_actionPerformed(ActionEvent e)
161 textarea.getSelectedText();
162 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
163 c.setContents(new StringSelection(textarea.getSelectedText()), null);
172 public void pasteMenu_actionPerformed(ActionEvent e)
174 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
175 Transferable contents = c.getContents(this);
177 if (contents == null)
184 textarea.append((String) contents
185 .getTransferData(DataFlavor.stringFlavor));
186 } catch (Exception ex)
197 public void ok_actionPerformed(ActionEvent e)
199 String text = getText();
200 if (text.trim().length() < 1)
205 String format = new IdentifyFile().Identify(text, "Paste");
206 // TODO: identify feature, annotation or tree file and parse appropriately.
207 AlignmentI al = null;
209 if (FormatAdapter.isValidFormat(format))
213 FormatAdapter fa = new FormatAdapter(alignpanel);
214 al = fa.readFile(getText(), "Paste", format);
215 source = fa.getAlignFile();
217 } catch (java.io.IOException ex)
219 JOptionPane.showInternalMessageDialog(Desktop.desktop,
220 MessageManager.formatMessage(
221 "label.couldnt_read_pasted_text", new String[]
222 { ex.toString() }), MessageManager
223 .getString("label.error_parsing_text"),
224 JOptionPane.WARNING_MESSAGE);
230 String title = MessageManager.formatMessage(
231 "label.input_cut_paste_params", new String[]
233 if (viewport != null)
235 ((AlignViewport) viewport).addAlignment(al, title);
241 if (source instanceof ComplexAlignFile)
243 ColumnSelection colSel = ((ComplexAlignFile) source)
244 .getColumnSelection();
245 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
246 .getHiddenSequences();
247 boolean showSeqFeatures = ((ComplexAlignFile) source)
248 .isShowSeqFeatures();
249 ColourSchemeI cs = ((ComplexAlignFile) source).getColourScheme();
250 FeaturesDisplayedI fd = ((ComplexAlignFile) source)
251 .getDisplayedFeatures();
252 af = new AlignFrame(al, hiddenSeqs, colSel,
253 AlignFrame.DEFAULT_WIDTH,
254 AlignFrame.DEFAULT_HEIGHT);
255 af.getViewport().setShowSequenceFeatures(showSeqFeatures);
256 af.getViewport().setFeaturesDisplayed(fd);
261 af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
262 AlignFrame.DEFAULT_HEIGHT);
265 af.currentFileFormat = format;
266 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
267 AlignFrame.DEFAULT_HEIGHT);
268 af.statusBar.setText(MessageManager
269 .getString("label.successfully_pasted_alignment_file"));
273 af.setMaximum(Cache.getDefault("SHOW_FULLSCREEN",
275 } catch (Exception ex)
289 public void cancel_actionPerformed(ActionEvent e)
293 this.setClosed(true);
294 } catch (Exception ex)
299 public void textarea_mousePressed(MouseEvent e)
301 if (SwingUtilities.isRightMouseButton(e))
303 JPopupMenu popup = new JPopupMenu(
304 MessageManager.getString("action.edit"));
305 JMenuItem item = new JMenuItem(
306 MessageManager.getString("action.copy"));
307 item.addActionListener(new ActionListener()
309 public void actionPerformed(ActionEvent e)
311 copyItem_actionPerformed(e);
315 item = new JMenuItem(MessageManager.getString("action.paste"));
316 item.addActionListener(new ActionListener()
318 public void actionPerformed(ActionEvent e)
320 pasteMenu_actionPerformed(e);
324 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);