IdentifyFile should not be static
[jalview.git] / src / jalview / bin / JalviewLite.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 package jalview.bin;\r
20 \r
21 import jalview.appletgui.AlignFrame;\r
22 \r
23 import jalview.datamodel.*;\r
24 \r
25 import jalview.io.*;\r
26 \r
27 import java.applet.*;\r
28 \r
29 import java.awt.*;\r
30 import java.awt.event.*;\r
31 import jalview.appletgui.TreePanel;\r
32 \r
33 \r
34 /**\r
35  * Jalview Applet. Runs in Java 1.18 runtime\r
36  *\r
37  * @author $author$\r
38  * @version $Revision$\r
39  */\r
40 public class JalviewLite extends Applet\r
41 {\r
42     static int lastFrameX = 200;\r
43     static int lastFrameY = 200;\r
44     boolean fileFound = true;\r
45     String file = "No file";\r
46     Button launcher = new Button("Start Jalview");\r
47 \r
48     //The currentAlignFrame is static, it will change\r
49     //if and when the user selects a new window\r
50     static AlignFrame currentAlignFrame;\r
51 \r
52     //This is the first frame to be displayed, and does not change\r
53     AlignFrame initialAlignFrame;\r
54 \r
55     boolean embedded = false;\r
56 \r
57 \r
58     /**\r
59      * init method for Jalview Applet\r
60      */\r
61     public void init()\r
62     {\r
63         int r = 255;\r
64         int g = 255;\r
65         int b = 255;\r
66         String param = getParameter("RGB");\r
67 \r
68         if (param != null)\r
69         {\r
70             try\r
71             {\r
72                 r = Integer.parseInt(param.substring(0, 2), 16);\r
73                 g = Integer.parseInt(param.substring(2, 4), 16);\r
74                 b = Integer.parseInt(param.substring(4, 6), 16);\r
75             }\r
76             catch (Exception ex)\r
77             {\r
78                 r = 255;\r
79                 g = 255;\r
80                 b = 255;\r
81             }\r
82         }\r
83 \r
84         param = getParameter("label");\r
85         if(param != null)\r
86           launcher.setLabel(param);\r
87 \r
88         this.setBackground(new Color(r, g, b));\r
89 \r
90         file = getParameter("file");\r
91 \r
92         final JalviewLite applet = this;\r
93         if(getParameter("embedded")!=null\r
94            && getParameter("embedded").equalsIgnoreCase("true"))\r
95         {\r
96           embedded = true;\r
97           LoadingThread loader = new LoadingThread(file,\r
98                             AppletFormatAdapter.URL,\r
99                             applet);\r
100           loader.start();\r
101         }\r
102         else if (file != null)\r
103         {\r
104             add(launcher);\r
105 \r
106             launcher.addActionListener(new java.awt.event.ActionListener()\r
107                 {\r
108                     public void actionPerformed(ActionEvent e)\r
109                     {\r
110                         LoadingThread loader = new LoadingThread(file,\r
111                             AppletFormatAdapter.URL,\r
112                             applet);\r
113                         loader.start();\r
114                       }\r
115                 });\r
116         }\r
117         else\r
118         {\r
119             file = "NO FILE";\r
120             fileFound = false;\r
121         }\r
122     }\r
123 \r
124 \r
125     public static void main(String [] args)\r
126     {\r
127       if(args.length!=1)\r
128       {\r
129         System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");\r
130         System.exit(1);\r
131       }\r
132 \r
133       String format = new jalview.io.IdentifyFile().Identify(args[0],AppletFormatAdapter.FILE);\r
134 \r
135       SequenceI[] sequences = null;\r
136      try{\r
137        sequences = new AppletFormatAdapter().readFile(args[0], AppletFormatAdapter.FILE, format);\r
138      }catch(java.io.IOException ex)\r
139      {\r
140        ex.printStackTrace();\r
141      }\r
142       if ( (sequences != null) && (sequences.length > 0))\r
143       {\r
144         AlignFrame af = new AlignFrame(new Alignment(sequences), null, args[0]);\r
145         af.statusBar.setText("Successfully loaded file " + args[0]);\r
146       }\r
147     }\r
148 \r
149 \r
150     /**\r
151      * Initialises and displays a new java.awt.Frame\r
152      *\r
153      * @param frame java.awt.Frame to be displayed\r
154      * @param title title of new frame\r
155      * @param width width if new frame\r
156      * @param height height of new frame\r
157      */\r
158     public static void addFrame(final Frame frame, String title, int width,\r
159         int height)\r
160     {\r
161         frame.setLocation(lastFrameX, lastFrameY);\r
162         lastFrameX += 40;\r
163         lastFrameY += 40;\r
164         frame.setSize(width, height);\r
165         frame.setTitle(title);\r
166         frame.addWindowListener(new WindowAdapter()\r
167             {\r
168                 public void windowClosing(WindowEvent e)\r
169                 {\r
170                     if(currentAlignFrame == frame)\r
171                     {\r
172                       currentAlignFrame = null;\r
173                     }\r
174                     lastFrameX -=40;\r
175                     lastFrameY -=40;\r
176                     frame.setMenuBar(null);\r
177                     frame.dispose();\r
178                 }\r
179                 public void windowActivated(WindowEvent e)\r
180                 {\r
181                   if(frame instanceof AlignFrame)\r
182                     currentAlignFrame = (AlignFrame)frame;\r
183                 }\r
184 \r
185             });\r
186         frame.setVisible(true);\r
187     }\r
188 \r
189     public String getSelectedSequences()\r
190     {\r
191       StringBuffer result = new StringBuffer("");\r
192 \r
193       if(initialAlignFrame.viewport.getSelectionGroup()!=null)\r
194       {\r
195         SequenceI[] seqs = initialAlignFrame.viewport.getSelectionGroup().\r
196             getSequencesInOrder(\r
197                 initialAlignFrame.viewport.getAlignment());\r
198 \r
199         for (int i = 0; i < seqs.length; i++)\r
200           result.append(seqs[i].getName() + "¬");\r
201       }\r
202 \r
203       return result.toString();\r
204     }\r
205 \r
206     public String getAlignment(String format)\r
207     {\r
208       return getAlignment(format, "true");\r
209     }\r
210 \r
211     public String getAlignment(String format, String suffix)\r
212     {\r
213       try\r
214       {\r
215         boolean seqlimits = suffix.equalsIgnoreCase("true");\r
216 \r
217         String reply = new AppletFormatAdapter().formatSequences(format,\r
218             currentAlignFrame.viewport.getAlignment().getSequences(), seqlimits);\r
219         return reply;\r
220       }\r
221       catch (Exception ex)\r
222       {\r
223         ex.printStackTrace();\r
224         return "Error retrieving alignment in " + format + " format. ";\r
225       }\r
226     }\r
227 \r
228     /**\r
229      * This paints the background surrounding the "Launch Jalview button"\r
230      * <br>\r
231      * <br>If file given in parameter not found, displays error message\r
232      *\r
233      * @param g graphics context\r
234      */\r
235     public void paint(Graphics g)\r
236     {\r
237         if (!fileFound)\r
238         {\r
239             g.setColor(new Color(200, 200, 200));\r
240             g.setColor(Color.cyan);\r
241             g.fillRect(0, 0, getSize().width, getSize().height);\r
242             g.setColor(Color.red);\r
243             g.drawString("Jalview can't open file", 5, 15);\r
244             g.drawString("\"" + file + "\"", 5, 30);\r
245         }\r
246         else if(embedded)\r
247         {\r
248           g.setColor(Color.black);\r
249           g.setFont(new Font("Arial", Font.BOLD, 24));\r
250           g.drawString("Jalview Applet", 50, this.size().height/2 -30);\r
251           g.drawString("Loading Data...", 50, this.size().height/2);\r
252         }\r
253 \r
254 \r
255     }\r
256 \r
257     class LoadingThread extends Thread\r
258     {\r
259         String file;\r
260         String protocol;\r
261         String format;\r
262         JalviewLite applet;\r
263 \r
264         public LoadingThread(String _file,\r
265                              String _protocol,\r
266                              JalviewLite _applet)\r
267         {\r
268             file = _file;\r
269             if(inArchive(file))\r
270               protocol = AppletFormatAdapter.CLASSLOADER;\r
271             else\r
272               protocol = _protocol;\r
273 \r
274             format = new jalview.io.IdentifyFile().Identify(file, protocol);\r
275             applet = _applet;\r
276         }\r
277 \r
278         public void run()\r
279         {\r
280             SequenceI[] sequences = null;\r
281             try{\r
282               sequences = new AppletFormatAdapter().readFile(file, protocol,\r
283                   format);\r
284             }catch(java.io.IOException ex)\r
285             {\r
286               ex.printStackTrace();\r
287             }\r
288             if ((sequences != null) && (sequences.length > 0))\r
289             {\r
290               currentAlignFrame = new AlignFrame(new Alignment(sequences),\r
291                                                  applet, file);\r
292 \r
293               initialAlignFrame = currentAlignFrame;\r
294 \r
295               if (embedded)\r
296                 currentAlignFrame.setEmbedded();\r
297 \r
298               currentAlignFrame.statusBar.setText("Successfully loaded file " + file);\r
299 \r
300 \r
301                 String treeFile = applet.getParameter("treeFile");\r
302                 if (treeFile != null)\r
303                 {\r
304                   try\r
305                   {\r
306                     if(inArchive(treeFile))\r
307                       protocol = AppletFormatAdapter.CLASSLOADER;\r
308                     else\r
309                     {\r
310                       protocol = AppletFormatAdapter.URL;\r
311                       treeFile = addProtocol(treeFile);\r
312                     }\r
313 \r
314                     jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile, protocol);\r
315 \r
316                     fin.parse();\r
317 \r
318                     if (fin.getTree() != null)\r
319                     {\r
320                       TreePanel tp = null;\r
321                       tp = new TreePanel(currentAlignFrame.viewport,\r
322                                          currentAlignFrame.viewport.getAlignment().getSequences(),\r
323                                          fin, "FromFile", treeFile);\r
324                       addFrame(tp, treeFile, 600, 500);\r
325                       currentAlignFrame.addTreeMenuItem(tp, treeFile);\r
326                     }\r
327                   }\r
328                   catch (Exception ex)\r
329                   {\r
330                     ex.printStackTrace();\r
331                   }\r
332               }\r
333 \r
334               String param = getParameter("features");\r
335               if (param != null)\r
336               {\r
337                 if( !inArchive(param) )\r
338                   param = addProtocol( param );\r
339 \r
340                 currentAlignFrame.parseFeaturesFile(param);\r
341               }\r
342 \r
343              param = getParameter("annotations");\r
344              if (param != null)\r
345              {\r
346                if( !inArchive(param) )\r
347                   param = addProtocol( param );\r
348 \r
349                new AnnotationReader().readAnnotationFile(\r
350                    currentAlignFrame.viewport.getAlignment(),\r
351                    param);\r
352 \r
353                currentAlignFrame.alignPanel.fontChanged();\r
354              }\r
355 \r
356 \r
357                 String pdbfile = applet.getParameter("PDBFILE");\r
358                 if(pdbfile!=null)\r
359                 {\r
360                   if( inArchive(pdbfile) )\r
361                     protocol = AppletFormatAdapter.CLASSLOADER;\r
362                   else\r
363                   {\r
364                     protocol = AppletFormatAdapter.URL;\r
365                     pdbfile = addProtocol(pdbfile);\r
366                   }\r
367 \r
368                   String sequence = applet.getParameter("PDBSEQ");\r
369 \r
370                   if(sequence!=null)\r
371                   {\r
372                     new MCview.AppletPDBViewer(pdbfile, protocol,\r
373                                                (Sequence)currentAlignFrame.getAlignViewport().getAlignment().findName(sequence),\r
374                                                currentAlignFrame.getSeqcanvas());\r
375                   }\r
376 \r
377                 }\r
378             }\r
379             else\r
380             {\r
381                 fileFound = false;\r
382                 remove(launcher);\r
383                 repaint();\r
384             }\r
385         }\r
386 \r
387         /**\r
388          * Discovers whether the given file is in the Applet Archive\r
389          * @param file String\r
390          * @return boolean\r
391          */\r
392         boolean inArchive(String file)\r
393         {\r
394           return ( getClass().getResourceAsStream("/" + file) != null );\r
395         }\r
396 \r
397         String addProtocol(String file)\r
398         {\r
399           if (file.indexOf("://") == -1)\r
400              file = getCodeBase() + file;\r
401 \r
402           return file;\r
403         }\r
404 \r
405     }\r
406 }\r