apply gpl development license
[jalview.git] / src / jalview / gui / CutAndPasteTransfer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3  * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.gui;
20
21 import java.awt.*;
22 import java.awt.datatransfer.*;
23 import java.awt.event.*;
24 import javax.swing.*;
25
26 import jalview.datamodel.*;
27 import jalview.io.*;
28 import jalview.jbgui.*;
29
30 /**
31  * DOCUMENT ME!
32  * 
33  * @author $author$
34  * @version $Revision$
35  */
36 public class CutAndPasteTransfer extends GCutAndPasteTransfer
37 {
38
39   AlignViewport viewport;
40
41   public CutAndPasteTransfer()
42   {
43     SwingUtilities.invokeLater(new Runnable()
44     {
45       public void run()
46       {
47         textarea.requestFocus();
48       }
49     });
50
51   }
52
53   /**
54    * DOCUMENT ME!
55    */
56   public void setForInput(AlignViewport viewport)
57   {
58     this.viewport = viewport;
59     if (viewport != null)
60     {
61       ok.setText("Add");
62     }
63
64     getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
65   }
66
67   /**
68    * DOCUMENT ME!
69    * 
70    * @return DOCUMENT ME!
71    */
72   public String getText()
73   {
74     return textarea.getText();
75   }
76
77   /**
78    * DOCUMENT ME!
79    * 
80    * @param text
81    *                DOCUMENT ME!
82    */
83   public void setText(String text)
84   {
85     textarea.setText(text);
86   }
87
88   public void appendText(String text)
89   {
90     textarea.append(text);
91   }
92
93   public void save_actionPerformed(ActionEvent e)
94   {
95     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
96             .getProperty("LAST_DIRECTORY"));
97
98     chooser.setAcceptAllFileFilterUsed(false);
99     chooser.setFileView(new JalviewFileView());
100     chooser.setDialogTitle("Save Text to File");
101     chooser.setToolTipText("Save");
102
103     int value = chooser.showSaveDialog(this);
104
105     if (value == JalviewFileChooser.APPROVE_OPTION)
106     {
107       try
108       {
109         java.io.PrintWriter out = new java.io.PrintWriter(
110                 new java.io.FileWriter(chooser.getSelectedFile()));
111
112         out.print(getText());
113         out.close();
114       } catch (Exception ex)
115       {
116         ex.printStackTrace();
117       }
118
119     }
120   }
121
122   /**
123    * DOCUMENT ME!
124    * 
125    * @param e
126    *                DOCUMENT ME!
127    */
128   public void copyItem_actionPerformed(ActionEvent e)
129   {
130     textarea.getSelectedText();
131     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
132     c.setContents(new StringSelection(textarea.getSelectedText()), null);
133   }
134
135   /**
136    * DOCUMENT ME!
137    * 
138    * @param e
139    *                DOCUMENT ME!
140    */
141   public void pasteMenu_actionPerformed(ActionEvent e)
142   {
143     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
144     Transferable contents = c.getContents(this);
145
146     if (contents == null)
147     {
148       return;
149     }
150
151     try
152     {
153       textarea.append((String) contents
154               .getTransferData(DataFlavor.stringFlavor));
155     } catch (Exception ex)
156     {
157     }
158   }
159
160   /**
161    * DOCUMENT ME!
162    * 
163    * @param e
164    *                DOCUMENT ME!
165    */
166   public void ok_actionPerformed(ActionEvent e)
167   {
168     String format = new IdentifyFile().Identify(getText(), "Paste");
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 }