X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FFileLoader.java;h=86c1cf4cc139ed5790de492c951682bd5842fe6c;hb=7bc226b58110fa26d9dbd3f0c78095d06909ffc3;hp=40b8ffb3fa3c0198f9fd229629f804387a9f0635;hpb=258c0056231d8a97c1d40dd665260e7814d11206;p=jalview.git diff --git a/src/jalview/io/FileLoader.java b/src/jalview/io/FileLoader.java index 40b8ffb..86c1cf4 100755 --- a/src/jalview/io/FileLoader.java +++ b/src/jalview/io/FileLoader.java @@ -1,99 +1,246 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer + * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + package jalview.io; -import jalview.gui.AlignFrame; -import jalview.gui.Jalview2XML; -import javax.swing.JOptionPane; -import jalview.datamodel.Alignment; -import jalview.gui.Desktop; -import jalview.datamodel.SequenceI; -import jalview.gui.Preferences; +import java.util.*; + +import javax.swing.*; + +import jalview.datamodel.*; +import jalview.gui.*; public class FileLoader + implements Runnable { + String file; + String protocol; + String format; + AlignViewport viewport; + AlignFrame alignFrame; + + public void LoadFile(AlignViewport viewport, String file, String protocol, + String format) + { + this.viewport = viewport; + LoadFile(file, protocol, format); + } + public void LoadFile(String file, String protocol, String format) { - LoadingThread loader = new LoadingThread(file, protocol, format); - loader.start(); + this.file = file; + this.protocol = protocol; + this.format = format; + + final Thread loader = new Thread(this); + + SwingUtilities.invokeLater(new Runnable() + { + public void run() + { + loader.start(); + } + }); } public AlignFrame LoadFileWaitTillLoaded(String file, String protocol, - String format) + String format) { - LoadingThread loader = new LoadingThread(file, protocol, format); + this.file = file; + this.protocol = protocol; + this.format = format; + + Thread loader = new Thread(this); loader.start(); + while (loader.isAlive()) { try { - Thread.sleep(50); + Thread.sleep(500); } catch (Exception ex) {} } - return loader.af; + return alignFrame; } + public void updateRecentlyOpened() + { + Vector recent = new Vector(); - class LoadingThread - extends Thread -{ - String file; - String protocol; - String format; - AlignFrame af; + String type = protocol.equals(FormatAdapter.FILE) + ? "RECENT_FILE" : "RECENT_URL"; - public LoadingThread(String file, String protocol, String format) - { - this.file = file; - this.protocol = protocol; - this.format = format; - } + String historyItems = jalview.bin.Cache.getProperty(type); - public void run() - { - SequenceI[] sequences = null; + StringTokenizer st; - if (format.equalsIgnoreCase("Jalview")) + if (historyItems != null) { - af = Jalview2XML.LoadJalviewAlign(file); + st = new StringTokenizer(historyItems, "\t"); + + while (st.hasMoreTokens()) + { + recent.addElement(st.nextElement().toString().trim()); + } } - else + + if (recent.contains(file)) { - if (FormatAdapter.formats.contains(format)) + recent.remove(file); + } + + StringBuffer newHistory = new StringBuffer(file); + for (int i = 0; i < recent.size() && i < 10; i++) + { + newHistory.append("\t"); + newHistory.append(recent.elementAt(i)); + } + + jalview.bin.Cache.setProperty(type, newHistory.toString()); + + if (type.equals(FormatAdapter.FILE)) + { + jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format); + } + } + + public void run() + { + try + { + if (Desktop.instance != null) { - sequences = FormatAdapter.readFile(file, protocol, format); + Desktop.instance.startLoading(file); } - if ( (sequences != null) && (sequences.length > 0)) - { - af = new AlignFrame(new Alignment(sequences)); - af.currentFileFormat = format; - af.statusBar.setText("Successfully loaded file " + file); + Alignment al = null; - Desktop.addInternalFrame(af, file, AlignFrame.NEW_WINDOW_WIDTH, - AlignFrame.NEW_WINDOW_HEIGHT); + if (format.equalsIgnoreCase("Jalview")) + { + alignFrame = new Jalview2XML().LoadJalviewAlign(file); + } + else + { + String error = AppletFormatAdapter.SUPPORTED_FORMATS; + if (FormatAdapter.isValidFormat(format)) + { + try + { + al = new FormatAdapter().readFile(file, protocol, format); + } + catch (java.io.IOException ex) + { + error = ex.getMessage(); + } + } - try + if ( (al != null) && (al.getHeight() > 0)) { - af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false)); + if (viewport != null) + { + for (int i = 0; i < al.getHeight(); i++) + { + viewport.getAlignment().addSequence(al.getSequenceAt(i)); + } + viewport.firePropertyChange("alignment", null, + viewport.getAlignment().getSequences()); + + } + else + { + alignFrame = new AlignFrame(al, + AlignFrame.DEFAULT_WIDTH, + AlignFrame.DEFAULT_HEIGHT); + + alignFrame.statusBar.setText("Successfully loaded file " + file); + alignFrame.setFileName(file, format); + + Desktop.addInternalFrame(alignFrame, file, + AlignFrame.DEFAULT_WIDTH, + AlignFrame.DEFAULT_HEIGHT); + + try + { + alignFrame.setMaximum(jalview.bin.Cache.getDefault( + "SHOW_FULLSCREEN", false)); + } + catch (java.beans.PropertyVetoException ex) + { + } + } } - catch (Exception ex) + else { + if (Desktop.instance != null) + { + Desktop.instance.stopLoading(); + } + + final String errorMessage = "Couldn't load file " + file + "\n" + + error; + + javax.swing.SwingUtilities.invokeLater(new Runnable() + { + public void run() + { + JOptionPane.showInternalMessageDialog(Desktop.desktop, + errorMessage, + "Error loading file", + JOptionPane.WARNING_MESSAGE); + } + }); } } - else + + updateRecentlyOpened(); + + } + catch (OutOfMemoryError er) + { + + er.printStackTrace(); + alignFrame = null; + + javax.swing.SwingUtilities.invokeLater(new Runnable() { - JOptionPane.showInternalMessageDialog(Desktop.desktop, - "Couldn't open file.\n" + - "Formats currently supported are\n" + - "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM" // JBPNote - message should be generated through FormatAdapter! - , "Error loading file", - JOptionPane.WARNING_MESSAGE); - } + public void run() + { + javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop, + "Out of memory loading file " + file + "!!" + + + "\nSee help files for increasing Java Virtual Machine memory." + , "Out of memory", + javax.swing.JOptionPane.WARNING_MESSAGE); + } + }); } + + System.gc(); + if (Desktop.instance != null) + { + Desktop.instance.stopLoading(); + } + } -} }