JAL-1925 update source version in license
[jalview.git] / src / jalview / gui / CutAndPasteTransfer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b2)
3  * Copyright (C) 2015 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.gui;
22
23 import jalview.api.AlignViewportI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.api.ComplexAlignFile;
26 import jalview.api.FeaturesDisplayedI;
27 import jalview.bin.Jalview;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.ColumnSelection;
30 import jalview.datamodel.SequenceI;
31 import jalview.io.AppletFormatAdapter;
32 import jalview.io.FileParse;
33 import jalview.io.FormatAdapter;
34 import jalview.io.IdentifyFile;
35 import jalview.io.JalviewFileChooser;
36 import jalview.io.JalviewFileView;
37 import jalview.jbgui.GCutAndPasteTransfer;
38 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
39 import jalview.schemes.ColourSchemeI;
40 import jalview.util.MessageManager;
41
42 import java.awt.Toolkit;
43 import java.awt.datatransfer.Clipboard;
44 import java.awt.datatransfer.DataFlavor;
45 import java.awt.datatransfer.StringSelection;
46 import java.awt.datatransfer.Transferable;
47 import java.awt.event.ActionEvent;
48 import java.awt.event.ActionListener;
49 import java.awt.event.MouseEvent;
50
51 import javax.swing.JMenuItem;
52 import javax.swing.JOptionPane;
53 import javax.swing.JPopupMenu;
54 import javax.swing.SwingUtilities;
55
56 /**
57  * Cut'n'paste files into the desktop See JAL-1105
58  * 
59  * @author $author$
60  * @version $Revision$
61  */
62 public class CutAndPasteTransfer extends GCutAndPasteTransfer
63 {
64
65   AlignmentViewPanel alignpanel;
66
67   AlignViewportI viewport;
68
69   FileParse source = null;
70
71   public CutAndPasteTransfer()
72   {
73     SwingUtilities.invokeLater(new Runnable()
74     {
75       public void run()
76       {
77         textarea.requestFocus();
78       }
79     });
80
81   }
82
83   /**
84    * DOCUMENT ME!
85    */
86   public void setForInput(AlignmentViewPanel viewpanel)
87   {
88     this.alignpanel = viewpanel;
89     if (alignpanel != null)
90     {
91       this.viewport = alignpanel.getAlignViewport();
92     }
93     if (viewport != null)
94     {
95       ok.setText(MessageManager.getString("action.add"));
96     }
97
98     getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
99   }
100
101   /**
102    * DOCUMENT ME!
103    * 
104    * @return DOCUMENT ME!
105    */
106   public String getText()
107   {
108     return textarea.getText();
109   }
110
111   /**
112    * DOCUMENT ME!
113    * 
114    * @param text
115    *          DOCUMENT ME!
116    */
117   public void setText(String text)
118   {
119     textarea.setText(text);
120   }
121
122   public void appendText(String text)
123   {
124     textarea.append(text);
125   }
126
127   public void save_actionPerformed(ActionEvent e)
128   {
129     JalviewFileChooser chooser = new JalviewFileChooser(
130             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
131
132     chooser.setAcceptAllFileFilterUsed(false);
133     chooser.setFileView(new JalviewFileView());
134     chooser.setDialogTitle(MessageManager
135             .getString("label.save_text_to_file"));
136     chooser.setToolTipText(MessageManager.getString("action.save"));
137
138     int value = chooser.showSaveDialog(this);
139
140     if (value == JalviewFileChooser.APPROVE_OPTION)
141     {
142       try
143       {
144         java.io.PrintWriter out = new java.io.PrintWriter(
145                 new java.io.FileWriter(chooser.getSelectedFile()));
146
147         out.print(getText());
148         out.close();
149       } catch (Exception ex)
150       {
151         ex.printStackTrace();
152       }
153
154     }
155   }
156
157   /**
158    * DOCUMENT ME!
159    * 
160    * @param e
161    *          DOCUMENT ME!
162    */
163   public void copyItem_actionPerformed(ActionEvent e)
164   {
165     textarea.getSelectedText();
166     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
167     c.setContents(new StringSelection(textarea.getSelectedText()), null);
168   }
169
170   /**
171    * DOCUMENT ME!
172    * 
173    * @param e
174    *          DOCUMENT ME!
175    */
176   public void pasteMenu_actionPerformed(ActionEvent e)
177   {
178     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
179     Transferable contents = c.getContents(this);
180
181     if (contents == null)
182     {
183       return;
184     }
185
186     try
187     {
188       textarea.append((String) contents
189               .getTransferData(DataFlavor.stringFlavor));
190     } catch (Exception ex)
191     {
192     }
193   }
194
195   /**
196    * DOCUMENT ME!
197    * 
198    * @param e
199    *          DOCUMENT ME!
200    */
201   public void ok_actionPerformed(ActionEvent e)
202   {
203     String text = getText();
204     if (text.trim().length() < 1)
205     {
206       return;
207     }
208
209     String format = new IdentifyFile().Identify(text, "Paste");
210     if (format == null || format.equalsIgnoreCase("EMPTY DATA FILE"))
211     {
212       System.err.println(MessageManager
213               .getString("label.couldnt_read_data"));
214       if (!Jalview.isHeadlessMode())
215       {
216         javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
217                 AppletFormatAdapter.SUPPORTED_FORMATS,
218                 MessageManager.getString("label.couldnt_read_data"),
219                 JOptionPane.WARNING_MESSAGE);
220       }
221       return;
222     }
223
224     // TODO: identify feature, annotation or tree file and parse appropriately.
225     AlignmentI al = null;
226
227     if (FormatAdapter.isValidFormat(format))
228     {
229       try
230       {
231         FormatAdapter fa = new FormatAdapter(alignpanel);
232         al = fa.readFile(getText(), "Paste", format);
233         source = fa.getAlignFile();
234
235       } catch (java.io.IOException ex)
236       {
237         JOptionPane.showInternalMessageDialog(Desktop.desktop,
238                 MessageManager.formatMessage(
239                         "label.couldnt_read_pasted_text",
240                         new String[] { ex.toString() }), MessageManager
241                         .getString("label.error_parsing_text"),
242                 JOptionPane.WARNING_MESSAGE);
243       }
244     }
245
246     if (al != null && al.hasValidSequence())
247     {
248       String title = MessageManager.formatMessage(
249               "label.input_cut_paste_params", new String[] { format });
250       if (viewport != null)
251       {
252         ((AlignViewport) viewport).addAlignment(al, title);
253       }
254       else
255       {
256
257         AlignFrame af;
258         if (source instanceof ComplexAlignFile)
259         {
260           ColumnSelection colSel = ((ComplexAlignFile) source)
261                   .getColumnSelection();
262           SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
263                   .getHiddenSequences();
264           boolean showSeqFeatures = ((ComplexAlignFile) source)
265                   .isShowSeqFeatures();
266           String colourSchemeName = ((ComplexAlignFile) source)
267                   .getGlobalColourScheme();
268           FeaturesDisplayedI fd = ((ComplexAlignFile) source)
269                   .getDisplayedFeatures();
270           af = new AlignFrame(al, hiddenSeqs, colSel,
271                   AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
272           af.getViewport().setShowSequenceFeatures(showSeqFeatures);
273           af.getViewport().setFeaturesDisplayed(fd);
274           ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme(
275                   colourSchemeName, al);
276           if (cs != null)
277           {
278             af.changeColour(cs);
279           }
280         }
281         else
282         {
283           af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
284                   AlignFrame.DEFAULT_HEIGHT);
285         }
286
287         af.currentFileFormat = format;
288         Desktop.addInternalFrame(af, title, AlignFrame.DEFAULT_WIDTH,
289                 AlignFrame.DEFAULT_HEIGHT);
290         af.statusBar.setText(MessageManager
291                 .getString("label.successfully_pasted_alignment_file"));
292
293         try
294         {
295           af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
296                   false));
297         } catch (Exception ex)
298         {
299         }
300       }
301     }
302     else
303     {
304       System.err.println(MessageManager
305               .getString("label.couldnt_read_data"));
306       if (!Jalview.isHeadlessMode())
307       {
308         javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
309                 AppletFormatAdapter.SUPPORTED_FORMATS,
310                 MessageManager.getString("label.couldnt_read_data"),
311                 JOptionPane.WARNING_MESSAGE);
312       }
313     }
314   }
315
316   /**
317    * DOCUMENT ME!
318    * 
319    * @param e
320    *          DOCUMENT ME!
321    */
322   public void cancel_actionPerformed(ActionEvent e)
323   {
324     try
325     {
326       this.setClosed(true);
327     } catch (Exception ex)
328     {
329     }
330   }
331
332   public void textarea_mousePressed(MouseEvent e)
333   {
334     if (SwingUtilities.isRightMouseButton(e))
335     {
336       JPopupMenu popup = new JPopupMenu(
337               MessageManager.getString("action.edit"));
338       JMenuItem item = new JMenuItem(
339               MessageManager.getString("action.copy"));
340       item.addActionListener(new ActionListener()
341       {
342         public void actionPerformed(ActionEvent e)
343         {
344           copyItem_actionPerformed(e);
345         }
346       });
347       popup.add(item);
348       item = new JMenuItem(MessageManager.getString("action.paste"));
349       item.addActionListener(new ActionListener()
350       {
351         public void actionPerformed(ActionEvent e)
352         {
353           pasteMenu_actionPerformed(e);
354         }
355       });
356       popup.add(item);
357       popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);
358
359     }
360   }
361
362 }