Formatting
[jalview.git] / src / jalview / io / FileLoader.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2007 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 java.util.*;\r
23 \r
24 import javax.swing.*;\r
25 \r
26 import jalview.datamodel.*;\r
27 import jalview.gui.*;\r
28 \r
29 public class FileLoader\r
30     implements Runnable\r
31 {\r
32   String file;\r
33   String protocol;\r
34   String format;\r
35   AlignViewport viewport;\r
36   AlignFrame alignFrame;\r
37 \r
38   public void LoadFile(AlignViewport viewport, String file, String protocol,\r
39                        String format)\r
40   {\r
41     this.viewport = viewport;\r
42     LoadFile(file, protocol, format);\r
43   }\r
44 \r
45   public void LoadFile(String file, String protocol, String format)\r
46   {\r
47     this.file = file;\r
48     this.protocol = protocol;\r
49     this.format = format;\r
50 \r
51     final Thread loader = new Thread(this);\r
52 \r
53     SwingUtilities.invokeLater(new Runnable()\r
54     {\r
55       public void run()\r
56       {\r
57         loader.start();\r
58       }\r
59     });\r
60   }\r
61 \r
62   public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,\r
63                                            String format)\r
64   {\r
65     this.file = file;\r
66     this.protocol = protocol;\r
67     this.format = format;\r
68 \r
69     Thread loader = new Thread(this);\r
70     loader.start();\r
71 \r
72     while (loader.isAlive())\r
73     {\r
74       try\r
75       {\r
76         Thread.sleep(500);\r
77       }\r
78       catch (Exception ex)\r
79       {}\r
80     }\r
81 \r
82     return alignFrame;\r
83   }\r
84 \r
85   public void updateRecentlyOpened()\r
86   {\r
87     Vector recent = new Vector();\r
88 \r
89     String type = protocol.equals(FormatAdapter.FILE)\r
90         ? "RECENT_FILE" : "RECENT_URL";\r
91 \r
92     String historyItems = jalview.bin.Cache.getProperty(type);\r
93 \r
94     StringTokenizer st;\r
95 \r
96     if (historyItems != null)\r
97     {\r
98       st = new StringTokenizer(historyItems, "\t");\r
99 \r
100       while (st.hasMoreTokens())\r
101       {\r
102         recent.addElement(st.nextElement().toString().trim());\r
103       }\r
104     }\r
105 \r
106     if (recent.contains(file))\r
107     {\r
108       recent.remove(file);\r
109     }\r
110 \r
111     StringBuffer newHistory = new StringBuffer(file);\r
112     for (int i = 0; i < recent.size() && i < 10; i++)\r
113     {\r
114       newHistory.append("\t");\r
115       newHistory.append(recent.elementAt(i));\r
116     }\r
117 \r
118     jalview.bin.Cache.setProperty(type, newHistory.toString());\r
119 \r
120     if (type.equals(FormatAdapter.FILE))\r
121     {\r
122       jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);\r
123     }\r
124   }\r
125 \r
126   public void run()\r
127   {\r
128     try\r
129     {\r
130       if (Desktop.instance != null)\r
131       {\r
132         Desktop.instance.startLoading(file);\r
133       }\r
134 \r
135       Alignment al = null;\r
136 \r
137       if (format.equalsIgnoreCase("Jalview"))\r
138       {\r
139         alignFrame = new Jalview2XML().LoadJalviewAlign(file);\r
140       }\r
141       else\r
142       {\r
143         String error = AppletFormatAdapter.SUPPORTED_FORMATS;\r
144 \r
145         if (FormatAdapter.isValidFormat(format))\r
146         {\r
147           try\r
148           {\r
149             al = new FormatAdapter().readFile(file, protocol, format);\r
150           }\r
151           catch (java.io.IOException ex)\r
152           {\r
153             error = ex.getMessage();\r
154           }\r
155         }\r
156 \r
157         if ( (al != null) && (al.getHeight() > 0))\r
158         {\r
159           if (viewport != null)\r
160           {\r
161             for (int i = 0; i < al.getHeight(); i++)\r
162             {\r
163               viewport.getAlignment().addSequence(al.getSequenceAt(i));\r
164             }\r
165             viewport.firePropertyChange("alignment", null,\r
166                                         viewport.getAlignment().getSequences());\r
167 \r
168           }\r
169           else\r
170           {\r
171             alignFrame = new AlignFrame(al,\r
172                                         AlignFrame.DEFAULT_WIDTH,\r
173                                         AlignFrame.DEFAULT_HEIGHT);\r
174 \r
175             alignFrame.statusBar.setText("Successfully loaded file " + file);\r
176             alignFrame.setFileName(file, format);\r
177 \r
178             Desktop.addInternalFrame(alignFrame, file,\r
179                                      AlignFrame.DEFAULT_WIDTH,\r
180                                      AlignFrame.DEFAULT_HEIGHT);\r
181 \r
182             try\r
183             {\r
184               alignFrame.setMaximum(jalview.bin.Cache.getDefault(\r
185                   "SHOW_FULLSCREEN", false));\r
186             }\r
187             catch (java.beans.PropertyVetoException ex)\r
188             {\r
189             }\r
190           }\r
191         }\r
192         else\r
193         {\r
194           if (Desktop.instance != null)\r
195           {\r
196             Desktop.instance.stopLoading();\r
197           }\r
198 \r
199           final String errorMessage = "Couldn't load file " + file + "\n" +\r
200               error;\r
201 \r
202           javax.swing.SwingUtilities.invokeLater(new Runnable()\r
203           {\r
204             public void run()\r
205             {\r
206               JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
207                   errorMessage,\r
208                   "Error loading file",\r
209                   JOptionPane.WARNING_MESSAGE);\r
210             }\r
211           });\r
212         }\r
213       }\r
214 \r
215       updateRecentlyOpened();\r
216 \r
217     }\r
218     catch (OutOfMemoryError er)\r
219     {\r
220 \r
221       er.printStackTrace();\r
222       alignFrame = null;\r
223 \r
224       javax.swing.SwingUtilities.invokeLater(new Runnable()\r
225       {\r
226         public void run()\r
227         {\r
228           javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
229               "Out of memory loading file " + file + "!!"\r
230               +\r
231               "\nSee help files for increasing Java Virtual Machine memory."\r
232               , "Out of memory",\r
233               javax.swing.JOptionPane.WARNING_MESSAGE);\r
234         }\r
235       });\r
236     }\r
237 \r
238     System.gc();\r
239     if (Desktop.instance != null)\r
240     {\r
241       Desktop.instance.stopLoading();\r
242     }\r
243 \r
244   }\r
245 \r
246 }\r