Wrap alignment moved to alignFrame
[jalview.git] / src / jalview / io / FileLoader.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 \r
20 package jalview.io;\r
21 \r
22 import jalview.gui.AlignFrame;\r
23 import jalview.gui.Jalview2XML;\r
24 import javax.swing.JOptionPane;\r
25 \r
26 import jalview.gui.*;\r
27 import jalview.datamodel.*;\r
28 import java.util.Vector;\r
29 import java.util.StringTokenizer;\r
30 \r
31 public class FileLoader\r
32 {\r
33   String file;\r
34   String protocol;\r
35   String format;\r
36   AlignViewport viewport;\r
37 \r
38   public void LoadFile(AlignViewport viewport, String file, String protocol, String format)\r
39   {\r
40     this.viewport = viewport;\r
41     LoadFile(file, protocol, format);\r
42   }\r
43 \r
44   public void LoadFile(String file, String protocol, String format)\r
45   {\r
46     this.file = file;\r
47     this.protocol = protocol;\r
48     this.format = format;\r
49 \r
50     LoadingThread loader = new LoadingThread();\r
51     loader.start();\r
52   }\r
53 \r
54   public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,\r
55                                                   String format)\r
56   {\r
57     this.file = file;\r
58     this.protocol = protocol;\r
59     this.format = format;\r
60 \r
61     LoadingThread loader = new LoadingThread();\r
62     loader.start();\r
63     while (loader.isAlive())\r
64     {\r
65       try\r
66       {\r
67         Thread.sleep(500);\r
68       }\r
69       catch (Exception ex)\r
70       {}\r
71     }\r
72 \r
73     return loader.af;\r
74   }\r
75 \r
76 \r
77 \r
78   public void updateRecentlyOpened()\r
79   {\r
80     Vector recent = new Vector();\r
81 \r
82     String type = protocol.equals(FormatAdapter.FILE)\r
83         ? "RECENT_FILE" : "RECENT_URL";\r
84 \r
85     String historyItems = jalview.bin.Cache.getProperty(type);\r
86 \r
87     StringTokenizer st;\r
88 \r
89     if (historyItems != null)\r
90     {\r
91       st = new StringTokenizer(historyItems, "\t");\r
92 \r
93       while (st.hasMoreTokens())\r
94       {\r
95         recent.addElement(st.nextElement().toString().trim());\r
96       }\r
97     }\r
98 \r
99     if (recent.contains(file))\r
100     {\r
101       recent.remove(file);\r
102     }\r
103 \r
104     StringBuffer newHistory = new StringBuffer(file);\r
105     for (int i = 0; i < recent.size() && i < 10; i++)\r
106     {\r
107       newHistory.append("\t");\r
108       newHistory.append(recent.elementAt(i));\r
109     }\r
110 \r
111     jalview.bin.Cache.setProperty(type, newHistory.toString());\r
112 \r
113     if(type.equals(FormatAdapter.FILE))\r
114       jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);\r
115   }\r
116 \r
117 \r
118   class LoadingThread\r
119       extends Thread\r
120   {\r
121 \r
122     AlignFrame af;\r
123 \r
124 \r
125 \r
126     public void run()\r
127     {\r
128       Desktop.instance.startLoading(file);\r
129 \r
130       SequenceI[] sequences = null;\r
131 \r
132       if (format.equalsIgnoreCase("Jalview"))\r
133       {\r
134         af = new Jalview2XML().LoadJalviewAlign(file);\r
135       }\r
136       else\r
137       {\r
138         String errorMessage = AppletFormatAdapter.SUPPORTED_FORMATS;\r
139 \r
140         if (FormatAdapter.formats.contains(format))\r
141         {\r
142           try\r
143           {\r
144             sequences = new FormatAdapter().readFile(file, protocol, format);\r
145           }\r
146           catch (java.io.IOException ex)\r
147           {\r
148             errorMessage = ex.getMessage();\r
149           }\r
150         }\r
151 \r
152         if ( (sequences != null) && (sequences.length > 0))\r
153         {\r
154           if(viewport!=null)\r
155           {\r
156             for(int i=0; i<sequences.length; i++)\r
157               viewport.getAlignment().addSequence(sequences[i]);\r
158 \r
159               viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences());\r
160           }\r
161           else\r
162           {\r
163             af = new AlignFrame(new Alignment(sequences));\r
164             af.currentFileFormat = format;\r
165             af.statusBar.setText("Successfully loaded file " + file);\r
166 \r
167             Desktop.addInternalFrame(af, file, AlignFrame.NEW_WINDOW_WIDTH,\r
168                                      AlignFrame.NEW_WINDOW_HEIGHT);\r
169 \r
170             try\r
171             {\r
172               af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));\r
173             }\r
174             catch (java.beans.PropertyVetoException ex)\r
175             {\r
176             }\r
177           }\r
178 \r
179         }\r
180         else\r
181         {\r
182           JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
183                                                 "Couldn't load file " + file +\r
184                                                 "\n"\r
185                                                 + errorMessage,\r
186                                                 "Error loading file",\r
187                                                 JOptionPane.WARNING_MESSAGE);\r
188         }\r
189       }\r
190 \r
191       if (af != null)\r
192       {\r
193         updateRecentlyOpened();\r
194       }\r
195 \r
196       Desktop.instance.stopLoading();\r
197     }\r
198   }\r
199 \r
200 }\r