/*
WIMSchem Elements: Chemistry molecular diagram drawing tool.
(c) 2005 Dr. Alex M. Clark
Released as GNUware, under the Gnu Public License (GPL)
See www.gnu.org for details.
*/
package WIMSchem;
import java.io.*;
import java.net.*;
import java.util.*;
// For obtaining the template list.
// jm.evers: copies... of functions in applet. Should be reorganised !!
int bufSize = 1024 * 2;
byte [] buf = new byte[bufSize];
int n;
while ((n = in.read(buf)) > 0){
bout.write(buf, 0, n);
}
try
{ in.
close(); } catch (Exception ignored
) { }
return bout.toByteArray();
}
byte[] bytes = loadURL
(new URL("file:" + fname
));
}
try {
return new String(loadURL
(url
));
} catch (Exception e
) { return loadFile
(fileOrURL
);}
}
// jm.evers : if an param template is defined, load them in the ArrayList.
boolean inapplet=false;
if(MainApplet.templateURL != null){
for(int p=0; p<MainApplet.templateURL.length ;p++ ){
try {
templ.add(mol);
inapplet=true;
System.
out.
println("loading template"+p
);
} catch (IOException e
) {System.
out.
println("FAILED loading template"+p+
"\n Are you using correct MDLMol or Native files??");}
}
// if this was not successfull: maybe the templates are javascript strings?
// not tested 30/12/2008 !!!
if(!inapplet){
for(int p=0; p<MainApplet.templateURL.length ;p++ ){
try {
System.
out.
println("MainApplet.templateURL[p]="+MainApplet.
templateURL[p
]);
templ.add(mol);
inapplet=true;
System.
out.
println("loading template"+p
);
} catch (IOException e
) {System.
out.
println("FAILED loading template"+p+
"\n Are you using correct javascript strings??");}
}
}
}
if(!inapplet){
// read the list of molecules from the directory file, then create each one of them
try {
InputStream istr=cls.
getResourceAsStream("/templ/list");
while ((line=in.readLine())!=null) {list.add(line);}
istr.close();
}
System.
out.
println("Failed to obtain list of templates:\n"+e.
toString());
return;
}
try {
for (int n=0;n<list.size();n++){
InputStream istr=cls.
getResourceAsStream("/templ/"+list.
get(n
));
Molecule mol=MoleculeStream.ReadNative(istr);
templ.add(mol);
istr.close();
}
}
System.
out.
println("Failed to obtain particular template:\n"+e.
toString());
return;
}
}
// sort the molecules by an index of "complexity" (smaller molecules first, carbon-only favoured)
int[] complex=new int[templ.size()];
for (int n=0;n<templ.size();n++)
{
Molecule mol=templ.get(n);
complex[n]=mol.NumAtoms()*100;
boolean nonCH=false;
for (int i=1;i<=mol.NumAtoms();i++)
if (mol.AtomElement(i).compareTo("C")!=0 && mol.AtomElement(i).compareTo("H")!=0) nonCH=true;
if (!nonCH) complex[n]-=1000;
for (int i=1;i<=mol.NumBonds();i++) complex[n]=complex[n]+mol.BondOrder(i);
}
int p=0;
while (p<templ.size()-1)
{
if (complex[p]>complex[p+1])
{
int i=complex[p]; complex[p]=complex[p+1]; complex[p+1]=i;
Molecule mol=templ.get(p); templ.set(p,templ.get(p+1)); templ.set(p+1,mol);
if (p>0) p--;
}
else p++;
}
}
public int NumTemplates() {return templ.size();}
public Molecule GetTemplate(int N) {return templ.get(N);}
public void AddTemplate(Molecule Mol) {templ.add(Mol);}
}