Create instance of FormatAdapter
[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       if (FormatAdapter.formats.contains(format))\r
82       {\r
83         sequences = new FormatAdapter().readFile(file, protocol, format);\r
84       }\r
85 \r
86       if ( (sequences != null) && (sequences.length > 0))\r
87       {\r
88         af = new AlignFrame(new Alignment(sequences));\r
89         af.currentFileFormat = format;\r
90         af.statusBar.setText("Successfully loaded file " + file);\r
91 \r
92         Desktop.addInternalFrame(af, file, AlignFrame.NEW_WINDOW_WIDTH,\r
93                            AlignFrame.NEW_WINDOW_HEIGHT);\r
94 \r
95 \r
96         try\r
97         {\r
98           af.setMaximum(jalview.bin.Cache.getDefault("SHOW_FULLSCREEN", false));\r
99         }\r
100         catch (Exception ex)\r
101         {\r
102         }\r
103       }\r
104       else\r
105       {\r
106         JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
107                                               "Couldn't open file.\n" +\r
108                                               "Formats currently supported are\n" +\r
109                                               "Fasta, MSF, Clustal, BLC, PIR, MSP, and PFAM" // JBPNote - message should be generated through FormatAdapter!\r
110                                               , "Error loading file",\r
111                                               JOptionPane.WARNING_MESSAGE);\r
112       }\r
113     }\r
114   }\r
115 }\r
116 \r
117 }\r