2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
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 // TODO: create undo object for this JAL-1101
336 for (int i = 0; i < al.getHeight(); i++)
338 viewport.getAlignment().addSequence(al.getSequenceAt(i));
340 viewport.firePropertyChange("alignment", null, viewport
341 .getAlignment().getSequences());
345 alignFrame = new AlignFrame(al, AlignFrame.DEFAULT_WIDTH,
346 AlignFrame.DEFAULT_HEIGHT);
348 alignFrame.statusBar.setText(MessageManager.formatMessage(
349 "label.successfully_loaded_file", new String[]
352 if (!protocol.equals(AppletFormatAdapter.PASTE))
354 alignFrame.setFileName(file, format);
356 if (source instanceof HtmlFile)
358 ((HtmlFile) source).LoadAlignmentFeatures(alignFrame);
363 // add the window to the GUI
364 // note - this actually should happen regardless of raiseGUI
365 // status in Jalview 3
366 // TODO: define 'virtual desktop' for benefit of headless scripts
367 // that perform queries to find the 'current working alignment'
368 Desktop.addInternalFrame(alignFrame, title,
369 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
374 alignFrame.setMaximum(jalview.bin.Cache.getDefault(
375 "SHOW_FULLSCREEN", false));
376 } catch (java.beans.PropertyVetoException ex)
383 if (Desktop.instance != null)
385 Desktop.instance.stopLoading();
388 final String errorMessage = "Couldn't load file " + title + "\n"
392 javax.swing.SwingUtilities.invokeLater(new Runnable()
396 JOptionPane.showInternalMessageDialog(Desktop.desktop,
397 errorMessage, MessageManager
398 .getString("label.error_loading_file"),
399 JOptionPane.WARNING_MESSAGE);
405 System.err.println(errorMessage);
410 updateRecentlyOpened();
412 } catch (Exception er)
414 System.err.println("Exception whilst opening file '" + file);
415 er.printStackTrace();
418 javax.swing.SwingUtilities.invokeLater(new Runnable()
422 javax.swing.JOptionPane.showInternalMessageDialog(
423 Desktop.desktop, MessageManager.formatMessage(
424 "label.problems_opening_file", new String[]
425 { file }), MessageManager
426 .getString("label.file_open_error"),
427 javax.swing.JOptionPane.WARNING_MESSAGE);
432 } catch (OutOfMemoryError er)
435 er.printStackTrace();
439 javax.swing.SwingUtilities.invokeLater(new Runnable()
443 javax.swing.JOptionPane
444 .showInternalMessageDialog(
446 MessageManager.formatMessage("warn.out_of_memory_loading_file", new String[]{file}),
447 MessageManager.getString("label.out_of_memory"),
448 javax.swing.JOptionPane.WARNING_MESSAGE);
452 System.err.println("Out of memory loading file " + file + "!!");
455 loadtime += System.currentTimeMillis();
456 // TODO: Estimate percentage of memory used by a newly loaded alignment -
457 // warn if more memory will be needed to work with it
460 - (rt.maxMemory() - rt.totalMemory() + rt.freeMemory()); // difference
465 if (Desktop.desktop != null && Desktop.desktop.isShowMemoryUsage())
467 if (alignFrame != null)
469 AlignmentI al = alignFrame.getViewport().getAlignment();
471 System.out.println("Loaded '" + title + "' in "
472 + (loadtime / 1000.0) + "s, took an additional "
473 + (1.0 * memused / (1024.0 * 1024.0)) + " MB ("
474 + al.getHeight() + " seqs by " + al.getWidth() + " cols)");
478 // report that we didn't load anything probably due to an out of memory
480 System.out.println("Failed to load '" + title + "' in "
481 + (loadtime / 1000.0) + "s, took an additional "
482 + (1.0 * memused / (1024.0 * 1024.0))
483 + " MB (alignment is null)");
486 // remove the visual delay indicator
487 if (Desktop.instance != null)
489 Desktop.instance.stopLoading();
497 * @see java.lang.Object#finalize()
499 protected void finalize() throws Throwable