Back to index

wims  3.65+svn20090927
MainApplet.java
Go to the documentation of this file.
00001 /*
00002     WIMSchem: modified version of SketchEl.
00003     Elements: Chemistry molecular diagram drawing tool.
00004     
00005     (c) 2005 Dr. Alex M. Clark
00006     
00007     Released as GNUware, under the Gnu Public License (GPL)
00008     
00009     See www.gnu.org for details.
00010 */
00011 
00012 package WIMSchem;
00013 
00014 import java.io.*;
00015 import java.awt.*;
00016 import java.awt.event.*;
00017 import java.util.*;
00018 import javax.swing.*;
00019 import java.net.*;
00020 
00021 public class MainApplet extends JApplet implements ComponentListener{
00022     
00023     // amateur solutions...
00024     MainPanel mainPanel=null;
00025     MainPanel Language=null;
00026     MainPanel Translation=null;
00027     static String[] tools={"TOOL_CURSOR","TOOL_ROTATOR","TOOL_ERASOR","TOOL_DIALOG","TOOL_EDIT","TOOL_SETATOM","TOOL_SINGLE","TOOL_DOUBLE","TOOL_TRIPLE","TOOL_ZERO","TOOL_INCLINED","TOOL_DECLINED","TOOL_UNKNOWN","TOOL_CHARGE","TOOL_UNDO","TOOL_REDO","TOOL_TEMPLATE","TOOL_CUT","TOOL_COPY","TOOL_PASTE"};
00028     static String[] menus={"MENU_BLOCK","MENU_SELECT","MENU_TRANSFORM","MENU_ZOOM","MENU_SHOW","MENU_HYDROGEN","MENU_STEREO","MENU_HELP"};
00029     public boolean[] selected_tools;
00030     public boolean[] selected_menus;
00031     public static Properties translation=new Properties();
00032     public String Correct_answer="";
00033     public String feedback="";
00034     public boolean continu=true;
00035     public String replytext="";
00036     public String Student_answer="";
00037     public String language;
00038     public int typeset=0;
00039     public String plain_formula_ca="";
00040     public String html_formula_ca="";
00041     public String latex_formula_ca="";
00042     public String plain_formula_sa="";
00043     public String html_formula_sa="";
00044     public String latex_formula_sa="";
00045     static int N=Molecule.ELEMENTS.length;
00046     public int[] charge_ca;// charge of the atom
00047     public int[] radical_ca;// radicals?
00048     public int[] charge_sa;// charge of the atom
00049     public int[] radical_sa;// radicals?
00050     public String[] elements_ca;
00051     public String[] elements_sa;
00052     public int[] hydro_ca;
00053     public int[] hydro_sa;
00054     double weight_ca=0;
00055     double weight_sa=0;
00056     String html_smiles_ca="";
00057     String html_smiles_sa="";
00058     public int num_sa=0;
00059     public int num_ca=0;
00060     public int bond_sa=0;
00061     public int bond_ca=0;
00062     public String[] my_atoms;
00063     public boolean ViewH;
00064     public boolean ViewC;
00065     public static String[] templateURL;
00066 
00067     public void init(){
00068        templateURL=GetTemplateURL();
00069        ViewH=ViewHydrogen();
00070        ViewC=ViewCarbon();
00071        selected_tools=getTools();
00072        selected_menus=getMenus();
00073        my_atoms=GetMyAtoms();
00074        language=getLanguage();
00075        translation=loadProperties(language);
00076        getContentPane().setLayout(new BorderLayout());
00077        mainPanel=new MainPanel(null,false,true,null,language,translation,selected_tools,selected_menus,my_atoms,ViewH,ViewC);
00078        getContentPane().add(mainPanel,BorderLayout.CENTER);
00079        Molecule mol=ParseParams();
00080        if (mol!=null){mainPanel.SetMolecule(mol);}      
00081        addComponentListener(this);
00082        GetFile();
00083        repaint(1000);
00084     }
00085      
00086     public static byte [] loadURL(URL url) throws IOException {
00087     // jm.evers :handy functions !!! not mine :(
00088         int bufSize = 1024 * 2;
00089        byte [] buf = new byte[bufSize];
00090        ByteArrayOutputStream bout = new ByteArrayOutputStream();
00091        BufferedInputStream   in   = new BufferedInputStream(url.openStream());
00092        int n;
00093        while ((n = in.read(buf)) > 0) {
00094            bout.write(buf, 0, n);
00095        }
00096        try 
00097        { in.close(); } catch (Exception ignored) { }
00098        return bout.toByteArray();
00099     }
00100 
00101     public static String loadFile(String fname) throws IOException {
00102     // jm.evers :handy functions !!! not mine :(
00103        byte[] bytes = loadURL(new URL("file:" + fname));
00104        return new String(bytes);
00105     }
00106     
00107     public static String load(String fileOrURL) throws IOException {
00108     // jm.evers :handy functions !!! not mine :(
00109        try {
00110            URL url = new URL(fileOrURL);
00111            return new String(loadURL(url));
00112        } catch (Exception e) {
00113        return loadFile(fileOrURL);
00114        }
00115     }
00116     
00117     public String[] GetTemplateURL(){
00118        // jm.evers : retreive an unknown amount of templates via params called template1,template2...
00119        // these templates will be called by Template.java
00120        String parm=getParameter("template1");
00121        if( parm != null && parm.length()!=0 ){
00122            int p=0;
00123            while( parm!=null && parm.length()!=0 ){
00124               p++;
00125               parm=getParameter("template"+p);
00126            }
00127            String[] templateURL=new String[p-1];
00128            for(int s=1;s<p;s++){
00129               parm=getParameter("template"+s);
00130                templateURL[s-1]=parm;
00131            }
00132            return templateURL;
00133        }
00134        else
00135        {
00136            return null;
00137        }
00138     }
00139     
00140     public void GetFile(){
00141     //jm.evers : try to load a file as string from the URL given in the params
00142        String filename=getParameter("file");
00143        if (filename!=null){
00144            try
00145            {
00146               //System.out.println("trying to load "+filename);
00147               // jm.evers: is param showfile is set, draw to canvas...
00148               String showfile=getParameter("showfile");
00149               if(showfile.equalsIgnoreCase("yes") ||  showfile.equals("1")){
00150                   //System.out.println("loaded file will be drawn to canvas ");
00151                   String DemoMol=(load(filename)).toString();
00152                   if(DemoMol.indexOf("WIMSchem!")!=-1 && DemoMol.indexOf("!FIN")!=-1){
00153               //     System.out.println("it is in Native fileformat");
00154                      AppendMoleculeNative(DemoMol);
00155                   }
00156                   else
00157                   {
00158                      if( DemoMol.indexOf("molfile")!=-1){
00159               //         System.out.println("it is in MDLMol fileformat");
00160                          AppendMoleculeMDLMol(DemoMol);
00161                      }
00162                      else
00163                      {
00164                          System.out.println("Unknown filetype or corrupt file "+filename+"\n"+DemoMol);
00165                      }
00166                   }
00167               }
00168               else
00169               {
00170                   Correct_answer=(load(filename)).toString();
00171                   //System.out.println("loaded file will be used as Correct_answer molecule \n"+Correct_answer);              
00172                   GetQuestion(Correct_answer);
00173               }
00174            }
00175            catch (Exception e){System.out.println("Could not load "+filename);continu=false;}
00176        }
00177        else
00178        {
00179            System.out.println("param \"file\" is empty\nWill not use fileloading");
00180        }
00181     }  
00182 
00183     public String getLanguage(){
00184        // jm.evers: reading language. default english
00185        String param = getParameter("language");
00186        if(param != null){param = param.toLowerCase();}else{param = "en";}
00187        return param;
00188     }
00189 
00190     public Boolean ViewHydrogen(){
00191        // jm.evers: reading hydrogen param. 27/12/2008
00192        String param = getParameter("show_hydrogen");
00193        boolean h=true;
00194        if(param != null){param = param.toLowerCase();
00195            if(param.equals("0") || param.equals("no")){
00196               h=false;
00197            }
00198        }
00199        System.out.println("viewH="+h+" param="+param);
00200        return h;
00201     }
00202 
00203     public Boolean ViewCarbon(){
00204        // jm.evers: reading elements param. 27/12/2008
00205        String param = getParameter("show_carbon");
00206        boolean c=true;
00207        if(param != null){param = param.toLowerCase();
00208            if(param.equals("0") || param.equals("no")){
00209               c=false;
00210            }
00211        }
00212        System.out.println("viewC="+c+" param="+param);
00213        return c;
00214     }
00215 
00216     public String[] GetMyAtoms(){
00217        // jm.evers: reading atoms from params
00218        String param = getParameter("atoms");
00219        if(param != null){
00220            System.out.println("found Atoms: "+param);
00221            my_atoms=AtomString2AtomArray(param);
00222            return my_atoms;
00223        }
00224        else
00225        {
00226            return null;
00227        }
00228     }
00229     
00230     public String[] AtomString2AtomArray(String t){
00231        //jm.evers: parsing params into array...fieldseparators are "," ":" ";" " "
00232        StringTokenizer q;
00233        if(t.indexOf(",")!=-1){
00234            q = new StringTokenizer(t, ",");
00235        }
00236        else
00237        {
00238            if(t.indexOf(":")!=-1){
00239               q = new StringTokenizer(t, ":");   
00240            }
00241            else
00242            {
00243               if(t.indexOf(";")!=-1){
00244                   q = new StringTokenizer(t, ";");      
00245               }
00246               else
00247               {
00248                   if(t.indexOf(" ")>2){
00249                      q = new StringTokenizer(t, " ");   
00250                   }
00251                   else
00252                   {
00253                      return null;
00254                   }
00255               }
00256            }
00257        }
00258        int max=q.countTokens();if( max > 50 ){ max = 50;}
00259        String[] tmp=new String[max];String s="";
00260        for( int p = 0 ; p<max ; p++){
00261            s=q.nextToken();
00262            //System.out.println("found atom "+s);
00263            if(s.length()==1 || s.length()==2){
00264               tmp[p]=s;
00265               //System.out.println("added");
00266            }
00267        }
00268        return tmp;
00269     }     
00270     
00271     public boolean[] getMenus(){
00272        // jm.evers: configuring menus through appletparams
00273        String param;
00274        selected_menus=new boolean[8];
00275        for(int p=0;p<8;p++){
00276            param=getParameter(menus[p]);
00277            if(param!=null){
00278               if(param.equalsIgnoreCase("yes") || param.equals("1")){
00279                   selected_menus[p]=true;
00280               }
00281               else
00282               {
00283                   selected_menus[p]=false;
00284               }
00285            }
00286            else
00287            {
00288               selected_menus[p]=true;
00289            }
00290        }
00291        return selected_menus;
00292     
00293     }
00294     public boolean[] getTools(){
00295        // jm.evers: configuring toolbar through appletparams
00296        String param;
00297        selected_tools=new boolean[20];
00298        for(int p=0;p<20;p++){
00299            param=getParameter(tools[p]);
00300            if(param!=null){
00301               if(param.equalsIgnoreCase("yes") || param.equals("1")){
00302                   selected_tools[p]=true;
00303               }
00304               else
00305               {
00306                   selected_tools[p]=false;
00307               }
00308            }
00309            else
00310            {
00311               selected_tools[p]=true;
00312            }
00313        }
00314        return selected_tools;
00315     }
00316     
00317     public Properties loadProperties (String lang){
00318        translation=new Properties();
00319        // jm.evers : trying global properties file with translations
00320        try{
00321            InputStream in = getClass().getResourceAsStream("/lang/WIMSchemProperties_"+lang+".properties");
00322            translation.load(in);in.close();
00323            return translation;
00324        } 
00325        catch (Exception e){ System.out.println("error reading /lang/WIMSchemProperties_"+lang+".properties\n"+e);}         
00326             return null;                                                                                                               
00327     }  
00328 
00329 
00330     public String getAppletInfo(){
00331        return  translation.getProperty("WIMSchem_Applet_Description");
00332     }
00333 
00334     // if the source webpage wishes to supply a molecule at startup time, there is a slightly cumbersome way to accomplish this:
00335     // the <params> tag may be used; "nlines" should have the total number of lines of text, and each "line{#}" (e.g. "line1", "line50",
00336     // etc.) should contain the content; the string that emerges should be either WIMSchem native format, or MDL MOL
00337     
00338     private Molecule ParseParams(){
00339        try
00340        {
00341            String str=getParameter("nlines");
00342            if (str==null) return null;
00343            int nlines=Integer.valueOf(str).intValue();
00344            if (nlines<3 || nlines>10000) return null; // insanity
00345 
00346            StringBuffer buff=new StringBuffer();
00347            for (int n=1;n<=nlines;n++)
00348            {
00349               str=getParameter("line"+n);
00350               if (str==null) return null;
00351               for (int i=0;i<str.length();i++) 
00352               {
00353                   if (str.charAt(i)=='_') str=str.substring(0,i)+" "+str.substring(i+1); else break;
00354               }
00355               buff.append(str+"\n");
00356            }
00357            
00358            Molecule mol=MoleculeStream.ReadNative(new BufferedReader(new StringReader(buff.toString())));
00359            if (mol.NumAtoms()>0) return mol;
00360            return null;
00361        }
00362        catch (Exception e) 
00363        {
00364            JOptionPane.showMessageDialog(null,translation.getProperty("Unable_to_parse_parameter_molecule")+e.getMessage(),"zog",JOptionPane.ERROR_MESSAGE); 
00365        }
00366 
00367        return null;
00368     }
00369     
00370     // methods intended to interact with Javascript
00371     // replace the current molecule with the content of the string, which can be any of the formats which WIMSchem
00372     // is able to read out from a file; returns true if successful
00373     public boolean SetMoleculeNative(String Source) {
00374        try
00375        {
00376            Molecule mol=MoleculeStream.ReadNative(new BufferedReader(new StringReader(Source.toString())));
00377            //Molecule mol=MoleculeStream.ReadUnknown(new BufferedReader(new StringReader(Source)));
00378            mainPanel.SetMolecule(mol);
00379            mainPanel.repaint();
00380            return true;
00381        }
00382        catch (IOException e) { System.out.println("error"+e);}
00383        return false;
00384     }
00385 
00386     public boolean SetMoleculeMDLMol(String Source) {
00387        try
00388        {
00389            Molecule mol=MoleculeStream.ReadMDLMOL(new BufferedReader(new StringReader(Source.toString())));
00390            //Molecule mol=MoleculeStream.ReadUnknown(new BufferedReader(new StringReader(Source)));
00391            mainPanel.SetMolecule(mol);
00392            mainPanel.repaint();
00393            return true;
00394        }
00395        catch (IOException e) { System.out.println("error"+e);}
00396        return false;
00397     }
00398     
00399     // appends the indicated molecular source to the current molecule, in the same way as the paste feature; otherwise works
00400     // the same as SetMolecule
00401     public boolean AppendMoleculeNative(String Source){
00402        try
00403        {
00404            Molecule mol=MoleculeStream.ReadNative(new BufferedReader(new StringReader(Source.toString())));
00405            mainPanel.AddMolecule(mol);
00406            mainPanel.repaint();
00407            return true;
00408        }
00409        catch (IOException e) {System.out.println("error"+e);}
00410        
00411        return false;
00412     }
00413 
00414     public boolean AppendMoleculeMDLMol(String Source){
00415        try
00416        {
00417            Molecule mol=MoleculeStream.ReadMDLMOL(new BufferedReader(new StringReader(Source.toString())));     
00418            mainPanel.AddMolecule(mol);
00419            mainPanel.repaint();
00420            return true;
00421        }
00422        catch (IOException e) {System.out.println("error"+e);}
00423        
00424        return false;
00425     }
00426     
00427     // return the string representation of the molecule, in WIMSchem native format
00428     public String GetMoleculeNative(){
00429        try
00430        {
00431            StringWriter sw=new StringWriter();
00432            BufferedWriter bw=new BufferedWriter(sw);
00433            MoleculeStream.WriteNative(bw,mainPanel.MolData());
00434            return sw.toString();
00435        }
00436        catch (IOException e) {System.out.println("error"+e);}
00437        
00438        return null;
00439     }
00440     
00441     // return the string representation of the molecule, in MDL MOL-file format
00442     public String GetMoleculeMDLMol(){
00443        try
00444        {
00445            StringWriter sw=new StringWriter();
00446            BufferedWriter bw=new BufferedWriter(sw);
00447            MoleculeStream.WriteMDLMOL(bw,mainPanel.MolData());
00448            return sw.toString();
00449        }
00450        catch (IOException e) {System.out.println("error"+e);}
00451        
00452        return null;
00453     }
00454 
00455     public void GetQuestion(String i){
00456        //jm.evers: 
00457        // called by javascript to "upload" the chemical from a javascript variable. [a wims "record" in my modules]
00458        // and the same javascript function could call "CompareNative()" to compare this molecule with the drawing  
00459        // OR
00460        // if <param name="file" value="http://whatever_wims_server/filename"> is set
00461        feedback="";
00462        if(i.indexOf("WIMSchem!")!=-1 && i.indexOf("!FIN")!=-1){
00463            //Correct_answer=i.replaceAll(" ","");continu=true;
00464            //System.out.println("found Native WIMSchem file :"+Correct_answer);
00465            System.out.println("Native fileformat detected");
00466               Molecule elmol=new Molecule();
00467               try
00468               {
00469                   elmol=MoleculeStream.ReadNative(new BufferedReader(new StringReader(i.toString())));
00470                   System.out.println("Native stringfile converted to molecule");
00471               }
00472               catch (IOException e) {System.out.println("error"+e);continu=false;}
00473               // then convert molecule to Native string...
00474               try
00475               {
00476                   Correct_answer=MoleculeStream.Molecule2String(elmol);
00477                   continu=true;
00478               }
00479               catch (IOException e) {System.out.println("error"+e+"trouble with converting native molecule to string:"+i );continu=false;}
00480        }
00481        else
00482        {
00483            if( i.indexOf("molfile")!=-1){
00484               System.out.println("molfile detected");
00485               // first convert MDMOL to molecule
00486               Molecule mdmol=new Molecule();
00487               try
00488               {
00489                   mdmol=MoleculeStream.ReadMDLMOL(new BufferedReader(new StringReader(i.toString())));
00490                   System.out.println("MDMOL file converted to molecule");
00491               }
00492               catch (IOException e) {System.out.println("error"+e);continu=false;}
00493               // then convert molecule to Native string...
00494               try
00495               {
00496                   Correct_answer=MoleculeStream.Molecule2String(mdmol);
00497                   continu=true;
00498               }
00499               catch (IOException e) {System.out.println("error"+e+"trouble with converting molecule to string:"+i );continu=false;}
00500            }
00501            else
00502            {
00503               if(Correct_answer.length() == 0){
00504                   feedback=translation.getProperty("empy_string_question");continu=false;
00505               }
00506               else
00507               {
00508                   feedback=translation.getProperty("invalid_question");continu=false;
00509               }
00510            }
00511        }
00512     }
00513     
00514     public String ReadApplet(){
00515        //jm.evers:
00516        // return info of the shown molecule... e.g. using the applet as image viewer
00517        // accet
00518        try
00519        {
00520            StringWriter sw=new StringWriter();BufferedWriter bw=new BufferedWriter(sw);MoleculeStream.WriteNative(bw,mainPanel.MolData());
00521            Correct_answer=sw.toString();Correct_answer=Correct_answer.replaceAll(" ","");
00522            if(Correct_answer.indexOf("WIMSchem!")!=-1 || Correct_answer.indexOf("!FIN")!=-1){
00523               if(Correct_answer.length() < 22){// !WIMSchem(0,0) \n !FIN 
00524                   feedback=translation.getProperty("empty_answer");continu=false;
00525               }
00526            }
00527            else
00528            {
00529               continu=false;feedback=translation.getProperty("no_sketchfile_answer");
00530            }
00531        }
00532        catch (IOException e) {System.out.println("error"+e);continu=false;feedback=translation.getProperty("no_sketchfile_answer");}
00533        AnalyseMolecule(Correct_answer,false);
00534        html_smiles_ca=MoleculeStream.GetSmiles(mainPanel.MolData());
00535        return        weight_ca+"\n"+plain_formula_ca+"\n"+html_formula_ca+"\n"+html_smiles_ca+"\n"+latex_formula_ca;
00536     }
00537     
00538     public String CompareNative(){ 
00539        // jm.evers: 
00540        // only usefull if called [javascript] together with GetQuestion(). See example in javascript !
00541        System.out.println("entering function CompareNative()");
00542        try
00543        {
00544            StringWriter sw=new StringWriter();BufferedWriter bw=new BufferedWriter(sw);
00545            MoleculeStream.WriteNative(bw,mainPanel.MolData());
00546            Student_answer=sw.toString();Student_answer=Student_answer.replaceAll(" ","");
00547            System.out.println("Student answer is:"+Student_answer);
00548            
00549            if(Student_answer.indexOf("WIMSchem!")!=-1 || Student_answer.indexOf("!FIN")!=-1){
00550               if(Student_answer.length() < 22){// !WIMSchem(0,0) \n !FIN 
00551                   feedback=translation.getProperty("empty_answer");;
00552                   continu=false;
00553               }
00554            }
00555            else
00556            {
00557               continu=false;
00558               feedback=translation.getProperty("no_sketchfile_answer");
00559            }
00560        }
00561        catch (IOException e) {System.out.println("error"+e);continu=false;feedback=translation.getProperty("no_sketchfile_answer");}
00562 
00563        if(continu){
00564            feedback="";num_sa=0;num_ca=0;bond_sa=0;bond_ca=0;weight_sa=0.0;weight_ca=0.0;
00565            AnalyseMolecule(Student_answer,true);
00566            AnalyseMolecule(Correct_answer,false);
00567            double score=getScore();
00568            // a smiles representation -at least it could look like smiles ...
00569            html_smiles_sa=MoleculeStream.GetSmiles(mainPanel.MolData());
00570            try {
00571               html_smiles_ca=MoleculeStream.GetSmiles(MoleculeStream.ReadNative(new BufferedReader(new StringReader(Correct_answer))));
00572            }
00573            catch(IOException e) { System.out.println("error"+e);}
00574            return score+"\n"+weight_sa+"\n"+weight_ca+"\n"+plain_formula_sa+"\n"+plain_formula_ca+"\n"+html_formula_sa+"\n"+html_formula_ca+"\n"+html_smiles_sa+"\n"+html_smiles_ca+"\n"+latex_formula_sa+"\n"+latex_formula_ca+"\n"+feedback+"\n@"+Student_answer+"\n@"+Correct_answer;
00575        }
00576        else
00577        {
00578            return "error\n"+feedback;
00579        }
00580     }
00581     
00582     public void AnalyseMolecule(String S , boolean whatisit){
00583        // jm.evers
00584        // whatisit=false : correct answer e.g. from javascript by wims.
00585        // whatisit=true  : student drawing
00586        // reading data , preparing representation formulas
00587        if(whatisit){System.out.println("analysing student reply");}else{System.out.println("analysing correct answer");}
00588        String[] s=S.split("\n");String[] atom;int s1=s[0].indexOf('(');int s2=s[0].indexOf(',');int s3=s[0].indexOf(')');
00589        int n_s=Integer.parseInt(s[0].substring(s1+1,s2).trim());int b_s=Integer.parseInt(s[0].substring(s2+1,s3).trim());
00590        String[] E_s=new String[n_s];int[] H_s=new int[n_s];int[] C_s=new int[n_s];int[] R_s=new int[n_s];
00591        int[] this_A=new int[N];int[] this_C=new int[N];int[] this_R=new int[N];int[] this_H=new int[N];
00592        String plainformula="";String htmlformula="";String latexformula="";
00593        String R_plain="";String R_html="";String R_latex="";
00594        String C_plain="";String C_html="";String C_latex="";
00595        String sign;
00596        double weight_s=0.0D;int r=0;
00597        
00598        // loop only through the atom part of the file-string 
00599        for( int p=0; p<n_s ;p++){
00600            atom=s[p+1].split("[\\=\\,\\;]"); //first line is mimetype
00601            E_s[p]=atom[0];r=0;
00602            for(int n=0;n<N;n++){
00603               if(atom[0].equals(Molecule.ELEMENTS[n])){
00604                   weight_s=(double)(weight_s + Molecule.WEIGHTS[n]);
00605                   this_A[n]++;r=n;n=N;
00606               }
00607            }
00608            C_s[p]=Integer.parseInt(atom[3]);
00609            this_C[r]=C_s[p];
00610            R_s[p]=Integer.parseInt(atom[4]);
00611            this_R[p]=R_s[p];
00612            if( atom[5].indexOf("i")!=-1){
00613               atom[5]=atom[5].replaceAll("i","");H_s[p]=Integer.parseInt(atom[5]);this_H[r]=H_s[p];this_A[1]=this_A[1]+H_s[p];//hydrogen
00614            }
00615            else
00616            {
00617               atom[5]=atom[5].replaceAll("e","");H_s[p]=Integer.parseInt(atom[5]);this_H[r]=H_s[p];this_A[1]=this_A[1]+H_s[p];//hydrogen
00618            }      
00619            weight_s=(double)(weight_s + H_s[p]);
00620        }
00621 
00622        if(this_A[6]!=0){
00623            if(this_R[6] != 0){R_plain="\u2022";R_latex="^{^{\\cdot}}";R_html="<sup>&cdot;</sup>";}
00624            if(this_C[6] != 0){
00625               if(this_C[6]>0){sign="+";}else{sign="-";this_C[6]=(int) (Math.abs(this_C[6]));}
00626               C_plain=this_C[6]+sign;C_latex="^{"+this_C[6]+sign+"}";C_html="<sup><small>"+this_C[6]+sign+"</small></sup>";
00627            }
00628            if(this_A[6] == 1 ){// C1 is not done:  CH4
00629               plainformula="C"+C_plain+R_plain+" ";
00630                htmlformula="<font size=+1><tt>C"+C_html+R_html+"</tt></font>";
00631               latexformula="\\Bf{C"+C_latex+R_latex+"}";
00632            }
00633            else
00634            {//C2H5OH
00635               plainformula="C"+C_plain+R_plain+this_A[6];
00636               htmlformula="<font size=+1><tt>C"+C_html+R_html+"</tt></font><sub><small>"+this_A[6]+"</small></sub>";
00637               latexformula="\\Bf{C"+C_latex+R_latex+"}_{"+this_A[6]+"}";
00638            }
00639        }
00640        // the rest
00641        for(int p = 0; p<N; p++){
00642            if( p == 6 ){ p = 7;} // we already covered carbon
00643            if( this_A[p] != 0 ){
00644               R_plain="";R_html="";R_latex="";C_plain="";C_html="";C_latex="";sign="";
00645               if( this_R[p] != 0 ){ R_plain=this_R[p]+"\u2022";R_latex="^{^{"+this_R[p]+"\\cdot}}";R_html="<sup><sup>"+this_R[p]+"&cdot;</sup></sup>";}
00646               if( this_C[p] != 0 ){ if( this_C[p]>0 ){ sign="+"; } else { sign="-"; this_C[p]=-1*this_C[p];}C_plain=this_C[p]+sign;C_latex="^{"+this_C[p]+sign+"}";C_html="<sup><small>"+this_C[p]+sign+"</small></sup>";}
00647               if( this_A[p] == 1 ){
00648                   plainformula=plainformula+" "+Molecule.ELEMENTS[p]+""+C_plain+""+R_plain;
00649                   htmlformula=htmlformula+" <font size=+1><tt>"+Molecule.ELEMENTS[p]+C_html+R_html+"</tt></font>";
00650                   latexformula=latexformula+"\\,\\Bf{"+Molecule.ELEMENTS[p]+C_latex+R_latex+"}";
00651               }
00652               else
00653               {
00654                   plainformula=plainformula+" "+Molecule.ELEMENTS[p]+""+C_plain+""+R_plain+""+this_A[p];
00655                   htmlformula=htmlformula+" <font size=+1><tt>"+Molecule.ELEMENTS[p]+C_html+R_html+"</tt></font><sub><small>"+this_A[p]+"</small></sub>";
00656                   latexformula=latexformula+"\\,\\Bf{"+Molecule.ELEMENTS[p]+C_latex+R_latex+"}_{"+this_A[p]+"}";
00657               }
00658            }
00659        }
00660        
00661        if(whatisit){// these are used for getting a score
00662            elements_sa=E_s;hydro_sa=H_s;charge_sa=C_s;radical_sa=R_s;weight_sa=weight_s;
00663            plain_formula_sa=plainformula;html_formula_sa=htmlformula;latex_formula_sa=latexformula;num_sa=n_s;bond_sa=b_s;
00664        }
00665        else
00666        {
00667            elements_ca=E_s;hydro_ca=H_s;charge_ca=C_s;radical_ca=R_s;weight_ca=weight_s;
00668            plain_formula_ca=plainformula;html_formula_ca=htmlformula;latex_formula_ca=latexformula;num_ca=n_s;bond_ca=b_s;
00669        }
00670     }// end AnalyseMolecule()
00671 
00672     public double getScore(){
00673        System.out.println("entering function getScore()");
00674        // jm.evers:
00675        // comparing the molecules studentanswer [sa] and correct answer [ca]
00676        // giving somekind of score: ofcourse any "score < 10" is technically plain wrong !!
00677        // feedback is not yet operational...because it gives disturbing messages :(
00678        int hit=0;String tmp_remark="";
00679        boolean[] done=new boolean[num_sa];
00680        double finalscore=0.0D;
00681        for(int p=0;p<num_sa;p++){
00682            done[p]=true;
00683        }
00684        if( num_ca == num_sa  && bond_ca == bond_sa ){
00685            for( int c=0; c < num_ca ; c++){
00686               for( int s=0; s < num_sa ;s++){
00687                   if(done[s]){
00688                      if( elements_ca[c].equals(elements_sa[s]) && charge_ca[c] == charge_sa[s] && radical_ca[c] == radical_sa[s] && hydro_ca[c] == hydro_sa[s]){
00689                          done[s]=false;
00690                          hit++;s=num_sa;break;
00691                      }
00692                      else
00693                      {
00694                          if( elements_ca[c].equals(elements_sa[s]) ){
00695                             if( charge_ca[c] != charge_sa[s] ){ tmp_remark="wrong_charge "+elements_ca[c]+"="+charge_ca[c]+",";}
00696                              if( radical_ca[c] != radical_sa[s] ){ tmp_remark="wrong_radical "+elements_ca[c]+"="+radical_ca[c]+",";}
00697                             if( hydro_ca[c] != hydro_sa[s] ){ tmp_remark="wrong_amount_hydrogen "+elements_ca[c]+"="+hydro_ca[c]+","; }
00698                          }
00699                      }
00700                   }
00701               }
00702            }
00703        }      
00704        feedback=feedback+tmp_remark;
00705        if(num_ca > num_sa){
00706            finalscore=(double)(10*hit/num_ca);
00707        }
00708        else
00709        {
00710            finalscore=(double)(10*hit/num_sa);
00711        }
00712        System.out.println("finalscore="+finalscore);
00713        return finalscore;
00714     }
00715 
00716     public void componentHidden(ComponentEvent e){}
00717     public void componentMoved(ComponentEvent e){}
00718     public void componentResized(ComponentEvent e){}
00719     public void componentShown(ComponentEvent e){
00720     mainPanel.ScaleToFit();
00721     mainPanel.repaint();
00722     }
00723 
00724 }
00725 
00726