2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\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
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
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
19 package jalview.bin;
\r
21 import jalview.appletgui.AlignFrame;
\r
23 import jalview.datamodel.*;
\r
25 import jalview.io.*;
\r
27 import java.applet.*;
\r
30 import java.awt.event.*;
\r
31 import jalview.appletgui.TreePanel;
\r
35 * Jalview Applet. Runs in Java 1.18 runtime
\r
38 * @version $Revision$
\r
40 public class JalviewLite extends Applet
\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
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
52 //This is the first frame to be displayed, and does not change
\r
53 AlignFrame initialAlignFrame;
\r
55 boolean embedded = false;
\r
59 * init method for Jalview Applet
\r
66 String param = getParameter("RGB");
\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
76 catch (Exception ex)
\r
84 param = getParameter("label");
\r
86 launcher.setLabel(param);
\r
88 this.setBackground(new Color(r, g, b));
\r
90 file = getParameter("file");
\r
92 final JalviewLite applet = this;
\r
93 if(getParameter("embedded")!=null
\r
94 && getParameter("embedded").equalsIgnoreCase("true"))
\r
97 LoadingThread loader = new LoadingThread(file, applet);
\r
100 else if (file != null)
\r
104 launcher.addActionListener(new java.awt.event.ActionListener()
\r
106 public void actionPerformed(ActionEvent e)
\r
108 LoadingThread loader = new LoadingThread(file,
\r
122 public static void main(String [] args)
\r
126 System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");
\r
130 String format = new jalview.io.IdentifyFile().Identify(args[0],AppletFormatAdapter.FILE);
\r
132 SequenceI[] sequences = null;
\r
134 sequences = new AppletFormatAdapter().readFile(args[0], AppletFormatAdapter.FILE, format);
\r
135 }catch(java.io.IOException ex)
\r
137 ex.printStackTrace();
\r
139 if ( (sequences != null) && (sequences.length > 0))
\r
141 AlignFrame af = new AlignFrame(new Alignment(sequences), null, args[0], false);
\r
142 af.statusBar.setText("Successfully loaded file " + args[0]);
\r
148 * Initialises and displays a new java.awt.Frame
\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
155 public static void addFrame(final Frame frame, String title, int width,
\r
158 frame.setLocation(lastFrameX, lastFrameY);
\r
161 frame.setSize(width, height);
\r
162 frame.setTitle(title);
\r
163 frame.addWindowListener(new WindowAdapter()
\r
165 public void windowClosing(WindowEvent e)
\r
167 if(currentAlignFrame == frame)
\r
169 currentAlignFrame = null;
\r
173 frame.setMenuBar(null);
\r
176 public void windowActivated(WindowEvent e)
\r
178 if(frame instanceof AlignFrame)
\r
179 currentAlignFrame = (AlignFrame)frame;
\r
183 frame.setVisible(true);
\r
186 public String getSelectedSequences()
\r
188 StringBuffer result = new StringBuffer("");
\r
190 if(initialAlignFrame.viewport.getSelectionGroup()!=null)
\r
192 SequenceI[] seqs = initialAlignFrame.viewport.getSelectionGroup().
\r
193 getSequencesInOrder(
\r
194 initialAlignFrame.viewport.getAlignment());
\r
196 for (int i = 0; i < seqs.length; i++)
\r
197 result.append(seqs[i].getName() + "¬");
\r
200 return result.toString();
\r
203 public String getAlignment(String format)
\r
205 return getAlignment(format, "true");
\r
208 public String getAlignment(String format, String suffix)
\r
212 boolean seqlimits = suffix.equalsIgnoreCase("true");
\r
214 String reply = new AppletFormatAdapter().formatSequences(format,
\r
215 currentAlignFrame.viewport.getAlignment().getSequences(), seqlimits);
\r
218 catch (Exception ex)
\r
220 ex.printStackTrace();
\r
221 return "Error retrieving alignment in " + format + " format. ";
\r
226 * This paints the background surrounding the "Launch Jalview button"
\r
228 * <br>If file given in parameter not found, displays error message
\r
230 * @param g graphics context
\r
232 public void paint(Graphics g)
\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
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
254 class LoadingThread extends Thread
\r
259 JalviewLite applet;
\r
261 public LoadingThread(String _file,
\r
262 JalviewLite _applet)
\r
265 if(inArchive(file))
\r
266 protocol = AppletFormatAdapter.CLASSLOADER;
\r
269 file = addProtocol(file);
\r
270 protocol = AppletFormatAdapter.URL;
\r
272 format = new jalview.io.IdentifyFile().Identify(file, protocol);
\r
278 SequenceI[] sequences = null;
\r
280 sequences = new AppletFormatAdapter().readFile(file, protocol,
\r
282 }catch(java.io.IOException ex)
\r
284 ex.printStackTrace();
\r
286 if ((sequences != null) && (sequences.length > 0))
\r
288 currentAlignFrame = new AlignFrame(new Alignment(sequences),
\r
293 initialAlignFrame = currentAlignFrame;
\r
295 currentAlignFrame.statusBar.setText("Successfully loaded file " + file);
\r
298 String treeFile = applet.getParameter("treeFile");
\r
299 if (treeFile != null)
\r
303 if(inArchive(treeFile))
\r
304 protocol = AppletFormatAdapter.CLASSLOADER;
\r
307 protocol = AppletFormatAdapter.URL;
\r
308 treeFile = addProtocol(treeFile);
\r
311 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile, protocol);
\r
315 if (fin.getTree() != null)
\r
317 currentAlignFrame.loadTree(fin, treeFile);
\r
320 catch (Exception ex)
\r
322 ex.printStackTrace();
\r
326 String param = getParameter("features");
\r
329 if( !inArchive(param) )
\r
330 param = addProtocol( param );
\r
332 currentAlignFrame.parseFeaturesFile(param);
\r
335 param = getParameter("annotations");
\r
338 if( !inArchive(param) )
\r
339 param = addProtocol( param );
\r
341 new AnnotationReader().readAnnotationFile(
\r
342 currentAlignFrame.viewport.getAlignment(),
\r
345 currentAlignFrame.alignPanel.fontChanged();
\r
349 String pdbfile = applet.getParameter("PDBFILE");
\r
352 if( inArchive(pdbfile) )
\r
353 protocol = AppletFormatAdapter.CLASSLOADER;
\r
356 protocol = AppletFormatAdapter.URL;
\r
357 pdbfile = addProtocol(pdbfile);
\r
360 String sequence = applet.getParameter("PDBSEQ");
\r
364 new MCview.AppletPDBViewer(pdbfile, protocol,
\r
365 (Sequence)currentAlignFrame.getAlignViewport().getAlignment().findName(sequence),
\r
366 currentAlignFrame.getSeqcanvas());
\r
380 * Discovers whether the given file is in the Applet Archive
\r
381 * @param file String
\r
384 boolean inArchive(String file)
\r
386 //This might throw a security exception in certain browsers
\r
387 //Netscape Communicator for instance.
\r
389 return (getClass().getResourceAsStream("/" + file) != null);
\r
390 }catch(Exception ex)
\r
392 System.out.println("Exception checking resources: "+file+" "+ex);
\r
397 String addProtocol(String file)
\r
399 if (file.indexOf("://") == -1)
\r
400 file = getCodeBase() + file;
\r