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