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