7057e3c01afaa2edd7e0c9d614f72bb1f4da11ad
[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 \r
32 \r
33 /**\r
34  * Jalview Applet. Runs in Java 1.18 runtime\r
35  *\r
36  * @author $author$\r
37  * @version $Revision$\r
38  */\r
39 public class JalviewLite extends Applet\r
40 {\r
41     static int lastFrameX = 200;\r
42     static int lastFrameY = 200;\r
43     boolean fileFound = true;\r
44     String file = "No file";\r
45     Button launcher = new Button("Start Jalview");\r
46 \r
47     AlignFrame currentAlignFrame;\r
48 \r
49 \r
50     /**\r
51      * init method for Jalview Applet\r
52      */\r
53     public void init()\r
54     {\r
55         int r = 255;\r
56         int g = 255;\r
57         int b = 255;\r
58         String param = getParameter("RGB");\r
59 \r
60         if (param != null)\r
61         {\r
62             try\r
63             {\r
64                 r = Integer.parseInt(param.substring(0, 2), 16);\r
65                 g = Integer.parseInt(param.substring(2, 4), 16);\r
66                 b = Integer.parseInt(param.substring(4, 6), 16);\r
67             }\r
68             catch (Exception ex)\r
69             {\r
70                 r = 255;\r
71                 g = 255;\r
72                 b = 255;\r
73             }\r
74         }\r
75 \r
76         this.setBackground(new Color(r, g, b));\r
77 \r
78 \r
79         param = getParameter("features");\r
80         if(param!=null)\r
81         {\r
82           if(param.indexOf("://")==-1)\r
83                param = getCodeBase() + param;\r
84         }\r
85         final String featuresFile = param;\r
86 \r
87         file = getParameter("file");\r
88         final JalviewLite applet = this;\r
89         if (file != null)\r
90         {\r
91             add(launcher);\r
92             if(file.indexOf("://")==-1)\r
93                file = getCodeBase() + file;\r
94 \r
95             launcher.addActionListener(new java.awt.event.ActionListener()\r
96                 {\r
97                     public void actionPerformed(ActionEvent e)\r
98                     {\r
99                         String format = jalview.io.IdentifyFile.Identify(file,\r
100                                 "URL");\r
101                         LoadingThread loader = new LoadingThread(file,\r
102                             "URL",\r
103                             format,\r
104                             featuresFile,\r
105                             applet);\r
106                         loader.start();\r
107                       }\r
108                 });\r
109         }\r
110         else\r
111         {\r
112             file = "NO FILE";\r
113             fileFound = false;\r
114         }\r
115     }\r
116 \r
117     public static void main(String [] args)\r
118     {\r
119       if(args.length!=1)\r
120       {\r
121         System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");\r
122         System.exit(1);\r
123       }\r
124 \r
125       String format = jalview.io.IdentifyFile.Identify(args[0],"File");\r
126       SequenceI[] sequences = new AppletFormatAdapter().readFile(args[0], "File", format);\r
127 \r
128       if ( (sequences != null) && (sequences.length > 0))\r
129       {\r
130         AlignFrame af = new AlignFrame(new Alignment(sequences), null, args[0]);\r
131         af.statusBar.setText("Successfully loaded file " + args[0]);\r
132       }\r
133     }\r
134 \r
135 \r
136     /**\r
137      * Initialises and displays a new java.awt.Frame\r
138      *\r
139      * @param frame java.awt.Frame to be displayed\r
140      * @param title title of new frame\r
141      * @param width width if new frame\r
142      * @param height height of new frame\r
143      */\r
144     public static void addFrame(final Frame frame, String title, int width,\r
145         int height)\r
146     {\r
147         frame.setLocation(lastFrameX, lastFrameY);\r
148         lastFrameX += 40;\r
149         lastFrameY += 40;\r
150         frame.setSize(width, height);\r
151         frame.setTitle(title);\r
152         frame.addWindowListener(new WindowAdapter()\r
153             {\r
154                 public void windowClosing(WindowEvent e)\r
155                 {\r
156                     frame.dispose();\r
157                 }\r
158             });\r
159         frame.setVisible(true);\r
160     }\r
161 \r
162     public String getAlignment(String format)\r
163     {\r
164       return getAlignment(format, "true");\r
165     }\r
166 \r
167     public String getAlignment(String format, String suffix)\r
168     {\r
169       try\r
170       {\r
171         boolean seqlimits = suffix.equalsIgnoreCase("true");\r
172 \r
173         String reply = new AppletFormatAdapter().formatSequences(format,\r
174             currentAlignFrame.viewport.getAlignment().getSequences(),\r
175             true, seqlimits);\r
176         return reply;\r
177       }\r
178       catch (Exception ex)\r
179       {ex.printStackTrace();\r
180         return "Error retrieving alignment in " + format + " format. ";\r
181 \r
182       }\r
183     }\r
184 \r
185     /**\r
186      * This paints the background surrounding the "Launch Jalview button"\r
187      * <br>\r
188      * <br>If file given in parameter not found, displays error message\r
189      *\r
190      * @param g graphics context\r
191      */\r
192     public void paint(Graphics g)\r
193     {\r
194         if (!fileFound)\r
195         {\r
196             g.setColor(new Color(200, 200, 200));\r
197             g.setColor(Color.cyan);\r
198             g.fillRect(0, 0, getSize().width, getSize().height);\r
199             g.setColor(Color.red);\r
200             g.drawString("Jalview can't open file", 5, 15);\r
201             g.drawString("\"" + file + "\"", 5, 30);\r
202         }\r
203     }\r
204 \r
205     class LoadingThread extends Thread\r
206     {\r
207         String file;\r
208         String protocol;\r
209         String format;\r
210         String featuresFile;\r
211         JalviewLite applet;\r
212 \r
213         public LoadingThread(String file,\r
214                              String protocol,\r
215                              String format,\r
216                              String features,\r
217                              JalviewLite applet)\r
218         {\r
219             this.file = file;\r
220             this.protocol = protocol;\r
221             this.format = format;\r
222             featuresFile = features;\r
223             this.applet = applet;\r
224         }\r
225 \r
226         public void run()\r
227         {\r
228             SequenceI[] sequences = null;\r
229             sequences = new AppletFormatAdapter().readFile(file, protocol, format);\r
230 \r
231             if ((sequences != null) && (sequences.length > 0))\r
232             {\r
233                 currentAlignFrame = new AlignFrame(new Alignment(sequences),\r
234                         applet, file);\r
235                 currentAlignFrame.statusBar.setText("Successfully loaded file " + file);\r
236 \r
237                 if(featuresFile!=null)\r
238                   currentAlignFrame.parseFeaturesFile(featuresFile);\r
239             }\r
240             else\r
241             {\r
242                 fileFound = false;\r
243                 remove(launcher);\r
244                 repaint();\r
245             }\r
246         }\r
247     }\r
248 }\r