+ String file = "No file";
+
+ Button launcher = new Button("Start Jalview");
+
+ /**
+ * The currentAlignFrame is static, it will change if and when the user
+ * selects a new window. Note that it will *never* point back to the embedded
+ * AlignFrame if the applet is started as embedded on the page and then
+ * afterwards a new view is created.
+ */
+ public AlignFrame currentAlignFrame = null;
+
+ /**
+ * This is the first frame to be displayed, and does not change. API calls
+ * will default to this instance if currentAlignFrame is null.
+ */
+ AlignFrame initialAlignFrame = null;
+
+ boolean embedded = false;
+
+ private boolean checkForJmol = true;
+
+ private boolean checkedForJmol = false; // ensure we don't check for jmol
+
+ // every time the app is re-inited
+
+ public boolean jmolAvailable = false;
+
+ private boolean alignPdbStructures = false;
+
+ /**
+ * use an external structure viewer exclusively (no jmols or MCViews will be
+ * opened by JalviewLite itself)
+ */
+ public boolean useXtrnalSviewer = false;
+
+ public static boolean debug = false;
+
+ static String builddate = null, version = null;
+
+ private static void initBuildDetails()
+ {
+ if (builddate == null)
+ {
+ builddate = "unknown";
+ version = "test";
+ java.net.URL url = JalviewLite.class
+ .getResource("/.build_properties");
+ if (url != null)
+ {
+ try
+ {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(
+ url.openStream()));
+ String line;
+ while ((line = reader.readLine()) != null)
+ {
+ if (line.indexOf("VERSION") > -1)
+ {
+ version = line.substring(line.indexOf("=") + 1);
+ }
+ if (line.indexOf("BUILD_DATE") > -1)
+ {
+ builddate = line.substring(line.indexOf("=") + 1);
+ }
+ }
+ } catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+ }
+ }
+
+ public static String getBuildDate()
+ {
+ initBuildDetails();
+ return builddate;
+ }
+
+ public static String getVersion()
+ {
+ initBuildDetails();
+ return version;
+ }
+
+ // public JSObject scriptObject = null;
+
+ /**
+ * init method for Jalview Applet
+ */
+ public void init()
+ {
+ // remove any handlers that might be hanging around from an earlier instance
+ try
+ {
+ if (debug)
+ {
+ System.err.println("Applet context is '"
+ + getAppletContext().getClass().toString() + "'");
+ }
+ JSObject scriptObject = JSObject.getWindow(this);
+ if (debug && scriptObject != null)
+ {
+ System.err.println("Applet has Javascript callback support.");
+ }
+
+ } catch (Exception ex)
+ {
+ System.err
+ .println("Warning: No JalviewLite javascript callbacks available.");
+ if (debug)
+ {
+ ex.printStackTrace();
+ }
+ }
+ /**
+ * turn on extra applet debugging
+ */
+ String dbg = getParameter("debug");
+ if (dbg != null)
+ {
+ debug = dbg.toLowerCase().equals("true");
+ }
+ if (debug)
+ {
+
+ System.err.println("JalviewLite Version " + getVersion());
+ System.err.println("Build Date : " + getBuildDate());
+
+ }
+ String externalsviewer = getParameter("externalstructureviewer");
+ if (externalsviewer != null)
+ {
+ useXtrnalSviewer = externalsviewer.trim().toLowerCase()
+ .equals("true");
+ }
+ /**
+ * if true disable the check for jmol
+ */
+ String chkforJmol = getParameter("nojmol");
+ if (chkforJmol != null)
+ {
+ checkForJmol = !chkforJmol.equals("true");
+ }
+ /**
+ * get the separator parameter if present
+ */
+ String sep = getParameter("separator");
+ if (sep != null)
+ {
+ if (sep.length() > 0)
+ {
+ separator = sep;
+ if (debug)
+ {
+ System.err.println("Separator set to '" + separator + "'");
+ }
+ }
+ else
+ {
+ throw new Error(
+ "Invalid separator parameter - must be non-zero length");
+ }
+ }
+ int r = 255;
+ int g = 255;
+ int b = 255;
+ String param = getParameter("RGB");
+
+ if (param != null)
+ {
+ try
+ {
+ r = Integer.parseInt(param.substring(0, 2), 16);
+ g = Integer.parseInt(param.substring(2, 4), 16);
+ b = Integer.parseInt(param.substring(4, 6), 16);
+ } catch (Exception ex)
+ {
+ r = 255;
+ g = 255;
+ b = 255;
+ }
+ }
+ param = getParameter("label");
+ if (param != null)
+ {
+ launcher.setLabel(param);
+ }
+
+ setBackground(new Color(r, g, b));
+
+ file = getParameter("file");
+
+ if (file == null)
+ {
+ // Maybe the sequences are added as parameters
+ StringBuffer data = new StringBuffer("PASTE");
+ int i = 1;
+ while ((file = getParameter("sequence" + i)) != null)
+ {
+ data.append(file.toString() + "\n");
+ i++;
+ }
+ if (data.length() > 5)
+ {
+ file = data.toString();
+ }
+ }
+
+ final JalviewLite jvapplet = this;
+ if (getParameter("embedded") != null
+ && getParameter("embedded").equalsIgnoreCase("true"))
+ {
+ // Launch as embedded applet in page
+ embedded = true;
+ LoadingThread loader = new LoadingThread(file, jvapplet);
+ loader.start();
+ }
+ else if (file != null)
+ {
+ if (getParameter("showbutton") == null
+ || !getParameter("showbutton").equalsIgnoreCase("false"))
+ {
+ // Add the JalviewLite 'Button' to the page
+ add(launcher);
+ launcher.addActionListener(new java.awt.event.ActionListener()
+ {
+ public void actionPerformed(ActionEvent e)
+ {
+ LoadingThread loader = new LoadingThread(file, jvapplet);
+ loader.start();
+ }
+ });
+ }
+ else
+ {
+ // Open jalviewLite immediately.
+ LoadingThread loader = new LoadingThread(file, jvapplet);
+ loader.start();
+ }
+ }
+ else
+ {
+ // jalview initialisation with no alignment. loadAlignment() method can
+ // still be called to open new alignments.
+ file = "NO FILE";
+ fileFound = false;
+ callInitCallback();
+ }
+ }
+
+ private void callInitCallback()
+ {
+ String initjscallback = getParameter("oninit");
+ if (initjscallback == null)
+ {
+ return;
+ }
+ initjscallback = initjscallback.trim();
+ if (initjscallback.length() > 0)
+ {
+ JSObject scriptObject = null;
+ try
+ {
+ scriptObject = JSObject.getWindow(this);
+ } catch (Exception ex)
+ {
+ }
+ ;
+ if (scriptObject != null)
+ {
+ try
+ {
+ // do onInit with the JS executor thread
+ new JSFunctionExec(this).executeJavascriptFunction(true,
+ initjscallback, null, "Calling oninit callback '"
+ + initjscallback + "'.");
+ } catch (Exception e)
+ {
+ System.err.println("Exception when executing _oninit callback '"
+ + initjscallback + "'.");
+ e.printStackTrace();
+ }
+ }
+ else
+ {
+ System.err.println("Not executing _oninit callback '"
+ + initjscallback + "' - no scripting allowed.");
+ }
+ }
+ }
+
+ /**
+ * Initialises and displays a new java.awt.Frame
+ *
+ * @param frame
+ * java.awt.Frame to be displayed
+ * @param title
+ * title of new frame
+ * @param width
+ * width if new frame
+ * @param height
+ * height of new frame
+ */
+ public static void addFrame(final Frame frame, String title, int width,
+ int height)
+ {
+ frame.setLocation(lastFrameX, lastFrameY);
+ lastFrameX += 40;
+ lastFrameY += 40;
+ frame.setSize(width, height);
+ frame.setTitle(title);
+ frame.addWindowListener(new WindowAdapter()
+ {
+ public void windowClosing(WindowEvent e)
+ {
+ if (frame instanceof AlignFrame)
+ {
+ AlignViewport vp = ((AlignFrame) frame).viewport;
+ ((AlignFrame) frame).closeMenuItem_actionPerformed();
+ if (vp.applet.currentAlignFrame == frame)
+ {
+ vp.applet.currentAlignFrame = null;
+ }
+ vp.applet = null;
+ vp = null;
+
+ }
+ lastFrameX -= 40;
+ lastFrameY -= 40;
+ if (frame instanceof EmbmenuFrame)
+ {
+ ((EmbmenuFrame) frame).destroyMenus();
+ }
+ frame.setMenuBar(null);
+ frame.dispose();
+ }
+
+ public void windowActivated(WindowEvent e)
+ {
+ if (frame instanceof AlignFrame)
+ {
+ ((AlignFrame) frame).viewport.applet.currentAlignFrame = (AlignFrame) frame;
+ if (debug)
+ {
+ System.err.println("Activated window " + frame);
+ }
+ }
+ // be good.
+ super.windowActivated(e);
+ }
+ /*
+ * Probably not necessary to do this - see TODO above. (non-Javadoc)
+ *
+ * @see
+ * java.awt.event.WindowAdapter#windowDeactivated(java.awt.event.WindowEvent
+ * )
+ *
+ * public void windowDeactivated(WindowEvent e) { if (currentAlignFrame ==
+ * frame) { currentAlignFrame = null; if (debug) {
+ * System.err.println("Deactivated window "+frame); } }
+ * super.windowDeactivated(e); }
+ */
+ });
+ frame.setVisible(true);
+ }
+
+ /**
+ * This paints the background surrounding the "Launch Jalview button" <br>
+ * <br>
+ * If file given in parameter not found, displays error message
+ *
+ * @param g
+ * graphics context
+ */
+ public void paint(Graphics g)
+ {
+ if (!fileFound)
+ {
+ g.setColor(new Color(200, 200, 200));
+ g.setColor(Color.cyan);
+ g.fillRect(0, 0, getSize().width, getSize().height);
+ g.setColor(Color.red);
+ g.drawString(MessageManager.getString("label.jalview_cannot_open_file"), 5, 15);
+ g.drawString("\"" + file + "\"", 5, 30);
+ }
+ else if (embedded)
+ {
+ g.setColor(Color.black);
+ g.setFont(new Font("Arial", Font.BOLD, 24));
+ g.drawString(MessageManager.getString("label.jalview_applet"), 50, getSize().height / 2 - 30);
+ g.drawString(MessageManager.getString("label.loading_data") + "...", 50, getSize().height / 2);
+ }
+ }
+
+ /**
+ * get all components associated with the applet of the given type
+ *
+ * @param class1
+ * @return
+ */
+ public Vector getAppletWindow(Class class1)
+ {
+ Vector wnds = new Vector();
+ Component[] cmp = getComponents();
+ if (cmp != null)
+ {
+ for (int i = 0; i < cmp.length; i++)
+ {
+ if (class1.isAssignableFrom(cmp[i].getClass()))
+ {
+ wnds.addElement(cmp);
+ }
+ }
+ }
+ return wnds;
+ }
+
+ class LoadJmolThread extends Thread
+ {
+ private boolean running = false;
+
+ public void run()
+ {
+ if (running || checkedForJmol)
+ {
+ return;
+ }
+ running = true;
+ if (checkForJmol)
+ {
+ try
+ {
+ if (!System.getProperty("java.version").startsWith("1.1"))
+ {
+ Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter");
+ jmolAvailable = true;
+ }
+ if (!jmolAvailable)
+ {
+ System.out
+ .println("Jmol not available - Using MCview for structures");
+ }
+ } catch (java.lang.ClassNotFoundException ex)
+ {
+ }
+ }
+ else
+ {
+ jmolAvailable = false;
+ if (debug)
+ {
+ System.err
+ .println("Skipping Jmol check. Will use MCView (probably)");
+ }
+ }
+ checkedForJmol = true;
+ running = false;
+ }
+
+ public boolean notFinished()
+ {
+ return running || !checkedForJmol;
+ }
+ }
+
+ class LoadingThread extends Thread
+ {
+ /**
+ * State variable: File source
+ */
+ String file;
+
+ /**
+ * State variable: protocol for access to file source
+ */
+ String protocol;
+
+ /**
+ * State variable: format of file source
+ */
+ String format;
+
+ String _file;
+
+ JalviewLite applet;
+
+ private void dbgMsg(String msg)
+ {
+ if (applet.debug)
+ {
+ System.err.println(msg);
+ }
+ }
+
+ /**
+ * update the protocol state variable for accessing the datasource located
+ * by file.
+ *
+ * @param file
+ * @return possibly updated datasource string
+ */
+ public String setProtocolState(String file)
+ {
+ if (file.startsWith("PASTE"))
+ {
+ file = file.substring(5);
+ protocol = AppletFormatAdapter.PASTE;
+ }
+ else if (inArchive(file))
+ {
+ protocol = AppletFormatAdapter.CLASSLOADER;
+ }
+ else
+ {
+ file = addProtocol(file);
+ protocol = AppletFormatAdapter.URL;
+ }
+ dbgMsg("Protocol identified as '" + protocol + "'");
+ return file;
+ }
+
+ public LoadingThread(String _file, JalviewLite _applet)
+ {
+ this._file = _file;
+ applet = _applet;
+ }
+
+ public void run()
+ {
+ LoadJmolThread jmolchecker = new LoadJmolThread();
+ jmolchecker.start();
+ while (jmolchecker.notFinished())
+ {
+ // wait around until the Jmol check is complete.
+ try
+ {
+ Thread.sleep(2);
+ } catch (Exception e)
+ {
+ }
+ ;
+ }
+ startLoading();
+ // applet.callInitCallback();
+ }
+
+ private void startLoading()
+ {
+ AlignFrame newAlignFrame;
+ dbgMsg("Loading thread started with:\n>>file\n" + _file + ">>endfile");
+ file = setProtocolState(_file);
+
+ format = new jalview.io.IdentifyFile().Identify(file, protocol);
+ dbgMsg("File identified as '" + format + "'");
+ dbgMsg("Loading started.");
+ Alignment al = null;
+ try
+ {
+ al = new AppletFormatAdapter().readFile(file, protocol, format);
+ } catch (java.io.IOException ex)
+ {
+ dbgMsg("File load exception.");
+ ex.printStackTrace();
+ if (debug)
+ {
+ try
+ {
+ FileParse fp = new FileParse(file, protocol);
+ String ln = null;
+ dbgMsg(">>>Dumping contents of '" + file + "' " + "("
+ + protocol + ")");
+ while ((ln = fp.nextLine()) != null)
+ {
+ dbgMsg(ln);
+ }
+ dbgMsg(">>>Dump finished.");
+ } catch (Exception e)
+ {
+ System.err
+ .println("Exception when trying to dump the content of the file parameter.");
+ e.printStackTrace();