update author list in license for (JAL-826)
[jalview.git] / src / jalview / gui / CutAndPasteTransfer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.gui;
19
20 import java.awt.*;
21 import java.awt.datatransfer.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24
25 import jalview.datamodel.*;
26 import jalview.io.*;
27 import jalview.jbgui.*;
28
29 /**
30  * DOCUMENT ME!
31  * 
32  * @author $author$
33  * @version $Revision$
34  */
35 public class CutAndPasteTransfer extends GCutAndPasteTransfer
36 {
37
38   AlignViewport viewport;
39
40   public CutAndPasteTransfer()
41   {
42     SwingUtilities.invokeLater(new Runnable()
43     {
44       public void run()
45       {
46         textarea.requestFocus();
47       }
48     });
49
50   }
51
52   /**
53    * DOCUMENT ME!
54    */
55   public void setForInput(AlignViewport viewport)
56   {
57     this.viewport = viewport;
58     if (viewport != null)
59     {
60       ok.setText("Add");
61     }
62
63     getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
64   }
65
66   /**
67    * DOCUMENT ME!
68    * 
69    * @return DOCUMENT ME!
70    */
71   public String getText()
72   {
73     return textarea.getText();
74   }
75
76   /**
77    * DOCUMENT ME!
78    * 
79    * @param text
80    *          DOCUMENT ME!
81    */
82   public void setText(String text)
83   {
84     textarea.setText(text);
85   }
86
87   public void appendText(String text)
88   {
89     textarea.append(text);
90   }
91
92   public void save_actionPerformed(ActionEvent e)
93   {
94     JalviewFileChooser chooser = new JalviewFileChooser(
95             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
96
97     chooser.setAcceptAllFileFilterUsed(false);
98     chooser.setFileView(new JalviewFileView());
99     chooser.setDialogTitle("Save Text to File");
100     chooser.setToolTipText("Save");
101
102     int value = chooser.showSaveDialog(this);
103
104     if (value == JalviewFileChooser.APPROVE_OPTION)
105     {
106       try
107       {
108         java.io.PrintWriter out = new java.io.PrintWriter(
109                 new java.io.FileWriter(chooser.getSelectedFile()));
110
111         out.print(getText());
112         out.close();
113       } catch (Exception ex)
114       {
115         ex.printStackTrace();
116       }
117
118     }
119   }
120
121   /**
122    * DOCUMENT ME!
123    * 
124    * @param e
125    *          DOCUMENT ME!
126    */
127   public void copyItem_actionPerformed(ActionEvent e)
128   {
129     textarea.getSelectedText();
130     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
131     c.setContents(new StringSelection(textarea.getSelectedText()), null);
132   }
133
134   /**
135    * DOCUMENT ME!
136    * 
137    * @param e
138    *          DOCUMENT ME!
139    */
140   public void pasteMenu_actionPerformed(ActionEvent e)
141   {
142     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
143     Transferable contents = c.getContents(this);
144
145     if (contents == null)
146     {
147       return;
148     }
149
150     try
151     {
152       textarea.append((String) contents
153               .getTransferData(DataFlavor.stringFlavor));
154     } catch (Exception ex)
155     {
156     }
157   }
158
159   /**
160    * DOCUMENT ME!
161    * 
162    * @param e
163    *          DOCUMENT ME!
164    */
165   public void ok_actionPerformed(ActionEvent e)
166   {
167     String format = new IdentifyFile().Identify(getText(), "Paste");
168     // TODO: identify feature, annotation or tree file and parse appropriately.
169     Alignment al = null;
170
171     if (FormatAdapter.isValidFormat(format))
172     {
173       try
174       {
175         al = new FormatAdapter().readFile(getText(), "Paste", format);
176       } catch (java.io.IOException ex)
177       {
178         JOptionPane.showInternalMessageDialog(Desktop.desktop,
179                 "Couldn't read the pasted text.\n" + ex.toString(),
180                 "Error parsing text", JOptionPane.WARNING_MESSAGE);
181       }
182     }
183
184     if (al != null)
185     {
186       if (viewport != null)
187       {
188         for (int i = 0; i < al.getHeight(); i++)
189         {
190           viewport.getAlignment().addSequence(al.getSequenceAt(i));
191         }
192
193         viewport.firePropertyChange("alignment", null, viewport
194                 .getAlignment().getSequences());
195       }
196       else
197       {
198         AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
199                 AlignFrame.DEFAULT_HEIGHT);
200         af.currentFileFormat = format;
201         Desktop.addInternalFrame(af, "Cut & Paste input - " + format,
202                 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
203         af.statusBar.setText("Successfully pasted alignment file");
204
205         try
206         {
207           af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
208                   false));
209         } catch (Exception ex)
210         {
211         }
212       }
213     }
214   }
215
216   /**
217    * DOCUMENT ME!
218    * 
219    * @param e
220    *          DOCUMENT ME!
221    */
222   public void cancel_actionPerformed(ActionEvent e)
223   {
224     try
225     {
226       this.setClosed(true);
227     } catch (Exception ex)
228     {
229     }
230   }
231
232   public void textarea_mousePressed(MouseEvent e)
233   {
234     if (SwingUtilities.isRightMouseButton(e))
235     {
236       JPopupMenu popup = new JPopupMenu("Edit");
237       JMenuItem item = new JMenuItem("Copy");
238       item.addActionListener(new ActionListener()
239       {
240         public void actionPerformed(ActionEvent e)
241         {
242           copyItem_actionPerformed(e);
243         }
244       });
245       popup.add(item);
246       item = new JMenuItem("Paste");
247       item.addActionListener(new ActionListener()
248       {
249         public void actionPerformed(ActionEvent e)
250         {
251           pasteMenu_actionPerformed(e);
252         }
253       });
254       popup.add(item);
255       popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);
256
257     }
258   }
259
260 }