JAL-1432 updated copyright notices
[jalview.git] / src / jalview / gui / CutAndPasteTransfer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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  * The Jalview Authors are detailed in the 'AUTHORS' file.
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  * Cut'n'paste files into the desktop See JAL-1105
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(
96             jalview.bin.Cache.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     // TODO: identify feature, annotation or tree file and parse appropriately.
170     Alignment al = null;
171
172     if (FormatAdapter.isValidFormat(format))
173     {
174       try
175       {
176         al = new FormatAdapter().readFile(getText(), "Paste", format);
177       } catch (java.io.IOException ex)
178       {
179         JOptionPane.showInternalMessageDialog(Desktop.desktop,
180                 "Couldn't read the pasted text.\n" + ex.toString(),
181                 "Error parsing text", JOptionPane.WARNING_MESSAGE);
182       }
183     }
184
185     if (al != null)
186     {
187       if (viewport != null)
188       {
189         for (int i = 0; i < al.getHeight(); i++)
190         {
191           viewport.getAlignment().addSequence(al.getSequenceAt(i));
192         }
193
194         viewport.firePropertyChange("alignment", null, viewport
195                 .getAlignment().getSequences());
196       }
197       else
198       {
199         AlignFrame af = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
200                 AlignFrame.DEFAULT_HEIGHT);
201         af.currentFileFormat = format;
202         Desktop.addInternalFrame(af, "Cut & Paste input - " + format,
203                 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
204         af.statusBar.setText("Successfully pasted alignment file");
205
206         try
207         {
208           af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN",
209                   false));
210         } catch (Exception ex)
211         {
212         }
213       }
214     }
215   }
216
217   /**
218    * DOCUMENT ME!
219    * 
220    * @param e
221    *          DOCUMENT ME!
222    */
223   public void cancel_actionPerformed(ActionEvent e)
224   {
225     try
226     {
227       this.setClosed(true);
228     } catch (Exception ex)
229     {
230     }
231   }
232
233   public void textarea_mousePressed(MouseEvent e)
234   {
235     if (SwingUtilities.isRightMouseButton(e))
236     {
237       JPopupMenu popup = new JPopupMenu("Edit");
238       JMenuItem item = new JMenuItem("Copy");
239       item.addActionListener(new ActionListener()
240       {
241         public void actionPerformed(ActionEvent e)
242         {
243           copyItem_actionPerformed(e);
244         }
245       });
246       popup.add(item);
247       item = new JMenuItem("Paste");
248       item.addActionListener(new ActionListener()
249       {
250         public void actionPerformed(ActionEvent e)
251         {
252           pasteMenu_actionPerformed(e);
253         }
254       });
255       popup.add(item);
256       popup.show(this, e.getX() + 10, e.getY() + textarea.getY() + 40);
257
258     }
259   }
260
261 }