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