262fc41d14992dea85e2a885c59b26d37e0a6b33
[jalview.git] / src / jalview / gui / CutAndPasteTransfer.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2005 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
20 package jalview.gui;
21
22 import jalview.jbgui.GCutAndPasteTransfer;
23 import java.awt.*;
24 import java.awt.event.ActionEvent;
25 import java.awt.datatransfer.*;
26 import jalview.datamodel.Alignment;
27 import jalview.io.FormatAdapter;
28 import jalview.datamodel.SequenceI;
29 import javax.swing.JOptionPane;
30 import jalview.io.IdentifyFile;
31
32 public class CutAndPasteTransfer extends GCutAndPasteTransfer
33 {
34   public void setForInput()
35   {
36     getContentPane().add(inputButtonPanel, java.awt.BorderLayout.SOUTH);
37   }
38
39   public String getText()
40   {
41     return textarea.getText();
42   }
43
44   public void setText(String text)
45   {
46     textarea.setText(text);
47   }
48
49   public void copyItem_actionPerformed(ActionEvent e)
50   {
51     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
52     c.setContents( new StringSelection( textarea.getText()) , null ) ;
53   }
54
55   public void pasteMenu_actionPerformed(ActionEvent e)
56   {
57     Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
58     Transferable contents = c.getContents(this);
59     if (contents == null)
60       return;
61     try{
62       textarea.setText( (String) contents.getTransferData(DataFlavor.
63           stringFlavor));
64     }catch(Exception ex){}
65   }
66
67   public void ok_actionPerformed(ActionEvent e)
68   {
69       String format = IdentifyFile.Identify(getText(), "Paste");
70       SequenceI [] sequences = null;
71
72       if (FormatAdapter.formats.contains( format ))
73         sequences = FormatAdapter.readFile(getText(), "Paste", format);
74
75
76         if(sequences!=null)
77         {
78           AlignFrame af = new AlignFrame(new Alignment(sequences));
79           af.currentFileFormat = format;
80           Desktop.addInternalFrame(af, "Cut & Paste input - " + format,
81                                    AlignFrame.NEW_WINDOW_WIDTH,
82                                    AlignFrame.NEW_WINDOW_HEIGHT);
83           af.statusBar.setText("Successfully pasted alignment file");
84           try
85           {  af.setMaximum(Preferences.showFullscreen);    }
86           catch (Exception ex)
87           {}
88           try
89           {
90             this.setClosed(true);
91           }
92           catch (Exception ex)
93           {}
94
95
96         }
97         else
98           JOptionPane.showInternalMessageDialog(Desktop.desktop,
99                                                 "Couldn't read the pasted text.\n"
100                                                 + "Formats currently supported are\n"
101                                                 +
102               "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM",
103                                                 "Error parsing text",
104                                                 JOptionPane.WARNING_MESSAGE);
105   }
106
107   public void cancel_actionPerformed(ActionEvent e)
108   {
109     try{
110       this.setClosed(true);
111     }catch(Exception ex){}
112   }
113
114
115 }