Merge branch 'releases/Release_2_11_3_Branch'
[jalview.git] / 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       lastFrameX = 200;\r
56       lastFrameY = 200;\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         final JalviewLite applet = this;\r
95         if (file != null)\r
96         {\r
97             add(launcher);\r
98             if(file.indexOf("://")==-1)\r
99                file = getCodeBase() + file;\r
100 \r
101             launcher.addActionListener(new java.awt.event.ActionListener()\r
102                 {\r
103                     public void actionPerformed(ActionEvent e)\r
104                     {\r
105                         String format = jalview.io.IdentifyFile.Identify(file,\r
106                                 "URL");\r
107                         LoadingThread loader = new LoadingThread(file,\r
108                             "URL",\r
109                             format,\r
110                             featuresFile,\r
111                             applet);\r
112                         loader.start();\r
113                       }\r
114                 });\r
115         }\r
116         else\r
117         {\r
118             file = "NO FILE";\r
119             fileFound = false;\r
120         }\r
121     }\r
122 \r
123 \r
124     public static void main(String [] args)\r
125     {\r
126       if(args.length!=1)\r
127       {\r
128         System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");\r
129         System.exit(1);\r
130       }\r
131 \r
132       String format = jalview.io.IdentifyFile.Identify(args[0],"File");\r
133       SequenceI[] sequences = new AppletFormatAdapter().readFile(args[0], "File", format);\r
134 \r
135       if ( (sequences != null) && (sequences.length > 0))\r
136       {\r
137         AlignFrame af = new AlignFrame(new Alignment(sequences), null, args[0]);\r
138         af.statusBar.setText("Successfully loaded file " + args[0]);\r
139       }\r
140     }\r
141 \r
142 \r
143     /**\r
144      * Initialises and displays a new java.awt.Frame\r
145      *\r
146      * @param frame java.awt.Frame to be displayed\r
147      * @param title title of new frame\r
148      * @param width width if new frame\r
149      * @param height height of new frame\r
150      */\r
151     public static void addFrame(final Frame frame, String title, int width,\r
152         int height)\r
153     {\r
154         frame.setLocation(lastFrameX, lastFrameY);\r
155         lastFrameX += 40;\r
156         lastFrameY += 40;\r
157         frame.setSize(width, height);\r
158         frame.setTitle(title);\r
159         frame.addWindowListener(new WindowAdapter()\r
160             {\r
161                 public void windowClosing(WindowEvent e)\r
162                 {\r
163                     frame.dispose();\r
164                 }\r
165             });\r
166         frame.setVisible(true);\r
167     }\r
168 \r
169     public String getAlignment(String format)\r
170     {\r
171       return getAlignment(format, "true");\r
172     }\r
173 \r
174     public String getAlignment(String format, String suffix)\r
175     {\r
176       try\r
177       {\r
178         boolean seqlimits = suffix.equalsIgnoreCase("true");\r
179 \r
180         String reply = new AppletFormatAdapter().formatSequences(format,\r
181             currentAlignFrame.viewport.getAlignment().getSequences(), seqlimits);\r
182         return reply;\r
183       }\r
184       catch (Exception ex)\r
185       {ex.printStackTrace();\r
186         return "Error retrieving alignment in " + format + " format. ";\r
187 \r
188       }\r
189     }\r
190 \r
191     /**\r
192      * This paints the background surrounding the "Launch Jalview button"\r
193      * <br>\r
194      * <br>If file given in parameter not found, displays error message\r
195      *\r
196      * @param g graphics context\r
197      */\r
198     public void paint(Graphics g)\r
199     {\r
200         if (!fileFound)\r
201         {\r
202             g.setColor(new Color(200, 200, 200));\r
203             g.setColor(Color.cyan);\r
204             g.fillRect(0, 0, getSize().width, getSize().height);\r
205             g.setColor(Color.red);\r
206             g.drawString("Jalview can't open file", 5, 15);\r
207             g.drawString("\"" + file + "\"", 5, 30);\r
208         }\r
209     }\r
210 \r
211     class LoadingThread extends Thread\r
212     {\r
213         String file;\r
214         String protocol;\r
215         String format;\r
216         String featuresFile;\r
217         JalviewLite applet;\r
218 \r
219         public LoadingThread(String file,\r
220                              String protocol,\r
221                              String format,\r
222                              String features,\r
223                              JalviewLite applet)\r
224         {\r
225             this.file = file;\r
226             this.protocol = protocol;\r
227             this.format = format;\r
228             featuresFile = features;\r
229             this.applet = applet;\r
230         }\r
231 \r
232         public void run()\r
233         {\r
234             SequenceI[] sequences = null;\r
235             sequences = new AppletFormatAdapter().readFile(file, protocol, format);\r
236 \r
237             if ((sequences != null) && (sequences.length > 0))\r
238             {\r
239                 currentAlignFrame = new AlignFrame(new Alignment(sequences),\r
240                         applet, file);\r
241                 currentAlignFrame.statusBar.setText("Successfully loaded file " + file);\r
242 \r
243                 if(featuresFile!=null)\r
244                   currentAlignFrame.parseFeaturesFile(featuresFile);\r
245 \r
246                 String pdbfile = applet.getParameter("PDBFILE");\r
247                 if(pdbfile!=null)\r
248                 {\r
249                   if(pdbfile.indexOf("://")==-1)\r
250                        pdbfile = getCodeBase() + pdbfile;\r
251 \r
252                   String sequence = applet.getParameter("PDBSEQ");\r
253 \r
254                   if(sequence!=null)\r
255                   {\r
256                     new MCview.AppletPDBViewer(pdbfile, "URL",\r
257                                                (Sequence)currentAlignFrame.getAlignViewport().getAlignment().findName(sequence),\r
258                                                currentAlignFrame.getSeqcanvas());\r
259 \r
260 \r
261                   }\r
262 \r
263                 }\r
264             }\r
265             else\r
266             {\r
267                 fileFound = false;\r
268                 remove(launcher);\r
269                 repaint();\r
270             }\r
271         }\r
272     }\r
273 }\r