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