2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 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
25 import jalview.datamodel.*;
28 public class FileLoader implements Runnable
36 FileParse source = null; // alternative specification of where data comes
40 AlignViewport viewport;
42 AlignFrame alignFrame;
48 boolean raiseGUI = true;
51 * default constructor always raised errors in GUI dialog boxes
59 * construct a Fileloader that may raise errors non-interactively
62 * true if errors are to be raised as GUI dialog boxes
64 public FileLoader(boolean raiseGUI)
66 this.raiseGUI = raiseGUI;
69 public void LoadFile(AlignViewport viewport, String file,
70 String protocol, String format)
72 this.viewport = viewport;
73 LoadFile(file, protocol, format);
76 public void LoadFile(String file, String protocol, String format)
79 this.protocol = protocol;
82 final Thread loader = new Thread(this);
84 SwingUtilities.invokeLater(new Runnable()
94 * Load a (file, protocol) source of unknown type
99 public void LoadFile(String file, String protocol)
101 LoadFile(file, protocol, null);
105 * Load alignment from (file, protocol) and wait till loaded
109 * @return alignFrame constructed from file contents
111 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol)
113 return LoadFileWaitTillLoaded(file, protocol, null);
117 * Load alignment from (file, protocol) of type format and wait till loaded
122 * @return alignFrame constructed from file contents
124 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
128 this.protocol = protocol;
129 this.format = format;
130 return _LoadFileWaitTillLoaded();
134 * Load alignment from FileParse source of type format and wait till loaded
138 * @return alignFrame constructed from file contents
140 public AlignFrame LoadFileWaitTillLoaded(FileParse source, String format)
142 this.source = source;
143 file = source.getInFile();
144 protocol = source.type;
145 this.format = format;
146 return _LoadFileWaitTillLoaded();
150 * start thread and wait until finished, then return the alignFrame that's
151 * (hopefully) been read.
155 protected AlignFrame _LoadFileWaitTillLoaded()
157 Thread loader = new Thread(this);
160 while (loader.isAlive())
165 } catch (Exception ex)
173 public void updateRecentlyOpened()
175 Vector recent = new Vector();
176 if (protocol.equals(FormatAdapter.PASTE))
178 // do nothing if the file was pasted in as text... there is no filename to refer to it as.
181 String type = protocol.equals(FormatAdapter.FILE) ? "RECENT_FILE"
184 String historyItems = jalview.bin.Cache.getProperty(type);
188 if (historyItems != null)
190 st = new StringTokenizer(historyItems, "\t");
192 while (st.hasMoreTokens())
194 recent.addElement(st.nextElement().toString().trim());
198 if (recent.contains(file))
203 StringBuffer newHistory = new StringBuffer(file);
204 for (int i = 0; i < recent.size() && i < 10; i++)
206 newHistory.append("\t");
207 newHistory.append(recent.elementAt(i));
210 jalview.bin.Cache.setProperty(type, newHistory.toString());
212 if (protocol.equals(FormatAdapter.FILE))
214 jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
220 String title = protocol.equals(AppletFormatAdapter.PASTE) ? "Copied From Clipboard"
222 Runtime rt = Runtime.getRuntime();
225 if (Desktop.instance != null)
227 Desktop.instance.startLoading(file);
231 // just in case the caller didn't identify the file for us
234 format = new IdentifyFile().Identify(source, false); // identify
241 format = new IdentifyFile().Identify(file, protocol);
244 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
247 memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // free
252 loadtime = -System.currentTimeMillis();
255 if (format.equalsIgnoreCase("Jalview"))
259 // Tell the user (developer?) that this is going to cause a problem
261 .println("IMPLEMENTATION ERROR: Cannot read consecutive Jalview XML projects from a stream.");
262 // We read the data anyway - it might make sense.
264 alignFrame = new Jalview2XML(raiseGUI).LoadJalviewAlign(file);
268 String error = AppletFormatAdapter.SUPPORTED_FORMATS;
269 if (FormatAdapter.isValidFormat(format))
275 // read from the provided source
276 al = new FormatAdapter().readFromFile(source, format);
281 // open a new source and read from it
282 FormatAdapter fa = new FormatAdapter();
283 al = fa.readFile(file, protocol, format);
284 source = fa.afile; // keep reference for later if necessary.
286 } catch (java.io.IOException ex)
288 error = ex.getMessage();
293 if (format != null && format.length() > 7)
295 // ad hoc message in format.
296 error = format + "\n" + error;
300 if ((al != null) && (al.getHeight() > 0))
302 if (viewport != null)
304 for (int i = 0; i < al.getHeight(); i++)
306 viewport.getAlignment().addSequence(al.getSequenceAt(i));
308 viewport.firePropertyChange("alignment", null, viewport
309 .getAlignment().getSequences());
314 alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
315 AlignFrame.DEFAULT_HEIGHT);
317 alignFrame.statusBar.setText("Successfully loaded file "
320 if (!protocol.equals(AppletFormatAdapter.PASTE))
321 alignFrame.setFileName(file, format);
323 Desktop.addInternalFrame(alignFrame, title,
324 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
328 alignFrame.setMaximum(jalview.bin.Cache.getDefault(
329 "SHOW_FULLSCREEN", false));
330 } catch (java.beans.PropertyVetoException ex)
337 if (Desktop.instance != null)
339 Desktop.instance.stopLoading();
342 final String errorMessage = "Couldn't load file " + title + "\n"
346 javax.swing.SwingUtilities.invokeLater(new Runnable()
350 JOptionPane.showInternalMessageDialog(Desktop.desktop,
351 errorMessage, "Error loading file",
352 JOptionPane.WARNING_MESSAGE);
358 System.err.println(errorMessage);
363 updateRecentlyOpened();
365 } catch (Exception er)
367 System.err.println("Exception whilst opening file '" + file);
368 er.printStackTrace();
371 javax.swing.SwingUtilities.invokeLater(new Runnable()
375 javax.swing.JOptionPane.showInternalMessageDialog(
376 Desktop.desktop, "Encountered problems opening " + file
377 + "!!", "File open error",
378 javax.swing.JOptionPane.WARNING_MESSAGE);
383 } catch (OutOfMemoryError er)
386 er.printStackTrace();
390 javax.swing.SwingUtilities.invokeLater(new Runnable()
394 javax.swing.JOptionPane
395 .showInternalMessageDialog(
397 "Out of memory loading file "
400 + "\nSee help files for increasing Java Virtual Machine memory.",
402 javax.swing.JOptionPane.WARNING_MESSAGE);
406 System.err.println("Out of memory loading file " + file + "!!");
409 loadtime += System.currentTimeMillis();
410 // TODO: Estimate percentage of memory used by a newly loaded alignment -
411 // warn if more memory will be needed to work with it
414 - (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // difference
419 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
421 if (alignFrame != null)
423 AlignmentI al = alignFrame.getViewport().getAlignment();
425 System.out.println("Loaded '" + title + "' in "
426 + (loadtime / 1000.0) + "s, took an additional "
427 + (1.0 * memused / (1024.0 * 1024.0)) + " MB ("
428 + al.getHeight() + " seqs by " + al.getWidth() + " cols)");
432 // report that we didn't load anything probably due to an out of memory
434 System.out.println("Failed to load '" + title + "' in "
435 + (loadtime / 1000.0) + "s, took an additional "
436 + (1.0 * memused / (1024.0 * 1024.0))
437 + " MB (alignment is null)");
440 // remove the visual delay indicator
441 if (Desktop.instance != null)
443 Desktop.instance.stopLoading();
451 * @see java.lang.Object#finalize()
453 protected void finalize() throws Throwable