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;
50 * default constructor always raised errors in GUI dialog boxes
56 * construct a Fileloader that may raise errors non-interactively
57 * @param raiseGUI true if errors are to be raised as GUI dialog boxes
59 public FileLoader(boolean raiseGUI)
61 this.raiseGUI = raiseGUI;
64 public void LoadFile(AlignViewport viewport, String file,
65 String protocol, String format)
67 this.viewport = viewport;
68 LoadFile(file, protocol, format);
71 public void LoadFile(String file, String protocol, String format)
74 this.protocol = protocol;
77 final Thread loader = new Thread(this);
79 SwingUtilities.invokeLater(new Runnable()
89 * Load a (file, protocol) source of unknown type
94 public void LoadFile(String file, String protocol)
96 LoadFile(file, protocol, null);
100 * Load alignment from (file, protocol) and wait till loaded
104 * @return alignFrame constructed from file contents
106 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol)
108 return LoadFileWaitTillLoaded(file, protocol, null);
112 * Load alignment from (file, protocol) of type format and wait till loaded
117 * @return alignFrame constructed from file contents
119 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
123 this.protocol = protocol;
124 this.format = format;
125 return _LoadFileWaitTillLoaded();
129 * Load alignment from FileParse source of type format and wait till loaded
133 * @return alignFrame constructed from file contents
135 public AlignFrame LoadFileWaitTillLoaded(FileParse source, String format)
137 this.source = source;
138 file = source.getInFile();
139 protocol = source.type;
140 this.format = format;
141 return _LoadFileWaitTillLoaded();
145 * start thread and wait until finished, then return the alignFrame that's
146 * (hopefully) been read.
150 protected AlignFrame _LoadFileWaitTillLoaded()
152 Thread loader = new Thread(this);
155 while (loader.isAlive())
160 } catch (Exception ex)
168 public void updateRecentlyOpened()
170 Vector recent = new Vector();
172 String type = protocol.equals(FormatAdapter.FILE) ? "RECENT_FILE"
175 String historyItems = jalview.bin.Cache.getProperty(type);
179 if (historyItems != null)
181 st = new StringTokenizer(historyItems, "\t");
183 while (st.hasMoreTokens())
185 recent.addElement(st.nextElement().toString().trim());
189 if (recent.contains(file))
194 StringBuffer newHistory = new StringBuffer(file);
195 for (int i = 0; i < recent.size() && i < 10; i++)
197 newHistory.append("\t");
198 newHistory.append(recent.elementAt(i));
201 jalview.bin.Cache.setProperty(type, newHistory.toString());
203 if (protocol.equals(FormatAdapter.FILE))
205 jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
211 String title = protocol.equals(AppletFormatAdapter.PASTE) ? "Copied From Clipboard"
213 Runtime rt = Runtime.getRuntime();
216 if (Desktop.instance != null)
218 Desktop.instance.startLoading(file);
222 // just in case the caller didn't identify the file for us
225 format = new IdentifyFile().Identify(source, false); // identify
232 format = new IdentifyFile().Identify(file, protocol);
235 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
238 memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // free memory before load
240 loadtime = -System.currentTimeMillis();
243 if (format.equalsIgnoreCase("Jalview"))
247 // Tell the user (developer?) that this is going to cause a problem
249 .println("IMPLEMENTATION ERROR: Cannot read consecutive Jalview XML projects from a stream.");
250 // We read the data anyway - it might make sense.
252 alignFrame = new Jalview2XML(raiseGUI).LoadJalviewAlign(file);
256 String error = AppletFormatAdapter.SUPPORTED_FORMATS;
257 if (FormatAdapter.isValidFormat(format))
263 // read from the provided source
264 al = new FormatAdapter().readFromFile(source, format);
269 // open a new source and read from it
270 FormatAdapter fa = new FormatAdapter();
271 al = fa.readFile(file, protocol, format);
272 source = fa.afile; // keep reference for later if necessary.
274 } catch (java.io.IOException ex)
276 error = ex.getMessage();
281 if (format != null && format.length() > 7)
283 // ad hoc message in format.
284 error = format + "\n" + error;
288 if ((al != null) && (al.getHeight() > 0))
290 if (viewport != null)
292 for (int i = 0; i < al.getHeight(); i++)
294 viewport.getAlignment().addSequence(al.getSequenceAt(i));
296 viewport.firePropertyChange("alignment", null, viewport
297 .getAlignment().getSequences());
302 alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
303 AlignFrame.DEFAULT_HEIGHT);
305 alignFrame.statusBar.setText("Successfully loaded file "
308 if (!protocol.equals(AppletFormatAdapter.PASTE))
309 alignFrame.setFileName(file, format);
311 Desktop.addInternalFrame(alignFrame, title,
312 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
316 alignFrame.setMaximum(jalview.bin.Cache.getDefault(
317 "SHOW_FULLSCREEN", false));
318 } catch (java.beans.PropertyVetoException ex)
325 if (Desktop.instance != null)
327 Desktop.instance.stopLoading();
330 final String errorMessage = "Couldn't load file " + title + "\n"
334 javax.swing.SwingUtilities.invokeLater(new Runnable()
338 JOptionPane.showInternalMessageDialog(Desktop.desktop,
339 errorMessage, "Error loading file",
340 JOptionPane.WARNING_MESSAGE);
346 System.err.println(errorMessage);
351 updateRecentlyOpened();
356 System.err.println("Exception whilst opening file '"+file);
357 er.printStackTrace();
360 javax.swing.SwingUtilities.invokeLater(new Runnable()
364 javax.swing.JOptionPane
365 .showInternalMessageDialog(
367 "Encountered problems opening "
371 javax.swing.JOptionPane.WARNING_MESSAGE);
377 catch (OutOfMemoryError er)
380 er.printStackTrace();
384 javax.swing.SwingUtilities.invokeLater(new Runnable()
388 javax.swing.JOptionPane
389 .showInternalMessageDialog(
391 "Out of memory loading file "
394 + "\nSee help files for increasing Java Virtual Machine memory.",
396 javax.swing.JOptionPane.WARNING_MESSAGE);
402 System.err.println("Out of memory loading file " + file + "!!");
405 loadtime += System.currentTimeMillis();
408 memused = memused - (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // difference in free memory after load
409 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
411 if (alignFrame != null)
413 AlignmentI al = alignFrame.getViewport().getAlignment();
415 System.out.println("Loaded '" + title + "' in "
416 + (loadtime / 1000.0) + "s, took an additional "
417 + (1.0 * memused / (1024.0 * 1024.0)) + " MB ("
418 + al.getHeight() + " seqs by " + al.getWidth() + " cols)");
420 // report that we didn't load anything probably due to an out of memory error
421 System.out.println("Failed to load '"+title+"' in "
422 + (loadtime / 1000.0) + "s, took an additional "
423 + (1.0 * memused / (1024.0 * 1024.0)) + " MB (alignment is null)");
426 // remove the visual delay indicator
427 if (Desktop.instance != null)
429 Desktop.instance.stopLoading();
437 * @see java.lang.Object#finalize()
439 protected void finalize() throws Throwable