alignFrame.setFileName
[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 \r
178               alignFrame.statusBar.setText("Successfully loaded file " + file);\r
179               alignFrame.setFileName(file, format);\r
180 \r
181 \r
182               Desktop.addInternalFrame(alignFrame, file,\r
183                                        AlignFrame.DEFAULT_WIDTH,\r
184                                        AlignFrame.DEFAULT_HEIGHT);\r
185 \r
186               try\r
187               {\r
188                alignFrame.setMaximum(jalview.bin.Cache.getDefault(\r
189                    "SHOW_FULLSCREEN", false));\r
190               }\r
191               catch (java.beans.PropertyVetoException ex)\r
192               {\r
193               }\r
194             }\r
195           }\r
196           else\r
197           {\r
198             if (Desktop.instance != null)\r
199               Desktop.instance.stopLoading();\r
200 \r
201             final String errorMessage = "Couldn't load file " + file + "\n" +\r
202                 error;\r
203 \r
204             javax.swing.SwingUtilities.invokeLater(new Runnable()\r
205             {\r
206               public void run()\r
207               {\r
208                 JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
209                     errorMessage,\r
210                     "Error loading file",\r
211                     JOptionPane.WARNING_MESSAGE);\r
212               }\r
213             });\r
214           }\r
215         }\r
216 \r
217          updateRecentlyOpened();\r
218 \r
219       }\r
220       catch (OutOfMemoryError er)\r
221       {\r
222 \r
223         er.printStackTrace();\r
224         alignFrame = null;\r
225 \r
226         javax.swing.SwingUtilities.invokeLater(new Runnable()\r
227         {\r
228           public void run()\r
229           {\r
230             javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
231                 "Out of memory loading file "+file+"!!"\r
232                 +\r
233                 "\nSee help files for increasing Java Virtual Machine memory."\r
234                 , "Out of memory",\r
235                 javax.swing.JOptionPane.WARNING_MESSAGE);\r
236           }\r
237         });\r
238       }\r
239 \r
240      System.gc();\r
241      if (Desktop.instance != null)\r
242         Desktop.instance.stopLoading();\r
243 \r
244     }\r
245 \r
246 }\r