Subversion Repositories wimsdev

Rev

Rev 3662 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3662 Rev 7246
Line 1... Line 1...
1
/*
1
/*
2
    WIMSchem Elements: Chemistry molecular diagram drawing tool.
2
    Sketch Elements: Chemistry molecular diagram drawing tool.
3
   
3
   
4
    (c) 2005 Dr. Alex M. Clark
4
    (c) 2005 Dr. Alex M. Clark
5
   
5
   
6
    Released as GNUware, under the Gnu Public License (GPL)
6
    Released as GNUware, under the Gnu Public License (GPL)
7
   
7
   
Line 14... Line 14...
14
import java.net.*;
14
import java.net.*;
15
import java.util.*;
15
import java.util.*;
16
 
16
 
17
// For obtaining the template list.
17
// For obtaining the template list.
18
 
18
 
19
public class Templates{
19
public class Templates
20
 
20
{
21
    ArrayList<Molecule> templ=new ArrayList<Molecule>();
21
    ArrayList<Molecule> templ=new ArrayList<Molecule>();
22
 
22
 
23
    // jm.evers: copies... of functions in applet. Should be reorganised !!
23
    // jm.evers: copies... of functions in applet. Should be reorganised !!
24
    public static byte [] loadURL(URL url) throws IOException {
24
    public static byte [] loadURL(URL url) throws IOException {
25
        int bufSize = 1024 * 2;
25
        int bufSize = 1024 * 2;
Line 46... Line 46...
46
            return new String(loadURL(url));
46
            return new String(loadURL(url));
47
        } catch (Exception e) { return loadFile(fileOrURL);}
47
        } catch (Exception e) { return loadFile(fileOrURL);}
48
    }
48
    }
49
       
49
       
50
 
50
 
51
    public Templates(Class cls){
51
    public Templates(Class cls)
52
 
52
    {
53
        ArrayList<String> list=new ArrayList<String>();
53
        ArrayList<String> list=new ArrayList<String>();
54
 
-
 
55
        // jm.evers : if an param template is defined, load them in the ArrayList.
54
        // jm.evers : if an param template is defined, load them in the ArrayList.
56
        boolean inapplet=false;
-
 
57
        if(MainApplet.templateURL != null){
55
        if( MainApplet.templateURL != null ){
58
            for(int p=0; p<MainApplet.templateURL.length ;p++ ){
56
            for(int p=0; p<MainApplet.templateURL.length ;p++ ){
59
                try {
57
                try {
-
 
58
                    //Molecule mol = MoleculeStream.readMDLMOL(new BufferedReader(new StringReader((load(MainApplet.templateURL[p])).toString())));    
60
                    Molecule mol=MoleculeStream.ReadUnknown(new BufferedReader(new StringReader((load(MainApplet.templateURL[p])))));
59
                    Molecule mol = MoleculeStream.readUnknown(new BufferedReader(new StringReader((load(MainApplet.templateURL[p])).toString())));    
61
                    templ.add(mol);
60
                    templ.add(mol);
62
                    inapplet=true;
-
 
63
                    System.out.println("loading template"+p);
61
                    System.out.println("loading template no"+p);
64
                } catch (IOException e) {System.out.println("FAILED loading template"+p+"\n Are you using correct MDLMol or Native files??");}
62
                } catch (IOException e) {System.out.println("FAILED loading template"+p+"\n Are you using correct MDLMol or Native files??");}
65
            }
63
            }
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
        }
64
        }
80
       
65
        else   
81
        if(!inapplet){
-
 
82
        // read the list of molecules from the directory file, then create each one of them
66
        {    // read the list of molecules from the directory file, then create each one of them
-
 
67
            try
83
            try {
68
            {
84
                InputStream istr=cls.getResourceAsStream("/templ/list");
69
                InputStream istr=cls.getResourceAsStream("/templ/list");
85
                BufferedReader in=new BufferedReader(new InputStreamReader(istr));
70
                BufferedReader in=new BufferedReader(new InputStreamReader(istr));
86
                String line;
71
                String line;
87
                while ((line=in.readLine())!=null) {list.add(line);}
72
                while ((line=in.readLine())!=null) {list.add(line);}
88
                istr.close();
73
                istr.close();
89
            }
74
            }
90
            catch (IOException e) {
75
            catch (IOException e)
-
 
76
            {
91
                System.out.println("Failed to obtain list of templates:\n"+e.toString());
77
                System.out.println("Failed to obtain list of jar included default templates:\n"+e.toString());
92
                return;
78
                return;
93
            }
79
            }
94
           
80
            try
95
            try {
81
            {
96
                for (int n=0;n<list.size();n++){
82
                for (int n=0;n<list.size();n++)
-
 
83
                {
97
                    InputStream istr=cls.getResourceAsStream("/templ/"+list.get(n));
84
                    InputStream istr=cls.getResourceAsStream("/templ/"+list.get(n));
98
                    Molecule mol=MoleculeStream.ReadNative(istr);
85
                    Molecule mol=MoleculeStream.readNative(istr);
99
                    templ.add(mol);
86
                    templ.add(mol);
100
                    istr.close();
87
                    istr.close();
101
                }
88
                }
102
            }
89
            }
103
            catch (IOException e){
90
            catch (IOException e)
-
 
91
            {
104
                System.out.println("Failed to obtain particular template:\n"+e.toString());
92
                System.out.println("Failed to obtain particular template:\n"+e.toString());
105
                return;
93
                return;
106
            }
94
            }
107
        }
95
        }      
108
        // sort the molecules by an index of "complexity" (smaller molecules first, carbon-only favoured)
96
        // sort the molecules by an index of "complexity" (smaller molecules first, carbon-only favoured)
109
       
97
       
110
        int[] complex=new int[templ.size()];
98
        int[] complex=new int[templ.size()];
111
        for (int n=0;n<templ.size();n++)
99
        for (int n=0;n<templ.size();n++)
112
        {
100
        {
113
            Molecule mol=templ.get(n);
101
            Molecule mol=templ.get(n);
114
            complex[n]=mol.NumAtoms()*100;
102
            complex[n]=mol.numAtoms()*100;
115
            boolean nonCH=false;
103
            boolean nonCH=false;
116
            for (int i=1;i<=mol.NumAtoms();i++)
104
            for (int i=1;i<=mol.numAtoms();i++)
117
                if (mol.AtomElement(i).compareTo("C")!=0 && mol.AtomElement(i).compareTo("H")!=0) nonCH=true;
105
                if (mol.atomElement(i).compareTo("C")!=0 && mol.atomElement(i).compareTo("H")!=0) nonCH=true;
118
            if (!nonCH) complex[n]-=1000;
106
            if (!nonCH) complex[n]-=1000;
119
            for (int i=1;i<=mol.NumBonds();i++) complex[n]=complex[n]+mol.BondOrder(i);
107
            for (int i=1;i<=mol.numBonds();i++) complex[n]=complex[n]+mol.bondOrder(i);
120
        }
108
        }
121
       
109
       
122
        int p=0;
110
        int p=0;
123
        while (p<templ.size()-1)
111
        while (p<templ.size()-1)
124
        {
112
        {
Line 129... Line 117...
129
                if (p>0) p--;
117
                if (p>0) p--;
130
            }
118
            }
131
            else p++;
119
            else p++;
132
        }
120
        }
133
    }
121
    }
-
 
122
   
134
    public int NumTemplates() {return templ.size();}
123
    public int numTemplates() {return templ.size();}
135
    public Molecule GetTemplate(int N) {return templ.get(N);}
124
    public Molecule getTemplate(int N) {return templ.get(N);}
136
    public void AddTemplate(Molecule Mol) {templ.add(Mol);}
125
    public void addTemplate(Molecule Mol) {templ.add(Mol);}
137
}
126
}
138
 
127
 
139
 
128
 
140
 
129
 
141
 
130