updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / appletgui / 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
20 package jalview.appletgui;
21
22 import java.awt.*;
23 import java.awt.event.*;
24
25 import jalview.datamodel.*;
26 import jalview.io.*;
27
28 public class CutAndPasteTransfer extends Panel implements ActionListener, MouseListener
29 {
30   boolean pdbImport = false;
31   boolean treeImport = false;
32   Sequence seq;
33   AlignFrame alignFrame;
34
35   public CutAndPasteTransfer(boolean forImport, AlignFrame alignFrame)
36   {
37     try {
38         jbInit();
39     } catch (Exception e) {
40         e.printStackTrace();
41     }
42
43     this.alignFrame = alignFrame;
44
45     if (!forImport)
46     {
47       buttonPanel.setVisible(false);
48     }
49   }
50
51   public String getText()
52   {
53     return textarea.getText();
54   }
55
56   public void setText(String text)
57   {
58     textarea.setText(text);
59   }
60
61   public void setPDBImport(Sequence seq)
62   {
63     this.seq = seq;
64     pdbImport = true;
65   }
66
67
68   public void actionPerformed(ActionEvent evt)
69   {
70     if(evt.getSource()==ok)
71       ok_actionPerformed();
72     else if(evt.getSource()==cancel)
73       cancel_actionPerformed();
74   }
75
76   protected void ok_actionPerformed()
77   {
78     String text = getText();
79     int length = text.length();
80     textarea.append("\n");
81     if(textarea.getText().length()==length)
82     {
83       String warning = "\n\n#################################################\n"
84           +"WARNING!! THIS IS THE MAXIMUM SIZE OF TEXTAREA!!\n"
85           +"\nCAN'T INPUT FULL ALIGNMENT"
86           +"\n\nYOU MUST DELETE THIS WARNING TO CONTINUE"
87           +"\n\nMAKE SURE LAST SEQUENCE PASTED IS COMPLETE"
88           +"\n#################################################\n";
89       textarea.setText(text.substring(0, text.length()-warning.length())
90           +warning);
91
92       textarea.setCaretPosition(text.length());
93     }
94
95     if(pdbImport)
96     {
97       new MCview.AppletPDBViewer(text, AppletFormatAdapter.PASTE,
98                                  seq,
99                                  alignFrame.getSeqcanvas());
100     }
101     else if(treeImport)
102     {
103       try{
104         jalview.io.NewickFile fin = new jalview.io.NewickFile(textarea.getText(),
105             "Paste");
106
107         fin.parse();
108         if(fin.getTree()!=null)
109           alignFrame.loadTree(fin, "Pasted tree file");
110
111       }
112       catch (Exception ex)
113       {
114         textarea.setText("Could not parse Newick file!\n" + ex);
115         return;
116       }
117     }
118     else if(alignFrame!=null)
119     {
120       SequenceI[] sequences = null;
121
122       String format = new IdentifyFile().Identify(text, AppletFormatAdapter.PASTE);
123       try{
124         sequences = new AppletFormatAdapter().readFile(text, AppletFormatAdapter.PASTE, format);
125       }catch(java.io.IOException ex)
126       {
127         ex.printStackTrace();
128       }
129       if (sequences != null)
130       {
131         AlignFrame af = new AlignFrame(new Alignment(sequences), alignFrame.viewport.applet,
132                                        "Cut & Paste input - " + format,
133                                        false);
134         af.statusBar.setText("Successfully pasted alignment file");
135       }
136     }
137
138    if(this.getParent() instanceof Frame)
139     ((Frame)this.getParent()).setVisible(false);
140    else
141      ((Dialog)this.getParent()).setVisible(false);
142   }
143
144   protected void cancel_actionPerformed()
145   {
146     textarea.setText("");
147     if(this.getParent() instanceof Frame)
148      ((Frame)this.getParent()).setVisible(false);
149     else
150      ((Dialog)this.getParent()).setVisible(false);
151   }
152
153   protected TextArea textarea = new TextArea();
154   Button ok = new Button();
155   Button cancel = new Button();
156   protected Panel buttonPanel = new Panel();
157   BorderLayout borderLayout1 = new BorderLayout();
158
159
160   private void jbInit() throws Exception {
161       textarea.setFont(new java.awt.Font("Monospaced", Font.PLAIN, 10));
162       textarea.setText("Paste your alignment file here");
163       textarea.addMouseListener(this);
164       this.setLayout(borderLayout1);
165       ok.setLabel("OK");
166       ok.addActionListener(this);
167       cancel.setLabel("Cancel");
168       cancel.addActionListener(this);
169       this.add(buttonPanel, BorderLayout.SOUTH);
170       buttonPanel.add(ok, null);
171       buttonPanel.add(cancel, null);
172       this.add(textarea, java.awt.BorderLayout.CENTER);
173   }
174
175   public void mousePressed(MouseEvent evt) {
176       if (textarea.getText().startsWith("Paste your")) {
177           textarea.setText("");
178      }
179   }
180   public void mouseReleased(MouseEvent evt){}
181   public void mouseClicked(MouseEvent evt){}
182   public void mouseEntered(MouseEvent evt){}
183   public void mouseExited(MouseEvent evt){}
184 }