fix recent file type bug
[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 (protocol.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     String title = protocol.equals(AppletFormatAdapter.PASTE)\r
129         ? "Copied From Clipboard" : file;\r
130 \r
131     try\r
132     {\r
133       if (Desktop.instance != null)\r
134       {\r
135         Desktop.instance.startLoading(file);\r
136       }\r
137 \r
138       Alignment al = null;\r
139 \r
140       if (format.equalsIgnoreCase("Jalview"))\r
141       {\r
142         alignFrame = new Jalview2XML().LoadJalviewAlign(file);\r
143       }\r
144       else\r
145       {\r
146         String error = AppletFormatAdapter.SUPPORTED_FORMATS;\r
147 \r
148         if (FormatAdapter.isValidFormat(format))\r
149         {\r
150           try\r
151           {\r
152             al = new FormatAdapter().readFile(file, protocol, format);\r
153           }\r
154           catch (java.io.IOException ex)\r
155           {\r
156             error = ex.getMessage();\r
157           }\r
158         }\r
159 \r
160         if ( (al != null) && (al.getHeight() > 0))\r
161         {\r
162           if (viewport != null)\r
163           {\r
164             for (int i = 0; i < al.getHeight(); i++)\r
165             {\r
166               viewport.getAlignment().addSequence(al.getSequenceAt(i));\r
167             }\r
168             viewport.firePropertyChange("alignment", null,\r
169                                         viewport.getAlignment().getSequences());\r
170 \r
171           }\r
172           else\r
173           {\r
174             alignFrame = new AlignFrame(al,\r
175                                         AlignFrame.DEFAULT_WIDTH,\r
176                                         AlignFrame.DEFAULT_HEIGHT);\r
177 \r
178             alignFrame.statusBar.setText("Successfully loaded file " + title);\r
179 \r
180             if (!protocol.equals(AppletFormatAdapter.PASTE))\r
181               alignFrame.setFileName(file, format);\r
182 \r
183             Desktop.addInternalFrame(alignFrame, title,\r
184                                      AlignFrame.DEFAULT_WIDTH,\r
185                                      AlignFrame.DEFAULT_HEIGHT);\r
186 \r
187             try\r
188             {\r
189               alignFrame.setMaximum(jalview.bin.Cache.getDefault(\r
190                   "SHOW_FULLSCREEN", false));\r
191             }\r
192             catch (java.beans.PropertyVetoException ex)\r
193             {\r
194             }\r
195           }\r
196         }\r
197         else\r
198         {\r
199           if (Desktop.instance != null)\r
200           {\r
201             Desktop.instance.stopLoading();\r
202           }\r
203 \r
204           final String errorMessage = "Couldn't load file " + title + "\n" +\r
205               error;\r
206 \r
207           javax.swing.SwingUtilities.invokeLater(new Runnable()\r
208           {\r
209             public void run()\r
210             {\r
211               JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
212                   errorMessage,\r
213                   "Error loading file",\r
214                   JOptionPane.WARNING_MESSAGE);\r
215             }\r
216           });\r
217         }\r
218       }\r
219 \r
220       updateRecentlyOpened();\r
221 \r
222     }\r
223     catch (OutOfMemoryError er)\r
224     {\r
225 \r
226       er.printStackTrace();\r
227       alignFrame = null;\r
228 \r
229       javax.swing.SwingUtilities.invokeLater(new Runnable()\r
230       {\r
231         public void run()\r
232         {\r
233           javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
234               "Out of memory loading file " + file + "!!"\r
235               +\r
236               "\nSee help files for increasing Java Virtual Machine memory."\r
237               , "Out of memory",\r
238               javax.swing.JOptionPane.WARNING_MESSAGE);\r
239         }\r
240       });\r
241     }\r
242 \r
243     System.gc();\r
244     if (Desktop.instance != null)\r
245     {\r
246       Desktop.instance.stopLoading();\r
247     }\r
248 \r
249   }\r
250 \r
251 }\r