updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / gui / CutAndPasteTransfer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 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 jalview.datamodel.*;
22
23 import jalview.io.*;
24
25 import jalview.jbgui.*;
26
27 import java.awt.*;
28 import java.awt.datatransfer.*;
29 import java.awt.event.*;
30
31 import javax.swing.*;
32
33
34 /**
35  * DOCUMENT ME!
36  *
37  * @author $author$
38  * @version $Revision$
39  */
40 public class CutAndPasteTransfer extends GCutAndPasteTransfer
41 {
42
43   AlignViewport viewport;
44
45   public CutAndPasteTransfer()
46   {
47     SwingUtilities.invokeLater(new Runnable()
48     {
49       public void run()
50       {
51         textarea.requestFocus();
52       }
53     });
54
55   }
56
57     /**
58      * DOCUMENT ME!
59      */
60     public void setForInput(AlignViewport viewport)
61     {
62       this.viewport = viewport;
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 DOCUMENT ME!
80      */
81     public void setText(String text)
82     {
83         textarea.setText(text);
84     }
85
86     public void appendText(String text)
87     {
88       textarea.append(text);
89     }
90
91
92     public void save_actionPerformed(ActionEvent e)
93     {
94       JalviewFileChooser chooser = new JalviewFileChooser(
95           jalview.bin.Cache.getProperty(
96               "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         }
115         catch (Exception ex)
116         {
117           ex.printStackTrace();
118         }
119
120       }
121     }
122
123
124
125     /**
126      * DOCUMENT ME!
127      *
128      * @param e DOCUMENT ME!
129      */
130     public void copyItem_actionPerformed(ActionEvent e)
131     {
132         textarea.getSelectedText();
133         Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
134         c.setContents(new StringSelection(textarea.getSelectedText()), null);
135     }
136
137     /**
138      * DOCUMENT ME!
139      *
140      * @param e DOCUMENT ME!
141      */
142     public void pasteMenu_actionPerformed(ActionEvent e)
143     {
144         Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
145         Transferable contents = c.getContents(this);
146
147         if (contents == null)
148         {
149             return;
150         }
151
152         try
153         {
154             textarea.append((String) contents.getTransferData(
155                     DataFlavor.stringFlavor));
156         }
157         catch (Exception ex)
158         {
159         }
160     }
161
162     /**
163      * DOCUMENT ME!
164      *
165      * @param e DOCUMENT ME!
166      */
167     public void ok_actionPerformed(ActionEvent e)
168     {
169         String format = new IdentifyFile().Identify(getText(), "Paste");
170         SequenceI[] sequences = null;
171
172         if (FormatAdapter.isValidFormat(format))
173         {
174           try{
175             sequences = 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",
181                                                   JOptionPane.WARNING_MESSAGE);
182           }
183         }
184
185         if (sequences != null)
186         {
187           if(viewport!=null)
188           {
189             for(int i=0; i<sequences.length; i++)
190               viewport.getAlignment().addSequence(sequences[i]);
191
192             viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences());
193           }
194           else
195           {
196             AlignFrame af = new AlignFrame(new Alignment(sequences));
197             af.currentFileFormat = format;
198             Desktop.addInternalFrame(af, "Cut & Paste input - " + format,
199                                      AlignFrame.NEW_WINDOW_WIDTH,
200                                      AlignFrame.NEW_WINDOW_HEIGHT);
201             af.statusBar.setText("Successfully pasted alignment file");
202
203             try
204             {
205               af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));
206             }
207             catch (Exception ex)
208             {
209             }
210           }
211         }
212     }
213
214     /**
215      * DOCUMENT ME!
216      *
217      * @param e DOCUMENT ME!
218      */
219     public void cancel_actionPerformed(ActionEvent e)
220     {
221         try
222         {
223             this.setClosed(true);
224         }
225         catch (Exception ex)
226         {
227         }
228     }
229
230     public void textarea_mousePressed(MouseEvent e)
231     {
232       if(SwingUtilities.isRightMouseButton(e))
233       {
234         JPopupMenu popup = new JPopupMenu("Edit");
235         JMenuItem item = new JMenuItem("Copy");
236         item.addActionListener(new ActionListener()
237             {public void actionPerformed(ActionEvent e)
238                   {
239                     copyItem_actionPerformed(e);
240                   }
241             });
242         popup.add(item);
243         item = new JMenuItem("Paste");
244         item.addActionListener(new ActionListener()
245             {public void actionPerformed(ActionEvent e)
246                   {
247                     pasteMenu_actionPerformed(e);
248                   }
249             });
250         popup.add(item);
251         popup.show(this, e.getX()+10, e.getY()+textarea.getY()+40);
252
253       }
254     }
255
256 }