6310cc456573b37a7ddc8aa70c6f9ea932daf965
[jalview.git] / src / jalview / io / FileLoader.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 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 java.util.*;
23
24 import javax.swing.*;
25
26 import jalview.datamodel.*;
27 import jalview.gui.*;
28
29 public class FileLoader
30     implements Runnable
31 {
32   String file;
33   String protocol;
34   String format;
35   AlignViewport viewport;
36   AlignFrame alignFrame;
37
38   public void LoadFile(AlignViewport viewport, String file, String protocol,
39                        String format)
40   {
41     this.viewport = viewport;
42     LoadFile(file, protocol, format);
43   }
44
45   public void LoadFile(String file, String protocol, String format)
46   {
47     this.file = file;
48     this.protocol = protocol;
49     this.format = format;
50
51     final Thread loader = new Thread(this);
52
53     SwingUtilities.invokeLater(new Runnable()
54     {
55       public void run()
56       {
57         loader.start();
58       }
59     });
60   }
61
62   public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,
63                                            String format)
64   {
65     this.file = file;
66     this.protocol = protocol;
67     this.format = format;
68
69     Thread loader = new Thread(this);
70     loader.start();
71
72     while (loader.isAlive())
73     {
74       try
75       {
76         Thread.sleep(500);
77       }
78       catch (Exception ex)
79       {}
80     }
81
82     return alignFrame;
83   }
84
85   public void updateRecentlyOpened()
86   {
87     Vector recent = new Vector();
88
89     String type = protocol.equals(FormatAdapter.FILE)
90         ? "RECENT_FILE" : "RECENT_URL";
91
92     String historyItems = jalview.bin.Cache.getProperty(type);
93
94     StringTokenizer st;
95
96     if (historyItems != null)
97     {
98       st = new StringTokenizer(historyItems, "\t");
99
100       while (st.hasMoreTokens())
101       {
102         recent.addElement(st.nextElement().toString().trim());
103       }
104     }
105
106     if (recent.contains(file))
107     {
108       recent.remove(file);
109     }
110
111     StringBuffer newHistory = new StringBuffer(file);
112     for (int i = 0; i < recent.size() && i < 10; i++)
113     {
114       newHistory.append("\t");
115       newHistory.append(recent.elementAt(i));
116     }
117
118     jalview.bin.Cache.setProperty(type, newHistory.toString());
119
120     if (protocol.equals(FormatAdapter.FILE))
121     {
122       jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", format);
123     }
124   }
125
126   public void run()
127   {
128     String title = protocol.equals(AppletFormatAdapter.PASTE)
129         ? "Copied From Clipboard" : file;
130
131     try
132     {
133       if (Desktop.instance != null)
134       {
135         Desktop.instance.startLoading(file);
136       }
137
138       Alignment al = null;
139
140       if (format.equalsIgnoreCase("Jalview"))
141       {
142         alignFrame = new Jalview2XML().LoadJalviewAlign(file);
143       }
144       else
145       {
146         String error = AppletFormatAdapter.SUPPORTED_FORMATS;
147
148         if (FormatAdapter.isValidFormat(format))
149         {
150           try
151           {
152             al = new FormatAdapter().readFile(file, protocol, format);
153           }
154           catch (java.io.IOException ex)
155           {
156             error = ex.getMessage();
157           }
158         } else {
159           if (format!=null && format.length()>7)
160           {
161             // ad hoc message in format.
162             error = format +"\n"+error;
163           }
164         }
165
166         if ( (al != null) && (al.getHeight() > 0))
167         {
168           if (viewport != null)
169           {
170             for (int i = 0; i < al.getHeight(); i++)
171             {
172               viewport.getAlignment().addSequence(al.getSequenceAt(i));
173             }
174             viewport.firePropertyChange("alignment", null,
175                                         viewport.getAlignment().getSequences());
176
177           }
178           else
179           {
180             alignFrame = new AlignFrame(al,
181                                         AlignFrame.DEFAULT_WIDTH,
182                                         AlignFrame.DEFAULT_HEIGHT);
183
184             alignFrame.statusBar.setText("Successfully loaded file " + title);
185
186             if (!protocol.equals(AppletFormatAdapter.PASTE))
187               alignFrame.setFileName(file, format);
188
189             Desktop.addInternalFrame(alignFrame, title,
190                                      AlignFrame.DEFAULT_WIDTH,
191                                      AlignFrame.DEFAULT_HEIGHT);
192
193             try
194             {
195               alignFrame.setMaximum(jalview.bin.Cache.getDefault(
196                   "SHOW_FULLSCREEN", false));
197             }
198             catch (java.beans.PropertyVetoException ex)
199             {
200             }
201           }
202         }
203         else
204         {
205           if (Desktop.instance != null)
206           {
207             Desktop.instance.stopLoading();
208           }
209
210           final String errorMessage = "Couldn't load file " + title + "\n" +
211               error;
212
213           javax.swing.SwingUtilities.invokeLater(new Runnable()
214           {
215             public void run()
216             {
217               JOptionPane.showInternalMessageDialog(Desktop.desktop,
218                   errorMessage,
219                   "Error loading file",
220                   JOptionPane.WARNING_MESSAGE);
221             }
222           });
223         }
224       }
225
226       updateRecentlyOpened();
227
228     }
229     catch (OutOfMemoryError er)
230     {
231
232       er.printStackTrace();
233       alignFrame = null;
234
235       javax.swing.SwingUtilities.invokeLater(new Runnable()
236       {
237         public void run()
238         {
239           javax.swing.JOptionPane.showInternalMessageDialog(Desktop.desktop,
240               "Out of memory loading file " + file + "!!"
241               +
242               "\nSee help files for increasing Java Virtual Machine memory."
243               , "Out of memory",
244               javax.swing.JOptionPane.WARNING_MESSAGE);
245         }
246       });
247     }
248
249     System.gc();
250     if (Desktop.instance != null)
251     {
252       Desktop.instance.stopLoading();
253     }
254
255   }
256
257 }