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/>.
24 import jalview.datamodel.*;
26 import jalview.util.MessageManager;
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;
51 * default constructor always raised errors in GUI dialog boxes
59 * construct a Fileloader that may raise errors non-interactively
62 * true if errors are to be raised as GUI dialog boxes
64 public FileLoader(boolean raiseGUI)
66 this.raiseGUI = raiseGUI;
69 public void LoadFile(AlignViewport viewport, String file,
70 String protocol, String format)
72 this.viewport = viewport;
73 LoadFile(file, protocol, format);
76 public void LoadFile(String file, String protocol, String format)
79 this.protocol = protocol;
82 final Thread loader = new Thread(this);
84 SwingUtilities.invokeLater(new Runnable()
94 * Load a (file, protocol) source of unknown type
99 public void LoadFile(String file, String protocol)
101 LoadFile(file, protocol, null);
105 * Load alignment from (file, protocol) and wait till loaded
109 * @return alignFrame constructed from file contents
111 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol)
113 return LoadFileWaitTillLoaded(file, protocol, null);
117 * Load alignment from (file, protocol) of type format and wait till loaded
122 * @return alignFrame constructed from file contents
124 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
128 this.protocol = protocol;
129 this.format = format;
130 return _LoadFileWaitTillLoaded();
134 * Load alignment from FileParse source of type format and wait till loaded
138 * @return alignFrame constructed from file contents
140 public AlignFrame LoadFileWaitTillLoaded(FileParse source, String format)
142 this.source = source;
143 file = source.getInFile();
144 protocol = source.type;
145 this.format = format;
146 return _LoadFileWaitTillLoaded();
150 * start thread and wait until finished, then return the alignFrame that's
151 * (hopefully) been read.
155 protected AlignFrame _LoadFileWaitTillLoaded()
157 Thread loader = new Thread(this);
160 while (loader.isAlive())
165 } catch (Exception ex)
173 public void updateRecentlyOpened()
175 Vector recent = new Vector();
176 if (protocol.equals(FormatAdapter.PASTE))
178 // do nothing if the file was pasted in as text... there is no filename to
182 String type = protocol.equals(FormatAdapter.FILE) ? "RECENT_FILE"
185 String historyItems = jalview.bin.Cache.getProperty(type);
189 if (historyItems != null)
191 st = new StringTokenizer(historyItems, "\t");
193 while (st.hasMoreTokens())
195 recent.addElement(st.nextElement().toString().trim());
199 if (recent.contains(file))
204 StringBuffer newHistory = new StringBuffer(file);
205 for (int i = 0; i < recent.size() && i < 10; i++)
207 newHistory.append("\t");
208 newHistory.append(recent.elementAt(i));
211 jalview.bin.Cache.setProperty(type, newHistory.toString());
213 if (protocol.equals(FormatAdapter.FILE))
215 jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
221 String title = protocol.equals(AppletFormatAdapter.PASTE) ? "Copied From Clipboard"
223 Runtime rt = Runtime.getRuntime();
226 if (Desktop.instance != null)
228 Desktop.instance.startLoading(file);
232 // just in case the caller didn't identify the file for us
235 format = new IdentifyFile().Identify(source, false); // identify
242 format = new IdentifyFile().Identify(file, protocol);
245 // TODO: cache any stream datasources as a temporary file (eg. PDBs
246 // retrieved via URL)
247 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
250 memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // free
255 loadtime = -System.currentTimeMillis();
258 if (format.equalsIgnoreCase("Jalview"))
262 // Tell the user (developer?) that this is going to cause a problem
264 .println("IMPLEMENTATION ERROR: Cannot read consecutive Jalview XML projects from a stream.");
265 // We read the data anyway - it might make sense.
267 alignFrame = new Jalview2XML(raiseGUI).LoadJalviewAlign(file);
271 String error = AppletFormatAdapter.SUPPORTED_FORMATS;
272 if (FormatAdapter.isValidFormat(format))
278 // read from the provided source
279 al = new FormatAdapter().readFromFile(source, format);
284 // open a new source and read from it
285 FormatAdapter fa = new FormatAdapter();
286 al = fa.readFile(file, protocol, format);
287 source = fa.afile; // keep reference for later if necessary.
289 } catch (java.io.IOException ex)
291 error = ex.getMessage();
296 if (format != null && format.length() > 7)
298 // ad hoc message in format.
299 error = format + "\n" + error;
303 if ((al != null) && (al.getHeight() > 0))
305 if (viewport != null)
307 // TODO: create undo object for this JAL-1101
308 for (int i = 0; i < al.getHeight(); i++)
310 viewport.getAlignment().addSequence(al.getSequenceAt(i));
312 viewport.firePropertyChange("alignment", null, viewport
313 .getAlignment().getSequences());
318 alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
319 AlignFrame.DEFAULT_HEIGHT);
321 alignFrame.statusBar.setText(MessageManager.formatMessage("label.successfully_loaded_file", new String[]{title}));
323 if (!protocol.equals(AppletFormatAdapter.PASTE))
324 alignFrame.setFileName(file, format);
327 // add the window to the GUI
328 // note - this actually should happen regardless of raiseGUI status in Jalview 3
329 // TODO: define 'virtual desktop' for benefit of headless scripts that perform queries to find the 'current working alignment'
330 Desktop.addInternalFrame(alignFrame, title,
331 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
336 alignFrame.setMaximum(jalview.bin.Cache.getDefault(
337 "SHOW_FULLSCREEN", false));
338 } catch (java.beans.PropertyVetoException ex)
345 if (Desktop.instance != null)
347 Desktop.instance.stopLoading();
350 final String errorMessage = "Couldn't load file " + title + "\n"
354 javax.swing.SwingUtilities.invokeLater(new Runnable()
358 JOptionPane.showInternalMessageDialog(Desktop.desktop,
359 errorMessage, MessageManager.getString("label.error_loading_file"),
360 JOptionPane.WARNING_MESSAGE);
366 System.err.println(errorMessage);
371 updateRecentlyOpened();
373 } catch (Exception er)
375 System.err.println("Exception whilst opening file '" + file);
376 er.printStackTrace();
379 javax.swing.SwingUtilities.invokeLater(new Runnable()
383 javax.swing.JOptionPane.showInternalMessageDialog(
384 Desktop.desktop, MessageManager.formatMessage("label.problems_opening_file", new String[]{file}),
385 MessageManager.getString("label.file_open_error"),
386 javax.swing.JOptionPane.WARNING_MESSAGE);
391 } catch (OutOfMemoryError er)
394 er.printStackTrace();
398 javax.swing.SwingUtilities.invokeLater(new Runnable()
402 javax.swing.JOptionPane
403 .showInternalMessageDialog(
405 "Out of memory loading file "
408 + "\nSee help files for increasing Java Virtual Machine memory.",
410 javax.swing.JOptionPane.WARNING_MESSAGE);
414 System.err.println("Out of memory loading file " + file + "!!");
417 loadtime += System.currentTimeMillis();
418 // TODO: Estimate percentage of memory used by a newly loaded alignment -
419 // warn if more memory will be needed to work with it
422 - (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // difference
427 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
429 if (alignFrame != null)
431 AlignmentI al = alignFrame.getViewport().getAlignment();
433 System.out.println("Loaded '" + title + "' in "
434 + (loadtime / 1000.0) + "s, took an additional "
435 + (1.0 * memused / (1024.0 * 1024.0)) + " MB ("
436 + al.getHeight() + " seqs by " + al.getWidth() + " cols)");
440 // report that we didn't load anything probably due to an out of memory
442 System.out.println("Failed to load '" + title + "' in "
443 + (loadtime / 1000.0) + "s, took an additional "
444 + (1.0 * memused / (1024.0 * 1024.0))
445 + " MB (alignment is null)");
448 // remove the visual delay indicator
449 if (Desktop.instance != null)
451 Desktop.instance.stopLoading();
459 * @see java.lang.Object#finalize()
461 protected void finalize() throws Throwable