2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 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
26 import jalview.datamodel.*;
29 public class FileLoader
35 AlignViewport viewport;
36 AlignFrame alignFrame;
38 public void LoadFile(AlignViewport viewport, String file, String protocol,
41 this.viewport = viewport;
42 LoadFile(file, protocol, format);
45 public void LoadFile(String file, String protocol, String format)
48 this.protocol = protocol;
51 final Thread loader = new Thread(this);
53 SwingUtilities.invokeLater(new Runnable()
62 public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
66 this.protocol = protocol;
69 Thread loader = new Thread(this);
72 while (loader.isAlive())
85 public void updateRecentlyOpened()
87 Vector recent = new Vector();
89 String type = protocol.equals(FormatAdapter.FILE)
90 ? "RECENT_FILE" : "RECENT_URL";
92 String historyItems = jalview.bin.Cache.getProperty(type);
96 if (historyItems != null)
98 st = new StringTokenizer(historyItems, "\t");
100 while (st.hasMoreTokens())
102 recent.addElement(st.nextElement().toString().trim());
106 if (recent.contains(file))
111 StringBuffer newHistory = new StringBuffer(file);
112 for (int i = 0; i < recent.size() && i < 10; i++)
114 newHistory.append("\t");
115 newHistory.append(recent.elementAt(i));
118 jalview.bin.Cache.setProperty(type, newHistory.toString());
120 if (protocol.equals(FormatAdapter.FILE))
122 jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
128 String title = protocol.equals(AppletFormatAdapter.PASTE)
129 ? "Copied From Clipboard" : file;
133 if (Desktop.instance != null)
135 Desktop.instance.startLoading(file);
140 if (format.equalsIgnoreCase("Jalview"))
142 alignFrame = new Jalview2XML().LoadJalviewAlign(file);
146 String error = AppletFormatAdapter.SUPPORTED_FORMATS;
148 if (FormatAdapter.isValidFormat(format))
152 al = new FormatAdapter().readFile(file, protocol, format);
154 catch (java.io.IOException ex)
156 error = ex.getMessage();
159 if (format!=null && format.length()>7)
161 // ad hoc message in format.
162 error = format +"\n"+error;
166 if ( (al != null) && (al.getHeight() > 0))
168 if (viewport != null)
170 for (int i = 0; i < al.getHeight(); i++)
172 viewport.getAlignment().addSequence(al.getSequenceAt(i));
174 viewport.firePropertyChange("alignment", null,
175 viewport.getAlignment().getSequences());
180 alignFrame = new AlignFrame(al,
181 AlignFrame.DEFAULT_WIDTH,
182 AlignFrame.DEFAULT_HEIGHT);
184 alignFrame.statusBar.setText("Successfully loaded file " + title);
186 if (!protocol.equals(AppletFormatAdapter.PASTE))
187 alignFrame.setFileName(file, format);
189 Desktop.addInternalFrame(alignFrame, title,
190 AlignFrame.DEFAULT_WIDTH,
191 AlignFrame.DEFAULT_HEIGHT);
195 alignFrame.setMaximum(jalview.bin.Cache.getDefault(
196 "SHOW_FULLSCREEN", false));
198 catch (java.beans.PropertyVetoException ex)
205 if (Desktop.instance != null)
207 Desktop.instance.stopLoading();
210 final String errorMessage = "Couldn't load file " + title + "\n" +
213 javax.swing.SwingUtilities.invokeLater(new Runnable()
217 JOptionPane.showInternalMessageDialog(Desktop.desktop,
219 "Error loading file",
220 JOptionPane.WARNING_MESSAGE);
226 updateRecentlyOpened();
229 catch (OutOfMemoryError er)
232 er.printStackTrace();
235 javax.swing.SwingUtilities.invokeLater(new Runnable()
239 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
240 "Out of memory loading file " + file + "!!"
242 "\nSee help files for increasing Java Virtual Machine memory."
244 javax.swing.JOptionPane.WARNING_MESSAGE);
250 if (Desktop.instance != null)
252 Desktop.instance.stopLoading();