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.datamodel.AlignmentI;
28 import jalview.datamodel.ColumnSelection;
29 import jalview.datamodel.SequenceI;
30 import jalview.io.FileParse;
31 import jalview.io.FormatAdapter;
32 import jalview.io.IdentifyFile;
33 import jalview.io.JalviewFileChooser;
34 import jalview.io.JalviewFileView;
35 import jalview.jbgui.GCutAndPasteTransfer;
36 import jalview.schemes.ColourSchemeI;
37 import jalview.util.MessageManager;
39 import java.awt.Toolkit;
40 import java.awt.datatransfer.Clipboard;
41 import java.awt.datatransfer.DataFlavor;
42 import java.awt.datatransfer.StringSelection;
43 import java.awt.datatransfer.Transferable;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.awt.event.MouseEvent;
48 import javax.swing.JMenuItem;
49 import javax.swing.JOptionPane;
50 import javax.swing.JPopupMenu;
51 import javax.swing.SwingUtilities;
54 * Cut'n'paste files into the desktop See JAL-1105
59 public class CutAndPasteTransfer extends GCutAndPasteTransfer
62 AlignmentViewPanel alignpanel;
64 AlignViewportI viewport;
66 FileParse source = null;
67 public CutAndPasteTransfer()
69 SwingUtilities.invokeLater(new Runnable()
73 textarea.requestFocus();
82 public void setForInput(AlignmentViewPanel viewpanel)
84 this.alignpanel = viewpanel;
85 if (alignpanel != null)
87 this.viewport = alignpanel.getAlignViewport();
91 ok.setText(MessageManager.getString("action.add"));
94 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
100 * @return DOCUMENT ME!
102 public String getText()
104 return textarea.getText();
113 public void setText(String text)
115 textarea.setText(text);
118 public void appendText(String text)
120 textarea.append(text);
123 public void save_actionPerformed(ActionEvent e)
125 JalviewFileChooser chooser = new JalviewFileChooser(
126 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
128 chooser.setAcceptAllFileFilterUsed(false);
129 chooser.setFileView(new JalviewFileView());
130 chooser.setDialogTitle(MessageManager.getString("label.save_text_to_file"));
131 chooser.setToolTipText(MessageManager.getString("action.save"));
133 int value = chooser.showSaveDialog(this);
135 if (value == JalviewFileChooser.APPROVE_OPTION)
139 java.io.PrintWriter out = new java.io.PrintWriter(
140 new java.io.FileWriter(chooser.getSelectedFile()));
142 out.print(getText());
144 } catch (Exception ex)
146 ex.printStackTrace();
158 public void copyItem_actionPerformed(ActionEvent e)
160 textarea.getSelectedText();
161 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
162 c.setContents(new StringSelection(textarea.getSelectedText()), null);
171 public void pasteMenu_actionPerformed(ActionEvent e)
173 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
174 Transferable contents = c.getContents(this);
176 if (contents == null)
183 textarea.append((String) contents
184 .getTransferData(DataFlavor.stringFlavor));
185 } catch (Exception ex)
196 public void ok_actionPerformed(ActionEvent e)
198 String format = new IdentifyFile().Identify(getText(), "Paste");
199 // TODO: identify feature, annotation or tree file and parse appropriately.
200 AlignmentI al = null;
202 if (FormatAdapter.isValidFormat(format))
206 FormatAdapter fa = new FormatAdapter(alignpanel);
207 al = fa.readFile(getText(), "Paste", format);
208 source = fa.getAlignFile();
210 } catch (java.io.IOException ex)
212 JOptionPane.showInternalMessageDialog(Desktop.desktop,
213 MessageManager.formatMessage(
214 "label.couldnt_read_pasted_text", new String[]
215 { ex.toString() }), MessageManager
216 .getString("label.error_parsing_text"),
217 JOptionPane.WARNING_MESSAGE);
223 String title = MessageManager.formatMessage(
224 "label.input_cut_paste_params", new String[]
226 if (viewport != null)
228 ((AlignViewport) viewport).addAlignment(al, title);
234 if (source instanceof ComplexAlignFile)
236 ColumnSelection colSel = ((ComplexAlignFile) source)
237 .getColumnSelection();
238 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
239 .getHiddenSequences();
240 boolean showSeqFeatures = ((ComplexAlignFile) source)
241 .isShowSeqFeatures();
242 ColourSchemeI cs = ((ComplexAlignFile) source).getColourScheme();
243 FeaturesDisplayedI fd = ((ComplexAlignFile) source)
244 .getDisplayedFeatures();
245 af = new AlignFrame(al, hiddenSeqs, colSel,
246 AlignFrame.DEFAULT_WIDTH,
247 AlignFrame.DEFAULT_HEIGHT);
248 af.getViewport().setShowSequenceFeatures(showSeqFeatures);
249 af.getViewport().setFeaturesDisplayed(fd);
254 af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
255 AlignFrame.DEFAULT_HEIGHT);
258 af.currentFileFormat = format;
259 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
260 AlignFrame.DEFAULT_HEIGHT);
261 af.statusBar.setText(MessageManager
262 .getString("label.successfully_pasted_alignment_file"));
266 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
268 } catch (Exception ex)
282 public void cancel_actionPerformed(ActionEvent e)
286 this.setClosed(true);
287 } catch (Exception ex)
292 public void textarea_mousePressed(MouseEvent e)
294 if (SwingUtilities.isRightMouseButton(e))
296 JPopupMenu popup = new JPopupMenu(
297 MessageManager.getString("action.edit"));
298 JMenuItem item = new JMenuItem(
299 MessageManager.getString("action.copy"));
300 item.addActionListener(new ActionListener()
302 public void actionPerformed(ActionEvent e)
304 copyItem_actionPerformed(e);
308 item = new JMenuItem(MessageManager.getString("action.paste"));
309 item.addActionListener(new ActionListener()
311 public void actionPerformed(ActionEvent e)
313 pasteMenu_actionPerformed(e);
317 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);