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,
\r
98 AppletFormatAdapter.URL,
\r
102 else if (file != null)
\r
106 launcher.addActionListener(new java.awt.event.ActionListener()
\r
108 public void actionPerformed(ActionEvent e)
\r
110 LoadingThread loader = new LoadingThread(file,
\r
111 AppletFormatAdapter.URL,
\r
125 public static void main(String [] args)
\r
129 System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");
\r
133 String format = new jalview.io.IdentifyFile().Identify(args[0],AppletFormatAdapter.FILE);
\r
135 SequenceI[] sequences = null;
\r
137 sequences = new AppletFormatAdapter().readFile(args[0], AppletFormatAdapter.FILE, format);
\r
138 }catch(java.io.IOException ex)
\r
140 ex.printStackTrace();
\r
142 if ( (sequences != null) && (sequences.length > 0))
\r
144 AlignFrame af = new AlignFrame(new Alignment(sequences), null, args[0]);
\r
145 af.statusBar.setText("Successfully loaded file " + args[0]);
\r
151 * Initialises and displays a new java.awt.Frame
\r
153 * @param frame java.awt.Frame to be displayed
\r
154 * @param title title of new frame
\r
155 * @param width width if new frame
\r
156 * @param height height of new frame
\r
158 public static void addFrame(final Frame frame, String title, int width,
\r
161 frame.setLocation(lastFrameX, lastFrameY);
\r
164 frame.setSize(width, height);
\r
165 frame.setTitle(title);
\r
166 frame.addWindowListener(new WindowAdapter()
\r
168 public void windowClosing(WindowEvent e)
\r
170 if(currentAlignFrame == frame)
\r
172 currentAlignFrame = null;
\r
176 frame.setMenuBar(null);
\r
179 public void windowActivated(WindowEvent e)
\r
181 if(frame instanceof AlignFrame)
\r
182 currentAlignFrame = (AlignFrame)frame;
\r
186 frame.setVisible(true);
\r
189 public String getSelectedSequences()
\r
191 StringBuffer result = new StringBuffer("");
\r
193 if(initialAlignFrame.viewport.getSelectionGroup()!=null)
\r
195 SequenceI[] seqs = initialAlignFrame.viewport.getSelectionGroup().
\r
196 getSequencesInOrder(
\r
197 initialAlignFrame.viewport.getAlignment());
\r
199 for (int i = 0; i < seqs.length; i++)
\r
200 result.append(seqs[i].getName() + "¬");
\r
203 return result.toString();
\r
206 public String getAlignment(String format)
\r
208 return getAlignment(format, "true");
\r
211 public String getAlignment(String format, String suffix)
\r
215 boolean seqlimits = suffix.equalsIgnoreCase("true");
\r
217 String reply = new AppletFormatAdapter().formatSequences(format,
\r
218 currentAlignFrame.viewport.getAlignment().getSequences(), seqlimits);
\r
221 catch (Exception ex)
\r
223 ex.printStackTrace();
\r
224 return "Error retrieving alignment in " + format + " format. ";
\r
229 * This paints the background surrounding the "Launch Jalview button"
\r
231 * <br>If file given in parameter not found, displays error message
\r
233 * @param g graphics context
\r
235 public void paint(Graphics g)
\r
239 g.setColor(new Color(200, 200, 200));
\r
240 g.setColor(Color.cyan);
\r
241 g.fillRect(0, 0, getSize().width, getSize().height);
\r
242 g.setColor(Color.red);
\r
243 g.drawString("Jalview can't open file", 5, 15);
\r
244 g.drawString("\"" + file + "\"", 5, 30);
\r
248 g.setColor(Color.black);
\r
249 g.setFont(new Font("Arial", Font.BOLD, 24));
\r
250 g.drawString("Jalview Applet", 50, this.size().height/2 -30);
\r
251 g.drawString("Loading Data...", 50, this.size().height/2);
\r
257 class LoadingThread extends Thread
\r
262 JalviewLite applet;
\r
264 public LoadingThread(String _file,
\r
266 JalviewLite _applet)
\r
269 if(inArchive(file))
\r
270 protocol = AppletFormatAdapter.CLASSLOADER;
\r
272 protocol = _protocol;
\r
274 format = new jalview.io.IdentifyFile().Identify(file, protocol);
\r
280 SequenceI[] sequences = null;
\r
282 sequences = new AppletFormatAdapter().readFile(file, protocol,
\r
284 }catch(java.io.IOException ex)
\r
286 ex.printStackTrace();
\r
288 if ((sequences != null) && (sequences.length > 0))
\r
290 currentAlignFrame = new AlignFrame(new Alignment(sequences),
\r
293 initialAlignFrame = currentAlignFrame;
\r
296 currentAlignFrame.setEmbedded();
\r
298 currentAlignFrame.statusBar.setText("Successfully loaded file " + file);
\r
301 String treeFile = applet.getParameter("treeFile");
\r
302 if (treeFile != null)
\r
306 if(inArchive(treeFile))
\r
307 protocol = AppletFormatAdapter.CLASSLOADER;
\r
310 protocol = AppletFormatAdapter.URL;
\r
311 treeFile = addProtocol(treeFile);
\r
314 jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile, protocol);
\r
318 if (fin.getTree() != null)
\r
320 TreePanel tp = null;
\r
321 tp = new TreePanel(currentAlignFrame.viewport,
\r
322 currentAlignFrame.viewport.getAlignment().getSequences(),
\r
323 fin, "FromFile", treeFile);
\r
324 addFrame(tp, treeFile, 600, 500);
\r
325 currentAlignFrame.addTreeMenuItem(tp, treeFile);
\r
328 catch (Exception ex)
\r
330 ex.printStackTrace();
\r
334 String param = getParameter("features");
\r
337 if( !inArchive(param) )
\r
338 param = addProtocol( param );
\r
340 currentAlignFrame.parseFeaturesFile(param);
\r
343 param = getParameter("annotations");
\r
346 if( !inArchive(param) )
\r
347 param = addProtocol( param );
\r
349 new AnnotationReader().readAnnotationFile(
\r
350 currentAlignFrame.viewport.getAlignment(),
\r
353 currentAlignFrame.alignPanel.fontChanged();
\r
357 String pdbfile = applet.getParameter("PDBFILE");
\r
360 if( inArchive(pdbfile) )
\r
361 protocol = AppletFormatAdapter.CLASSLOADER;
\r
364 protocol = AppletFormatAdapter.URL;
\r
365 pdbfile = addProtocol(pdbfile);
\r
368 String sequence = applet.getParameter("PDBSEQ");
\r
372 new MCview.AppletPDBViewer(pdbfile, protocol,
\r
373 (Sequence)currentAlignFrame.getAlignViewport().getAlignment().findName(sequence),
\r
374 currentAlignFrame.getSeqcanvas());
\r
388 * Discovers whether the given file is in the Applet Archive
\r
389 * @param file String
\r
392 boolean inArchive(String file)
\r
394 return ( getClass().getResourceAsStream("/" + file) != null );
\r
397 String addProtocol(String file)
\r
399 if (file.indexOf("://") == -1)
\r
400 file = getCodeBase() + file;
\r