2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.datamodel.AlignmentI;
24 import jalview.gui.AlignFrame;
25 import jalview.gui.AlignViewport;
26 import jalview.gui.Desktop;
27 import jalview.gui.Jalview2XML;
29 import java.util.StringTokenizer;
30 import java.util.Vector;
32 import jalview.util.MessageManager;
33 import javax.swing.JOptionPane;
34 import javax.swing.SwingUtilities;
36 public class FileLoader implements Runnable
44 FileParse source = null; // alternative specification of where data comes
48 AlignViewport viewport;
50 AlignFrame alignFrame;
56 boolean raiseGUI = true;
59 * default constructor always raised errors in GUI dialog boxes
67 * construct a Fileloader that may raise errors non-interactively
70 * true if errors are to be raised as GUI dialog boxes
72 public FileLoader(boolean raiseGUI)
74 this.raiseGUI = raiseGUI;
77 public void LoadFile(AlignViewport viewport, String file,
78 String protocol, String format)
80 this.viewport = viewport;
81 LoadFile(file, protocol, format);
84 public void LoadFile(String file, String protocol, String format)
87 this.protocol = protocol;
90 final Thread loader = new Thread(this);
92 SwingUtilities.invokeLater(new Runnable()
102 * Load a (file, protocol) source of unknown type
107 public void LoadFile(String file, String protocol)
109 LoadFile(file, protocol, null);
113 * Load alignment from (file, protocol) and wait till loaded
117 * @return alignFrame constructed from file contents
119 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol)
121 return LoadFileWaitTillLoaded(file, protocol, null);
125 * Load alignment from (file, protocol) of type format and wait till loaded
130 * @return alignFrame constructed from file contents
132 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
136 this.protocol = protocol;
137 this.format = format;
138 return _LoadFileWaitTillLoaded();
142 * Load alignment from FileParse source of type format and wait till loaded
146 * @return alignFrame constructed from file contents
148 public AlignFrame LoadFileWaitTillLoaded(FileParse source, String format)
150 this.source = source;
151 file = source.getInFile();
152 protocol = source.type;
153 this.format = format;
154 return _LoadFileWaitTillLoaded();
158 * start thread and wait until finished, then return the alignFrame that's
159 * (hopefully) been read.
163 protected AlignFrame _LoadFileWaitTillLoaded()
165 Thread loader = new Thread(this);
168 while (loader.isAlive())
173 } catch (Exception ex)
181 public void updateRecentlyOpened()
183 Vector recent = new Vector();
184 if (protocol.equals(FormatAdapter.PASTE))
186 // do nothing if the file was pasted in as text... there is no filename to
190 String type = protocol.equals(FormatAdapter.FILE) ? "RECENT_FILE"
193 String historyItems = jalview.bin.Cache.getProperty(type);
197 if (historyItems != null)
199 st = new StringTokenizer(historyItems, "\t");
201 while (st.hasMoreTokens())
203 recent.addElement(st.nextElement().toString().trim());
207 if (recent.contains(file))
212 StringBuffer newHistory = new StringBuffer(file);
213 for (int i = 0; i < recent.size() && i < 10; i++)
215 newHistory.append("\t");
216 newHistory.append(recent.elementAt(i));
219 jalview.bin.Cache.setProperty(type, newHistory.toString());
221 if (protocol.equals(FormatAdapter.FILE))
223 jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
229 String title = protocol.equals(AppletFormatAdapter.PASTE) ? "Copied From Clipboard"
231 Runtime rt = Runtime.getRuntime();
234 if (Desktop.instance != null)
236 Desktop.instance.startLoading(file);
240 // just in case the caller didn't identify the file for us
243 format = new IdentifyFile().Identify(source, false); // identify
250 format = new IdentifyFile().Identify(file, protocol);
253 // TODO: cache any stream datasources as a temporary file (eg. PDBs
254 // retrieved via URL)
255 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
258 memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // free
263 loadtime = -System.currentTimeMillis();
264 AlignmentI al = null;
266 if (format.equalsIgnoreCase("Jalview"))
270 // Tell the user (developer?) that this is going to cause a problem
272 .println("IMPLEMENTATION ERROR: Cannot read consecutive Jalview XML projects from a stream.");
273 // We read the data anyway - it might make sense.
275 alignFrame = new Jalview2XML(raiseGUI).LoadJalviewAlign(file);
279 String error = AppletFormatAdapter.SUPPORTED_FORMATS;
280 if (FormatAdapter.isValidFormat(format))
286 // read from the provided source
287 al = new FormatAdapter().readFromFile(source, format);
292 // open a new source and read from it
293 FormatAdapter fa = new FormatAdapter();
294 al = fa.readFile(file, protocol, format);
295 source = fa.afile; // keep reference for later if necessary.
297 } catch (java.io.IOException ex)
299 error = ex.getMessage();
304 if (format != null && format.length() > 7)
306 // ad hoc message in format.
307 error = format + "\n" + error;
311 if ((al != null) && (al.getHeight() > 0))
313 if (viewport != null)
315 // TODO: create undo object for this JAL-1101
316 for (int i = 0; i < al.getHeight(); i++)
318 viewport.getAlignment().addSequence(al.getSequenceAt(i));
320 viewport.firePropertyChange("alignment", null, viewport
321 .getAlignment().getSequences());
326 alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
327 AlignFrame.DEFAULT_HEIGHT);
329 alignFrame.statusBar.setText(MessageManager.formatMessage(
330 "label.successfully_loaded_file", new String[]
333 if (!protocol.equals(AppletFormatAdapter.PASTE))
334 alignFrame.setFileName(file, format);
337 // add the window to the GUI
338 // note - this actually should happen regardless of raiseGUI
339 // status in Jalview 3
340 // TODO: define 'virtual desktop' for benefit of headless scripts
341 // that perform queries to find the 'current working alignment'
342 Desktop.addInternalFrame(alignFrame, title,
343 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
348 alignFrame.setMaximum(jalview.bin.Cache.getDefault(
349 "SHOW_FULLSCREEN", false));
350 } catch (java.beans.PropertyVetoException ex)
357 if (Desktop.instance != null)
359 Desktop.instance.stopLoading();
362 final String errorMessage = "Couldn't load file " + title + "\n"
366 javax.swing.SwingUtilities.invokeLater(new Runnable()
370 JOptionPane.showInternalMessageDialog(Desktop.desktop,
371 errorMessage, MessageManager
372 .getString("label.error_loading_file"),
373 JOptionPane.WARNING_MESSAGE);
379 System.err.println(errorMessage);
384 updateRecentlyOpened();
386 } catch (Exception er)
388 System.err.println("Exception whilst opening file '" + file);
389 er.printStackTrace();
392 javax.swing.SwingUtilities.invokeLater(new Runnable()
396 javax.swing.JOptionPane.showInternalMessageDialog(
397 Desktop.desktop, MessageManager.formatMessage(
398 "label.problems_opening_file", new String[]
399 { file }), MessageManager
400 .getString("label.file_open_error"),
401 javax.swing.JOptionPane.WARNING_MESSAGE);
406 } catch (OutOfMemoryError er)
409 er.printStackTrace();
413 javax.swing.SwingUtilities.invokeLater(new Runnable()
417 javax.swing.JOptionPane
418 .showInternalMessageDialog(
420 "Out of memory loading file "
423 + "\nSee help files for increasing Java Virtual Machine memory.",
425 javax.swing.JOptionPane.WARNING_MESSAGE);
429 System.err.println("Out of memory loading file " + file + "!!");
432 loadtime += System.currentTimeMillis();
433 // TODO: Estimate percentage of memory used by a newly loaded alignment -
434 // warn if more memory will be needed to work with it
437 - (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // difference
442 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
444 if (alignFrame != null)
446 AlignmentI al = alignFrame.getViewport().getAlignment();
448 System.out.println("Loaded '" + title + "' in "
449 + (loadtime / 1000.0) + "s, took an additional "
450 + (1.0 * memused / (1024.0 * 1024.0)) + " MB ("
451 + al.getHeight() + " seqs by " + al.getWidth() + " cols)");
455 // report that we didn't load anything probably due to an out of memory
457 System.out.println("Failed to load '" + title + "' in "
458 + (loadtime / 1000.0) + "s, took an additional "
459 + (1.0 * memused / (1024.0 * 1024.0))
460 + " MB (alignment is null)");
463 // remove the visual delay indicator
464 if (Desktop.instance != null)
466 Desktop.instance.stopLoading();
474 * @see java.lang.Object#finalize()
476 protected void finalize() throws Throwable