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