2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.datamodel.PDBEntry;
25 import jalview.datamodel.SequenceI;
26 import jalview.gui.AlignFrame;
27 import jalview.gui.AlignViewport;
28 import jalview.gui.Desktop;
29 import jalview.gui.Jalview2XML;
30 import jalview.structure.StructureSelectionManager;
31 import jalview.util.MessageManager;
33 import java.util.StringTokenizer;
34 import java.util.Vector;
36 import javax.swing.JOptionPane;
37 import javax.swing.SwingUtilities;
39 public class FileLoader implements Runnable
47 FileParse source = null; // alternative specification of where data comes
51 AlignViewport viewport;
53 AlignFrame alignFrame;
59 boolean raiseGUI = true;
62 * default constructor always raised errors in GUI dialog boxes
70 * construct a Fileloader that may raise errors non-interactively
73 * true if errors are to be raised as GUI dialog boxes
75 public FileLoader(boolean raiseGUI)
77 this.raiseGUI = raiseGUI;
80 public void LoadFile(AlignViewport viewport, String file,
81 String protocol, String format)
83 this.viewport = viewport;
84 LoadFile(file, protocol, format);
87 public void LoadFile(String file, String protocol, String format)
90 this.protocol = protocol;
93 final Thread loader = new Thread(this);
95 SwingUtilities.invokeLater(new Runnable()
105 * Load a (file, protocol) source of unknown type
110 public void LoadFile(String file, String protocol)
112 LoadFile(file, protocol, null);
116 * Load alignment from (file, protocol) and wait till loaded
120 * @return alignFrame constructed from file contents
122 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol)
124 return LoadFileWaitTillLoaded(file, protocol, null);
128 * Load alignment from (file, protocol) of type format and wait till loaded
133 * @return alignFrame constructed from file contents
135 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
139 this.protocol = protocol;
140 this.format = format;
141 return _LoadFileWaitTillLoaded();
145 * Load alignment from FileParse source of type format and wait till loaded
149 * @return alignFrame constructed from file contents
151 public AlignFrame LoadFileWaitTillLoaded(FileParse source, String format)
153 this.source = source;
155 file = source.getInFile();
156 protocol = source.type;
157 this.format = format;
158 return _LoadFileWaitTillLoaded();
162 * start thread and wait until finished, then return the alignFrame that's
163 * (hopefully) been read.
167 protected AlignFrame _LoadFileWaitTillLoaded()
169 Thread loader = new Thread(this);
172 while (loader.isAlive())
177 } catch (Exception ex)
185 public void updateRecentlyOpened()
187 Vector recent = new Vector();
188 if (protocol.equals(FormatAdapter.PASTE))
190 // do nothing if the file was pasted in as text... there is no filename to
194 String type = protocol.equals(FormatAdapter.FILE) ? "RECENT_FILE"
197 String historyItems = jalview.bin.Cache.getProperty(type);
201 if (historyItems != null)
203 st = new StringTokenizer(historyItems, "\t");
205 while (st.hasMoreTokens())
207 recent.addElement(st.nextElement().toString().trim());
211 if (recent.contains(file))
216 StringBuffer newHistory = new StringBuffer(file);
217 for (int i = 0; i < recent.size() && i < 10; i++)
219 newHistory.append("\t");
220 newHistory.append(recent.elementAt(i));
223 jalview.bin.Cache.setProperty(type, newHistory.toString());
225 if (protocol.equals(FormatAdapter.FILE))
227 jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
233 String title = protocol.equals(AppletFormatAdapter.PASTE) ? "Copied From Clipboard"
235 Runtime rt = Runtime.getRuntime();
238 if (Desktop.instance != null)
240 Desktop.instance.startLoading(file);
244 // just in case the caller didn't identify the file for us
247 format = new IdentifyFile().Identify(source, false); // identify
254 format = new IdentifyFile().Identify(file, protocol);
257 // TODO: cache any stream datasources as a temporary file (eg. PDBs
258 // retrieved via URL)
259 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
262 memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // free
267 loadtime = -System.currentTimeMillis();
268 AlignmentI al = null;
270 if (format.equalsIgnoreCase("Jalview"))
274 // Tell the user (developer?) that this is going to cause a problem
276 .println("IMPLEMENTATION ERROR: Cannot read consecutive Jalview XML projects from a stream.");
277 // We read the data anyway - it might make sense.
279 alignFrame = new Jalview2XML(raiseGUI).loadJalviewAlign(file);
283 String error = AppletFormatAdapter.SUPPORTED_FORMATS;
284 if (FormatAdapter.isValidFormat(format))
290 // read from the provided source
291 al = new FormatAdapter().readFromFile(source, format);
296 // open a new source and read from it
297 FormatAdapter fa = new FormatAdapter();
298 al = fa.readFile(file, protocol, format);
299 source = fa.getAlignFile(); // keep reference for later if necessary.
301 } catch (java.io.IOException ex)
303 error = ex.getMessage();
308 if (format != null && format.length() > 7)
310 // ad hoc message in format.
311 error = format + "\n" + error;
315 if ((al != null) && (al.getHeight() > 0))
317 for (SequenceI sq : al.getSequences())
319 while (sq.getDatasetSequence() != null)
321 sq = sq.getDatasetSequence();
323 if (sq.getPDBId() != null)
325 for (PDBEntry pdbe : sq.getPDBId())
327 StructureSelectionManager.getStructureSelectionManager(
328 Desktop.instance).registerPDBEntry(pdbe);
332 if (viewport != null)
334 viewport.addAlignment(al, title);
338 alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
339 AlignFrame.DEFAULT_HEIGHT);
341 alignFrame.statusBar.setText(MessageManager.formatMessage(
342 "label.successfully_loaded_file", new String[]
345 if (!protocol.equals(AppletFormatAdapter.PASTE))
347 alignFrame.setFileName(file, format);
349 if (source instanceof HtmlFile)
351 ((HtmlFile) source).applySettingsToAlignFrame(alignFrame);
353 else if (source instanceof JSONFile)
355 ((JSONFile) source).applySettingsToAlignFrame(alignFrame);
360 // add the window to the GUI
361 // note - this actually should happen regardless of raiseGUI
362 // status in Jalview 3
363 // TODO: define 'virtual desktop' for benefit of headless scripts
364 // that perform queries to find the 'current working alignment'
365 Desktop.addInternalFrame(alignFrame, title,
366 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
371 alignFrame.setMaximum(jalview.bin.Cache.getDefault(
372 "SHOW_FULLSCREEN", false));
373 } catch (java.beans.PropertyVetoException ex)
380 if (Desktop.instance != null)
382 Desktop.instance.stopLoading();
385 final String errorMessage = "Couldn't load file " + title + "\n"
389 javax.swing.SwingUtilities.invokeLater(new Runnable()
393 JOptionPane.showInternalMessageDialog(Desktop.desktop,
394 errorMessage, MessageManager
395 .getString("label.error_loading_file"),
396 JOptionPane.WARNING_MESSAGE);
402 System.err.println(errorMessage);
407 updateRecentlyOpened();
409 } catch (Exception er)
411 System.err.println("Exception whilst opening file '" + file);
412 er.printStackTrace();
415 javax.swing.SwingUtilities.invokeLater(new Runnable()
419 javax.swing.JOptionPane.showInternalMessageDialog(
420 Desktop.desktop, MessageManager.formatMessage(
421 "label.problems_opening_file", new String[]
422 { file }), MessageManager
423 .getString("label.file_open_error"),
424 javax.swing.JOptionPane.WARNING_MESSAGE);
429 } catch (OutOfMemoryError er)
432 er.printStackTrace();
436 javax.swing.SwingUtilities.invokeLater(new Runnable()
440 javax.swing.JOptionPane
441 .showInternalMessageDialog(
443 MessageManager.formatMessage("warn.out_of_memory_loading_file", new String[]{file}),
444 MessageManager.getString("label.out_of_memory"),
445 javax.swing.JOptionPane.WARNING_MESSAGE);
449 System.err.println("Out of memory loading file " + file + "!!");
452 loadtime += System.currentTimeMillis();
453 // TODO: Estimate percentage of memory used by a newly loaded alignment -
454 // warn if more memory will be needed to work with it
457 - (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // difference
462 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
464 if (alignFrame != null)
466 AlignmentI al = alignFrame.getViewport().getAlignment();
468 System.out.println("Loaded '" + title + "' in "
469 + (loadtime / 1000.0) + "s, took an additional "
470 + (1.0 * memused / (1024.0 * 1024.0)) + " MB ("
471 + al.getHeight() + " seqs by " + al.getWidth() + " cols)");
475 // report that we didn't load anything probably due to an out of memory
477 System.out.println("Failed to load '" + title + "' in "
478 + (loadtime / 1000.0) + "s, took an additional "
479 + (1.0 * memused / (1024.0 * 1024.0))
480 + " MB (alignment is null)");
483 // remove the visual delay indicator
484 if (Desktop.instance != null)
486 Desktop.instance.stopLoading();
494 * @see java.lang.Object#finalize()
496 protected void finalize() throws Throwable