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