331e7f53acd348d834572fd50ee2756dca94cd89
[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     static AlignFrame currentAlignFrame;\r
49     boolean embedded = false;\r
50 \r
51 \r
52     /**\r
53      * init method for Jalview Applet\r
54      */\r
55     public void init()\r
56     {\r
57         int r = 255;\r
58         int g = 255;\r
59         int b = 255;\r
60         String param = getParameter("RGB");\r
61 \r
62         if (param != null)\r
63         {\r
64             try\r
65             {\r
66                 r = Integer.parseInt(param.substring(0, 2), 16);\r
67                 g = Integer.parseInt(param.substring(2, 4), 16);\r
68                 b = Integer.parseInt(param.substring(4, 6), 16);\r
69             }\r
70             catch (Exception ex)\r
71             {\r
72                 r = 255;\r
73                 g = 255;\r
74                 b = 255;\r
75             }\r
76         }\r
77 \r
78         param = getParameter("label");\r
79         if(param != null)\r
80           launcher.setLabel(param);\r
81 \r
82         this.setBackground(new Color(r, g, b));\r
83 \r
84 \r
85         param = getParameter("features");\r
86         if(param!=null)\r
87         {\r
88           if(param.indexOf("://")==-1)\r
89                param = getCodeBase() + param;\r
90         }\r
91         final String featuresFile = param;\r
92 \r
93         file = getParameter("file");\r
94         if(file.indexOf("://")==-1)\r
95                file = getCodeBase() + file;\r
96 \r
97         final JalviewLite applet = this;\r
98         if(getParameter("embedded")!=null\r
99            && getParameter("embedded").equalsIgnoreCase("true"))\r
100         {\r
101           embedded = true;\r
102           LoadingThread loader = new LoadingThread(file,\r
103                             "URL",\r
104                             featuresFile,\r
105                             applet);\r
106           loader.start();\r
107         }\r
108         else if (file != null)\r
109         {\r
110             add(launcher);\r
111 \r
112             launcher.addActionListener(new java.awt.event.ActionListener()\r
113                 {\r
114                     public void actionPerformed(ActionEvent e)\r
115                     {\r
116                         LoadingThread loader = new LoadingThread(file,\r
117                             "URL",\r
118                             featuresFile,\r
119                             applet);\r
120                         loader.start();\r
121                       }\r
122                 });\r
123         }\r
124         else\r
125         {\r
126             file = "NO FILE";\r
127             fileFound = false;\r
128         }\r
129     }\r
130 \r
131 \r
132     public static void main(String [] args)\r
133     {\r
134       if(args.length!=1)\r
135       {\r
136         System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");\r
137         System.exit(1);\r
138       }\r
139 \r
140       String format = jalview.io.IdentifyFile.Identify(args[0],"File");\r
141       SequenceI[] sequences = new AppletFormatAdapter().readFile(args[0], "File", format);\r
142 \r
143       if ( (sequences != null) && (sequences.length > 0))\r
144       {\r
145         AlignFrame af = new AlignFrame(new Alignment(sequences), null, args[0]);\r
146         af.statusBar.setText("Successfully loaded file " + args[0]);\r
147       }\r
148     }\r
149 \r
150 \r
151     /**\r
152      * Initialises and displays a new java.awt.Frame\r
153      *\r
154      * @param frame java.awt.Frame to be displayed\r
155      * @param title title of new frame\r
156      * @param width width if new frame\r
157      * @param height height of new frame\r
158      */\r
159     public static void addFrame(final Frame frame, String title, int width,\r
160         int height)\r
161     {\r
162         frame.setLocation(lastFrameX, lastFrameY);\r
163         lastFrameX += 40;\r
164         lastFrameY += 40;\r
165         frame.setSize(width, height);\r
166         frame.setTitle(title);\r
167         frame.addWindowListener(new WindowAdapter()\r
168             {\r
169                 public void windowClosing(WindowEvent e)\r
170                 {\r
171                     if(currentAlignFrame == frame)\r
172                     {\r
173                       currentAlignFrame = null;\r
174                     }\r
175                     lastFrameX -=40;\r
176                     lastFrameY-=40;\r
177                     frame.setMenuBar(null);\r
178                     frame.dispose();\r
179                 }\r
180                 public void windowActivated(WindowEvent e)\r
181                 {\r
182                   if(frame instanceof AlignFrame)\r
183                     currentAlignFrame = (AlignFrame)frame;\r
184                 }\r
185 \r
186             });\r
187         frame.setVisible(true);\r
188     }\r
189 \r
190     public String getAlignment(String format)\r
191     {\r
192       return getAlignment(format, "true");\r
193     }\r
194 \r
195     public String getAlignment(String format, String suffix)\r
196     {\r
197       try\r
198       {\r
199         boolean seqlimits = suffix.equalsIgnoreCase("true");\r
200 \r
201         String reply = new AppletFormatAdapter().formatSequences(format,\r
202             currentAlignFrame.viewport.getAlignment().getSequences(), seqlimits);\r
203         return reply;\r
204       }\r
205       catch (Exception ex)\r
206       {\r
207         ex.printStackTrace();\r
208         return "Error retrieving alignment in " + format + " format. ";\r
209       }\r
210     }\r
211 \r
212     /**\r
213      * This paints the background surrounding the "Launch Jalview button"\r
214      * <br>\r
215      * <br>If file given in parameter not found, displays error message\r
216      *\r
217      * @param g graphics context\r
218      */\r
219     public void paint(Graphics g)\r
220     {\r
221         if (!fileFound)\r
222         {\r
223             g.setColor(new Color(200, 200, 200));\r
224             g.setColor(Color.cyan);\r
225             g.fillRect(0, 0, getSize().width, getSize().height);\r
226             g.setColor(Color.red);\r
227             g.drawString("Jalview can't open file", 5, 15);\r
228             g.drawString("\"" + file + "\"", 5, 30);\r
229         }\r
230         else if(embedded)\r
231         {\r
232           g.setColor(Color.black);\r
233           g.setFont(new Font("Arial", Font.BOLD, 24));\r
234           g.drawString("Jalview Applet", 50, this.size().height/2 -30);\r
235           g.drawString("Loading Data...", 50, this.size().height/2);\r
236         }\r
237 \r
238 \r
239     }\r
240 \r
241     class LoadingThread extends Thread\r
242     {\r
243         String file;\r
244         String protocol;\r
245         String format;\r
246         String featuresFile;\r
247         JalviewLite applet;\r
248 \r
249         public LoadingThread(String file,\r
250                              String protocol,\r
251                              String features,\r
252                              JalviewLite applet)\r
253         {\r
254             this.file = file;\r
255             this.protocol = protocol;\r
256             format = jalview.io.IdentifyFile.Identify(file, "URL");\r
257             featuresFile = features;\r
258             this.applet = applet;\r
259         }\r
260 \r
261         public void run()\r
262         {\r
263             SequenceI[] sequences = null;\r
264             sequences = new AppletFormatAdapter().readFile(file, protocol, format);\r
265 \r
266             if ((sequences != null) && (sequences.length > 0))\r
267             {\r
268               currentAlignFrame = new AlignFrame(new Alignment(sequences),\r
269                                                  applet, file);\r
270               if (embedded)\r
271                 currentAlignFrame.setEmbedded();\r
272 \r
273               currentAlignFrame.statusBar.setText("Successfully loaded file " + file);\r
274 \r
275               if (featuresFile != null)\r
276                   currentAlignFrame.parseFeaturesFile(featuresFile);\r
277 \r
278                 String treeFile = applet.getParameter("treeFile");\r
279                 if (treeFile != null)\r
280                 {\r
281                   try\r
282                   {\r
283                     jalview.io.NewickFile fin = new jalview.io.NewickFile(applet.\r
284                         getCodeBase() + treeFile, "URL");\r
285                     fin.parse();\r
286 \r
287                     if (fin.getTree() != null)\r
288                     {\r
289                       TreePanel tp = null;\r
290                       tp = new TreePanel(currentAlignFrame.viewport,\r
291                                          currentAlignFrame.viewport.getAlignment().getSequences(),\r
292                                          fin, "FromFile", applet.getCodeBase() + treeFile);\r
293                       addFrame(tp, treeFile, 600, 500);\r
294                       currentAlignFrame.addTreeMenuItem(tp, treeFile);\r
295                     }\r
296                   }\r
297                   catch (Exception ex)\r
298                   {\r
299                     ex.printStackTrace();\r
300                   }\r
301               }\r
302 \r
303 \r
304                 String pdbfile = applet.getParameter("PDBFILE");\r
305                 if(pdbfile!=null)\r
306                 {\r
307                   if(pdbfile.indexOf("://")==-1)\r
308                        pdbfile = getCodeBase() + pdbfile;\r
309 \r
310                   String sequence = applet.getParameter("PDBSEQ");\r
311 \r
312                   if(sequence!=null)\r
313                   {\r
314                     new MCview.AppletPDBViewer(pdbfile, "URL",\r
315                                                (Sequence)currentAlignFrame.getAlignViewport().getAlignment().findName(sequence),\r
316                                                currentAlignFrame.getSeqcanvas());\r
317                   }\r
318 \r
319                 }\r
320             }\r
321             else\r
322             {\r
323                 fileFound = false;\r
324                 remove(launcher);\r
325                 repaint();\r
326             }\r
327         }\r
328     }\r
329 }\r