2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 import jalview.gui.AlignFrame;
23 import jalview.gui.Jalview2XML;
24 import javax.swing.JOptionPane;
27 import jalview.datamodel.*;
28 import java.util.Vector;
29 import java.util.StringTokenizer;
31 public class FileLoader
36 AlignViewport viewport;
38 public void LoadFile(AlignViewport viewport, String file, String protocol, String format)
40 this.viewport = viewport;
41 LoadFile(file, protocol, format);
44 public void LoadFile(String file, String protocol, String format)
47 this.protocol = protocol;
50 LoadingThread loader = new LoadingThread();
54 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
58 this.protocol = protocol;
61 LoadingThread loader = new LoadingThread();
63 while (loader.isAlive())
78 public void updateRecentlyOpened()
80 Vector recent = new Vector();
82 String type = protocol.equals(FormatAdapter.FILE)
83 ? "RECENT_FILE" : "RECENT_URL";
85 String historyItems = jalview.bin.Cache.getProperty(type);
89 if (historyItems != null)
91 st = new StringTokenizer(historyItems, "\t");
93 while (st.hasMoreTokens())
95 recent.addElement(st.nextElement().toString().trim());
99 if (recent.contains(file))
104 StringBuffer newHistory = new StringBuffer(file);
105 for (int i = 0; i < recent.size() && i < 10; i++)
107 newHistory.append("\t");
108 newHistory.append(recent.elementAt(i));
111 jalview.bin.Cache.setProperty(type, newHistory.toString());
113 if(type.equals(FormatAdapter.FILE))
114 jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
128 if (Desktop.instance != null)
129 Desktop.instance.startLoading(file);
131 SequenceI[] sequences = null;
133 if (format.equalsIgnoreCase("Jalview"))
135 af = new Jalview2XML().LoadJalviewAlign(file);
139 String error = AppletFormatAdapter.SUPPORTED_FORMATS;
141 if (FormatAdapter.isValidFormat(format))
145 sequences = new FormatAdapter().readFile(file, protocol, format);
147 catch (java.io.IOException ex)
149 error = ex.getMessage();
153 if ( (sequences != null) && (sequences.length > 0))
157 for(int i=0; i<sequences.length; i++)
158 viewport.getAlignment().addSequence(sequences[i]);
160 viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences());
164 af = new AlignFrame(new Alignment(sequences));
165 af.currentFileFormat = format;
166 af.statusBar.setText("Successfully loaded file " + file);
168 Desktop.addInternalFrame(af, file, AlignFrame.NEW_WINDOW_WIDTH,
169 AlignFrame.NEW_WINDOW_HEIGHT);
173 af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));
175 catch (java.beans.PropertyVetoException ex)
182 if (Desktop.instance != null)
183 Desktop.instance.stopLoading();
185 final String errorMessage = "Couldn't load file "+file+"\n"+error;
187 javax.swing.SwingUtilities.invokeLater(new Runnable()
191 JOptionPane.showInternalMessageDialog(Desktop.desktop,
193 "Error loading file",
194 JOptionPane.WARNING_MESSAGE);
202 updateRecentlyOpened();
205 if (Desktop.instance != null)
206 Desktop.instance.stopLoading();