added d martin to authors and fixed faq/discuss message
[jalview.git] / src / jalview / gui / CutAndPasteTransfer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 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
37     extends GCutAndPasteTransfer
38 {
39
40   AlignViewport viewport;
41
42   public CutAndPasteTransfer()
43   {
44     SwingUtilities.invokeLater(new Runnable()
45     {
46       public void run()
47       {
48         textarea.requestFocus();
49       }
50     });
51
52   }
53
54   /**
55    * DOCUMENT ME!
56    */
57   public void setForInput(AlignViewport viewport)
58   {
59     this.viewport = viewport;
60     if (viewport != null)
61     {
62       ok.setText("Add");
63     }
64
65     getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
66   }
67
68   /**
69    * DOCUMENT ME!
70    *
71    * @return DOCUMENT ME!
72    */
73   public String getText()
74   {
75     return textarea.getText();
76   }
77
78   /**
79    * DOCUMENT ME!
80    *
81    * @param text 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(
96         jalview.bin.Cache.getProperty(
97             "LAST_DIRECTORY"));
98
99     chooser.setAcceptAllFileFilterUsed(false);
100     chooser.setFileView(new JalviewFileView());
101     chooser.setDialogTitle("Save Text to File");
102     chooser.setToolTipText("Save");
103
104     int value = chooser.showSaveDialog(this);
105
106     if (value == JalviewFileChooser.APPROVE_OPTION)
107     {
108       try
109       {
110         java.io.PrintWriter out = new java.io.PrintWriter(
111             new java.io.FileWriter(chooser.getSelectedFile()));
112
113         out.print(getText());
114         out.close();
115       }
116       catch (Exception ex)
117       {
118         ex.printStackTrace();
119       }
120
121     }
122   }
123
124   /**
125    * DOCUMENT ME!
126    *
127    * @param e DOCUMENT ME!
128    */
129   public void copyItem_actionPerformed(ActionEvent e)
130   {
131     textarea.getSelectedText();
132     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
133     c.setContents(new StringSelection(textarea.getSelectedText()), null);
134   }
135
136   /**
137    * DOCUMENT ME!
138    *
139    * @param e 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.getTransferData(
154           DataFlavor.stringFlavor));
155     }
156     catch (Exception ex)
157     {
158     }
159   }
160
161   /**
162    * DOCUMENT ME!
163    *
164    * @param e 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       }
177       catch (java.io.IOException ex)
178       {
179         JOptionPane.showInternalMessageDialog(Desktop.desktop,
180                                               "Couldn't read the pasted text.\n" +
181                                               ex.toString(),
182                                               "Error parsing text",
183                                               JOptionPane.WARNING_MESSAGE);
184       }
185     }
186
187     if (al != null)
188     {
189       if (viewport != null)
190       {
191         for (int i = 0; i < al.getHeight(); i++)
192         {
193           viewport.getAlignment().addSequence(al.getSequenceAt(i));
194         }
195
196         viewport.firePropertyChange("alignment", null,
197                                     viewport.getAlignment().getSequences());
198       }
199       else
200       {
201         AlignFrame af = new AlignFrame(al,
202                                        AlignFrame.DEFAULT_WIDTH,
203                                        AlignFrame.DEFAULT_HEIGHT);
204         af.currentFileFormat = format;
205         Desktop.addInternalFrame(af, "Cut & Paste input - " + format,
206                                  AlignFrame.DEFAULT_WIDTH,
207                                  AlignFrame.DEFAULT_HEIGHT);
208         af.statusBar.setText("Successfully pasted alignment file");
209
210         try
211         {
212           af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));
213         }
214         catch (Exception ex)
215         {
216         }
217       }
218     }
219   }
220
221   /**
222    * DOCUMENT ME!
223    *
224    * @param e DOCUMENT ME!
225    */
226   public void cancel_actionPerformed(ActionEvent e)
227   {
228     try
229     {
230       this.setClosed(true);
231     }
232     catch (Exception ex)
233     {
234     }
235   }
236
237   public void textarea_mousePressed(MouseEvent e)
238   {
239     if (SwingUtilities.isRightMouseButton(e))
240     {
241       JPopupMenu popup = new JPopupMenu("Edit");
242       JMenuItem item = new JMenuItem("Copy");
243       item.addActionListener(new ActionListener()
244       {
245         public void actionPerformed(ActionEvent e)
246         {
247           copyItem_actionPerformed(e);
248         }
249       });
250       popup.add(item);
251       item = new JMenuItem("Paste");
252       item.addActionListener(new ActionListener()
253       {
254         public void actionPerformed(ActionEvent e)
255         {
256           pasteMenu_actionPerformed(e);
257         }
258       });
259       popup.add(item);
260       popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);
261
262     }
263   }
264
265 }