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.List;
34 import java.util.StringTokenizer;
35 import java.util.Vector;
37 import javax.swing.JOptionPane;
38 import javax.swing.SwingUtilities;
40 public class FileLoader implements Runnable
48 FileParse source = null; // alternative specification of where data comes
52 AlignViewport viewport;
54 AlignFrame alignFrame;
60 boolean raiseGUI = true;
63 * default constructor always raised errors in GUI dialog boxes
71 * construct a Fileloader that may raise errors non-interactively
74 * true if errors are to be raised as GUI dialog boxes
76 public FileLoader(boolean raiseGUI)
78 this.raiseGUI = raiseGUI;
81 public void LoadFile(AlignViewport viewport, String file,
82 String protocol, String format)
84 this.viewport = viewport;
85 LoadFile(file, protocol, format);
88 public void LoadFile(String file, String protocol, String format)
91 this.protocol = protocol;
94 final Thread loader = new Thread(this);
96 SwingUtilities.invokeLater(new Runnable()
106 * Load a (file, protocol) source of unknown type
111 public void LoadFile(String file, String protocol)
113 LoadFile(file, protocol, null);
117 * Load alignment from (file, protocol) and wait till loaded
121 * @return alignFrame constructed from file contents
123 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol)
125 return LoadFileWaitTillLoaded(file, protocol, null);
129 * Load alignment from (file, protocol) of type format and wait till loaded
134 * @return alignFrame constructed from file contents
136 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
140 this.protocol = protocol;
141 this.format = format;
142 return _LoadFileWaitTillLoaded();
146 * Load alignment from FileParse source of type format and wait till loaded
150 * @return alignFrame constructed from file contents
152 public AlignFrame LoadFileWaitTillLoaded(FileParse source, String format)
154 this.source = source;
156 file = source.getInFile();
157 protocol = source.type;
158 this.format = format;
159 return _LoadFileWaitTillLoaded();
163 * start thread and wait until finished, then return the alignFrame that's
164 * (hopefully) been read.
168 protected AlignFrame _LoadFileWaitTillLoaded()
170 Thread loader = new Thread(this);
173 while (loader.isAlive())
178 } catch (Exception ex)
186 public void updateRecentlyOpened()
188 Vector recent = new Vector();
189 if (protocol.equals(FormatAdapter.PASTE))
191 // do nothing if the file was pasted in as text... there is no filename to
195 String type = protocol.equals(FormatAdapter.FILE) ? "RECENT_FILE"
198 String historyItems = jalview.bin.Cache.getProperty(type);
202 if (historyItems != null)
204 st = new StringTokenizer(historyItems, "\t");
206 while (st.hasMoreTokens())
208 recent.addElement(st.nextElement().toString().trim());
212 if (recent.contains(file))
217 StringBuffer newHistory = new StringBuffer(file);
218 for (int i = 0; i < recent.size() && i < 10; i++)
220 newHistory.append("\t");
221 newHistory.append(recent.elementAt(i));
224 jalview.bin.Cache.setProperty(type, newHistory.toString());
226 if (protocol.equals(FormatAdapter.FILE))
228 jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
234 String title = protocol.equals(AppletFormatAdapter.PASTE) ? "Copied From Clipboard"
236 Runtime rt = Runtime.getRuntime();
239 if (Desktop.instance != null)
241 Desktop.instance.startLoading(file);
245 // just in case the caller didn't identify the file for us
248 format = new IdentifyFile().Identify(source, false); // identify
255 format = new IdentifyFile().Identify(file, protocol);
258 // TODO: cache any stream datasources as a temporary file (eg. PDBs
259 // retrieved via URL)
260 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
263 memused = (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // free
268 loadtime = -System.currentTimeMillis();
269 AlignmentI al = null;
271 if (format.equalsIgnoreCase("Jalview"))
275 // Tell the user (developer?) that this is going to cause a problem
277 .println("IMPLEMENTATION ERROR: Cannot read consecutive Jalview XML projects from a stream.");
278 // We read the data anyway - it might make sense.
280 alignFrame = new Jalview2XML(raiseGUI).loadJalviewAlign(file);
284 String error = AppletFormatAdapter.SUPPORTED_FORMATS;
285 if (FormatAdapter.isValidFormat(format))
291 // read from the provided source
292 al = new FormatAdapter().readFromFile(source, format);
297 // open a new source and read from it
298 FormatAdapter fa = new FormatAdapter();
299 al = fa.readFile(file, protocol, format);
300 source = fa.afile; // keep reference for later if necessary.
302 } catch (java.io.IOException ex)
304 error = ex.getMessage();
309 if (format != null && format.length() > 7)
311 // ad hoc message in format.
312 error = format + "\n" + error;
316 if ((al != null) && (al.getHeight() > 0))
318 for (SequenceI sq : al.getSequences())
320 while (sq.getDatasetSequence() != null)
322 sq = sq.getDatasetSequence();
324 if (sq.getPDBId() != null)
326 for (PDBEntry pdbe : (List<PDBEntry>) sq.getPDBId())
328 StructureSelectionManager.getStructureSelectionManager(
329 Desktop.instance).registerPDBEntry(pdbe);
333 if (viewport != null)
335 viewport.addAlignment(al, title);
339 alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
340 AlignFrame.DEFAULT_HEIGHT);
342 alignFrame.statusBar.setText(MessageManager.formatMessage(
343 "label.successfully_loaded_file", new String[]
346 if (!protocol.equals(AppletFormatAdapter.PASTE))
348 alignFrame.setFileName(file, format);
350 if (source instanceof HtmlFile)
352 ((HtmlFile) source).LoadAlignmentFeatures(alignFrame);
357 // add the window to the GUI
358 // note - this actually should happen regardless of raiseGUI
359 // status in Jalview 3
360 // TODO: define 'virtual desktop' for benefit of headless scripts
361 // that perform queries to find the 'current working alignment'
362 Desktop.addInternalFrame(alignFrame, title,
363 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
368 alignFrame.setMaximum(jalview.bin.Cache.getDefault(
369 "SHOW_FULLSCREEN", false));
370 } catch (java.beans.PropertyVetoException ex)
377 if (Desktop.instance != null)
379 Desktop.instance.stopLoading();
382 final String errorMessage = "Couldn't load file " + title + "\n"
386 javax.swing.SwingUtilities.invokeLater(new Runnable()
390 JOptionPane.showInternalMessageDialog(Desktop.desktop,
391 errorMessage, MessageManager
392 .getString("label.error_loading_file"),
393 JOptionPane.WARNING_MESSAGE);
399 System.err.println(errorMessage);
404 updateRecentlyOpened();
406 } catch (Exception er)
408 System.err.println("Exception whilst opening file '" + file);
409 er.printStackTrace();
412 javax.swing.SwingUtilities.invokeLater(new Runnable()
416 javax.swing.JOptionPane.showInternalMessageDialog(
417 Desktop.desktop, MessageManager.formatMessage(
418 "label.problems_opening_file", new String[]
419 { file }), MessageManager
420 .getString("label.file_open_error"),
421 javax.swing.JOptionPane.WARNING_MESSAGE);
426 } catch (OutOfMemoryError er)
429 er.printStackTrace();
433 javax.swing.SwingUtilities.invokeLater(new Runnable()
437 javax.swing.JOptionPane
438 .showInternalMessageDialog(
440 MessageManager.formatMessage("warn.out_of_memory_loading_file", new String[]{file}),
441 MessageManager.getString("label.out_of_memory"),
442 javax.swing.JOptionPane.WARNING_MESSAGE);
446 System.err.println("Out of memory loading file " + file + "!!");
449 loadtime += System.currentTimeMillis();
450 // TODO: Estimate percentage of memory used by a newly loaded alignment -
451 // warn if more memory will be needed to work with it
454 - (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // difference
459 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
461 if (alignFrame != null)
463 AlignmentI al = alignFrame.getViewport().getAlignment();
465 System.out.println("Loaded '" + title + "' in "
466 + (loadtime / 1000.0) + "s, took an additional "
467 + (1.0 * memused / (1024.0 * 1024.0)) + " MB ("
468 + al.getHeight() + " seqs by " + al.getWidth() + " cols)");
472 // report that we didn't load anything probably due to an out of memory
474 System.out.println("Failed to load '" + title + "' in "
475 + (loadtime / 1000.0) + "s, took an additional "
476 + (1.0 * memused / (1024.0 * 1024.0))
477 + " MB (alignment is null)");
480 // remove the visual delay indicator
481 if (Desktop.instance != null)
483 Desktop.instance.stopLoading();
491 * @see java.lang.Object#finalize()
493 protected void finalize() throws Throwable