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