2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 import jalview.datamodel.*;
29 public class FileLoader implements Runnable
37 FileParse source = null; // alternative specification of where data comes
41 AlignViewport viewport;
43 AlignFrame alignFrame;
49 boolean raiseGUI = true;
51 * default constructor always raised errors in GUI dialog boxes
57 * construct a Fileloader that may raise errors non-interactively
58 * @param raiseGUI true if errors are to be raised as GUI dialog boxes
60 public FileLoader(boolean raiseGUI)
62 this.raiseGUI = raiseGUI;
65 public void LoadFile(AlignViewport viewport, String file,
66 String protocol, String format)
68 this.viewport = viewport;
69 LoadFile(file, protocol, format);
72 public void LoadFile(String file, String protocol, String format)
75 this.protocol = protocol;
78 final Thread loader = new Thread(this);
80 SwingUtilities.invokeLater(new Runnable()
90 * Load a (file, protocol) source of unknown type
95 public void LoadFile(String file, String protocol)
97 LoadFile(file, protocol, null);
101 * Load alignment from (file, protocol) and wait till loaded
105 * @return alignFrame constructed from file contents
107 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol)
109 return LoadFileWaitTillLoaded(file, protocol, null);
113 * Load alignment from (file, protocol) of type format and wait till loaded
118 * @return alignFrame constructed from file contents
120 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
124 this.protocol = protocol;
125 this.format = format;
126 return _LoadFileWaitTillLoaded();
130 * Load alignment from FileParse source of type format and wait till loaded
134 * @return alignFrame constructed from file contents
136 public AlignFrame LoadFileWaitTillLoaded(FileParse source, String format)
138 this.source = source;
139 file = source.getInFile();
140 protocol = source.type;
141 this.format = format;
142 return _LoadFileWaitTillLoaded();
146 * start thread and wait until finished, then return the alignFrame that's
147 * (hopefully) been read.
151 protected AlignFrame _LoadFileWaitTillLoaded()
153 Thread loader = new Thread(this);
156 while (loader.isAlive())
161 } catch (Exception ex)
169 public void updateRecentlyOpened()
171 Vector recent = new Vector();
173 String type = protocol.equals(FormatAdapter.FILE) ? "RECENT_FILE"
176 String historyItems = jalview.bin.Cache.getProperty(type);
180 if (historyItems != null)
182 st = new StringTokenizer(historyItems, "\t");
184 while (st.hasMoreTokens())
186 recent.addElement(st.nextElement().toString().trim());
190 if (recent.contains(file))
195 StringBuffer newHistory = new StringBuffer(file);
196 for (int i = 0; i < recent.size() && i < 10; i++)
198 newHistory.append("\t");
199 newHistory.append(recent.elementAt(i));
202 jalview.bin.Cache.setProperty(type, newHistory.toString());
204 if (protocol.equals(FormatAdapter.FILE))
206 jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
212 String title = protocol.equals(AppletFormatAdapter.PASTE) ? "Copied From Clipboard"
214 Runtime rt = Runtime.getRuntime();
217 if (Desktop.instance != null)
219 Desktop.instance.startLoading(file);
223 // just in case the caller didn't identify the file for us
226 format = new IdentifyFile().Identify(source, false); // identify
233 format = new IdentifyFile().Identify(file, protocol);
236 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
239 memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // free memory before load
241 loadtime = -System.currentTimeMillis();
244 if (format.equalsIgnoreCase("Jalview"))
248 // Tell the user (developer?) that this is going to cause a problem
250 .println("IMPLEMENTATION ERROR: Cannot read consecutive Jalview XML projects from a stream.");
251 // We read the data anyway - it might make sense.
253 alignFrame = new Jalview2XML(raiseGUI).LoadJalviewAlign(file);
257 String error = AppletFormatAdapter.SUPPORTED_FORMATS;
258 if (FormatAdapter.isValidFormat(format))
264 // read from the provided source
265 al = new FormatAdapter().readFromFile(source, format);
270 // open a new source and read from it
271 FormatAdapter fa = new FormatAdapter();
272 al = fa.readFile(file, protocol, format);
273 source = fa.afile; // keep reference for later if necessary.
275 } catch (java.io.IOException ex)
277 error = ex.getMessage();
282 if (format != null && format.length() > 7)
284 // ad hoc message in format.
285 error = format + "\n" + error;
289 if ((al != null) && (al.getHeight() > 0))
291 if (viewport != null)
293 for (int i = 0; i < al.getHeight(); i++)
295 viewport.getAlignment().addSequence(al.getSequenceAt(i));
297 viewport.firePropertyChange("alignment", null, viewport
298 .getAlignment().getSequences());
303 alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
304 AlignFrame.DEFAULT_HEIGHT);
306 alignFrame.statusBar.setText("Successfully loaded file "
309 if (!protocol.equals(AppletFormatAdapter.PASTE))
310 alignFrame.setFileName(file, format);
312 Desktop.addInternalFrame(alignFrame, title,
313 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
317 alignFrame.setMaximum(jalview.bin.Cache.getDefault(
318 "SHOW_FULLSCREEN", false));
319 } catch (java.beans.PropertyVetoException ex)
326 if (Desktop.instance != null)
328 Desktop.instance.stopLoading();
331 final String errorMessage = "Couldn't load file " + title + "\n"
335 javax.swing.SwingUtilities.invokeLater(new Runnable()
339 JOptionPane.showInternalMessageDialog(Desktop.desktop,
340 errorMessage, "Error loading file",
341 JOptionPane.WARNING_MESSAGE);
347 System.err.println(errorMessage);
352 updateRecentlyOpened();
357 System.err.println("Exception whilst opening file '"+file);
358 er.printStackTrace();
361 javax.swing.SwingUtilities.invokeLater(new Runnable()
365 javax.swing.JOptionPane
366 .showInternalMessageDialog(
368 "Encountered problems opening "
372 javax.swing.JOptionPane.WARNING_MESSAGE);
378 catch (OutOfMemoryError er)
381 er.printStackTrace();
385 javax.swing.SwingUtilities.invokeLater(new Runnable()
389 javax.swing.JOptionPane
390 .showInternalMessageDialog(
392 "Out of memory loading file "
395 + "\nSee help files for increasing Java Virtual Machine memory.",
397 javax.swing.JOptionPane.WARNING_MESSAGE);
403 System.err.println("Out of memory loading file " + file + "!!");
406 loadtime += System.currentTimeMillis();
408 if (Desktop.instance != null)
410 Desktop.instance.stopLoading();
413 memused = memused - (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // difference in free memory after load
414 if (alignFrame != null)
416 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
418 AlignmentI al = alignFrame.getViewport().getAlignment();
420 System.out.println("Loaded '" + title + "' in "
421 + (loadtime / 1000.0) + "s, took an additional "
422 + (1.0 * memused / (1024.0 * 1024.0)) + " MB ("
423 + al.getHeight() + " seqs by " + al.getWidth() + " cols)");
432 * @see java.lang.Object#finalize()
434 protected void finalize() throws Throwable