2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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/>.
20 import jalview.datamodel.Alignment;
21 import jalview.datamodel.AlignmentI;
22 import jalview.gui.AlignFrame;
23 import jalview.gui.AlignViewport;
24 import jalview.gui.Desktop;
25 import jalview.gui.Jalview2XML;
27 import java.util.StringTokenizer;
28 import java.util.Vector;
30 import javax.swing.JOptionPane;
31 import javax.swing.SwingUtilities;
33 public class FileLoader implements Runnable
41 FileParse source = null; // alternative specification of where data comes
45 AlignViewport viewport;
47 AlignFrame alignFrame;
53 boolean raiseGUI = true;
56 * default constructor always raised errors in GUI dialog boxes
64 * construct a Fileloader that may raise errors non-interactively
67 * true if errors are to be raised as GUI dialog boxes
69 public FileLoader(boolean raiseGUI)
71 this.raiseGUI = raiseGUI;
74 public void LoadFile(AlignViewport viewport, String file,
75 String protocol, String format)
77 this.viewport = viewport;
78 LoadFile(file, protocol, format);
81 public void LoadFile(String file, String protocol, String format)
84 this.protocol = protocol;
87 final Thread loader = new Thread(this);
89 SwingUtilities.invokeLater(new Runnable()
99 * Load a (file, protocol) source of unknown type
104 public void LoadFile(String file, String protocol)
106 LoadFile(file, protocol, null);
110 * Load alignment from (file, protocol) and wait till loaded
114 * @return alignFrame constructed from file contents
116 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol)
118 return LoadFileWaitTillLoaded(file, protocol, null);
122 * Load alignment from (file, protocol) of type format and wait till loaded
127 * @return alignFrame constructed from file contents
129 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
133 this.protocol = protocol;
134 this.format = format;
135 return _LoadFileWaitTillLoaded();
139 * Load alignment from FileParse source of type format and wait till loaded
143 * @return alignFrame constructed from file contents
145 public AlignFrame LoadFileWaitTillLoaded(FileParse source, String format)
147 this.source = source;
148 file = source.getInFile();
149 protocol = source.type;
150 this.format = format;
151 return _LoadFileWaitTillLoaded();
155 * start thread and wait until finished, then return the alignFrame that's
156 * (hopefully) been read.
160 protected AlignFrame _LoadFileWaitTillLoaded()
162 Thread loader = new Thread(this);
165 while (loader.isAlive())
170 } catch (Exception ex)
178 public void updateRecentlyOpened()
180 Vector recent = new Vector();
181 if (protocol.equals(FormatAdapter.PASTE))
183 // do nothing if the file was pasted in as text... there is no filename to
187 String type = protocol.equals(FormatAdapter.FILE) ? "RECENT_FILE"
190 String historyItems = jalview.bin.Cache.getProperty(type);
194 if (historyItems != null)
196 st = new StringTokenizer(historyItems, "\t");
198 while (st.hasMoreTokens())
200 recent.addElement(st.nextElement().toString().trim());
204 if (recent.contains(file))
209 StringBuffer newHistory = new StringBuffer(file);
210 for (int i = 0; i < recent.size() && i < 10; i++)
212 newHistory.append("\t");
213 newHistory.append(recent.elementAt(i));
216 jalview.bin.Cache.setProperty(type, newHistory.toString());
218 if (protocol.equals(FormatAdapter.FILE))
220 jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
226 String title = protocol.equals(AppletFormatAdapter.PASTE) ? "Copied From Clipboard"
228 Runtime rt = Runtime.getRuntime();
231 if (Desktop.instance != null)
233 Desktop.instance.startLoading(file);
237 // just in case the caller didn't identify the file for us
240 format = new IdentifyFile().Identify(source, false); // identify
247 format = new IdentifyFile().Identify(file, protocol);
250 // TODO: cache any stream datasources as a temporary file (eg. PDBs
251 // retrieved via URL)
252 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
255 memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // free
260 loadtime = -System.currentTimeMillis();
261 AlignmentI al = null;
263 if (format.equalsIgnoreCase("Jalview"))
267 // Tell the user (developer?) that this is going to cause a problem
269 .println("IMPLEMENTATION ERROR: Cannot read consecutive Jalview XML projects from a stream.");
270 // We read the data anyway - it might make sense.
272 alignFrame = new Jalview2XML(raiseGUI).LoadJalviewAlign(file);
276 String error = AppletFormatAdapter.SUPPORTED_FORMATS;
277 if (FormatAdapter.isValidFormat(format))
283 // read from the provided source
284 al = new FormatAdapter().readFromFile(source, format);
289 // open a new source and read from it
290 FormatAdapter fa = new FormatAdapter();
291 al = fa.readFile(file, protocol, format);
292 source = fa.afile; // keep reference for later if necessary.
294 } catch (java.io.IOException ex)
296 error = ex.getMessage();
301 if (format != null && format.length() > 7)
303 // ad hoc message in format.
304 error = format + "\n" + error;
308 if ((al != null) && (al.getHeight() > 0))
310 if (viewport != null)
312 // TODO: create undo object for this JAL-1101
313 for (int i = 0; i < al.getHeight(); i++)
315 viewport.getAlignment().addSequence(al.getSequenceAt(i));
317 viewport.firePropertyChange("alignment", null, viewport
318 .getAlignment().getSequences());
323 alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
324 AlignFrame.DEFAULT_HEIGHT);
326 alignFrame.statusBar.setText("Successfully loaded file "
329 if (!protocol.equals(AppletFormatAdapter.PASTE))
330 alignFrame.setFileName(file, format);
333 // add the window to the GUI
334 // note - this actually should happen regardless of raiseGUI status in Jalview 3
335 // TODO: define 'virtual desktop' for benefit of headless scripts that perform queries to find the 'current working alignment'
336 Desktop.addInternalFrame(alignFrame, title,
337 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
342 alignFrame.setMaximum(jalview.bin.Cache.getDefault(
343 "SHOW_FULLSCREEN", false));
344 } catch (java.beans.PropertyVetoException ex)
351 if (Desktop.instance != null)
353 Desktop.instance.stopLoading();
356 final String errorMessage = "Couldn't load file " + title + "\n"
360 javax.swing.SwingUtilities.invokeLater(new Runnable()
364 JOptionPane.showInternalMessageDialog(Desktop.desktop,
365 errorMessage, "Error loading file",
366 JOptionPane.WARNING_MESSAGE);
372 System.err.println(errorMessage);
377 updateRecentlyOpened();
379 } catch (Exception er)
381 System.err.println("Exception whilst opening file '" + file);
382 er.printStackTrace();
385 javax.swing.SwingUtilities.invokeLater(new Runnable()
389 javax.swing.JOptionPane.showInternalMessageDialog(
390 Desktop.desktop, "Encountered problems opening " + file
391 + "!!", "File open error",
392 javax.swing.JOptionPane.WARNING_MESSAGE);
397 } catch (OutOfMemoryError er)
400 er.printStackTrace();
404 javax.swing.SwingUtilities.invokeLater(new Runnable()
408 javax.swing.JOptionPane
409 .showInternalMessageDialog(
411 "Out of memory loading file "
414 + "\nSee help files for increasing Java Virtual Machine memory.",
416 javax.swing.JOptionPane.WARNING_MESSAGE);
420 System.err.println("Out of memory loading file " + file + "!!");
423 loadtime += System.currentTimeMillis();
424 // TODO: Estimate percentage of memory used by a newly loaded alignment -
425 // warn if more memory will be needed to work with it
428 - (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // difference
433 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
435 if (alignFrame != null)
437 AlignmentI al = alignFrame.getViewport().getAlignment();
439 System.out.println("Loaded '" + title + "' in "
440 + (loadtime / 1000.0) + "s, took an additional "
441 + (1.0 * memused / (1024.0 * 1024.0)) + " MB ("
442 + al.getHeight() + " seqs by " + al.getWidth() + " cols)");
446 // report that we didn't load anything probably due to an out of memory
448 System.out.println("Failed to load '" + title + "' in "
449 + (loadtime / 1000.0) + "s, took an additional "
450 + (1.0 * memused / (1024.0 * 1024.0))
451 + " MB (alignment is null)");
454 // remove the visual delay indicator
455 if (Desktop.instance != null)
457 Desktop.instance.stopLoading();
465 * @see java.lang.Object#finalize()
467 protected void finalize() throws Throwable