+ jalview.bin.JalviewLite.addFrame(this, this.getTitle(), frameWidth,
+ frameHeight);
+ }
+ }
+
+ /**
+ * Add the components of this AlignFrame to the applet container.
+ *
+ * @param theApplet
+ */
+ public void embedAlignFrameInApplet(final JalviewLite theApplet)
+ {
+ // ////
+ // Explicitly build the embedded menu panel for the on-page applet
+ //
+ // view cannot be closed if its actually on the page
+ fileMenu.remove(closeMenuItem);
+ fileMenu.remove(3); // Remove Separator
+ // construct embedded menu, using default font
+ embeddedMenu = makeEmbeddedPopupMenu(alignFrameMenuBar, false, false);
+ // and actually add the components to the applet area
+ theApplet.setLayout(new BorderLayout());
+ theApplet.add(embeddedMenu, BorderLayout.NORTH);
+ theApplet.add(statusBar, BorderLayout.SOUTH);
+ // TODO should size be left to the layout manager?
+ alignPanel.setSize(theApplet.getSize().width, theApplet.getSize().height
+ - embeddedMenu.getHeight() - statusBar.getHeight());
+ theApplet.add(alignPanel, BorderLayout.CENTER);
+ final AlignFrame me = this;
+ theApplet.addFocusListener(new FocusListener()
+ {
+
+ @Override
+ public void focusLost(FocusEvent e)
+ {
+ if (theApplet.currentAlignFrame == me)
+ {
+ theApplet.currentAlignFrame = null;
+ }
+ }
+
+ @Override
+ public void focusGained(FocusEvent e)
+ {
+ theApplet.currentAlignFrame = me;
+ }
+ });
+ theApplet.validate();
+ }
+
+ /**
+ * create a new binding between structures in an existing jmol viewer instance
+ * and an alignpanel with sequences that have existing PDBFile entries. Note,
+ * this does not open a new Jmol window, or modify the display of the
+ * structures in the original jmol window. Note This method doesn't work
+ * without an additional javascript library to exchange messages between the
+ * distinct applets. See http://issues.jalview.org/browse/JAL-621
+ *
+ * @param viewer
+ * JmolViewer instance
+ * @param sequenceIds
+ * - sequence Ids to search for associations
+ */
+ public SequenceStructureBinding addStructureViewInstance(
+ Object jmolviewer, String[] sequenceIds)
+ {
+ Viewer viewer = null;
+ try
+ {
+ viewer = (Viewer) jmolviewer;
+ } catch (ClassCastException ex)
+ {
+ System.err.println(
+ "Unsupported viewer object :" + jmolviewer.getClass());
+ }
+ if (viewer == null)
+ {
+ System.err.println("Can't use this object as a structure viewer:"
+ + jmolviewer.getClass());
+ return null;
+ }
+ SequenceI[] seqs = null;
+ if (sequenceIds == null || sequenceIds.length == 0)
+ {
+ seqs = viewport.getAlignment().getSequencesArray();
+ }
+ else
+ {
+ Vector sqi = new Vector();
+ AlignmentI al = viewport.getAlignment();
+ for (int sid = 0; sid < sequenceIds.length; sid++)
+ {
+ SequenceI sq = al.findName(sequenceIds[sid]);
+ if (sq != null)
+ {
+ sqi.addElement(sq);
+ }
+ }
+ if (sqi.size() > 0)
+ {
+ seqs = new SequenceI[sqi.size()];
+ for (int sid = 0, sSize = sqi.size(); sid < sSize; sid++)
+ {
+ seqs[sid] = (SequenceI) sqi.elementAt(sid);
+ }
+ }
+ else
+ {
+ return null;
+ }
+ }
+ AAStructureBindingModel jmv = null;
+ // TODO: search for a jmv that involves viewer
+ if (jmv == null)
+ { // create a new viewer/jalview binding.
+ jmv = new ExtJmol(viewer, alignPanel, new SequenceI[][] { seqs });
+ }
+ return jmv;
+
+ }
+
+ /**
+ * bind a pdb file to a sequence in the current view
+ *
+ * @param sequenceId
+ * - sequenceId within the dataset.
+ * @param pdbEntryString
+ * - the short name for the PDB file
+ * @param pdbFile
+ * - pdb file - either a URL or a valid PDB file.
+ * @return true if binding was as success TODO: consider making an exception
+ * structure for indicating when PDB parsing or sequenceId location
+ * fails.
+ */
+ public boolean addPdbFile(String sequenceId, String pdbEntryString,
+ String pdbFile)
+ {
+ SequenceI toaddpdb = viewport.getAlignment().findName(sequenceId);
+ boolean needtoadd = false;
+ if (toaddpdb != null)
+ {
+ Vector pdbe = toaddpdb.getAllPDBEntries();
+ PDBEntry pdbentry = null;
+ if (pdbe != null && pdbe.size() > 0)
+ {
+ for (int pe = 0, peSize = pdbe.size(); pe < peSize; pe++)
+ {
+ pdbentry = (PDBEntry) pdbe.elementAt(pe);
+ if (!pdbentry.getId().equals(pdbEntryString)
+ && !pdbentry.getFile().equals(pdbFile))
+ {
+ pdbentry = null;
+ }
+ else
+ {
+ continue;
+ }
+ }
+ }
+ if (pdbentry == null)
+ {
+ pdbentry = new PDBEntry();
+ pdbentry.setId(pdbEntryString);
+ pdbentry.setFile(pdbFile);
+ needtoadd = true; // add this new entry to sequence.
+ }
+ // resolve data source
+ // TODO: this code should be a refactored to an io package
+ DataSourceType protocol = AppletFormatAdapter.resolveProtocol(pdbFile,
+ FileFormat.PDB);
+ if (protocol == null)
+ {
+ return false;
+ }
+ if (needtoadd)
+ {
+ pdbentry.setProperty("protocol", protocol);
+ toaddpdb.addPDBId(pdbentry);
+ alignPanel.getStructureSelectionManager()
+ .registerPDBEntry(pdbentry);
+ }
+ }
+ return true;
+ }
+
+ private Object[] cleanSeqChainArrays(SequenceI[] seqs, String[] chains)
+ {
+ if (seqs != null)
+ {
+ Vector sequences = new Vector();
+ for (int i = 0; i < seqs.length; i++)
+ {
+ if (seqs[i] != null)
+ {
+ sequences
+ .addElement(new Object[]
+ { seqs[i], (chains != null) ? chains[i] : null });
+ }
+ }
+ seqs = new SequenceI[sequences.size()];
+ chains = new String[sequences.size()];
+ for (int i = 0, isize = sequences.size(); i < isize; i++)
+ {
+ Object[] oj = (Object[]) sequences.elementAt(i);
+
+ seqs[i] = (SequenceI) oj[0];
+ chains[i] = (String) oj[1];
+ }
+ }
+ return new Object[] { seqs, chains };
+
+ }
+
+ public void newStructureView(JalviewLite applet, PDBEntry pdb,
+ SequenceI[] seqs, String[] chains, DataSourceType protocol)
+ {
+ // Scrub any null sequences from the array
+ Object[] sqch = cleanSeqChainArrays(seqs, chains);
+ seqs = (SequenceI[]) sqch[0];
+ chains = (String[]) sqch[1];
+ if (seqs == null || seqs.length == 0)
+ {
+ System.err.println(
+ "JalviewLite.AlignFrame:newStructureView: No sequence to bind structure to.");
+ }
+ if (protocol == null)
+ {
+ String sourceType = (String) pdb.getProperty("protocol");
+ try
+ {
+ protocol = DataSourceType.valueOf(sourceType);
+ } catch (IllegalArgumentException e)
+ {
+ // ignore
+ }
+ if (protocol == null)
+ {
+ System.err.println("Couldn't work out protocol to open structure: "
+ + pdb.getId());
+ return;
+ }
+ }
+ if (applet.useXtrnalSviewer)
+ {
+ // register the association(s) and quit, don't create any windows.
+ if (StructureSelectionManager.getStructureSelectionManager(applet)
+ .setMapping(seqs, chains, pdb.getFile(), protocol, null) == null)
+ {
+ System.err.println("Failed to map " + pdb.getFile() + " ("
+ + protocol + ") to any sequences");
+ }
+ return;
+ }
+ if (applet.isAlignPdbStructures() && applet.jmolAvailable)
+ {
+ // can only do alignments with Jmol
+ // find the last jmol window assigned to this alignment
+ AppletJmol ajm = null, tajm;
+ Vector jmols = applet.getAppletWindow(AppletJmol.class);
+ for (int i = 0, iSize = jmols.size(); i < iSize; i++)
+ {
+ tajm = (AppletJmol) jmols.elementAt(i);
+ if (tajm.ap.alignFrame == this)
+ {
+ ajm = tajm;
+ break;
+ }
+ }
+ if (ajm != null)
+ {
+ System.err.println(
+ "Incremental adding and aligning structure to existing Jmol view not yet implemented.");
+ // try and add the pdb structure
+ // ajm.addS
+ ajm = null;
+ }
+ }
+ // otherwise, create a new window
+ if (applet.jmolAvailable)
+ {
+ new AppletJmol(pdb, seqs, chains, alignPanel, protocol);
+ applet.lastFrameX += 40;
+ applet.lastFrameY += 40;
+ }
+ else
+ {
+ new MCview.AppletPDBViewer(pdb, seqs, chains, alignPanel, protocol);