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.bin.Cache;
24 import jalview.api.AlignViewportI;
25 import jalview.api.AlignmentViewPanel;
26 import jalview.api.ComplexAlignFile;
27 import jalview.api.FeatureSettingsModelI;
28 import jalview.api.FeaturesDisplayedI;
29 import jalview.api.FeaturesSourceI;
30 import jalview.bin.Jalview;
31 import jalview.datamodel.AlignmentI;
32 import jalview.datamodel.HiddenColumns;
33 import jalview.datamodel.SequenceI;
34 import jalview.io.AlignmentFileReaderI;
35 import jalview.io.AppletFormatAdapter;
36 import jalview.io.DataSourceType;
37 import jalview.io.FileFormatException;
38 import jalview.io.FileFormatI;
39 import jalview.io.FormatAdapter;
40 import jalview.io.IdentifyFile;
41 import jalview.io.JalviewFileChooser;
42 import jalview.io.JalviewFileView;
43 import jalview.jbgui.GCutAndPasteTransfer;
44 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
45 import jalview.schemes.ColourSchemeI;
46 import jalview.util.MessageManager;
48 import java.awt.Toolkit;
49 import java.awt.datatransfer.Clipboard;
50 import java.awt.datatransfer.DataFlavor;
51 import java.awt.datatransfer.StringSelection;
52 import java.awt.datatransfer.Transferable;
53 import java.awt.event.ActionEvent;
54 import java.awt.event.ActionListener;
55 import java.awt.event.MouseEvent;
56 import java.io.FileWriter;
57 import java.io.IOException;
58 import java.io.PrintWriter;
60 import javax.swing.JMenuItem;
61 import javax.swing.JPopupMenu;
62 import javax.swing.SwingUtilities;
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 SwingUtilities.invokeLater(new Runnable()
86 textarea.requestFocus();
95 public void setForInput(AlignmentViewPanel viewpanel)
97 this.alignpanel = viewpanel;
98 if (alignpanel != null)
100 this.viewport = alignpanel.getAlignViewport();
102 if (viewport != null)
104 ok.setText(MessageManager.getString("action.add"));
107 getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
113 * @return DOCUMENT ME!
115 public String getText()
117 return textarea.getText();
126 public void setText(String text)
128 textarea.setText(text);
131 public void appendText(String text)
133 textarea.append(text);
137 public void save_actionPerformed(ActionEvent e)
139 // TODO: JAL-3048 JalviewFileChooser - Save option
141 JalviewFileChooser chooser = new JalviewFileChooser(
142 Cache.getProperty("LAST_DIRECTORY"));
144 chooser.setAcceptAllFileFilterUsed(false);
145 chooser.setFileView(new JalviewFileView());
146 chooser.setDialogTitle(
147 MessageManager.getString("label.save_text_to_file"));
148 chooser.setToolTipText(MessageManager.getString("action.save"));
150 int value = chooser.showSaveDialog(this);
152 if (value == JalviewFileChooser.APPROVE_OPTION)
156 PrintWriter out = new PrintWriter(
157 new FileWriter(chooser.getSelectedFile()));
159 out.print(getText());
161 } catch (Exception ex)
163 ex.printStackTrace();
176 public void copyItem_actionPerformed(ActionEvent e)
178 textarea.getSelectedText();
179 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
180 c.setContents(new StringSelection(textarea.getSelectedText()), null);
190 public void pasteMenu_actionPerformed(ActionEvent e)
192 Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
193 Transferable contents = c.getContents(this);
195 if (contents == null)
203 (String) contents.getTransferData(DataFlavor.stringFlavor));
204 } catch (Exception ex)
216 public void ok_actionPerformed(ActionEvent e)
218 String text = getText();
219 if (text.trim().length() < 1)
224 FileFormatI format = null;
227 format = new IdentifyFile().identify(text, DataSourceType.PASTE);
228 } catch (FileFormatException e1)
235 .println(MessageManager.getString("label.couldnt_read_data"));
236 if (!Jalview.isHeadlessMode())
238 JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
239 AppletFormatAdapter.getSupportedFormats(),
240 MessageManager.getString("label.couldnt_read_data"),
241 JvOptionPane.WARNING_MESSAGE);
246 // TODO: identify feature, annotation or tree file and parse appropriately.
247 AlignmentI al = null;
251 FormatAdapter fa = new FormatAdapter(alignpanel);
252 al = fa.readFile(getText(), DataSourceType.PASTE, format);
253 source = fa.getAlignFile();
255 } catch (IOException ex)
257 JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(), MessageManager
258 .formatMessage("label.couldnt_read_pasted_text", new String[]
260 MessageManager.getString("label.error_parsing_text"),
261 JvOptionPane.WARNING_MESSAGE);
264 if (al != null && al.hasValidSequence())
266 String title = MessageManager
267 .formatMessage("label.input_cut_paste_params", new String[]
268 { format.getName() });
269 FeatureSettingsModelI proxyColourScheme = source
270 .getFeatureColourScheme();
273 * if the view panel was closed its alignment is nulled
274 * and this is an orphaned cut and paste window
276 if (viewport != null && viewport.getAlignment() != null)
278 ((AlignViewport) viewport).addAlignment(al, title);
279 viewport.applyFeaturesStyle(proxyColourScheme);
285 if (source instanceof ComplexAlignFile)
287 HiddenColumns hidden = ((ComplexAlignFile) source)
289 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
290 .getHiddenSequences();
291 boolean showSeqFeatures = ((ComplexAlignFile) source)
292 .isShowSeqFeatures();
293 String colourSchemeName = ((ComplexAlignFile) source)
294 .getGlobalColourScheme();
295 FeaturesDisplayedI fd = ((ComplexAlignFile) source)
296 .getDisplayedFeatures();
297 af = new AlignFrame(al, hiddenSeqs, hidden,
298 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
299 af.getViewport().setShowSequenceFeatures(showSeqFeatures);
300 af.getViewport().setFeaturesDisplayed(fd);
301 af.setMenusForViewport();
302 ColourSchemeI cs = ColourSchemeMapper
303 .getJalviewColourScheme(colourSchemeName, al);
311 af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
312 AlignFrame.DEFAULT_HEIGHT);
313 if (source instanceof FeaturesSourceI)
315 af.getViewport().setShowSequenceFeatures(true);
318 if (proxyColourScheme != null)
320 af.getViewport().applyFeaturesStyle(proxyColourScheme);
322 af.currentFileFormat = format;
323 Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
324 AlignFrame.DEFAULT_HEIGHT);
325 af.setStatus(MessageManager
326 .getString("label.successfully_pasted_alignment_file"));
330 af.setMaximum(Cache.getDefault("SHOW_FULLSCREEN", false));
331 } catch (Exception ex)
339 .println(MessageManager.getString("label.couldnt_read_data"));
340 if (!Jalview.isHeadlessMode())
342 JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
343 AppletFormatAdapter.getSupportedFormats(),
344 MessageManager.getString("label.couldnt_read_data"),
345 JvOptionPane.WARNING_MESSAGE);
357 public void cancel_actionPerformed(ActionEvent e)
361 this.setClosed(true);
362 } catch (Exception ex)
368 public void textarea_mousePressed(MouseEvent e)
371 * isPopupTrigger is checked in mousePressed on Mac,
372 * in mouseReleased on Windows
374 if (e.isPopupTrigger())
376 JPopupMenu popup = new JPopupMenu(
377 MessageManager.getString("action.edit"));
378 JMenuItem item = new JMenuItem(
379 MessageManager.getString("action.copy"));
380 item.addActionListener(new ActionListener()
383 public void actionPerformed(ActionEvent e)
385 copyItem_actionPerformed(e);
389 item = new JMenuItem(MessageManager.getString("action.paste"));
390 item.addActionListener(new ActionListener()
393 public void actionPerformed(ActionEvent e)
395 pasteMenu_actionPerformed(e);
399 popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);