Formatted source
[jalview.git] / src / jalview / gui / CutAndPasteTransfer.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.gui;\r
20 \r
21 import java.awt.*;\r
22 import java.awt.datatransfer.*;\r
23 import java.awt.event.*;\r
24 import javax.swing.*;\r
25 \r
26 import jalview.datamodel.*;\r
27 import jalview.io.*;\r
28 import jalview.jbgui.*;\r
29 \r
30 public class CutAndPasteTransfer\r
31     extends GCutAndPasteTransfer\r
32 {\r
33   public void setForInput()\r
34   {\r
35     getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);\r
36   }\r
37 \r
38   public String getText()\r
39   {\r
40     return textarea.getText();\r
41   }\r
42 \r
43   public void setText(String text)\r
44   {\r
45     textarea.setText(text);\r
46   }\r
47 \r
48   public void copyItem_actionPerformed(ActionEvent e)\r
49   {\r
50     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();\r
51     c.setContents(new StringSelection(textarea.getText()), null);\r
52   }\r
53 \r
54   public void pasteMenu_actionPerformed(ActionEvent e)\r
55   {\r
56     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();\r
57     Transferable contents = c.getContents(this);\r
58 \r
59     if (contents == null)\r
60     {\r
61       return;\r
62     }\r
63 \r
64     try\r
65     {\r
66       textarea.setText( (String) contents.getTransferData(\r
67           DataFlavor.stringFlavor));\r
68     }\r
69     catch (Exception ex)\r
70     {\r
71     }\r
72   }\r
73 \r
74   public void ok_actionPerformed(ActionEvent e)\r
75   {\r
76     String format = IdentifyFile.Identify(getText(), "Paste");\r
77     SequenceI[] sequences = null;\r
78 \r
79     if (FormatAdapter.formats.contains(format))\r
80     {\r
81       sequences = FormatAdapter.readFile(getText(), "Paste", format);\r
82     }\r
83 \r
84     if (sequences != null)\r
85     {\r
86       AlignFrame af = new AlignFrame(new Alignment(sequences));\r
87       af.currentFileFormat = format;\r
88       Desktop.addInternalFrame(af, "Cut & Paste input - " + format,\r
89                                AlignFrame.NEW_WINDOW_WIDTH,\r
90                                AlignFrame.NEW_WINDOW_HEIGHT);\r
91       af.statusBar.setText("Successfully pasted alignment file");\r
92 \r
93       try\r
94       {\r
95         af.setMaximum(Preferences.showFullscreen);\r
96       }\r
97       catch (Exception ex)\r
98       {\r
99       }\r
100 \r
101       try\r
102       {\r
103         this.setClosed(true);\r
104       }\r
105       catch (Exception ex)\r
106       {\r
107       }\r
108     }\r
109     else\r
110     {\r
111       JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
112                                             "Couldn't read the pasted text.\n" +\r
113                                             "Formats currently supported are\n" +\r
114                                             "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM",\r
115                                             "Error parsing text",\r
116                                             JOptionPane.WARNING_MESSAGE);\r
117     }\r
118   }\r
119 \r
120   public void cancel_actionPerformed(ActionEvent e)\r
121   {\r
122     try\r
123     {\r
124       this.setClosed(true);\r
125     }\r
126     catch (Exception ex)\r
127     {\r
128     }\r
129   }\r
130 }\r