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