X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fbin%2FJalviewLite.java;h=1be926e91dd62240aee4139800a57e62506258cb;hb=ac753bb4f4b4270183b39679fb3cc4da2af07634;hp=d0c3829abfa1e3dbc21fb8fd7062299ec828eefd;hpb=de6ff2396211d2ef6ccb3db5e852ef492175f27d;p=jalview.git diff --git a/src/jalview/bin/JalviewLite.java b/src/jalview/bin/JalviewLite.java index d0c3829..1be926e 100644 --- a/src/jalview/bin/JalviewLite.java +++ b/src/jalview/bin/JalviewLite.java @@ -45,6 +45,7 @@ import jalview.io.NewickFile; import jalview.javascript.JSFunctionExec; import jalview.javascript.JalviewLiteJsApi; import jalview.javascript.JsCallBack; +import jalview.javascript.MouseOverStructureListener; import jalview.structure.SelectionListener; import jalview.structure.StructureSelectionManager; import jalview.util.MessageManager; @@ -65,6 +66,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; +import java.util.ArrayList; import java.util.Hashtable; import java.util.List; import java.util.StringTokenizer; @@ -92,7 +94,7 @@ public class JalviewLite extends Applet implements } // ///////////////////////////////////////// - // The following public methods maybe called + // The following public methods may be called // externally, eg via javascript in HTML page /* * (non-Javadoc) @@ -956,11 +958,16 @@ public class JalviewLite extends Applet implements } } - /* - * (non-Javadoc) - * + /** + * Callable from javascript to register a javascript function to pass events + * to a structure viewer. + * + * @param listener + * the name of a javascript function + * @param modelSet + * a token separated list of PDB file names listened for * @see jalview.bin.JalviewLiteJsApi#setStructureListener(java.lang.String, - * java.lang.String) + * java.lang.String) */ public void setStructureListener(String listener, String modelSet) { @@ -974,8 +981,8 @@ public class JalviewLite extends Applet implements return; } } - jalview.javascript.MouseOverStructureListener mol = new jalview.javascript.MouseOverStructureListener( - this, listener, separatorListToArray(modelSet)); + MouseOverStructureListener mol = new MouseOverStructureListener(this, + listener, separatorListToArray(modelSet)); javascriptListeners.addElement(mol); StructureSelectionManager.getStructureSelectionManager(this) .addStructureViewerListener(mol); @@ -1345,9 +1352,10 @@ public class JalviewLite extends Applet implements /** * init method for Jalview Applet */ + @Override public void init() { - // remove any handlers that might be hanging around from an earlier instance + debug = TRUE.equalsIgnoreCase(getParameter("debug")); try { if (debug) @@ -1370,23 +1378,17 @@ public class JalviewLite extends Applet implements ex.printStackTrace(); } } - /** - * turn on extra applet debugging - */ - debug = TRUE.equalsIgnoreCase(getParameter("debug")); + if (debug) { - System.err.println("JalviewLite Version " + getVersion()); System.err.println("Build Date : " + getBuildDate()); System.err.println("Installation : " + getInstallation()); - } String externalsviewer = getParameter("externalstructureviewer"); if (externalsviewer != null) { - useXtrnalSviewer = externalsviewer.trim().toLowerCase() - .equals(TRUE); + useXtrnalSviewer = externalsviewer.trim().toLowerCase().equals(TRUE); } /** * if true disable the check for jmol @@ -1412,7 +1414,9 @@ public class JalviewLite extends Applet implements } else { - throw new Error(MessageManager.getString("error.invalid_separator_parameter")); + throw new Error( + MessageManager + .getString("error.invalid_separator_parameter")); } } int r = 255; @@ -1788,27 +1792,70 @@ public class JalviewLite extends Applet implements * update the protocol state variable for accessing the datasource located * by file. * - * @param file + * @param path * @return possibly updated datasource string */ - public String setProtocolState(String file) + public String resolveFileProtocol(String path) { - if (file.startsWith("PASTE")) + /* + * paste data + */ + if (path.startsWith("PASTE")) { - file = file.substring(5); protocol = AppletFormatAdapter.PASTE; + return path.substring(5); } - else if (inArchive(file)) + + /* + * a URL + */ + if (path.indexOf("://") != -1) { - protocol = AppletFormatAdapter.CLASSLOADER; + protocol = AppletFormatAdapter.URL; + return path; } - else + + /* + * relative to document root + */ + URL documentBase = getDocumentBase(); + String url = resolveUrlForLocalOrAbsolute(path, documentBase); + if (urlExists(url)) + { + if (debug) + { + System.err.println("Prepended document base '" + documentBase + + "' to make: '" + url + "'"); + } + protocol = AppletFormatAdapter.URL; + return url; + } + + /* + * relative to codebase + */ + URL codeBase = getCodeBase(); + url = applet.resolveUrlForLocalOrAbsolute(path, codeBase); + if (urlExists(url)) { - file = addProtocol(file); protocol = AppletFormatAdapter.URL; + if (debug) + { + System.err.println("Prepended codebase '" + codeBase + + "' to make: '" + url + "'"); + } + return url; + } + + /* + * locatable by classloader; test for this last as files in the document + * root may also be found by the classloader + */ + if (inArchive(path)) + { + protocol = AppletFormatAdapter.CLASSLOADER; } - dbgMsg("Protocol identified as '" + protocol + "'"); - return file; + return path; } public LoadingThread(String file, String file2, JalviewLite _applet) @@ -1884,8 +1931,11 @@ public class JalviewLite extends Applet implements { AlignmentI al1 = af.viewport.getAlignment(); AlignmentI al2 = af2.viewport.getAlignment(); - if (AlignmentUtils.isMappable(al1, al2)) + AlignmentI cdna = al1.isNucleotide() ? al1 : al2; + AlignmentI prot = al1.isNucleotide() ? al2 : al1; + if (AlignmentUtils.mapProteinAlignmentToCdna(prot, cdna)) { + al2.alignAs(al1); SplitFrame sf = new SplitFrame(af, af2); sf.addToDisplay(embedded, JalviewLite.this); return; @@ -1915,13 +1965,14 @@ public class JalviewLite extends Applet implements { return null; } - String resolvedFile = setProtocolState(fileParam); + String resolvedFile = resolveFileProtocol(fileParam); String format = new IdentifyFile().Identify(resolvedFile, protocol); dbgMsg("File identified as '" + format + "'"); AlignmentI al = null; try { - al = new AppletFormatAdapter().readFile(resolvedFile, protocol, format); + al = new AppletFormatAdapter().readFile(resolvedFile, protocol, + format); if ((al != null) && (al.getHeight() > 0)) { dbgMsg("Successfully loaded file."); @@ -1939,13 +1990,13 @@ public class JalviewLite extends Applet implements if (protocol == AppletFormatAdapter.PASTE) { newAlignFrame.setTitle(MessageManager.formatMessage( - "label.sequences_from", new Object[] - { applet.getDocumentBase().toString() })); + "label.sequences_from", new Object[] { applet + .getDocumentBase().toString() })); } newAlignFrame.statusBar.setText(MessageManager.formatMessage( - "label.successfully_loaded_file", new Object[] - { resolvedFile })); + "label.successfully_loaded_file", + new Object[] { resolvedFile })); return newAlignFrame; } @@ -2040,18 +2091,17 @@ public class JalviewLite extends Applet implements String sequence = applet.getParameter("PDBSEQ"); if (sequence != null) { - seqs = new SequenceI[] - { matcher == null ? (Sequence) alignFrame.getAlignViewport() - .getAlignment().findName(sequence) : matcher - .findIdMatch(sequence) }; + seqs = new SequenceI[] { matcher == null ? (Sequence) alignFrame + .getAlignViewport().getAlignment().findName(sequence) + : matcher.findIdMatch(sequence) }; } } else { param = st.nextToken(); - Vector tmp = new Vector(); - Vector tmp2 = new Vector(); + List tmp = new ArrayList(); + List tmp2 = new ArrayList(); while (st.hasMoreTokens()) { @@ -2060,39 +2110,22 @@ public class JalviewLite extends Applet implements if (st2.countTokens() > 1) { // This is the chain - tmp2.addElement(st2.nextToken()); + tmp2.add(st2.nextToken()); seqstring = st2.nextToken(); } - tmp.addElement(matcher == null ? (Sequence) alignFrame + tmp.add(matcher == null ? (Sequence) alignFrame .getAlignViewport().getAlignment() .findName(seqstring) : matcher.findIdMatch(seqstring)); } - seqs = new SequenceI[tmp.size()]; - tmp.copyInto(seqs); + seqs = tmp.toArray(new SequenceI[tmp.size()]); if (tmp2.size() == tmp.size()) { - chains = new String[tmp2.size()]; - tmp2.copyInto(chains); + chains = tmp2.toArray(new String[tmp2.size()]); } } - param = setProtocolState(param); - - if (// !jmolAvailable - // && - protocol == AppletFormatAdapter.CLASSLOADER && !useXtrnalSviewer) - { - // Re: JAL-357 : the bug isn't a problem if we are using an - // external viewer! - // TODO: verify this Re: - // https://mantis.lifesci.dundee.ac.uk/view.php?id=36605 - // This exception preserves the current behaviour where, even if - // the local pdb file was identified in the class loader - protocol = AppletFormatAdapter.URL; // this is probably NOT - // CORRECT! - param = addProtocol(param); // - } - + param = resolveFileProtocol(param); + // TODO check JAL-357 for files in a jar (CLASSLOADER) pdb.setFile(param); if (seqs != null) @@ -2125,8 +2158,8 @@ public class JalviewLite extends Applet implements } else { - pdbs.addElement(new Object[] - { pdb, seqs, chains, new String(protocol) }); + pdbs.addElement(new Object[] { pdb, seqs, chains, + new String(protocol) }); } } } @@ -2169,7 +2202,7 @@ public class JalviewLite extends Applet implements { try { - param = setProtocolState(param); + param = resolveFileProtocol(param); JPredFile predictions = new JPredFile(param, protocol); JnetAnnotationMaker.add_annotation(predictions, alignFrame.viewport.getAlignment(), 0, false); @@ -2204,7 +2237,7 @@ public class JalviewLite extends Applet implements String param = applet.getParameter("annotations"); if (param != null) { - param = setProtocolState(param); + param = resolveFileProtocol(param); if (new AnnotationFile().annotateAlignmentView(alignFrame.viewport, param, protocol)) @@ -2257,7 +2290,7 @@ public class JalviewLite extends Applet implements param = applet.getParameter("features"); if (param != null) { - param = setProtocolState(param); + param = resolveFileProtocol(param); result = alignFrame.parseFeaturesFile(param, protocol); } @@ -2326,7 +2359,7 @@ public class JalviewLite extends Applet implements { try { - treeFile = setProtocolState(treeFile); + treeFile = resolveFileProtocol(treeFile); NewickFile fin = new NewickFile(treeFile, protocol); fin.parse(); @@ -2351,26 +2384,26 @@ public class JalviewLite extends Applet implements /** * Discovers whether the given file is in the Applet Archive * - * @param file + * @param f * String * @return boolean */ - boolean inArchive(String file) + boolean inArchive(String f) { // This might throw a security exception in certain browsers // Netscape Communicator for instance. try { - boolean rtn = (getClass().getResourceAsStream("/" + file) != null); + boolean rtn = (getClass().getResourceAsStream("/" + f) != null); if (debug) { - System.err.println("Resource '" + file + "' was " - + (rtn ? "" : "not") + " located by classloader."); + System.err.println("Resource '" + f + "' was " + + (rtn ? "" : "not ") + "located by classloader."); } return rtn; } catch (Exception ex) { - System.out.println("Exception checking resources: " + file + " " + System.out.println("Exception checking resources: " + f + " " + ex); return false; } @@ -2395,14 +2428,14 @@ public class JalviewLite extends Applet implements * Try relative to document base */ URL documentBase = getDocumentBase(); + System.err.println("Trying documentbase: " + documentBase); String url = applet.resolveUrlForLocalOrAbsolute(f, documentBase); if (urlExists(url)) { - if (debug) + if (true/* debug */) { System.err.println("Prepended document base '" + documentBase - + "' to make: '" + url - + "'"); + + "' to make: '" + url + "'"); } return url; } @@ -2411,10 +2444,11 @@ public class JalviewLite extends Applet implements * Try relative to codebase */ URL codeBase = getCodeBase(); + System.err.println("Trying codebase: " + codeBase); url = applet.resolveUrlForLocalOrAbsolute(f, codeBase); if (urlExists(url)) { - if (debug) + if (true/* debug */) { System.err.println("Prepended codebase '" + codeBase + "' to make: '" + url + "'"); @@ -2512,8 +2546,9 @@ public class JalviewLite extends Applet implements * @param separator * @return elements separated by separator */ - public String[] separatorListToArray(String list, String separator) + public static String[] separatorListToArray(String list, String separator) { + // TODO use StringUtils version (slightly different...) int seplen = separator.length(); if (list == null || list.equals("") || list.equals(separator)) { @@ -2579,8 +2614,9 @@ public class JalviewLite extends Applet implements * @param separator * @return concatenated string */ - public String arrayToSeparatorList(String[] list, String separator) + public static String arrayToSeparatorList(String[] list, String separator) { + // TODO use StringUtils version StringBuffer v = new StringBuffer(); if (list != null && list.length > 0) { @@ -2898,36 +2934,51 @@ public class JalviewLite extends Applet implements * form a complete URL given a path to a resource and a reference location on * the same server * - * @param url + * @param targetPath * - an absolute path on the same server as localref or a document * located relative to localref * @param localref * - a URL on the same server as url * @return a complete URL for the resource located by url */ - private String resolveUrlForLocalOrAbsolute(String url, URL localref) + private String resolveUrlForLocalOrAbsolute(String targetPath, + URL localref) { - String codebase = localref.toString(); - if (url.indexOf("/") == 0) + String resolvedPath = ""; + + /* + * get URL path and strip off any trailing file e.g. + * www.jalview.org/examples/index.html#applets?a=b is trimmed to + * www.jalview.org/examples/ + */ + String urlPath = localref.toString(); + String directoryPath = urlPath; + int lastSeparator = directoryPath.lastIndexOf("/"); + if (lastSeparator > 0) + { + directoryPath = directoryPath.substring(0, lastSeparator + 1); + } + + if (targetPath.startsWith("/")) { + /* + * construct absolute URL to a file on the server + */ String localfile = localref.getFile(); - url = codebase.substring(0, codebase.length() - - localfile.length()) - + url; + resolvedPath = urlPath.substring(0, + urlPath.length() - localfile.length()) + + targetPath; } else { - url = localref + url; + resolvedPath = directoryPath + targetPath; } if (debug) { - System.err.println("URL: " + localref.toString()); - System.err.println("URL.getFile: " + localref.getFile()); - System.err.println("URL.getPath: " + localref.getPath()); - System.err.println("URL.getQuery: " + localref.getQuery()); - System.err.println("returning " + url); + System.err.println("resolveUrlForLocalOrAbsolute returning " + + resolvedPath); } - return url; + return resolvedPath; } /** @@ -2949,8 +3000,8 @@ public class JalviewLite extends Applet implements URL prepend; url = resolveUrlForLocalOrAbsolute( url, - prepend = getDefaultParameter("resolvetocodebase", false) ? getDocumentBase() - : getCodeBase()); + prepend = getDefaultParameter("resolvetocodebase", false) ? getCodeBase() + : getDocumentBase()); if (debug) { System.err