updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / io / FileLoader.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4 *
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.
9 *
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.
14 *
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
18 */
19
20 package jalview.io;
21
22 import jalview.gui.AlignFrame;
23 import jalview.gui.Jalview2XML;
24 import javax.swing.JOptionPane;
25
26 import jalview.gui.*;
27 import jalview.datamodel.*;
28 import java.util.Vector;
29 import java.util.StringTokenizer;
30
31 public class FileLoader
32 {
33   String file;
34   String protocol;
35   String format;
36   AlignViewport viewport;
37
38   public void LoadFile(AlignViewport viewport, String file, String protocol, String format)
39   {
40     this.viewport = viewport;
41     LoadFile(file, protocol, format);
42   }
43
44   public void LoadFile(String file, String protocol, String format)
45   {
46     this.file = file;
47     this.protocol = protocol;
48     this.format = format;
49
50     LoadingThread loader = new LoadingThread();
51     loader.start();
52   }
53
54   public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
55                                                   String format)
56   {
57     this.file = file;
58     this.protocol = protocol;
59     this.format = format;
60
61     LoadingThread loader = new LoadingThread();
62     loader.start();
63     while (loader.isAlive())
64     {
65       try
66       {
67         Thread.sleep(500);
68       }
69       catch (Exception ex)
70       {}
71     }
72
73     return loader.af;
74   }
75
76
77
78   public void updateRecentlyOpened()
79   {
80     Vector recent = new Vector();
81
82     String type = protocol.equals(FormatAdapter.FILE)
83         ? "RECENT_FILE" : "RECENT_URL";
84
85     String historyItems = jalview.bin.Cache.getProperty(type);
86
87     StringTokenizer st;
88
89     if (historyItems != null)
90     {
91       st = new StringTokenizer(historyItems, "\t");
92
93       while (st.hasMoreTokens())
94       {
95         recent.addElement(st.nextElement().toString().trim());
96       }
97     }
98
99     if (recent.contains(file))
100     {
101       recent.remove(file);
102     }
103
104     StringBuffer newHistory = new StringBuffer(file);
105     for (int i = 0; i < recent.size() && i < 10; i++)
106     {
107       newHistory.append("\t");
108       newHistory.append(recent.elementAt(i));
109     }
110
111     jalview.bin.Cache.setProperty(type, newHistory.toString());
112
113     if(type.equals(FormatAdapter.FILE))
114       jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
115   }
116
117
118   class LoadingThread
119       extends Thread
120   {
121
122     AlignFrame af;
123
124
125
126     public void run()
127     {
128       if (Desktop.instance != null)
129         Desktop.instance.startLoading(file);
130
131       SequenceI[] sequences = null;
132
133       if (format.equalsIgnoreCase("Jalview"))
134       {
135         af = new Jalview2XML().LoadJalviewAlign(file);
136       }
137       else
138       {
139         String error = AppletFormatAdapter.SUPPORTED_FORMATS;
140
141         if (FormatAdapter.isValidFormat(format))
142         {
143           try
144           {
145             sequences = new FormatAdapter().readFile(file, protocol, format);
146           }
147           catch (java.io.IOException ex)
148           {
149             error = ex.getMessage();
150           }
151         }
152
153         if ( (sequences != null) && (sequences.length > 0))
154         {
155           if(viewport!=null)
156           {
157             for(int i=0; i<sequences.length; i++)
158               viewport.getAlignment().addSequence(sequences[i]);
159
160               viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences());
161           }
162           else
163           {
164             af = new AlignFrame(new Alignment(sequences));
165             af.currentFileFormat = format;
166             af.statusBar.setText("Successfully loaded file " + file);
167
168             Desktop.addInternalFrame(af, file, AlignFrame.NEW_WINDOW_WIDTH,
169                                      AlignFrame.NEW_WINDOW_HEIGHT);
170
171             try
172             {
173               af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));
174             }
175             catch (java.beans.PropertyVetoException ex)
176             {
177             }
178           }
179         }
180         else
181         {
182           if (Desktop.instance != null)
183             Desktop.instance.stopLoading();
184
185           final String errorMessage = "Couldn't load file "+file+"\n"+error;
186
187           javax.swing.SwingUtilities.invokeLater(new Runnable()
188           {
189             public void run()
190             {
191               JOptionPane.showInternalMessageDialog(Desktop.desktop,
192                                                     errorMessage,
193                                                     "Error loading file",
194                                                     JOptionPane.WARNING_MESSAGE);
195             }
196           });
197         }
198       }
199
200       if (af != null)
201       {
202         updateRecentlyOpened();
203       }
204
205       if (Desktop.instance != null)
206         Desktop.instance.stopLoading();
207
208     }
209   }
210
211 }