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