Back to index

wims  3.65+svn20090927
Templates.java
Go to the documentation of this file.
00001 /*
00002     WIMSchem Elements: Chemistry molecular diagram drawing tool.
00003     
00004     (c) 2005 Dr. Alex M. Clark
00005     
00006     Released as GNUware, under the Gnu Public License (GPL)
00007     
00008     See www.gnu.org for details.
00009 */
00010 
00011 package WIMSchem;
00012 
00013 import java.io.*;
00014 import java.net.*;
00015 import java.util.*;
00016 
00017 // For obtaining the template list.
00018 
00019 public class Templates{
00020 
00021     ArrayList<Molecule> templ=new ArrayList<Molecule>();
00022 
00023     // jm.evers: copies... of functions in applet. Should be reorganised !!
00024     public static byte [] loadURL(URL url) throws IOException {
00025         int bufSize = 1024 * 2;
00026        byte [] buf = new byte[bufSize];
00027        ByteArrayOutputStream bout = new ByteArrayOutputStream();
00028        BufferedInputStream   in   = new BufferedInputStream(url.openStream());
00029        int n;
00030        while ((n = in.read(buf)) > 0){
00031            bout.write(buf, 0, n);
00032        }
00033        try 
00034        { in.close(); } catch (Exception ignored) { }
00035        return bout.toByteArray();
00036     }
00037 
00038     public static String loadFile(String fname) throws IOException {
00039         byte[] bytes = loadURL(new URL("file:" + fname));
00040        return new String(bytes);
00041     }
00042 
00043     public static String load(String fileOrURL) throws IOException {
00044        try {
00045            URL url = new URL(fileOrURL);
00046            return new String(loadURL(url));
00047        } catch (Exception e) { return loadFile(fileOrURL);}
00048     }
00049         
00050 
00051     public Templates(Class cls){
00052 
00053        ArrayList<String> list=new ArrayList<String>();
00054 
00055        // jm.evers : if an param template is defined, load them in the ArrayList.
00056        boolean inapplet=false;
00057        if(MainApplet.templateURL != null){
00058            for(int p=0; p<MainApplet.templateURL.length ;p++ ){
00059               try {
00060                   Molecule mol=MoleculeStream.ReadUnknown(new BufferedReader(new StringReader((load(MainApplet.templateURL[p])))));
00061                   templ.add(mol);
00062                   inapplet=true;
00063                   System.out.println("loading template"+p);
00064               } catch (IOException e) {System.out.println("FAILED loading template"+p+"\n Are you using correct MDLMol or Native files??");}
00065            }
00066            // if this was not successfull: maybe the templates are javascript strings?
00067            if(!inapplet){
00068               for(int p=0; p<MainApplet.templateURL.length ;p++ ){
00069                   try {
00070                   System.out.println("MainApplet.templateURL[p]="+MainApplet.templateURL[p]);
00071                      Molecule mol=MoleculeStream.ReadUnknown(new BufferedReader(new StringReader(MainApplet.templateURL[p].toString())));
00072                      templ.add(mol);
00073                      inapplet=true;
00074                      System.out.println("loading template"+p);
00075                   }   catch (IOException e) {System.out.println("FAILED loading template"+p+"\n Are you using correct javascript strings??");}
00076               }
00077            }
00078        }
00079        
00080        if(!inapplet){
00081        // read the list of molecules from the directory file, then create each one of them
00082            try {
00083               InputStream istr=cls.getResourceAsStream("/templ/list");
00084               BufferedReader in=new BufferedReader(new InputStreamReader(istr));
00085               String line;
00086               while ((line=in.readLine())!=null) {list.add(line);}
00087               istr.close();
00088            }
00089            catch (IOException e) {
00090               System.out.println("Failed to obtain list of templates:\n"+e.toString());
00091               return;
00092            }
00093            
00094            try {
00095               for (int n=0;n<list.size();n++){
00096                   InputStream istr=cls.getResourceAsStream("/templ/"+list.get(n));
00097                   Molecule mol=MoleculeStream.ReadNative(istr);
00098                   templ.add(mol);
00099                   istr.close();
00100               }
00101            }
00102            catch (IOException e){
00103               System.out.println("Failed to obtain particular template:\n"+e.toString());
00104               return;
00105            }
00106        }
00107        // sort the molecules by an index of "complexity" (smaller molecules first, carbon-only favoured)
00108        
00109        int[] complex=new int[templ.size()];
00110        for (int n=0;n<templ.size();n++)
00111        {
00112            Molecule mol=templ.get(n);
00113            complex[n]=mol.NumAtoms()*100;
00114            boolean nonCH=false;
00115            for (int i=1;i<=mol.NumAtoms();i++) 
00116               if (mol.AtomElement(i).compareTo("C")!=0 && mol.AtomElement(i).compareTo("H")!=0) nonCH=true;
00117            if (!nonCH) complex[n]-=1000;
00118            for (int i=1;i<=mol.NumBonds();i++) complex[n]=complex[n]+mol.BondOrder(i);
00119        }
00120        
00121        int p=0;
00122        while (p<templ.size()-1)
00123        {
00124            if (complex[p]>complex[p+1])
00125            {
00126               int i=complex[p]; complex[p]=complex[p+1]; complex[p+1]=i;
00127               Molecule mol=templ.get(p); templ.set(p,templ.get(p+1)); templ.set(p+1,mol);
00128               if (p>0) p--;
00129            }
00130            else p++;
00131        }
00132     }
00133     public int NumTemplates() {return templ.size();}
00134     public Molecule GetTemplate(int N) {return templ.get(N);}
00135     public void AddTemplate(Molecule Mol) {templ.add(Mol);}
00136 }
00137 
00138 
00139 
00140