+ frame.setLocation(lastFrameX, lastFrameY);
+ lastFrameX += 40;
+ lastFrameY += 40;
+ frame.setSize(width, height);
+ frame.setTitle(title);
+ frame.addWindowListener(new WindowAdapter()
+ {
+ @Override
+ 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();
+ }
+
+ @Override
+ 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
+ */
+ @Override
+ 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;
+
+ @Override
+ 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: protocol for access to file source
+ */
+ DataSourceType protocol;
+
+ String _file; // alignment file or URL spec
+
+ String _file2; // second alignment file or URL spec
+
+ JalviewLite applet;
+
+ private void dbgMsg(String msg)
+ {
+ if (JalviewLite.debug)
+ {
+ System.err.println(msg);
+ }
+ }
+
+ /**
+ * update the protocol state variable for accessing the datasource located
+ * by file.
+ *
+ * @param path
+ * @return possibly updated datasource string
+ */
+ public String resolveFileProtocol(String path)
+ {
+ /*
+ * is it paste data?
+ */
+ if (path.startsWith("PASTE"))
+ {
+ protocol = DataSourceType.PASTE;
+ return path.substring(5);
+ }
+
+ /*
+ * is it a URL?
+ */
+ if (path.indexOf("://") != -1)
+ {
+ protocol = DataSourceType.URL;
+ return path;
+ }
+
+ /*
+ * try relative to document root
+ */
+ URL documentBase = getDocumentBase();
+ String withDocBase = resolveUrlForLocalOrAbsolute(path, documentBase);
+ if (HttpUtils.isValidUrl(withDocBase))
+ {
+ if (debug)
+ {
+ System.err.println("Prepended document base '" + documentBase
+ + "' to make: '" + withDocBase + "'");
+ }
+ protocol = DataSourceType.URL;
+ return withDocBase;
+ }
+
+ /*
+ * try relative to codebase (if different to document base)
+ */
+ URL codeBase = getCodeBase();
+ String withCodeBase = applet.resolveUrlForLocalOrAbsolute(path,
+ codeBase);
+ if (!withCodeBase.equals(withDocBase)
+ && HttpUtils.isValidUrl(withCodeBase))
+ {
+ protocol = DataSourceType.URL;
+ if (debug)
+ {
+ System.err.println("Prepended codebase '" + codeBase
+ + "' to make: '" + withCodeBase + "'");
+ }
+ return withCodeBase;
+ }
+
+ /*
+ * try locating by classloader; try this last so files in the directory
+ * are resolved using document base
+ */
+ if (inArchive(path))
+ {
+ protocol = DataSourceType.CLASSLOADER;
+ }
+ return path;
+ }
+
+ public LoadingThread(String file, String file2, JalviewLite _applet)
+ {
+ this._file = file;
+ this._file2 = file2;
+ applet = _applet;
+ }
+
+ @Override
+ 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();
+ }
+
+ /**
+ * Load the alignment and any related files as specified by applet
+ * parameters
+ */
+ private void startLoading()
+ {
+ dbgMsg("Loading thread started with:\n>>file\n" + _file + ">>endfile");
+
+ dbgMsg("Loading started.");
+
+ AlignFrame newAlignFrame = readAlignment(_file);
+ AlignFrame newAlignFrame2 = readAlignment(_file2);
+ if (newAlignFrame != null)
+ {
+ addToDisplay(newAlignFrame, newAlignFrame2);
+ loadTree(newAlignFrame);
+
+ loadScoreFile(newAlignFrame);
+
+ loadFeatures(newAlignFrame);
+
+ loadAnnotations(newAlignFrame);
+
+ loadJnetFile(newAlignFrame);
+
+ loadPdbFiles(newAlignFrame);
+ }
+ else
+ {
+ fileFound = false;
+ applet.remove(launcher);
+ applet.repaint();
+ }
+ callInitCallback();
+ }
+
+ /**
+ * Add an AlignFrame to the display; or if two are provided, a SplitFrame.
+ *
+ * @param af
+ * @param af2
+ */
+ public void addToDisplay(AlignFrame af, AlignFrame af2)
+ {
+ if (af2 != null)
+ {
+ AlignmentI al1 = af.viewport.getAlignment();
+ AlignmentI al2 = af2.viewport.getAlignment();
+ 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;
+ }
+ else
+ {
+ String msg = "Could not map any sequence in " + af2.getTitle()
+ + " as "
+ + (al1.isNucleotide() ? "protein product" : "cDNA")
+ + " for " + af.getTitle();
+ System.err.println(msg);
+ }
+ }
+
+ af.addToDisplay(embedded);
+ }
+
+ /**
+ * Read the alignment file (from URL, text 'paste', or archive by
+ * classloader).
+ *
+ * @return
+ */
+ protected AlignFrame readAlignment(String fileParam)
+ {
+ if (fileParam == null)
+ {
+ return null;
+ }
+ String resolvedFile = resolveFileProtocol(fileParam);
+ AlignmentI al = null;
+ try
+ {
+ FileFormatI format = new IdentifyFile().identify(resolvedFile,
+ protocol);
+ dbgMsg("File identified as '" + format + "'");
+ al = new AppletFormatAdapter().readFile(resolvedFile, protocol,
+ format);
+ if ((al != null) && (al.getHeight() > 0))
+ {
+ dbgMsg("Successfully loaded file.");
+ al.setDataset(null);
+ AlignFrame newAlignFrame = new AlignFrame(al, applet,
+ resolvedFile, embedded, false);
+ newAlignFrame.setTitle(resolvedFile);
+ if (initialAlignFrame == null)
+ {
+ initialAlignFrame = newAlignFrame;
+ }
+ // update the focus.
+ currentAlignFrame = newAlignFrame;
+
+ if (protocol == DataSourceType.PASTE)
+ {
+ newAlignFrame.setTitle(MessageManager.formatMessage(
+ "label.sequences_from", new Object[] { applet
+ .getDocumentBase().toString() }));
+ }
+
+ newAlignFrame.statusBar.setText(MessageManager.formatMessage(
+ "label.successfully_loaded_file",
+ new Object[] { resolvedFile }));
+
+ return newAlignFrame;
+ }
+ } catch (java.io.IOException ex)
+ {
+ dbgMsg("File load exception.");
+ ex.printStackTrace();
+ if (debug)
+ {
+ try
+ {
+ FileParse fp = new FileParse(resolvedFile, protocol);
+ String ln = null;
+ dbgMsg(">>>Dumping contents of '" + resolvedFile + "' " + "("
+ + 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();
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Load PDBFiles if any specified by parameter(s). Returns true if loaded,
+ * else false.
+ *
+ * @param alignFrame
+ * @return
+ */
+ protected boolean loadPdbFiles(AlignFrame alignFrame)
+ {
+ boolean result = false;
+ /*
+ * <param name="alignpdbfiles" value="false/true"/> Undocumented for 2.6 -
+ * related to JAL-434
+ */
+
+ applet.setAlignPdbStructures(getDefaultParameter("alignpdbfiles",
+ false));
+ /*
+ * <param name="PDBfile" value="1gaq.txt PDB|1GAQ|1GAQ|A PDB|1GAQ|1GAQ|B
+ * PDB|1GAQ|1GAQ|C">
+ *
+ * <param name="PDBfile2" value="1gaq.txt A=SEQA B=SEQB C=SEQB">
+ *
+ * <param name="PDBfile3" value="1q0o Q45135_9MICO">
+ */
+
+ int pdbFileCount = 0;
+ // Accumulate pdbs here if they are heading for the same view (if
+ // alignPdbStructures is true)
+ Vector pdbs = new Vector();
+ // create a lazy matcher if we're asked to
+ jalview.analysis.SequenceIdMatcher matcher = (applet
+ .getDefaultParameter("relaxedidmatch", false)) ? new jalview.analysis.SequenceIdMatcher(
+ alignFrame.getAlignViewport().getAlignment()
+ .getSequencesArray()) : null;
+
+ String param;
+ do
+ {
+ if (pdbFileCount > 0)
+ {
+ param = applet.getParameter("PDBFILE" + pdbFileCount);
+ }
+ else
+ {
+ param = applet.getParameter("PDBFILE");
+ }
+
+ if (param != null)
+ {
+ PDBEntry pdb = new PDBEntry();
+
+ String seqstring;
+ SequenceI[] seqs = null;
+ String[] chains = null;
+
+ StringTokenizer st = new StringTokenizer(param, " ");
+
+ if (st.countTokens() < 2)
+ {
+ String sequence = applet.getParameter("PDBSEQ");
+ if (sequence != null)
+ {
+ seqs = new SequenceI[] { matcher == null ? (Sequence) alignFrame
+ .getAlignViewport().getAlignment().findName(sequence)
+ : matcher.findIdMatch(sequence) };
+ }
+
+ }
+ else
+ {
+ param = st.nextToken();
+ List<SequenceI> tmp = new ArrayList<SequenceI>();
+ List<String> tmp2 = new ArrayList<String>();
+
+ while (st.hasMoreTokens())
+ {
+ seqstring = st.nextToken();
+ StringTokenizer st2 = new StringTokenizer(seqstring, "=");
+ if (st2.countTokens() > 1)
+ {
+ // This is the chain
+ tmp2.add(st2.nextToken());
+ seqstring = st2.nextToken();
+ }
+ tmp.add(matcher == null ? (Sequence) alignFrame
+ .getAlignViewport().getAlignment()
+ .findName(seqstring) : matcher.findIdMatch(seqstring));
+ }
+
+ seqs = tmp.toArray(new SequenceI[tmp.size()]);
+ if (tmp2.size() == tmp.size())
+ {
+ chains = tmp2.toArray(new String[tmp2.size()]);
+ }
+ }
+ param = resolveFileProtocol(param);
+ // TODO check JAL-357 for files in a jar (CLASSLOADER)
+ pdb.setFile(param);
+
+ if (seqs != null)
+ {
+ for (int i = 0; i < seqs.length; i++)
+ {
+ if (seqs[i] != null)
+ {
+ ((Sequence) seqs[i]).addPDBId(pdb);
+ StructureSelectionManager.getStructureSelectionManager(
+ applet).registerPDBEntry(pdb);
+ }
+ else
+ {
+ if (JalviewLite.debug)
+ {
+ // this may not really be a problem but we give a warning
+ // anyway
+ System.err
+ .println("Warning: Possible input parsing error: Null sequence for attachment of PDB (sequence "
+ + i + ")");
+ }
+ }
+ }
+
+ if (!alignPdbStructures)
+ {
+ alignFrame.newStructureView(applet, pdb, seqs, chains,
+ protocol);
+ }
+ else
+ {
+ pdbs.addElement(new Object[] { pdb, seqs, chains, protocol });
+ }
+ }
+ }
+
+ pdbFileCount++;
+ } while (param != null || pdbFileCount < 10);
+ if (pdbs.size() > 0)
+ {
+ SequenceI[][] seqs = new SequenceI[pdbs.size()][];
+ PDBEntry[] pdb = new PDBEntry[pdbs.size()];
+ String[][] chains = new String[pdbs.size()][];
+ String[] protocols = new String[pdbs.size()];
+ for (int pdbsi = 0, pdbsiSize = pdbs.size(); pdbsi < pdbsiSize; pdbsi++)
+ {
+ Object[] o = (Object[]) pdbs.elementAt(pdbsi);
+ pdb[pdbsi] = (PDBEntry) o[0];
+ seqs[pdbsi] = (SequenceI[]) o[1];
+ chains[pdbsi] = (String[]) o[2];
+ protocols[pdbsi] = (String) o[3];
+ }
+ alignFrame.alignedStructureView(applet, pdb, seqs, chains,
+ protocols);
+ result = true;
+ }
+ return result;
+ }
+
+ /**
+ * Load in a Jnetfile if specified by parameter. Returns true if loaded,
+ * else false.
+ *
+ * @param alignFrame
+ * @return
+ */
+ protected boolean loadJnetFile(AlignFrame alignFrame)
+ {
+ boolean result = false;
+ String param = applet.getParameter("jnetfile");
+ if (param == null)
+ {
+ // jnet became jpred around 2016
+ param = applet.getParameter("jpredfile");
+ }
+ if (param != null)
+ {
+ try
+ {
+ param = resolveFileProtocol(param);
+ JPredFile predictions = new JPredFile(param, protocol);
+ JnetAnnotationMaker.add_annotation(predictions,
+ alignFrame.viewport.getAlignment(), 0, false);
+ // false == do not add sequence profile from concise output
+ SequenceI repseq = alignFrame.viewport.getAlignment()
+ .getSequenceAt(0);
+ alignFrame.viewport.getAlignment().setSeqrep(repseq);
+ HiddenColumns cs = new HiddenColumns();
+ cs.hideInsertionsFor(repseq);
+ alignFrame.viewport.getAlignment().setHiddenColumns(cs);
+ alignFrame.alignPanel.fontChanged();
+ alignFrame.alignPanel.setScrollValues(0, 0);
+ result = true;
+ } catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Load annotations if specified by parameter. Returns true if loaded, else
+ * false.
+ *
+ * @param alignFrame
+ * @return
+ */
+ protected boolean loadAnnotations(AlignFrame alignFrame)
+ {
+ boolean result = false;
+ String param = applet.getParameter("annotations");
+ if (param != null)
+ {
+ param = resolveFileProtocol(param);
+
+ if (new AnnotationFile().annotateAlignmentView(alignFrame.viewport,
+ param, protocol))
+ {
+ alignFrame.alignPanel.fontChanged();
+ alignFrame.alignPanel.setScrollValues(0, 0);
+ result = true;
+ }
+ else
+ {
+ System.err
+ .println("Annotations were not added from annotation file '"
+ + param + "'");
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Load features file and view settings as specified by parameters. Returns
+ * true if features were loaded, else false.
+ *
+ * @param alignFrame
+ * @return
+ */
+ protected boolean loadFeatures(AlignFrame alignFrame)
+ {
+ boolean result = false;
+ // ///////////////////////////
+ // modify display of features
+ // we do this before any features have been loaded, ensuring any hidden
+ // groups are hidden when features first displayed
+ //
+ // hide specific groups
+ //
+ String param = applet.getParameter("hidefeaturegroups");
+ if (param != null)
+ {
+ alignFrame.setFeatureGroupState(separatorListToArray(param), false);
+ // applet.setFeatureGroupStateOn(newAlignFrame, param, false);
+ }
+ // show specific groups
+ param = applet.getParameter("showfeaturegroups");
+ if (param != null)
+ {
+ alignFrame.setFeatureGroupState(separatorListToArray(param), true);
+ // applet.setFeatureGroupStateOn(newAlignFrame, param, true);
+ }
+ // and now load features
+ param = applet.getParameter("features");
+ if (param != null)
+ {
+ param = resolveFileProtocol(param);
+
+ result = alignFrame.parseFeaturesFile(param, protocol);
+ }
+
+ param = applet.getParameter("showFeatureSettings");
+ if (param != null && param.equalsIgnoreCase(TRUE))
+ {
+ alignFrame.viewport.setShowSequenceFeatures(true);
+ new FeatureSettings(alignFrame.alignPanel);
+ }
+ return result;
+ }
+
+ /**
+ * Load a score file if specified by parameter. Returns true if file was
+ * loaded, else false.
+ *
+ * @param alignFrame
+ */
+ protected boolean loadScoreFile(AlignFrame alignFrame)
+ {
+ boolean result = false;
+ String sScoreFile = applet.getParameter("scoreFile");
+ if (sScoreFile != null && !"".equals(sScoreFile))
+ {
+ try
+ {
+ if (debug)
+ {
+ System.err
+ .println("Attempting to load T-COFFEE score file from the scoreFile parameter");
+ }
+ result = alignFrame.loadScoreFile(sScoreFile);
+ if (!result)
+ {
+ System.err
+ .println("Failed to parse T-COFFEE parameter as a valid score file ('"
+ + sScoreFile + "')");
+ }
+ } catch (Exception e)
+ {
+ System.err.printf("Cannot read score file: '%s'. Cause: %s \n",
+ sScoreFile, e.getMessage());
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Load a tree for the alignment if specified by parameter. Returns true if
+ * a tree was loaded, else false.
+ *
+ * @param alignFrame
+ * @return
+ */
+ protected boolean loadTree(AlignFrame alignFrame)
+ {
+ boolean result = false;
+ String treeFile = applet.getParameter("tree");
+ if (treeFile == null)
+ {
+ treeFile = applet.getParameter("treeFile");
+ }
+
+ if (treeFile != null)
+ {
+ try
+ {
+ treeFile = resolveFileProtocol(treeFile);
+ NewickFile fin = new NewickFile(treeFile, protocol);
+ fin.parse();
+
+ if (fin.getTree() != null)
+ {
+ alignFrame.loadTree(fin, treeFile);
+ result = true;
+ dbgMsg("Successfully imported tree.");
+ }
+ else
+ {
+ dbgMsg("Tree parameter did not resolve to a valid tree.");
+ }
+ } catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Discovers whether the given file is in the Applet Archive
+ *
+ * @param f
+ * String
+ * @return boolean
+ */
+ boolean inArchive(String f)
+ {
+ // This might throw a security exception in certain browsers
+ // Netscape Communicator for instance.
+ try
+ {
+ boolean rtn = (getClass().getResourceAsStream("/" + f) != null);
+ if (debug)
+ {
+ System.err.println("Resource '" + f + "' was "
+ + (rtn ? "" : "not ") + "located by classloader.");
+ }
+ return rtn;
+ } catch (Exception ex)
+ {
+ System.out.println("Exception checking resources: " + f + " " + ex);
+ return false;
+ }
+ }
+ }
+
+ /**
+ * @return the default alignFrame acted on by the public applet methods. May
+ * return null with an error message on System.err indicating the
+ * fact.
+ */
+ public AlignFrame getDefaultTargetFrame()
+ {
+ if (currentAlignFrame != null)
+ {
+ return currentAlignFrame;
+ }
+ if (initialAlignFrame != null)
+ {
+ return initialAlignFrame;
+ }
+ System.err
+ .println("Implementation error: Jalview Applet API cannot work out which AlignFrame to use.");
+ return null;
+ }
+
+ /**
+ * separator used for separatorList
+ */
+ protected String separator = "" + ((char) 0x00AC); // the default used to be
+ // '|' but many sequence
+ // IDS include pipes.
+
+ /**
+ * set to enable the URL based javascript execution mechanism
+ */
+ public boolean jsfallbackEnabled = false;
+
+ /**
+ * parse the string into a list
+ *
+ * @param list
+ * @return elements separated by separator
+ */
+ public String[] separatorListToArray(String list)
+ {
+ return separatorListToArray(list, separator);
+ }
+
+ /**
+ * parse the string into a list
+ *
+ * @param list
+ * @param separator
+ * @return elements separated by 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))
+ {
+ return null;
+ }
+ java.util.Vector jv = new Vector();
+ int cp = 0, pos;
+ while ((pos = list.indexOf(separator, cp)) > cp)
+ {
+ jv.addElement(list.substring(cp, pos));
+ cp = pos + seplen;
+ }
+ if (cp < list.length())
+ {
+ String c = list.substring(cp);
+ if (!c.equals(separator))
+ {
+ jv.addElement(c);
+ }
+ }
+ if (jv.size() > 0)
+ {
+ String[] v = new String[jv.size()];
+ for (int i = 0; i < v.length; i++)
+ {
+ v[i] = (String) jv.elementAt(i);
+ }
+ jv.removeAllElements();
+ if (debug)
+ {
+ System.err.println("Array from '" + separator
+ + "' separated List:\n" + v.length);
+ for (int i = 0; i < v.length; i++)
+ {
+ System.err.println("item " + i + " '" + v[i] + "'");
+ }
+ }
+ return v;
+ }
+ if (debug)
+ {
+ System.err.println("Empty Array from '" + separator
+ + "' separated List");
+ }
+ return null;
+ }
+
+ /**
+ * concatenate the list with separator
+ *
+ * @param list
+ * @return concatenated string
+ */
+ public String arrayToSeparatorList(String[] list)
+ {
+ return arrayToSeparatorList(list, separator);
+ }
+
+ /**
+ * concatenate the list with separator
+ *
+ * @param list
+ * @param separator
+ * @return concatenated string
+ */
+ public static String arrayToSeparatorList(String[] list, String separator)
+ {
+ // TODO use StringUtils version
+ StringBuffer v = new StringBuffer();
+ if (list != null && list.length > 0)
+ {
+ for (int i = 0, iSize = list.length; i < iSize; i++)
+ {
+ if (list[i] != null)
+ {
+ if (i > 0)
+ {
+ v.append(separator);
+ }
+ v.append(list[i]);
+ }
+ }
+ if (debug)
+ {
+ System.err.println("Returning '" + separator
+ + "' separated List:\n");
+ System.err.println(v);
+ }
+ return v.toString();
+ }
+ if (debug)
+ {
+ System.err.println("Returning empty '" + separator
+ + "' separated List\n");
+ }
+ return "" + separator;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see jalview.bin.JalviewLiteJsApi#getFeatureGroups()
+ */
+ @Override
+ public String getFeatureGroups()
+ {
+ String lst = arrayToSeparatorList(getDefaultTargetFrame()
+ .getFeatureGroups());
+ return lst;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * jalview.bin.JalviewLiteJsApi#getFeatureGroupsOn(jalview.appletgui.AlignFrame
+ * )
+ */
+ @Override
+ public String getFeatureGroupsOn(AlignFrame alf)
+ {
+ String lst = arrayToSeparatorList(alf.getFeatureGroups());
+ return lst;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see jalview.bin.JalviewLiteJsApi#getFeatureGroupsOfState(boolean)
+ */
+ @Override
+ public String getFeatureGroupsOfState(boolean visible)
+ {
+ return arrayToSeparatorList(getDefaultTargetFrame()
+ .getFeatureGroupsOfState(visible));
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * jalview.bin.JalviewLiteJsApi#getFeatureGroupsOfStateOn(jalview.appletgui
+ * .AlignFrame, boolean)
+ */
+ @Override
+ public String getFeatureGroupsOfStateOn(AlignFrame alf, boolean visible)
+ {
+ return arrayToSeparatorList(alf.getFeatureGroupsOfState(visible));
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see jalview.bin.JalviewLiteJsApi#setFeatureGroupStateOn(jalview.appletgui.
+ * AlignFrame, java.lang.String, boolean)
+ */
+ @Override
+ public void setFeatureGroupStateOn(final AlignFrame alf,
+ final String groups, boolean state)
+ {
+ final boolean st = state;// !(state==null || state.equals("") ||
+ // state.toLowerCase().equals("false"));
+ java.awt.EventQueue.invokeLater(new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ alf.setFeatureGroupState(separatorListToArray(groups), st);
+ }
+ });
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see jalview.bin.JalviewLiteJsApi#setFeatureGroupState(java.lang.String,
+ * boolean)
+ */
+ @Override
+ public void setFeatureGroupState(String groups, boolean state)
+ {
+ setFeatureGroupStateOn(getDefaultTargetFrame(), groups, state);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see jalview.bin.JalviewLiteJsApi#getSeparator()
+ */
+ @Override
+ public String getSeparator()
+ {
+ return separator;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see jalview.bin.JalviewLiteJsApi#setSeparator(java.lang.String)
+ */
+ @Override
+ public void setSeparator(String separator)
+ {
+ if (separator == null || separator.length() < 1)
+ {
+ // reset to default
+ separator = "" + ((char) 0x00AC);
+ }
+ this.separator = separator;
+ if (debug)
+ {
+ System.err.println("Default Separator now: '" + separator + "'");
+ }
+ }
+
+ /**
+ * get boolean value of applet parameter 'name' and return default if
+ * parameter is not set
+ *
+ * @param name
+ * name of paremeter
+ * @param def
+ * the value to return otherwise
+ * @return true or false
+ */
+ public boolean getDefaultParameter(String name, boolean def)
+ {
+ String stn;
+ if ((stn = getParameter(name)) == null)
+ {
+ return def;
+ }
+ if (TRUE.equalsIgnoreCase(stn))
+ {
+ return true;
+ }
+ return false;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see jalview.bin.JalviewLiteJsApi#addPdbFile(jalview.appletgui.AlignFrame,
+ * java.lang.String, java.lang.String, java.lang.String)
+ */
+ @Override
+ public boolean addPdbFile(AlignFrame alFrame, String sequenceId,
+ String pdbEntryString, String pdbFile)
+ {
+ return alFrame.addPdbFile(sequenceId, pdbEntryString, pdbFile);
+ }
+
+ protected void setAlignPdbStructures(boolean alignPdbStructures)
+ {
+ this.alignPdbStructures = alignPdbStructures;
+ }
+
+ public boolean isAlignPdbStructures()
+ {
+ return alignPdbStructures;
+ }
+
+ @Override
+ public void start()
+ {
+ // callInitCallback();
+ }
+
+ private Hashtable<String, long[]> jshashes = new Hashtable<String, long[]>();
+
+ private Hashtable<String, Hashtable<String, String[]>> jsmessages = new Hashtable<String, Hashtable<String, String[]>>();
+
+ public void setJsMessageSet(String messageclass, String viewId,
+ String[] colcommands)
+ {
+ Hashtable<String, String[]> msgset = jsmessages.get(messageclass);
+ if (msgset == null)
+ {
+ msgset = new Hashtable<String, String[]>();
+ jsmessages.put(messageclass, msgset);
+ }
+ msgset.put(viewId, colcommands);
+ long[] l = new long[colcommands.length];
+ for (int i = 0; i < colcommands.length; i++)
+ {
+ l[i] = colcommands[i].hashCode();
+ }
+ jshashes.put(messageclass + "|" + viewId, l);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see jalview.bin.JalviewLiteJsApi#getJsMessage(java.lang.String,
+ * java.lang.String)
+ */
+ @Override
+ public String getJsMessage(String messageclass, String viewId)
+ {
+ Hashtable<String, String[]> msgset = jsmessages.get(messageclass);
+ if (msgset != null)
+ {
+ String[] msgs = msgset.get(viewId);
+ if (msgs != null)
+ {
+ for (int i = 0; i < msgs.length; i++)
+ {
+ if (msgs[i] != null)
+ {
+ String m = msgs[i];
+ msgs[i] = null;
+ return m;
+ }
+ }
+ }
+ }
+ return "";
+ }
+
+ public boolean isJsMessageSetChanged(String string, String string2,
+ String[] colcommands)
+ {
+ long[] l = jshashes.get(string + "|" + string2);
+ if (l == null && colcommands != null)
+ {
+ return true;
+ }
+ for (int i = 0; i < colcommands.length; i++)
+ {
+ if (l[i] != colcommands[i].hashCode())
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private Vector jsExecQueue = new Vector();
+
+ public Vector getJsExecQueue()
+ {
+ return jsExecQueue;
+ }
+
+ public void setExecutor(JSFunctionExec jsFunctionExec2)
+ {
+ jsFunctionExec = jsFunctionExec2;
+ }
+
+ /**
+ * return the given colour value parameter or the given default if parameter
+ * not given
+ *
+ * @param colparam
+ * @param defcolour
+ * @return
+ */
+ public Color getDefaultColourParameter(String colparam, Color defcolour)
+ {
+ String colprop = getParameter(colparam);
+ if (colprop == null || colprop.trim().length() == 0)
+ {
+ return defcolour;
+ }
+ Color col = ColorUtils.parseColourString(colprop);
+ if (col == null)
+ {
+ System.err.println("Couldn't parse '" + colprop
+ + "' as a colour for " + colparam);
+ }
+ return (col == null) ? defcolour : col;
+ }
+
+ public void openJalviewHelpUrl()
+ {
+ String helpUrl = getParameter("jalviewhelpurl");
+ if (helpUrl == null || helpUrl.trim().length() < 5)
+ {
+ helpUrl = "http://www.jalview.org/help.html";
+ }
+ showURL(helpUrl, "HELP");
+ }
+
+ /**
+ * form a complete URL given a path to a resource and a reference location on
+ * the same server
+ *
+ * @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 targetPath,
+ URL localref)
+ {
+ String resolvedPath = "";
+ if (targetPath.startsWith("/"))
+ {
+ String codebase = localref.toString();
+ String localfile = localref.getFile();
+ resolvedPath = codebase.substring(0,
+ codebase.length() - localfile.length())
+ + targetPath;
+ return 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 - this is not allowed?
+ */
+ // String localfile = localref.getFile();
+ // resolvedPath = urlPath.substring(0,
+ // urlPath.length() - localfile.length())
+ // + targetPath;
+ resolvedPath = directoryPath + targetPath.substring(1);
+ }
+ else
+ {
+ resolvedPath = directoryPath + targetPath;
+ }
+ if (debug)
+ {
+ System.err.println("resolveUrlForLocalOrAbsolute returning "
+ + resolvedPath);
+ }
+ return resolvedPath;
+ }
+
+ /**
+ * open a URL in the browser - resolving it according to relative refs and
+ * coping with javascript: protocol if necessary.
+ *
+ * @param url
+ * @param target
+ */
+ public void showURL(String url, String target)
+ {
+ try
+ {
+ if (url.indexOf(":") == -1)
+ {
+ // TODO: verify (Bas Vroling bug) prepend codebase or server URL to
+ // form valid URL
+ // Should really use docbase, not codebase.
+ URL prepend;
+ url = resolveUrlForLocalOrAbsolute(
+ url,
+ prepend = getDefaultParameter("resolvetocodebase", false) ? getCodeBase()
+ : getDocumentBase());
+ if (debug)
+ {
+ System.err
+ .println("Show url (prepended "
+ + prepend
+ + " - toggle resolvetocodebase if code/docbase resolution is wrong): "
+ + url);
+ }
+ }
+ else
+ {
+ if (debug)
+ {
+ System.err.println("Show url: " + url);
+ }
+ }
+ if (url.indexOf("javascript:") == 0)
+ {
+ // no target for the javascript context
+ getAppletContext().showDocument(new java.net.URL(url));
+ }
+ else
+ {
+ getAppletContext().showDocument(new java.net.URL(url), target);
+ }
+ } catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+
+ /**
+ * bind structures in a viewer to any matching sequences in an alignFrame (use
+ * sequenceIds to limit scope of search to specific sequences)
+ *
+ * @param alFrame
+ * @param viewer
+ * @param sequenceIds
+ * @return TODO: consider making an exception structure for indicating when
+ * binding fails public SequenceStructureBinding
+ * addStructureViewInstance( AlignFrame alFrame, Object viewer, String
+ * sequenceIds) {
+ *
+ * if (sequenceIds != null && sequenceIds.length() > 0) { return
+ * alFrame.addStructureViewInstance(viewer,
+ * separatorListToArray(sequenceIds)); } else { return
+ * alFrame.addStructureViewInstance(viewer, null); } // return null; }
+ */