Subversion Repositories wimsdev

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     WIMSchem Elements: Chemistry molecular diagram drawing tool.
  3.    
  4.     (c) 2005 Dr. Alex M. Clark
  5.    
  6.     Released as GNUware, under the Gnu Public License (GPL)
  7.    
  8.     See www.gnu.org for details.
  9. */
  10.  
  11. package WIMSchem;
  12.  
  13. import java.io.*;
  14. import java.net.*;
  15. import java.util.*;
  16.  
  17. // For obtaining the template list.
  18.  
  19. public class Templates{
  20.  
  21.     ArrayList<Molecule> templ=new ArrayList<Molecule>();
  22.  
  23.     // jm.evers: copies... of functions in applet. Should be reorganised !!
  24.     public static byte [] loadURL(URL url) throws IOException {
  25.         int bufSize = 1024 * 2;
  26.         byte [] buf = new byte[bufSize];
  27.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  28.         BufferedInputStream   in   = new BufferedInputStream(url.openStream());
  29.         int n;
  30.         while ((n = in.read(buf)) > 0){
  31.             bout.write(buf, 0, n);
  32.         }
  33.         try
  34.         { in.close(); } catch (Exception ignored) { }
  35.         return bout.toByteArray();
  36.     }
  37.  
  38.     public static String loadFile(String fname) throws IOException {
  39.         byte[] bytes = loadURL(new URL("file:" + fname));
  40.         return new String(bytes);
  41.     }
  42.  
  43.     public static String load(String fileOrURL) throws IOException {
  44.         try {
  45.             URL url = new URL(fileOrURL);
  46.             return new String(loadURL(url));
  47.         } catch (Exception e) { return loadFile(fileOrURL);}
  48.     }
  49.        
  50.  
  51.     public Templates(Class cls){
  52.  
  53.         ArrayList<String> list=new ArrayList<String>();
  54.  
  55.         // jm.evers : if an param template is defined, load them in the ArrayList.
  56.         boolean inapplet=false;
  57.         if(MainApplet.templateURL != null){
  58.             for(int p=0; p<MainApplet.templateURL.length ;p++ ){
  59.                 try {
  60.                     Molecule mol=MoleculeStream.ReadUnknown(new BufferedReader(new StringReader((load(MainApplet.templateURL[p])))));
  61.                     templ.add(mol);
  62.                     inapplet=true;
  63.                     System.out.println("loading template"+p);
  64.                 } catch (IOException e) {System.out.println("FAILED loading template"+p+"\n Are you using correct MDLMol or Native files??");}
  65.             }
  66.             // if this was not successfull: maybe the templates are javascript strings?
  67.             // not tested 30/12/2008 !!!
  68.             if(!inapplet){
  69.                 for(int p=0; p<MainApplet.templateURL.length ;p++ ){
  70.                     try {
  71.                         System.out.println("MainApplet.templateURL[p]="+MainApplet.templateURL[p]);
  72.                         Molecule mol=MoleculeStream.ReadUnknown(new BufferedReader(new StringReader(MainApplet.templateURL[p].toString())));
  73.                         templ.add(mol);
  74.                         inapplet=true;
  75.                         System.out.println("loading template"+p);
  76.                     }    catch (IOException e) {System.out.println("FAILED loading template"+p+"\n Are you using correct javascript strings??");}
  77.                 }
  78.             }
  79.         }
  80.        
  81.         if(!inapplet){
  82.         // read the list of molecules from the directory file, then create each one of them
  83.             try {
  84.                 InputStream istr=cls.getResourceAsStream("/templ/list");
  85.                 BufferedReader in=new BufferedReader(new InputStreamReader(istr));
  86.                 String line;
  87.                 while ((line=in.readLine())!=null) {list.add(line);}
  88.                 istr.close();
  89.             }
  90.             catch (IOException e) {
  91.                 System.out.println("Failed to obtain list of templates:\n"+e.toString());
  92.                 return;
  93.             }
  94.            
  95.             try {
  96.                 for (int n=0;n<list.size();n++){
  97.                     InputStream istr=cls.getResourceAsStream("/templ/"+list.get(n));
  98.                     Molecule mol=MoleculeStream.ReadNative(istr);
  99.                     templ.add(mol);
  100.                     istr.close();
  101.                 }
  102.             }
  103.             catch (IOException e){
  104.                 System.out.println("Failed to obtain particular template:\n"+e.toString());
  105.                 return;
  106.             }
  107.         }
  108.         // sort the molecules by an index of "complexity" (smaller molecules first, carbon-only favoured)
  109.        
  110.         int[] complex=new int[templ.size()];
  111.         for (int n=0;n<templ.size();n++)
  112.         {
  113.             Molecule mol=templ.get(n);
  114.             complex[n]=mol.NumAtoms()*100;
  115.             boolean nonCH=false;
  116.             for (int i=1;i<=mol.NumAtoms();i++)
  117.                 if (mol.AtomElement(i).compareTo("C")!=0 && mol.AtomElement(i).compareTo("H")!=0) nonCH=true;
  118.             if (!nonCH) complex[n]-=1000;
  119.             for (int i=1;i<=mol.NumBonds();i++) complex[n]=complex[n]+mol.BondOrder(i);
  120.         }
  121.        
  122.         int p=0;
  123.         while (p<templ.size()-1)
  124.         {
  125.             if (complex[p]>complex[p+1])
  126.             {
  127.                 int i=complex[p]; complex[p]=complex[p+1]; complex[p+1]=i;
  128.                 Molecule mol=templ.get(p); templ.set(p,templ.get(p+1)); templ.set(p+1,mol);
  129.                 if (p>0) p--;
  130.             }
  131.             else p++;
  132.         }
  133.     }
  134.     public int NumTemplates() {return templ.size();}
  135.     public Molecule GetTemplate(int N) {return templ.get(N);}
  136.     public void AddTemplate(Molecule Mol) {templ.add(Mol);}
  137. }
  138.  
  139.  
  140.  
  141.  
  142.