2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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();
177 String type = protocol.equals(FormatAdapter.FILE) ? "RECENT_FILE"
180 String historyItems = jalview.bin.Cache.getProperty(type);
184 if (historyItems != null)
186 st = new StringTokenizer(historyItems, "\t");
188 while (st.hasMoreTokens())
190 recent.addElement(st.nextElement().toString().trim());
194 if (recent.contains(file))
199 StringBuffer newHistory = new StringBuffer(file);
200 for (int i = 0; i < recent.size() && i < 10; i++)
202 newHistory.append("\t");
203 newHistory.append(recent.elementAt(i));
206 jalview.bin.Cache.setProperty(type, newHistory.toString());
208 if (protocol.equals(FormatAdapter.FILE))
210 jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
216 String title = protocol.equals(AppletFormatAdapter.PASTE) ? "Copied From Clipboard"
218 Runtime rt = Runtime.getRuntime();
221 if (Desktop.instance != null)
223 Desktop.instance.startLoading(file);
227 // just in case the caller didn't identify the file for us
230 format = new IdentifyFile().Identify(source, false); // identify
237 format = new IdentifyFile().Identify(file, protocol);
240 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
243 memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // free
248 loadtime = -System.currentTimeMillis();
251 if (format.equalsIgnoreCase("Jalview"))
255 // Tell the user (developer?) that this is going to cause a problem
257 .println("IMPLEMENTATION ERROR: Cannot read consecutive Jalview XML projects from a stream.");
258 // We read the data anyway - it might make sense.
260 alignFrame = new Jalview2XML(raiseGUI).LoadJalviewAlign(file);
264 String error = AppletFormatAdapter.SUPPORTED_FORMATS;
265 if (FormatAdapter.isValidFormat(format))
271 // read from the provided source
272 al = new FormatAdapter().readFromFile(source, format);
277 // open a new source and read from it
278 FormatAdapter fa = new FormatAdapter();
279 al = fa.readFile(file, protocol, format);
280 source = fa.afile; // keep reference for later if necessary.
282 } catch (java.io.IOException ex)
284 error = ex.getMessage();
289 if (format != null && format.length() > 7)
291 // ad hoc message in format.
292 error = format + "\n" + error;
296 if ((al != null) && (al.getHeight() > 0))
298 if (viewport != null)
300 for (int i = 0; i < al.getHeight(); i++)
302 viewport.getAlignment().addSequence(al.getSequenceAt(i));
304 viewport.firePropertyChange("alignment", null, viewport
305 .getAlignment().getSequences());
310 alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
311 AlignFrame.DEFAULT_HEIGHT);
313 alignFrame.statusBar.setText("Successfully loaded file "
316 if (!protocol.equals(AppletFormatAdapter.PASTE))
317 alignFrame.setFileName(file, format);
319 Desktop.addInternalFrame(alignFrame, title,
320 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
324 alignFrame.setMaximum(jalview.bin.Cache.getDefault(
325 "SHOW_FULLSCREEN", false));
326 } catch (java.beans.PropertyVetoException ex)
333 if (Desktop.instance != null)
335 Desktop.instance.stopLoading();
338 final String errorMessage = "Couldn't load file " + title + "\n"
342 javax.swing.SwingUtilities.invokeLater(new Runnable()
346 JOptionPane.showInternalMessageDialog(Desktop.desktop,
347 errorMessage, "Error loading file",
348 JOptionPane.WARNING_MESSAGE);
354 System.err.println(errorMessage);
359 updateRecentlyOpened();
361 } catch (Exception er)
363 System.err.println("Exception whilst opening file '" + file);
364 er.printStackTrace();
367 javax.swing.SwingUtilities.invokeLater(new Runnable()
371 javax.swing.JOptionPane.showInternalMessageDialog(
372 Desktop.desktop, "Encountered problems opening " + file
373 + "!!", "File open error",
374 javax.swing.JOptionPane.WARNING_MESSAGE);
379 } catch (OutOfMemoryError er)
382 er.printStackTrace();
386 javax.swing.SwingUtilities.invokeLater(new Runnable()
390 javax.swing.JOptionPane
391 .showInternalMessageDialog(
393 "Out of memory loading file "
396 + "\nSee help files for increasing Java Virtual Machine memory.",
398 javax.swing.JOptionPane.WARNING_MESSAGE);
402 System.err.println("Out of memory loading file " + file + "!!");
405 loadtime += System.currentTimeMillis();
406 // TODO: Estimate percentage of memory used by a newly loaded alignment -
407 // warn if more memory will be needed to work with it
410 - (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // difference
415 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
417 if (alignFrame != null)
419 AlignmentI al = alignFrame.getViewport().getAlignment();
421 System.out.println("Loaded '" + title + "' in "
422 + (loadtime / 1000.0) + "s, took an additional "
423 + (1.0 * memused / (1024.0 * 1024.0)) + " MB ("
424 + al.getHeight() + " seqs by " + al.getWidth() + " cols)");
428 // report that we didn't load anything probably due to an out of memory
430 System.out.println("Failed to load '" + title + "' in "
431 + (loadtime / 1000.0) + "s, took an additional "
432 + (1.0 * memused / (1024.0 * 1024.0))
433 + " MB (alignment is null)");
436 // remove the visual delay indicator
437 if (Desktop.instance != null)
439 Desktop.instance.stopLoading();
447 * @see java.lang.Object#finalize()
449 protected void finalize() throws Throwable