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