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