2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
24 import jalview.datamodel.*;
27 public class FileLoader implements Runnable
35 FileParse source = null; // alternative specification of where data comes
39 AlignViewport viewport;
41 AlignFrame alignFrame;
47 boolean raiseGUI = true;
50 * default constructor always raised errors in GUI dialog boxes
58 * construct a Fileloader that may raise errors non-interactively
61 * true if errors are to be raised as GUI dialog boxes
63 public FileLoader(boolean raiseGUI)
65 this.raiseGUI = raiseGUI;
68 public void LoadFile(AlignViewport viewport, String file,
69 String protocol, String format)
71 this.viewport = viewport;
72 LoadFile(file, protocol, format);
75 public void LoadFile(String file, String protocol, String format)
78 this.protocol = protocol;
81 final Thread loader = new Thread(this);
83 SwingUtilities.invokeLater(new Runnable()
93 * Load a (file, protocol) source of unknown type
98 public void LoadFile(String file, String protocol)
100 LoadFile(file, protocol, null);
104 * Load alignment from (file, protocol) and wait till loaded
108 * @return alignFrame constructed from file contents
110 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol)
112 return LoadFileWaitTillLoaded(file, protocol, null);
116 * Load alignment from (file, protocol) of type format and wait till loaded
121 * @return alignFrame constructed from file contents
123 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
127 this.protocol = protocol;
128 this.format = format;
129 return _LoadFileWaitTillLoaded();
133 * Load alignment from FileParse source of type format and wait till loaded
137 * @return alignFrame constructed from file contents
139 public AlignFrame LoadFileWaitTillLoaded(FileParse source, String format)
141 this.source = source;
142 file = source.getInFile();
143 protocol = source.type;
144 this.format = format;
145 return _LoadFileWaitTillLoaded();
149 * start thread and wait until finished, then return the alignFrame that's
150 * (hopefully) been read.
154 protected AlignFrame _LoadFileWaitTillLoaded()
156 Thread loader = new Thread(this);
159 while (loader.isAlive())
164 } catch (Exception ex)
172 public void updateRecentlyOpened()
174 Vector recent = new Vector();
175 if (protocol.equals(FormatAdapter.PASTE))
177 // do nothing if the file was pasted in as text... there is no filename to
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 // TODO: cache any stream datasources as a temporary file (eg. PDBs
245 // retrieved via URL)
246 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
249 memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // free
254 loadtime = -System.currentTimeMillis();
257 if (format.equalsIgnoreCase("Jalview"))
261 // Tell the user (developer?) that this is going to cause a problem
263 .println("IMPLEMENTATION ERROR: Cannot read consecutive Jalview XML projects from a stream.");
264 // We read the data anyway - it might make sense.
266 alignFrame = new Jalview2XML(raiseGUI).LoadJalviewAlign(file);
270 String error = AppletFormatAdapter.SUPPORTED_FORMATS;
271 if (FormatAdapter.isValidFormat(format))
277 // read from the provided source
278 al = new FormatAdapter().readFromFile(source, format);
283 // open a new source and read from it
284 FormatAdapter fa = new FormatAdapter();
285 al = fa.readFile(file, protocol, format);
286 source = fa.afile; // keep reference for later if necessary.
288 } catch (java.io.IOException ex)
290 error = ex.getMessage();
295 if (format != null && format.length() > 7)
297 // ad hoc message in format.
298 error = format + "\n" + error;
302 if ((al != null) && (al.getHeight() > 0))
304 if (viewport != null)
306 // TODO: create undo object for this JAL-1101
307 for (int i = 0; i < al.getHeight(); i++)
309 viewport.getAlignment().addSequence(al.getSequenceAt(i));
311 viewport.firePropertyChange("alignment", null, viewport
312 .getAlignment().getSequences());
317 alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
318 AlignFrame.DEFAULT_HEIGHT);
320 alignFrame.statusBar.setText("Successfully loaded file "
323 if (!protocol.equals(AppletFormatAdapter.PASTE))
324 alignFrame.setFileName(file, format);
326 Desktop.addInternalFrame(alignFrame, title,
327 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
331 alignFrame.setMaximum(jalview.bin.Cache.getDefault(
332 "SHOW_FULLSCREEN", false));
333 } catch (java.beans.PropertyVetoException ex)
340 if (Desktop.instance != null)
342 Desktop.instance.stopLoading();
345 final String errorMessage = "Couldn't load file " + title + "\n"
349 javax.swing.SwingUtilities.invokeLater(new Runnable()
353 JOptionPane.showInternalMessageDialog(Desktop.desktop,
354 errorMessage, "Error loading file",
355 JOptionPane.WARNING_MESSAGE);
361 System.err.println(errorMessage);
366 updateRecentlyOpened();
368 } catch (Exception er)
370 System.err.println("Exception whilst opening file '" + file);
371 er.printStackTrace();
374 javax.swing.SwingUtilities.invokeLater(new Runnable()
378 javax.swing.JOptionPane.showInternalMessageDialog(
379 Desktop.desktop, "Encountered problems opening " + file
380 + "!!", "File open error",
381 javax.swing.JOptionPane.WARNING_MESSAGE);
386 } catch (OutOfMemoryError er)
389 er.printStackTrace();
393 javax.swing.SwingUtilities.invokeLater(new Runnable()
397 javax.swing.JOptionPane
398 .showInternalMessageDialog(
400 "Out of memory loading file "
403 + "\nSee help files for increasing Java Virtual Machine memory.",
405 javax.swing.JOptionPane.WARNING_MESSAGE);
409 System.err.println("Out of memory loading file " + file + "!!");
412 loadtime += System.currentTimeMillis();
413 // TODO: Estimate percentage of memory used by a newly loaded alignment -
414 // warn if more memory will be needed to work with it
417 - (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // difference
422 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
424 if (alignFrame != null)
426 AlignmentI al = alignFrame.getViewport().getAlignment();
428 System.out.println("Loaded '" + title + "' in "
429 + (loadtime / 1000.0) + "s, took an additional "
430 + (1.0 * memused / (1024.0 * 1024.0)) + " MB ("
431 + al.getHeight() + " seqs by " + al.getWidth() + " cols)");
435 // report that we didn't load anything probably due to an out of memory
437 System.out.println("Failed to load '" + title + "' in "
438 + (loadtime / 1000.0) + "s, took an additional "
439 + (1.0 * memused / (1024.0 * 1024.0))
440 + " MB (alignment is null)");
443 // remove the visual delay indicator
444 if (Desktop.instance != null)
446 Desktop.instance.stopLoading();
454 * @see java.lang.Object#finalize()
456 protected void finalize() throws Throwable