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