Subversion Repositories wimsdev

Rev

Rev 3662 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3662 schaersvoo 1
/*
7246 schaersvoo 2
    Sketch Elements: Chemistry molecular diagram drawing tool.
3662 schaersvoo 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
 
7246 schaersvoo 19
public class Templates
20
{
3662 schaersvoo 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
 
7246 schaersvoo 51
    public Templates(Class cls)
52
    {
3662 schaersvoo 53
        ArrayList<String> list=new ArrayList<String>();
54
        // jm.evers : if an param template is defined, load them in the ArrayList.
7246 schaersvoo 55
        if( MainApplet.templateURL != null ){
3662 schaersvoo 56
            for(int p=0; p<MainApplet.templateURL.length ;p++ ){
57
                try {
7246 schaersvoo 58
                    //Molecule mol = MoleculeStream.readMDLMOL(new BufferedReader(new StringReader((load(MainApplet.templateURL[p])).toString())));    
59
                    Molecule mol = MoleculeStream.readUnknown(new BufferedReader(new StringReader((load(MainApplet.templateURL[p])).toString())));    
3662 schaersvoo 60
                    templ.add(mol);
7246 schaersvoo 61
                    System.out.println("loading template no"+p);
3662 schaersvoo 62
                } catch (IOException e) {System.out.println("FAILED loading template"+p+"\n Are you using correct MDLMol or Native files??");}
63
            }
64
        }
7246 schaersvoo 65
        else   
66
        {    // read the list of molecules from the directory file, then create each one of them
67
            try
68
            {
69
                InputStream istr=cls.getResourceAsStream("/templ/list");
70
                BufferedReader in=new BufferedReader(new InputStreamReader(istr));
3662 schaersvoo 71
                String line;
7246 schaersvoo 72
                while ((line=in.readLine())!=null) {list.add(line);}
3662 schaersvoo 73
                istr.close();
74
            }
7246 schaersvoo 75
            catch (IOException e)
76
            {
77
                System.out.println("Failed to obtain list of jar included default templates:\n"+e.toString());
3662 schaersvoo 78
                return;
79
            }
7246 schaersvoo 80
            try
81
            {
82
                for (int n=0;n<list.size();n++)
83
                {
84
                    InputStream istr=cls.getResourceAsStream("/templ/"+list.get(n));
85
                    Molecule mol=MoleculeStream.readNative(istr);
3662 schaersvoo 86
                    templ.add(mol);
87
                    istr.close();
88
                }
7246 schaersvoo 89
            }
90
            catch (IOException e)
91
            {
3662 schaersvoo 92
                System.out.println("Failed to obtain particular template:\n"+e.toString());
93
                return;
94
            }
7246 schaersvoo 95
        }      
3662 schaersvoo 96
        // sort the molecules by an index of "complexity" (smaller molecules first, carbon-only favoured)
97
 
98
        int[] complex=new int[templ.size()];
99
        for (int n=0;n<templ.size();n++)
100
        {
101
            Molecule mol=templ.get(n);
7246 schaersvoo 102
            complex[n]=mol.numAtoms()*100;
3662 schaersvoo 103
            boolean nonCH=false;
7246 schaersvoo 104
            for (int i=1;i<=mol.numAtoms();i++)
105
                if (mol.atomElement(i).compareTo("C")!=0 && mol.atomElement(i).compareTo("H")!=0) nonCH=true;
3662 schaersvoo 106
            if (!nonCH) complex[n]-=1000;
7246 schaersvoo 107
            for (int i=1;i<=mol.numBonds();i++) complex[n]=complex[n]+mol.bondOrder(i);
3662 schaersvoo 108
        }
109
 
110
        int p=0;
111
        while (p<templ.size()-1)
112
        {
113
            if (complex[p]>complex[p+1])
114
            {
115
                int i=complex[p]; complex[p]=complex[p+1]; complex[p+1]=i;
116
                Molecule mol=templ.get(p); templ.set(p,templ.get(p+1)); templ.set(p+1,mol);
117
                if (p>0) p--;
118
            }
119
            else p++;
120
        }
121
    }
7246 schaersvoo 122
 
123
    public int numTemplates() {return templ.size();}
124
    public Molecule getTemplate(int N) {return templ.get(N);}
125
    public void addTemplate(Molecule Mol) {templ.add(Mol);}
3662 schaersvoo 126
}
127
 
128
 
129
 
130