2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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.
25 import jalview.datamodel.*;
27 import jalview.util.MessageManager;
29 public class FileLoader implements Runnable
37 FileParse source = null; // alternative specification of where data comes
41 AlignViewport viewport;
43 AlignFrame alignFrame;
49 boolean raiseGUI = true;
52 * default constructor always raised errors in GUI dialog boxes
60 * construct a Fileloader that may raise errors non-interactively
63 * true if errors are to be raised as GUI dialog boxes
65 public FileLoader(boolean raiseGUI)
67 this.raiseGUI = raiseGUI;
70 public void LoadFile(AlignViewport viewport, String file,
71 String protocol, String format)
73 this.viewport = viewport;
74 LoadFile(file, protocol, format);
77 public void LoadFile(String file, String protocol, String format)
80 this.protocol = protocol;
83 final Thread loader = new Thread(this);
85 SwingUtilities.invokeLater(new Runnable()
95 * Load a (file, protocol) source of unknown type
100 public void LoadFile(String file, String protocol)
102 LoadFile(file, protocol, null);
106 * Load alignment from (file, protocol) and wait till loaded
110 * @return alignFrame constructed from file contents
112 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol)
114 return LoadFileWaitTillLoaded(file, protocol, null);
118 * Load alignment from (file, protocol) of type format and wait till loaded
123 * @return alignFrame constructed from file contents
125 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
129 this.protocol = protocol;
130 this.format = format;
131 return _LoadFileWaitTillLoaded();
135 * Load alignment from FileParse source of type format and wait till loaded
139 * @return alignFrame constructed from file contents
141 public AlignFrame LoadFileWaitTillLoaded(FileParse source, String format)
143 this.source = source;
144 file = source.getInFile();
145 protocol = source.type;
146 this.format = format;
147 return _LoadFileWaitTillLoaded();
151 * start thread and wait until finished, then return the alignFrame that's
152 * (hopefully) been read.
156 protected AlignFrame _LoadFileWaitTillLoaded()
158 Thread loader = new Thread(this);
161 while (loader.isAlive())
166 } catch (Exception ex)
174 public void updateRecentlyOpened()
176 Vector recent = new Vector();
177 if (protocol.equals(FormatAdapter.PASTE))
179 // do nothing if the file was pasted in as text... there is no filename to
183 String type = protocol.equals(FormatAdapter.FILE) ? "RECENT_FILE"
186 String historyItems = jalview.bin.Cache.getProperty(type);
190 if (historyItems != null)
192 st = new StringTokenizer(historyItems, "\t");
194 while (st.hasMoreTokens())
196 recent.addElement(st.nextElement().toString().trim());
200 if (recent.contains(file))
205 StringBuffer newHistory = new StringBuffer(file);
206 for (int i = 0; i < recent.size() && i < 10; i++)
208 newHistory.append("\t");
209 newHistory.append(recent.elementAt(i));
212 jalview.bin.Cache.setProperty(type, newHistory.toString());
214 if (protocol.equals(FormatAdapter.FILE))
216 jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
222 String title = protocol.equals(AppletFormatAdapter.PASTE) ? "Copied From Clipboard"
224 Runtime rt = Runtime.getRuntime();
227 if (Desktop.instance != null)
229 Desktop.instance.startLoading(file);
233 // just in case the caller didn't identify the file for us
236 format = new IdentifyFile().Identify(source, false); // identify
243 format = new IdentifyFile().Identify(file, protocol);
246 // TODO: cache any stream datasources as a temporary file (eg. PDBs
247 // retrieved via URL)
248 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
251 memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // free
256 loadtime = -System.currentTimeMillis();
259 if (format.equalsIgnoreCase("Jalview"))
263 // Tell the user (developer?) that this is going to cause a problem
265 .println("IMPLEMENTATION ERROR: Cannot read consecutive Jalview XML projects from a stream.");
266 // We read the data anyway - it might make sense.
268 alignFrame = new Jalview2XML(raiseGUI).LoadJalviewAlign(file);
272 String error = AppletFormatAdapter.SUPPORTED_FORMATS;
273 if (FormatAdapter.isValidFormat(format))
279 // read from the provided source
280 al = new FormatAdapter().readFromFile(source, format);
285 // open a new source and read from it
286 FormatAdapter fa = new FormatAdapter();
287 al = fa.readFile(file, protocol, format);
288 source = fa.afile; // keep reference for later if necessary.
290 } catch (java.io.IOException ex)
292 error = ex.getMessage();
297 if (format != null && format.length() > 7)
299 // ad hoc message in format.
300 error = format + "\n" + error;
304 if ((al != null) && (al.getHeight() > 0))
306 if (viewport != null)
308 // TODO: create undo object for this JAL-1101
309 for (int i = 0; i < al.getHeight(); i++)
311 viewport.getAlignment().addSequence(al.getSequenceAt(i));
313 viewport.firePropertyChange("alignment", null, viewport
314 .getAlignment().getSequences());
319 alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
320 AlignFrame.DEFAULT_HEIGHT);
322 alignFrame.statusBar.setText(MessageManager.formatMessage("label.successfully_loaded_file", new String[]{title}));
324 if (!protocol.equals(AppletFormatAdapter.PASTE))
325 alignFrame.setFileName(file, format);
328 // add the window to the GUI
329 // note - this actually should happen regardless of raiseGUI status in Jalview 3
330 // TODO: define 'virtual desktop' for benefit of headless scripts that perform queries to find the 'current working alignment'
331 Desktop.addInternalFrame(alignFrame, title,
332 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
337 alignFrame.setMaximum(jalview.bin.Cache.getDefault(
338 "SHOW_FULLSCREEN", false));
339 } catch (java.beans.PropertyVetoException ex)
346 if (Desktop.instance != null)
348 Desktop.instance.stopLoading();
351 final String errorMessage = "Couldn't load file " + title + "\n"
355 javax.swing.SwingUtilities.invokeLater(new Runnable()
359 JOptionPane.showInternalMessageDialog(Desktop.desktop,
360 errorMessage, MessageManager.getString("label.error_loading_file"),
361 JOptionPane.WARNING_MESSAGE);
367 System.err.println(errorMessage);
372 updateRecentlyOpened();
374 } catch (Exception er)
376 System.err.println("Exception whilst opening file '" + file);
377 er.printStackTrace();
380 javax.swing.SwingUtilities.invokeLater(new Runnable()
384 javax.swing.JOptionPane.showInternalMessageDialog(
385 Desktop.desktop, MessageManager.formatMessage("label.problems_opening_file", new String[]{file}),
386 MessageManager.getString("label.file_open_error"),
387 javax.swing.JOptionPane.WARNING_MESSAGE);
392 } catch (OutOfMemoryError er)
395 er.printStackTrace();
399 javax.swing.SwingUtilities.invokeLater(new Runnable()
403 javax.swing.JOptionPane
404 .showInternalMessageDialog(
406 "Out of memory loading file "
409 + "\nSee help files for increasing Java Virtual Machine memory.",
411 javax.swing.JOptionPane.WARNING_MESSAGE);
415 System.err.println("Out of memory loading file " + file + "!!");
418 loadtime += System.currentTimeMillis();
419 // TODO: Estimate percentage of memory used by a newly loaded alignment -
420 // warn if more memory will be needed to work with it
423 - (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // difference
428 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
430 if (alignFrame != null)
432 AlignmentI al = alignFrame.getViewport().getAlignment();
434 System.out.println("Loaded '" + title + "' in "
435 + (loadtime / 1000.0) + "s, took an additional "
436 + (1.0 * memused / (1024.0 * 1024.0)) + " MB ("
437 + al.getHeight() + " seqs by " + al.getWidth() + " cols)");
441 // report that we didn't load anything probably due to an out of memory
443 System.out.println("Failed to load '" + title + "' in "
444 + (loadtime / 1000.0) + "s, took an additional "
445 + (1.0 * memused / (1024.0 * 1024.0))
446 + " MB (alignment is null)");
449 // remove the visual delay indicator
450 if (Desktop.instance != null)
452 Desktop.instance.stopLoading();
460 * @see java.lang.Object#finalize()
462 protected void finalize() throws Throwable