Report invalid chars
[jalview.git] / src / jalview / io / FileLoader.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 \r
20 package jalview.io;\r
21 \r
22 import jalview.gui.AlignFrame;\r
23 import jalview.gui.Jalview2XML;\r
24 import javax.swing.JOptionPane;\r
25 import jalview.datamodel.Alignment;\r
26 import jalview.gui.Desktop;\r
27 import jalview.datamodel.SequenceI;\r
28 \r
29 public class FileLoader\r
30 {\r
31   public void LoadFile(String file, String protocol, String format)\r
32   {\r
33     LoadingThread loader = new LoadingThread(file, protocol, format);\r
34     loader.start();\r
35   }\r
36 \r
37   public AlignFrame LoadFileWaitTillLoaded(String file, String protocol,\r
38                                                   String format)\r
39   {\r
40     LoadingThread loader = new LoadingThread(file, protocol, format);\r
41     loader.start();\r
42     while (loader.isAlive())\r
43     {\r
44       try\r
45       {\r
46         Thread.sleep(500);\r
47       }\r
48       catch (Exception ex)\r
49       {}\r
50     }\r
51 \r
52     return loader.af;\r
53   }\r
54 \r
55 \r
56   class LoadingThread\r
57     extends Thread\r
58 {\r
59   String file;\r
60   String protocol;\r
61   String format;\r
62   AlignFrame af;\r
63 \r
64   public LoadingThread(String file, String protocol, String format)\r
65   {\r
66     this.file = file;\r
67     this.protocol = protocol;\r
68     this.format = format;\r
69   }\r
70 \r
71   public void run()\r
72   {\r
73     SequenceI[] sequences = null;\r
74 \r
75     if (format.equalsIgnoreCase("Jalview"))\r
76     {\r
77       af = Jalview2XML.LoadJalviewAlign(file);\r
78     }\r
79     else\r
80     {\r
81       String errorMessage = AppletFormatAdapter.SUPPORTED_FORMATS;\r
82 \r
83       if (FormatAdapter.formats.contains(format))\r
84       {\r
85         try{\r
86           sequences = new FormatAdapter().readFile(file, protocol, format);\r
87         }catch(java.io.IOException ex)\r
88         {\r
89           errorMessage = ex.getMessage();\r
90         }\r
91       }\r
92 \r
93       if ( (sequences != null) && (sequences.length > 0))\r
94       {\r
95         af = new AlignFrame(new Alignment(sequences));\r
96         af.currentFileFormat = format;\r
97         af.statusBar.setText("Successfully loaded file " + file);\r
98 \r
99         Desktop.addInternalFrame(af, file, AlignFrame.NEW_WINDOW_WIDTH,\r
100                            AlignFrame.NEW_WINDOW_HEIGHT);\r
101 \r
102 \r
103         try\r
104         {\r
105           af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));\r
106         }\r
107         catch (Exception ex)\r
108         {\r
109         }\r
110       }\r
111       else\r
112       {\r
113         JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
114                                               "Couldn't load file "+file+"\n"\r
115                                               +errorMessage,\r
116                                               "Error loading file",\r
117                                               JOptionPane.WARNING_MESSAGE);\r
118       }\r
119     }\r
120   }\r
121 }\r
122 \r
123 }\r