spelling err
[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, applet);\r
98           loader.start();\r
99         }\r
100         else if (file != null)\r
101         {\r
102             add(launcher);\r
103 \r
104             launcher.addActionListener(new java.awt.event.ActionListener()\r
105                 {\r
106                     public void actionPerformed(ActionEvent e)\r
107                     {\r
108                         LoadingThread loader = new LoadingThread(file,\r
109                             applet);\r
110                         loader.start();\r
111                       }\r
112                 });\r
113         }\r
114         else\r
115         {\r
116             file = "NO FILE";\r
117             fileFound = false;\r
118         }\r
119     }\r
120 \r
121 \r
122     public static void main(String [] args)\r
123     {\r
124       if(args.length!=1)\r
125       {\r
126         System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");\r
127         System.exit(1);\r
128       }\r
129 \r
130       String format = new jalview.io.IdentifyFile().Identify(args[0],AppletFormatAdapter.FILE);\r
131 \r
132       SequenceI[] sequences = null;\r
133      try{\r
134        sequences = new AppletFormatAdapter().readFile(args[0], AppletFormatAdapter.FILE, format);\r
135      }catch(java.io.IOException ex)\r
136      {\r
137        ex.printStackTrace();\r
138      }\r
139       if ( (sequences != null) && (sequences.length > 0))\r
140       {\r
141         AlignFrame af = new AlignFrame(new Alignment(sequences), null, args[0], false);\r
142         af.statusBar.setText("Successfully loaded file " + args[0]);\r
143       }\r
144     }\r
145 \r
146 \r
147     /**\r
148      * Initialises and displays a new java.awt.Frame\r
149      *\r
150      * @param frame java.awt.Frame to be displayed\r
151      * @param title title of new frame\r
152      * @param width width if new frame\r
153      * @param height height of new frame\r
154      */\r
155     public static void addFrame(final Frame frame, String title, int width,\r
156         int height)\r
157     {\r
158         frame.setLocation(lastFrameX, lastFrameY);\r
159         lastFrameX += 40;\r
160         lastFrameY += 40;\r
161         frame.setSize(width, height);\r
162         frame.setTitle(title);\r
163         frame.addWindowListener(new WindowAdapter()\r
164             {\r
165                 public void windowClosing(WindowEvent e)\r
166                 {\r
167                     if(currentAlignFrame == frame)\r
168                     {\r
169                       currentAlignFrame = null;\r
170                     }\r
171                     lastFrameX -=40;\r
172                     lastFrameY -=40;\r
173                     frame.setMenuBar(null);\r
174                     frame.dispose();\r
175                 }\r
176                 public void windowActivated(WindowEvent e)\r
177                 {\r
178                   if(frame instanceof AlignFrame)\r
179                     currentAlignFrame = (AlignFrame)frame;\r
180                 }\r
181 \r
182             });\r
183         frame.setVisible(true);\r
184     }\r
185 \r
186     public String getSelectedSequences()\r
187     {\r
188       StringBuffer result = new StringBuffer("");\r
189 \r
190       if(initialAlignFrame.viewport.getSelectionGroup()!=null)\r
191       {\r
192         SequenceI[] seqs = initialAlignFrame.viewport.getSelectionGroup().\r
193             getSequencesInOrder(\r
194                 initialAlignFrame.viewport.getAlignment());\r
195 \r
196         for (int i = 0; i < seqs.length; i++)\r
197           result.append(seqs[i].getName() + "¬");\r
198       }\r
199 \r
200       return result.toString();\r
201     }\r
202 \r
203     public String getAlignment(String format)\r
204     {\r
205       return getAlignment(format, "true");\r
206     }\r
207 \r
208     public String getAlignment(String format, String suffix)\r
209     {\r
210       try\r
211       {\r
212         boolean seqlimits = suffix.equalsIgnoreCase("true");\r
213 \r
214         String reply = new AppletFormatAdapter().formatSequences(format,\r
215             currentAlignFrame.viewport.getAlignment().getSequences(), seqlimits);\r
216         return reply;\r
217       }\r
218       catch (Exception ex)\r
219       {\r
220         ex.printStackTrace();\r
221         return "Error retrieving alignment in " + format + " format. ";\r
222       }\r
223     }\r
224 \r
225     /**\r
226      * This paints the background surrounding the "Launch Jalview button"\r
227      * <br>\r
228      * <br>If file given in parameter not found, displays error message\r
229      *\r
230      * @param g graphics context\r
231      */\r
232     public void paint(Graphics g)\r
233     {\r
234         if (!fileFound)\r
235         {\r
236             g.setColor(new Color(200, 200, 200));\r
237             g.setColor(Color.cyan);\r
238             g.fillRect(0, 0, getSize().width, getSize().height);\r
239             g.setColor(Color.red);\r
240             g.drawString("Jalview can't open file", 5, 15);\r
241             g.drawString("\"" + file + "\"", 5, 30);\r
242         }\r
243         else if(embedded)\r
244         {\r
245           g.setColor(Color.black);\r
246           g.setFont(new Font("Arial", Font.BOLD, 24));\r
247           g.drawString("Jalview Applet", 50, this.size().height/2 -30);\r
248           g.drawString("Loading Data...", 50, this.size().height/2);\r
249         }\r
250 \r
251 \r
252     }\r
253 \r
254     class LoadingThread extends Thread\r
255     {\r
256         String file;\r
257         String protocol;\r
258         String format;\r
259         JalviewLite applet;\r
260 \r
261         public LoadingThread(String _file,\r
262                              JalviewLite _applet)\r
263         {\r
264             file = _file;\r
265             if(inArchive(file))\r
266               protocol = AppletFormatAdapter.CLASSLOADER;\r
267             else\r
268             {\r
269               file = addProtocol(file);\r
270               protocol = AppletFormatAdapter.URL;\r
271             }\r
272             format = new jalview.io.IdentifyFile().Identify(file, protocol);\r
273             applet = _applet;\r
274         }\r
275 \r
276         public void run()\r
277         {\r
278             SequenceI[] sequences = null;\r
279             try{\r
280               sequences = new AppletFormatAdapter().readFile(file, protocol,\r
281                   format);\r
282             }catch(java.io.IOException ex)\r
283             {\r
284               ex.printStackTrace();\r
285             }\r
286             if ((sequences != null) && (sequences.length > 0))\r
287             {\r
288               currentAlignFrame = new AlignFrame(new Alignment(sequences),\r
289                                                  applet,\r
290                                                  file,\r
291                                                  embedded);\r
292 \r
293 \r
294               initialAlignFrame = currentAlignFrame;\r
295 \r
296               currentAlignFrame.statusBar.setText("Successfully loaded file " + file);\r
297 \r
298 \r
299                 String treeFile = applet.getParameter("treeFile");\r
300                 if (treeFile != null)\r
301                 {\r
302                   try\r
303                   {\r
304                     if(inArchive(treeFile))\r
305                       protocol = AppletFormatAdapter.CLASSLOADER;\r
306                     else\r
307                     {\r
308                       protocol = AppletFormatAdapter.URL;\r
309                       treeFile = addProtocol(treeFile);\r
310                     }\r
311 \r
312                     jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile, protocol);\r
313 \r
314                     fin.parse();\r
315 \r
316                     if (fin.getTree() != null)\r
317                     {\r
318                       currentAlignFrame.loadTree(fin, treeFile);\r
319                     }\r
320                   }\r
321                   catch (Exception ex)\r
322                   {\r
323                     ex.printStackTrace();\r
324                   }\r
325               }\r
326 \r
327               String param = getParameter("features");\r
328               if (param != null)\r
329               {\r
330                 if( !inArchive(param) )\r
331                   param = addProtocol( param );\r
332 \r
333                 currentAlignFrame.parseFeaturesFile(param);\r
334               }\r
335 \r
336              param = getParameter("annotations");\r
337              if (param != null)\r
338              {\r
339                if( !inArchive(param) )\r
340                   param = addProtocol( param );\r
341 \r
342                new AnnotationReader().readAnnotationFile(\r
343                    currentAlignFrame.viewport.getAlignment(),\r
344                    param);\r
345 \r
346                currentAlignFrame.alignPanel.fontChanged();\r
347                currentAlignFrame.alignPanel.setScrollValues(0,0);\r
348 \r
349              }\r
350 \r
351 \r
352                 String pdbfile = applet.getParameter("PDBFILE");\r
353                 if(pdbfile!=null)\r
354                 {\r
355                   if( inArchive(pdbfile) )\r
356                     protocol = AppletFormatAdapter.CLASSLOADER;\r
357                   else\r
358                   {\r
359                     protocol = AppletFormatAdapter.URL;\r
360                     pdbfile = addProtocol(pdbfile);\r
361                   }\r
362 \r
363                   String sequence = applet.getParameter("PDBSEQ");\r
364 \r
365                   if(sequence!=null)\r
366                   {\r
367                     new MCview.AppletPDBViewer(pdbfile, protocol,\r
368                                                (Sequence)currentAlignFrame.getAlignViewport().getAlignment().findName(sequence),\r
369                                                currentAlignFrame.getSeqcanvas());\r
370                   }\r
371 \r
372                 }\r
373             }\r
374             else\r
375             {\r
376                 fileFound = false;\r
377                 remove(launcher);\r
378                 repaint();\r
379             }\r
380         }\r
381 \r
382         /**\r
383          * Discovers whether the given file is in the Applet Archive\r
384          * @param file String\r
385          * @return boolean\r
386          */\r
387         boolean inArchive(String file)\r
388         {\r
389           //This might throw a security exception in certain browsers\r
390           //Netscape Communicator for instance.\r
391           try{\r
392             return (getClass().getResourceAsStream("/" + file) != null);\r
393           }catch(Exception ex)\r
394           {\r
395             System.out.println("Exception checking resources: "+file+" "+ex);\r
396             return false;\r
397           }\r
398         }\r
399 \r
400         String addProtocol(String file)\r
401         {\r
402           if (file.indexOf("://") == -1)\r
403              file = getCodeBase() + file;\r
404 \r
405           return file;\r
406         }\r
407 \r
408     }\r
409 }\r