2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
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 jalview.util.MessageManager;
31 import javax.swing.JOptionPane;
32 import javax.swing.SwingUtilities;
34 public class FileLoader implements Runnable
42 FileParse source = null; // alternative specification of where data comes
46 AlignViewport viewport;
48 AlignFrame alignFrame;
54 boolean raiseGUI = true;
57 * default constructor always raised errors in GUI dialog boxes
65 * construct a Fileloader that may raise errors non-interactively
68 * true if errors are to be raised as GUI dialog boxes
70 public FileLoader(boolean raiseGUI)
72 this.raiseGUI = raiseGUI;
75 public void LoadFile(AlignViewport viewport, String file,
76 String protocol, String format)
78 this.viewport = viewport;
79 LoadFile(file, protocol, format);
82 public void LoadFile(String file, String protocol, String format)
85 this.protocol = protocol;
88 final Thread loader = new Thread(this);
90 SwingUtilities.invokeLater(new Runnable()
100 * Load a (file, protocol) source of unknown type
105 public void LoadFile(String file, String protocol)
107 LoadFile(file, protocol, null);
111 * Load alignment from (file, protocol) and wait till loaded
115 * @return alignFrame constructed from file contents
117 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol)
119 return LoadFileWaitTillLoaded(file, protocol, null);
123 * Load alignment from (file, protocol) of type format and wait till loaded
128 * @return alignFrame constructed from file contents
130 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
134 this.protocol = protocol;
135 this.format = format;
136 return _LoadFileWaitTillLoaded();
140 * Load alignment from FileParse source of type format and wait till loaded
144 * @return alignFrame constructed from file contents
146 public AlignFrame LoadFileWaitTillLoaded(FileParse source, String format)
148 this.source = source;
149 file = source.getInFile();
150 protocol = source.type;
151 this.format = format;
152 return _LoadFileWaitTillLoaded();
156 * start thread and wait until finished, then return the alignFrame that's
157 * (hopefully) been read.
161 protected AlignFrame _LoadFileWaitTillLoaded()
163 Thread loader = new Thread(this);
166 while (loader.isAlive())
171 } catch (Exception ex)
179 public void updateRecentlyOpened()
181 Vector recent = new Vector();
182 if (protocol.equals(FormatAdapter.PASTE))
184 // do nothing if the file was pasted in as text... there is no filename to
188 String type = protocol.equals(FormatAdapter.FILE) ? "RECENT_FILE"
191 String historyItems = jalview.bin.Cache.getProperty(type);
195 if (historyItems != null)
197 st = new StringTokenizer(historyItems, "\t");
199 while (st.hasMoreTokens())
201 recent.addElement(st.nextElement().toString().trim());
205 if (recent.contains(file))
210 StringBuffer newHistory = new StringBuffer(file);
211 for (int i = 0; i < recent.size() && i < 10; i++)
213 newHistory.append("\t");
214 newHistory.append(recent.elementAt(i));
217 jalview.bin.Cache.setProperty(type, newHistory.toString());
219 if (protocol.equals(FormatAdapter.FILE))
221 jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
227 String title = protocol.equals(AppletFormatAdapter.PASTE) ? "Copied From Clipboard"
229 Runtime rt = Runtime.getRuntime();
232 if (Desktop.instance != null)
234 Desktop.instance.startLoading(file);
238 // just in case the caller didn't identify the file for us
241 format = new IdentifyFile().Identify(source, false); // identify
248 format = new IdentifyFile().Identify(file, protocol);
251 // TODO: cache any stream datasources as a temporary file (eg. PDBs
252 // retrieved via URL)
253 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
256 memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // free
261 loadtime = -System.currentTimeMillis();
262 AlignmentI al = null;
264 if (format.equalsIgnoreCase("Jalview"))
268 // Tell the user (developer?) that this is going to cause a problem
270 .println("IMPLEMENTATION ERROR: Cannot read consecutive Jalview XML projects from a stream.");
271 // We read the data anyway - it might make sense.
273 alignFrame = new Jalview2XML(raiseGUI).LoadJalviewAlign(file);
277 String error = AppletFormatAdapter.SUPPORTED_FORMATS;
278 if (FormatAdapter.isValidFormat(format))
284 // read from the provided source
285 al = new FormatAdapter().readFromFile(source, format);
290 // open a new source and read from it
291 FormatAdapter fa = new FormatAdapter();
292 al = fa.readFile(file, protocol, format);
293 source = fa.afile; // keep reference for later if necessary.
295 } catch (java.io.IOException ex)
297 error = ex.getMessage();
302 if (format != null && format.length() > 7)
304 // ad hoc message in format.
305 error = format + "\n" + error;
309 if ((al != null) && (al.getHeight() > 0))
311 if (viewport != null)
313 // TODO: create undo object for this JAL-1101
314 for (int i = 0; i < al.getHeight(); i++)
316 viewport.getAlignment().addSequence(al.getSequenceAt(i));
318 viewport.firePropertyChange("alignment", null, viewport
319 .getAlignment().getSequences());
324 alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
325 AlignFrame.DEFAULT_HEIGHT);
327 alignFrame.statusBar.setText(MessageManager.formatMessage("label.successfully_loaded_file", new String[]{title}));
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, MessageManager.getString("label.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, MessageManager.formatMessage("label.problems_opening_file", new String[]{file}),
391 MessageManager.getString("label.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