Back to index

wims  3.65+svn20090927
MathView.java
Go to the documentation of this file.
00001 /*                                                                                                                                          
00002 *  This program is free software; you can redistribute it and/or modify                                       
00003 *  it under the terms of the GNU General Public License as published by                                       
00004 *  the Free Software Foundation; either version 2 of the License, or                                          
00005 *  (at your option) any later version.                                                                        
00006 *                                                                                                             
00007 *  This program is distributed in the hope that it will be useful,                                            
00008 *  but WITHOUT ANY WARRANTY; without even the implied warranty of                                             
00009 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                              
00010 *  GNU General Public License for more details.                                                               
00011 *                                                                                                             
00012 *  You should have received a copy of the GNU General Public License                                          
00013 *  along with this program; if not, write to the Free Software                                                
00014 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                                                  
00015 */     
00016 
00017 
00018 /*
00019 * This is a slight modification of a JomeInput.java program
00020 * The applet now accepts a few "parms" like:
00021 *
00022 * <param name="bgcolor" value="0xffff96">
00023 * <param name="largefont" value="24">
00024 * <param name="smallfont" value="16">
00025 * <param name="wimsvars" value="x,y,z,n,d">
00026 * <param name="oldreply" value="$wims_reply">
00027 * <param name="my_panel" value="module_title:log[3]():sqrt[5]():and:or:isin:notin:R:Z:N:Q">
00028 * <param name="helptext" value="f(x)=sin(3x)/cos(x^2)->f'(x)="> 
00029 * 
00030 * --> my_panel: a custom button panel ,tunable for the exercise at hand.
00031 * --> wimsvars: a rudimentary form of "rawmath"...
00032 * --> helptext: the exercise description ,some text may be allowed ?
00033 * 
00034 *
00035 * version 0.1 
00036 * 5/2007 
00037 * first rudimentary adaption for wims usage: filtering with javascript
00038 * 
00039 * version 0.2 
00040 * 11/2007 
00041 * --> Introduction of log[g](a) and sqrt[g](a)
00042 * --> Filtering now in poorly written java :)
00043 * --> There still are some unresolved issues with "spaces" being turned into "+" signs by the included jome-code of applet.
00044 *     This is due to the fact that the applet -the way I use it- is not suitable for multiple-answers.(single line)
00045 * --> The function "filter(String s1 , int k)" will change the "helptext" appletparam from ascii-math into "unicode" and reverse.
00046 *     Handling most common cases of false "+" signs. 
00047 * --> More reserved keywords can be included, if needed. [added div,times,notin,isin...etc etc]
00048 * --> The helptext now gets stripped away in this applet, when the function getLinear() is called by javascript.
00049 *     (and will thus not be send as part of the answer)
00050 *
00051 *
00052 * version 0.3 
00053 * 1/2008
00054 * --> Deleted all useless references to MathML,OML...reducing size > 50% !
00055 * --> Introduced <param name="oldreply" value="$wim_reply"> echoing the wims-rejected answer in the textareafield
00056 * --> All (?) flaws of the applet are -hopefully- corrected...it should now be a one-line matheditor.
00057 *     In "Multi-aswers" statements/parts can be "glued" with arrows,and,or 
00058 * --> Domainsyntax allowed: [0:+inf> --> javascript --> lbracket 0 : +inf rangle
00059 * --> Input sqrt[6](123) is also allowed...for "compatibility" with JS checkfiles 
00060 * --> Input log[8](123) is  also allowed...for "compatibility" with JS checkfiles
00061 * --> added reserved word array(1,2) used for a twoline "statement"
00062 * --> multilines: array(first line , array(line two,line three)) etc etc 
00063 * --> restored the "," as special symbol -> n-th root : root(p,n) == sqrt[n](p)
00064 * --> both n-th root syntaxes are read by javascript as: sqrt[n](p) --> send to wims as p^(1/n)
00065 *
00066 * version 0.4
00067 * 3/2008
00068 * --> compatible with java 1.4 ??? new replace function .... String.replace(String1,String2)
00069 * --> vlist(line1,line2,line3....) 
00070 */
00071 
00072 import fr.ove.openmath.jome.Jome;
00073 import java.applet.Applet;
00074 import javax.swing.*;
00075 import java.awt.*;
00076 import java.awt.event.*;
00077 import java.io.PrintStream;
00078 
00079 public class MathView extends Applet
00080     implements ActionListener, ItemListener, TextListener, WindowListener
00081 {
00082     // function that implements a java 1.5 style: String0.replace(String1,String2) --> replace(String0,String1,String2)
00083     // thanks to Bernadette Perrin and her java 1.4 ;)
00084     public static String replace(String source, String pattern, String replace)
00085     {
00086            if (source!=null)
00087            {
00088               final int len = pattern.length();
00089               StringBuffer sb = new StringBuffer();
00090               int found = -1;
00091               int start = 0;
00092               while( (found = source.indexOf(pattern, start) ) != -1) {
00093                   sb.append(source.substring(start, found));
00094                   sb.append(replace);
00095                   start = found + len;
00096               }
00097            sb.append(source.substring(start));
00098            return sb.toString();
00099            }
00100         else return "";
00101     }
00102 
00103     public void setWindow(JFrame frame)
00104     {
00105         standaloneWindow = frame;
00106         inAnApplet = false;
00107     }
00108 
00109     public void toggleFont()
00110     {
00111         Jome jome = j;
00112         if(jome.getFont() == bigFont)
00113         {
00114             jome.setFont(defaultFont);
00115             linearInput.setFont(defaultFont);
00116         } else
00117         {
00118             jome.setFont(bigFont);
00119             linearInput.setFont(bigFont);
00120         }
00121         invalidate();
00122         jome.invalidate();
00123         linearInput.invalidate();
00124         linearInput.requestFocus();
00125         repaint();
00126         jome.repaint();
00127         linearInput.repaint();
00128     }
00129 
00130     public static void main(String args[])
00131     {
00132         MathView mathview = new MathView();
00133         JFrame frame = new JFrame();
00134         mathview.setWindow(frame);
00135         frame.add(mathview);
00136         frame.setTitle("WIMS JAVA INPUT");
00137         frame.addWindowListener(mathview);
00138         mathview.init();
00139         frame.pack();
00140         frame.setSize(640,480);
00141         frame.setVisible(true);
00142         frame.validate();
00143         mathview.start();
00144         mathview.repaint();
00145 
00146 
00147     }
00148 
00149     private String removespecial(String t){
00151        int begin=t.indexOf("vlist(");
00152        if(begin==-1){return t;}
00153        String org=t;
00154        int t1;int t2;int tot;int Got;int Wait;int End;char chr;int s;
00155        String value;String exp;String val;String L;String R;int cnt=0;
00156        while (begin >= 0){
00157            cnt++;if(cnt>1000){return org+" vlist(a,b) syntax error" ;}
00158            tot=t.length();Wait=0;Got=0;End=0;
00159            for(s=begin+5;s<tot;s++){
00160               if(End==0){
00161                   chr=t.charAt(s);
00162                   if(chr=='('){Wait=Wait+1;}
00163                   if(chr==')'){Got=Got+1;}
00164                   if(Got!=0){if(Wait==Got){End=s;}}
00165               }
00166            }
00167            value=t.substring(begin+6,End);
00168            t1=value.indexOf(",");if(t1<0){return org+" syntax error: comma missing\nvlist(a,b,c,d)";}
00169            t2=value.length();
00170            val=value.substring(0,t2);
00171            if(val.indexOf("vlist(")!=-1){return org + "error :vlist within vlist";}
00172            L="vlist("+val+")";R=val;
00173            t=replace(t,L,R);
00174            //System.out.println("val= " + val +" L= "+L);
00175            begin=t.indexOf("vlist(");
00176        }
00177        t=replace(t,","," and ");
00178        return t;
00179     }
00180     
00181     
00182     private String root2sqrtn(String t){
00183        int begin=t.indexOf("root(");
00184        if(begin==-1){return t;}
00185        String org=t;
00186        int t1;int t2;int tot;int Got;int Wait;int End;char chr;int s;
00187        String value;String exp;String val;String L;String R;int cnt=0;
00188        while (begin >= 0){
00189            cnt++;if(cnt>1000){return org+" root(a,b) syntax error" ;}
00190            tot=t.length();Wait=0;Got=0;End=0;
00191            for(s=begin+4;s<tot;s++){
00192               if(End==0){
00193                   chr=t.charAt(s);
00194                   if(chr=='('){Wait=Wait+1;}
00195                   if(chr==')'){Got=Got+1;}
00196                   if(Got!=0){if(Wait==Got){End=s;}}
00197               }
00198            }
00199            value=t.substring(begin+5,End);
00200            t1=value.indexOf(",");if(t1<0){return org+" syntax error: comma missing\nroot(a,b)";}
00201            t2=value.length();
00202            val=value.substring(0,t1);exp=value.substring(t1+1,t2);
00203            if(val.indexOf("root(")!=-1){return org + "error : absurd root-syntax";}
00204            if(exp.indexOf("root(")!=-1){return org + "error : absurd root-syntax";}
00205            L="root("+val+","+exp+")";R="sqrt["+exp+"]#"+val+"@";
00206            t=replace(t,L,R);
00207            //System.out.println("val= " + val + "  exp=  "+ exp+ "  L= "+L+"  R= "+R);
00208            begin=t.indexOf("root(");
00209        }
00210        t=t.replace('#','(');
00211        t=t.replace('@',')');
00212        return t;
00213     }
00214 
00215     private String sqrtn2root(String t){
00216        int begin=t.indexOf("sqrt[");
00217        if(begin==-1){return t;}
00218        String org=t;
00219        int tot;int Got;int Wait;int End;char chr;int s;int restart;
00220        String value;String exp;String val;String L;String R;int cnt=0;
00221        while (begin >= 0){
00222            cnt++;if(cnt>1000){return org+"sqrt[]() syntax error" ;}
00223            tot=t.length();Wait=0;Got=0;End=0;
00224            for(s=begin+4;s<tot;s++){
00225               if(End==0){
00226                   chr=t.charAt(s);
00227                   if(chr=='['){Wait=Wait+1;}
00228                   if(chr==']'){Got=Got+1;}
00229                   if(Got!=0){if(Wait==Got){End=s;}}
00230               }
00231            }
00232            restart=End;    
00233            exp=t.substring(begin+5,End);
00234            Wait=0;Got=0;End=0;
00235            for(s=restart;s<tot;s++){
00236               if(End==0){
00237                   chr=t.charAt(s);
00238                   if(chr=='('){Wait=Wait+1;}
00239                   if(chr==')'){Got=Got+1;}
00240                   if(Got!=0){if(Wait==Got){End=s;}}
00241               }
00242            }
00243            val=t.substring(restart+2,End);
00244            R="root#"+val+","+exp+"@";L="sqrt["+exp+"]("+val+")";
00245            t=replace(t,L,R);
00246            //System.out.println("val= " + val + "  exp=  "+ exp+ "  L= "+L+"  R= "+R);
00247            begin=t.indexOf("sqrt[");
00248        }
00249        t=t.replace('#','(');
00250        t=t.replace('@',')');
00251        return t;
00252     }
00253 
00254     
00255     private String filter1(String s1 , int k ){
00256         int p;String r1;String r2;
00257        String d1[] = new String[]{"of","et","en","*","cdot","times","div","N","Z","Q","R","\\",
00258        "f^-1(x)","isin","and","or","notin","-->","=>","inf","->",
00259        "log[2]","log[3]","log[4]","log[5]","log[6]","log[7]","log[8]","log[9]"};
00260        
00261        String d2[] = new String[]{"\u22C1","\u22C0","\u22C0","\u2027","\u2027","X","\u00F7","\u2115","\u2124","\u211A","\u211D","\u2216",
00262        "f\u00AF\u00B9(x)","\u2208","\u22C0","\u22C1","\u2209","rightarrow","\u21D4","\u221E","\u2192",
00263        "\u00B2log","\u00B3log","\u2074log","\u2075log","\u2076log","\u2077log","\u2078log","\u2079log"};
00264        
00265        String d3[] = new String[]{" or "," and "," and ","*","*","*","/","N","Z","Q","R","\\",
00266        "f^-1(x)"," isin "," and "," or "," notin "," -> "," -> "," inf "," -> ",
00267        "log[2]","log[3]","log[4]","log[5]","log[6]","log[7]","log[8]","log[9]"};
00268        
00269        int d=d1.length;
00270        //int e=d1.length;int f=d1.length;if( d!=e || d!=f){System.out.println("ERROR array lengte verschil d= " + d +"\n e= "+ e +"\n f =" +f) ;}
00271        // d1=applettext from wims
00272        // d2=unchangable text in math-area from applet
00273        // d3=the syntax we want to read by javascript: basically spaces/words for readablitity 
00274        if(k==1){
00275            // from ascii applettext-param to unicode applet and visa versa
00276            for ( p=0;p<d;p++){                                                                                                                
00277               r1=d1[p];                                                                                                                 
00278               r2=d2[p];                                                                                                                 
00279               s1=replace(s1,r1,r2);                                                                                                          
00280            }
00281            s1=sqrtn2root(s1);
00282        }
00283        else
00284        {
00285            for ( p=0;p<d;p++){                                                                                                                
00286               r1=d3[p];                                                                                                                 
00287               r2=d2[p];                                                                                                                 
00288               s1=replace(s1,r2,r1);                                                                                                          
00289            }
00290        }
00291         return s1;                                                                                                                         
00292     }
00293     
00294     private String filter2(String s){
00295        String vars=readParams("wimsvars");
00296        if(vars.length()==0){vars="x";}
00297        vars=vars.toString();vars=replace(vars,",","");
00298        StringBuffer S=new StringBuffer(s);int p;int r;char x;char getal;char P1;char P2;char P3;String P4="";
00299        String nummers="0123456789"+vars;nummers=nummers.toString();int t=vars.length();
00300        int q=nummers.length();int m;int max=S.length();
00301        String[] mathopts={"sq","si","co","ta","ab","lo","ln"};int opts=7;String mop="";int a;
00302        //System.out.println("aantal chars is nu...max = " + max);
00303        if(max>1){
00304            for(r=0;r<t;r++){ 
00305               x=vars.charAt(r);
00306               for(p=0;p<q;p++){
00307                   getal=nummers.charAt(p);
00308                   for(m=0;m<max-2;m++){
00309                      P1=S.charAt(m);P2=S.charAt(m+1);
00310                      if(P1==getal && P2==x){
00311                          S=S.insert(m+1,"*");m=m+2;max++;linearInput.setText(S.toString());
00312                          //System.out.println("S="+S+" P1= "+P1 +" P2= "+P2 );
00313                      }
00314                      else
00315                      {
00316                          if(max>3){
00317                             P3=S.charAt(m+2);
00318                             if( (P1==' ' && P2==' ' && P3==' ') || (P1==' ' && P2=='e' && P3=='n') || (P1=='e' && P2=='n' && P3==' ') || (P1=='e' && P2=='n' && P3==x) ){
00319                                 S=S.delete(m,m+3);S=S.append(" and ");m=m+3;linearInput.setText(S.toString());
00320                             }
00321                             else
00322                             {
00323                                 if( (P1==' ' && P2=='o' && P3=='f') || (P1=='o' && P2=='f' && P3==' ') || (P1=='o' && P2=='f' && P3==x) ){
00324                                    S=S.delete(m,m+3);S=S.append(" or ");m=m+2;linearInput.setText(S.toString());
00325                                 }
00326                                 else
00327                                 {
00328                                    // poormans rawmath for "common function"
00329                                    if(P1==getal || P1==x ){
00330                                        P4="" + P2 + P3;
00331                                        for(a=0;a<opts;a++){
00332                                           mop=mathopts[a];if(P4.equals(mop)){S=S.insert(m+1,"*");a=opts;m=m+4;linearInput.setText(S.toString());}
00333                                        }
00334                                    }
00335                                 }
00336                             }
00337                          }
00338                      }
00339                      max=S.length();
00340                   }
00341               }
00342            }    
00343        }
00344        s=S.toString();
00345        s=sqrtn2root(s);
00346        return s;
00347     }
00348     
00349     private String readParams(String s){
00350        String c=getParameter(s);
00351        return c;
00352     }
00353 
00354     private void reportError(String s)
00355     {
00356         if(!inAnApplet)
00357             System.err.println(s);
00358     }
00359 
00360     private void addPaletteCard(Choice choice, Container container, String s, Container container1)
00361     {
00362         choice.add(s);
00363         container.add(container1, s);
00364     }
00365 
00366     private JButton addButton(Container container, String s, String s1)
00367     {
00368         JButton button = new JButton(s);
00369         button.setActionCommand(s1);
00370         button.addActionListener(this);
00371         container.add(button);
00372         return button;
00373     }
00374 
00375     private JButton addButton(Container container, String s, String s1, String s2)
00376     {
00377         JButton button = new JButton(s);
00378         button.setActionCommand(s1);
00379         button.addActionListener(this);
00380         container.add(button, s2);
00381         return button;
00382     }
00383 
00384     public void start()
00385     {
00386         linearInput.setFont(defaultFont);
00387         linearInput.invalidate();
00388         j.invalidate();
00389         repaint();
00390         linearInput.requestFocus();
00391        // echoing the WIMS rejected answer in the inputarea...what kindness of the heart ;)
00392        String oldreply=readParams("oldreply");
00393        if(oldreply.length()!=0){
00394            oldreply=replace(oldreply," ","");
00395            oldreply=filter1(oldreply,1);
00396            linearInput.setText(oldreply.toString());
00397        }
00398     }
00399     
00400     public void init()
00401     {
00402        String special=readParams("my_panel");
00403        String smallfont=readParams("smallfont");
00404        String largefont=readParams("largefont");
00405        String kleur=readParams("bgcolor");
00406        int font1;int font2;
00407        /* check op getallen */
00408        if(smallfont==null){font1=16;}else{font1 = Integer.parseInt(smallfont);}
00409        if(largefont==null){font2=24;}else{font2 = Integer.parseInt(largefont);}
00410         bigFont = new Font("Helvetica", 0, font2);defaultFont = new Font("Helvetica", 0, font1);
00411        Color bgcolor;
00412        if(kleur!=null){bgcolor=Color.decode(kleur);}else { bgcolor=Color.white; }
00413         setBackground(bgcolor);
00414         j = new Jome();
00415         j.setFont(defaultFont);
00416         j.setDrawBounds(true);
00417         //j.setBackground(new Color(0xffffdd));
00418         j.setBackground(bgcolor);  
00419         BorderLayout borderlayout = new BorderLayout();
00420         setLayout(new BorderLayout());
00421        
00422 
00423         controlPane = new JPanel();
00424        controlPane.setFont(defaultFont);
00425         controlPane.setLayout(new BorderLayout());
00426         palette = new JPanel();
00427         palette.setLayout(new BorderLayout());
00428        palette.setFont(defaultFont);
00429         paletteCards = new JPanel();
00430         paletteCardsLayout = new CardLayout(5, 4);
00431         paletteCards.setLayout(paletteCardsLayout);
00432         paletteChoice = new Choice();
00433         paletteChoice.addItemListener(this);
00434 
00435        // param name="my_panel" value="title : function1 : function1 : function1 : function1 "
00436        if(special!=null){
00437            int maxfun=7;
00438            String[] results = special.split(":");
00439            JPanel panel01 = new JPanel();
00440            panel01.setLayout(new GridLayout());
00441            int p;
00442            int pos=results.length;
00443            String P;
00444            String PP="";
00445            String titel=results[0];
00446            for (p=1;p<pos;p++){
00447            P=results[p];
00448            // converting wims-module syntax to panel buttons
00449            if(P.equals("sqrt()")){PP="\u221A";}
00450            else
00451            if(P.equals("sqrt[2]()")){P="sqrt()";PP="<html><sup>2</sup>\u221A</html>";}
00452            else
00453            if(P.equals("sqrt[3]()")){PP="<html><sup>3</sup>\u221A</font></html>";}
00454            else
00455            if(P.equals("sqrt[4]()")){PP="<html><sup>4</sup>\u221A</html>";}
00456            else
00457            if(P.equals("sqrt[5]()")){PP="<html><sup>5</sup>\u221A</html>";}
00458            else
00459            if(P.equals("sqrt[6]()")){PP="<html><sup>6</sup>\u221A</html>";}
00460            else
00461            if(P.equals("sqrt[7]()")){PP="<html><sup>7</sup>\u221A</html>";}
00462            else
00463            if(P.equals("sqrt[8]()")){PP="<html><sup>8</sup>\u221A</html>";}
00464            else
00465            if(P.equals("sqrt[9]()")){PP="<html><sup>9</sup>\u221A</html>";}
00466            else
00467            if(P.equals("sqrt[n]()")){PP="<html><sup>n</sup>\u221A</html>";}
00468            else
00469            if(P.equals("log[2]()")){P="\u00B2log()";PP="<html><sup>2</sup>log</html>";}
00470            else
00471            if(P.equals("log[3]()")){P="\u00B3log()";PP="<html><sup>3</sup>log</html>";}
00472            else
00473            if(P.equals("log[4]()")){P="\u2074log()";PP="<html><sup>4</sup>log</html>";}
00474            else
00475            if(P.equals("log[5]()")){P="\u2075log()";PP="<html><sup>5</sup>log</html>";}
00476            else
00477            if(P.equals("log[6]()")){P="\u2076log()";PP="<html><sup>6</sup>log</html>";}
00478            else
00479            if(P.equals("log[7]()")){P="\u2077log()";PP="<html><sup>7</sup>log</html>";}
00480            else
00481            if(P.equals("log[8]()")){P="\u2078log()";PP="<html><sup>8</sup>log</html>";}
00482            else
00483            if(P.equals("log[9]()")){P="\u2079log()";PP="<html><sup>9</sup>log</html>";}
00484            else
00485            if(P.equals("isin")){P="\u0020in\u0020" ;PP="\u2208 [isin]";}
00486            else
00487            if(P.equals("and")){P="\u0020and\u0020";PP="\u22C0 [and]";}
00488            else
00489            if(P.equals("or")){P="\u0020or\u0020";PP="\u22C1 [or]";}
00490            else
00491            if(P.equals("pi")){P="\u0020pi\u0020";PP="\u03C0";}    
00492            else
00493            if(P.equals("notin")){P="\u0020notin\u0020";PP="\u2209 [notin]";}    
00494            else
00495            if(P.equals("->")){P="\u2192";PP="\u2192";}    
00496            else
00497            if(P.equals("=>")){P="\u2192";PP=P;}    
00498            else
00499            if(P.equals("<=>")){P="\u21E4";PP=P;}    
00500            else
00501            if(P.equals("<->")){P="\u21D4";PP=P;}    
00502            else
00503            if(P.equals("<->")){P="\u21D4";PP=P;}    
00504            else
00505            if(P.equals("langle")){P="\u0020langle\u0020";PP="<";}    
00506            else
00507            if(P.equals("rangle")){P="\u0020rangle\u0020";PP=">";}    
00508            else
00509            if(P.equals("lbracket")){P="\u0020lbracket\u0020";PP="[";}    
00510            else
00511            if(P.equals("rbracket")){P="\u0020rbracket\u0020";PP="]";}    
00512            else
00513            if(P.equals("inf")){P="infty";PP="\u221E";}
00514            else{PP=P;}
00515            addButton(panel01,PP,"insert "+P);
00516            }
00517            addPaletteCard(paletteChoice, paletteCards , titel , panel01);
00518            palette.add(paletteChoice, "North");
00519            palette.add(paletteCards, "Center");
00520            controlPane.add(palette, "Center");
00521            j.setVisible(true);
00522            add(j, "Center");
00523        }
00524 
00525         JPanel panel04 = new JPanel();
00526         panel04.setLayout(new GridLayout());
00527         addButton(panel04, "\u22C0 [and]", "insert \u0020and ");
00528         addButton(panel04, "\u22C1 [or]", "insert \u0020or ");
00529         addButton(panel04, "\u2208 [in]", "insert \u0020in");
00530         addButton(panel04, "\u2209 [notin]", "insert \u0020notin");
00531         addButton(panel04, "\u2192", "insert \u0020\u2192\u0020");    
00532         addButton(panel04, "\u21D4", "insert \u21D4");
00533         addButton(panel04, "\u221E [inf]", "insert \u221E");
00534         addPaletteCard(paletteChoice, paletteCards, "Misc. Symbols", panel04);
00535         palette.add(paletteChoice, "North");
00536         palette.add(paletteCards, "Center");
00537         controlPane.add(palette, "Center");
00538         j.setVisible(true);
00539         add(j, "Center");
00540 
00541         JPanel panel05 = new JPanel();
00542         panel05.setLayout(new GridLayout());
00543         addButton(panel05, "multiple answers", "insert vlist(answer1,answer2,answer3,answer4)");
00544         addButton(panel05, "ln", "append ln()");
00545         addButton(panel05, "|x|", "append |x|");
00546         addButton(panel05, "f \u00AF\u00B9(x)= [inverse]", "append f\u00AF\u00B9(x)=");
00547         addPaletteCard(paletteChoice, paletteCards, "Misc.", panel05);
00548         palette.add(paletteChoice, "North");
00549         palette.add(paletteCards, "Center");
00550         controlPane.add(palette, "Center");
00551         j.setVisible(true);
00552         add(j, "Center");
00553 
00554         JPanel panel07 = new JPanel();
00555         panel07.setLayout(new GridLayout());
00556         addButton(panel07, "log", "append log()");
00557         addButton(panel07, "<html><sup>2</sup>log</html>", "append \u00B2log()");
00558         addButton(panel07, "<html><sup>3</sup>log</html>", "append \u00B3log()");
00559         addButton(panel07, "<html><sup>4</sup>log</html>", "append \u2074log()");
00560         addButton(panel07, "<html><sup>5</sup>log</html>", "append \u2075log()");
00561         addButton(panel07, "<html><sup>6</sup>log</html>", "append \u2076log()");
00562         addButton(panel07, "<html><sup>7</sup>log</html>", "append \u2077log()");
00563         addButton(panel07, "<html><sup>8</sup>log</html>", "append \u2078log()");
00564         addButton(panel07, "<html><sup>9</sup>log</html>", "append \u2079log()");
00565         addPaletteCard(paletteChoice, paletteCards, "Log", panel07);
00566         palette.add(paletteChoice, "North");
00567         palette.add(paletteCards, "Center");
00568         controlPane.add(palette, "Center");
00569         j.setVisible(true);
00570         add(j, "Center");
00571 
00572         JPanel panel08 = new JPanel();
00573         panel08.setLayout(new GridLayout());
00574         addButton(panel08, "\u221A", "append sqrt()");
00575         addButton(panel08, "<html><sup>3</sup>\u221A</html>", "append sqrt[3]()");
00576         addButton(panel08, "<html><sup>4</sup>\u221A</html>", "append sqrt[4]()");
00577         addButton(panel08, "<html><sup>5</sup>\u221A</html>", "append sqrt[5]()");
00578         addButton(panel08, "<html><sup>6</sup>\u221A</html>", "append sqrt[6]()");
00579         addButton(panel08, "<html><sup>7</sup>\u221A</html>", "append sqrt[7]()");
00580         addButton(panel08, "<html><sup>8</sup>\u221A</html>", "append sqrt[8]()");
00581         addButton(panel08, "<html><sup>n</sup>\u221A</html>", "append sqrt[n](p)");
00582         addPaletteCard(paletteChoice, paletteCards, "\u221A", panel08);
00583         palette.add(paletteChoice, "North");
00584         palette.add(paletteCards, "Center");
00585         controlPane.add(palette, "Center");
00586         j.setVisible(true);
00587         add(j, "Center");
00588 
00589         JPanel panel09 = new JPanel();
00590         panel09.setLayout(new GridLayout());
00591         addButton(panel09, "\u03C0 [pi]", "insert \u0020pi ");
00592         addButton(panel09, "sin()", "append sin()");
00593         addButton(panel09, "sin\u00B2()", "append sin()^2");
00594         addButton(panel09, "cos()", "append cos()");
00595         addButton(panel09, "cos\u00B2()", "append cos()^2");
00596         addButton(panel09, "tan()", "append tan()");
00597         addButton(panel09, "tan\u00B2()", "append tan()^2");
00598         addPaletteCard(paletteChoice, paletteCards, "Trigonometry", panel09);
00599         palette.add(paletteChoice, "North");
00600         palette.add(paletteCards, "Center");
00601         controlPane.add(palette, "Center");
00602         j.setVisible(true);
00603         add(j, "Center");
00604 
00605         JPanel panel10 = new JPanel();
00606         panel10.setLayout(new GridLayout());
00607         addButton(panel10, " < : > ", "append \u0020langle\u0020:\u0020rangle");
00608         addButton(panel10, " [ : > ", "append \u0020lbracket\u0020:\u0020rangle");
00609         addButton(panel10, " < : ] ", "append \u0020langle\u0020:\u0020rbracket");
00610         addButton(panel10, " [ : ] ", "append \u0020lbracket\u0020:\u0020rbracket");
00611         addButton(panel10, "-\u221E [-inf]", "insert -\u221E");
00612         addButton(panel10, "+\u221E [+inf]", "insert +\u221E");
00613         addPaletteCard(paletteChoice, paletteCards, "Domains I", panel10);
00614         palette.add(paletteChoice, "North");
00615         palette.add(paletteCards, "Center");
00616         controlPane.add(palette, "Center");
00617         j.setVisible(true);
00618         add(j, "Center");
00619 
00620         JPanel panel11 = new JPanel();
00621         panel11.setLayout(new GridLayout());
00622         addButton(panel11, "x \u2208 \u2115 \u2216 \u007B a \u003B b \u007D", "insert x\u2208\u2115\u0020\u2216\u0020\u007Ba:b:c\u007D");
00623         addButton(panel11, "x \u2208 \u2124 \u2216 \u007B a \u003B b \u007D", "insert x\u2208\u2124\u0020\u2216\u0020\u007Ba:b:c\u007D");
00624         addButton(panel11, "x \u2208 \u211A \u2216 \u007B a \u003B b \u007D", "insert x\u2208\u211A\u0020\u2216\u0020\u007Ba:b:c\u007D");
00625         addButton(panel11, "x \u2208 \u211D \u2216 \u007B a \u003B b \u007D", "insert x\u2208\u211D\u0020\u2216\u0020\u007Ba:b:c\u007D");
00626         addPaletteCard(paletteChoice, paletteCards, "Domains II", panel11);
00627         palette.add(paletteChoice, "North");
00628         palette.add(paletteCards, "Center");
00629         controlPane.add(palette, "Center");
00630         j.setVisible(true);
00631         add(j, "Center");
00632         
00633        JPanel panel2 = new JPanel();
00634         panel2.setLayout(new BorderLayout());
00635 
00636        JPanel panel4 = new JPanel();
00637         panel2.add(panel4, "South");
00638         reportError("OK");
00639         JPanel panel3 = new JPanel();
00640         panel2.add(panel3, "West");
00641        undoButton = addButton(panel3, "\u21A9", "undo");
00642         redoButton = null;
00643         addButton(panel3, "\261", "toggle_font", "Center");
00644         controlPane.add(panel2, "South");
00645         linearInput = new TextArea("",1,1,TextArea.SCROLLBARS_NONE);
00646         linearInput.addTextListener(this);
00647         linearInput.setFont(bigFont);
00648        linearInput.setBackground(Color.white);
00649         controlPane.add(linearInput, "North");
00650         add(controlPane, "South");
00651         setVisible(true);
00652         setLinear("");
00653         linearInput.requestFocus();
00654     }
00655 
00656     private void setLinear(String s)
00657     {
00658         markUndo();         
00659        linearInput.setText(s);
00660         updateJomeView();
00661     }
00662 
00663     private void markUndo()
00664     {
00665         if(redoButton != null)
00666         {
00667             undoBuffer[undoBufferCursor] = linearInput.getText();
00668             undoBufferCursor++;
00669             if(undoBufferCursor >= undoBuffer.length)
00670                 undoBufferCursor = 0;
00671             if(undoBufferValidSize < undoBuffer.length)
00672             {
00673                 undoBufferValidSize++;
00674                 undoBufferValidSizeMax = undoBufferValidSize;
00675             }
00676             updateUndoRedoButtons();
00677         } else
00678         {
00679             previousLinear = linearInput.getText();
00680         }
00681     }
00682 
00683     private void redo()
00684     {
00685         if(undoBufferValidSize < undoBufferValidSizeMax)
00686         {
00687             undoBufferCursor++;
00688             undoBufferValidSize++;
00689             if(undoBufferCursor >= undoBuffer.length)
00690                 undoBufferCursor = 0;
00691             linearInput.setText(undoBuffer[undoBufferCursor]);
00692         }
00693         updateUndoRedoButtons();
00694         updateJomeView();
00695     }
00696 
00697     private void undo()
00698     {
00699         if(redoButton != null)
00700         {
00701             if(undoBufferValidSize > 0)
00702             {
00703                 undoBufferValidSize--;
00704                 undoBufferCursor--;
00705                 if(undoBufferCursor < 0)
00706                     undoBufferCursor = undoBuffer.length - 1;
00707                 if(undoBuffer[undoBufferCursor] != null)
00708                     linearInput.setText(undoBuffer[undoBufferCursor]);
00709             }
00710             updateUndoRedoButtons();
00711         } else
00712         if(previousLinear != null)
00713         {
00714             String s = linearInput.getText();
00715             linearInput.setText(previousLinear);
00716             previousLinear = s;
00717         }
00718         updateJomeView();
00719     }
00720 
00721     private void updateUndoRedoButtons()
00722     {
00723         undoButton.setEnabled(undoBufferValidSize > 0);
00724         redoButton.setEnabled(undoBufferValidSize < undoBufferValidSizeMax);
00725     }
00726 
00727     private void updateJomeView()
00728     {
00729        String s=linearInput.getText().toString();
00730        s=filter2(s);
00731        String applettext=readParams("helptext");
00732        if(applettext!=null){
00733            applettext=filter1(applettext,1);
00734            s=applettext+s;
00735            s=s.toString();
00736        }
00737         j.setLinear(s);
00738     }
00739     
00740     public MathView()
00741     {
00742         standaloneWindow = null;
00743         inAnApplet = true;
00744         previousLinear = null;
00745         undoBuffer = new String[50];
00746         undoBufferCursor = 0;
00747         undoBufferValidSize = 0;
00748         undoBufferValidSizeMax = 0;
00749         bigFont = new Font("Helvetica", 0, 32);
00750         defaultFont = new Font("Helvetica", 0, 18);
00751     }
00752 
00753     public void actionPerformed(ActionEvent actionevent)
00754     {
00755         String s = actionevent.getActionCommand();
00756         if(s.startsWith("append "))
00757         {
00758             markUndo();
00759             linearInput.setText(linearInput.getText() + s.substring("append ".length()));
00760         } else
00761 
00762         if(s.startsWith("insert "))
00763         {
00764             markUndo();
00765             String s1 = linearInput.getText();
00766             int i = linearInput.getCaretPosition();
00767             linearInput.setText(s1.substring(0, i) + s.substring("insert ".length()) + s1.substring(i));
00768         } else
00769         if(s.startsWith("enclose "))
00770         {
00771             markUndo();
00772             String s2 = linearInput.getText();
00773             String s3 = s.substring("append ".length(), s.length() - 1);
00774             String s4 = s.substring(s.length() - 1);
00775             int k = linearInput.getSelectionStart();
00776             int l = linearInput.getSelectionEnd();
00777             linearInput.setText(s2.substring(0, k) + s3 + linearInput.getSelectedText() + s4 + s2.substring(l));
00778         } else
00779         if(s.equals("undo"))
00780             undo();
00781         else
00782         if(s.equals("redo"))
00783             redo();
00784         else
00785         if(s.equals("toggle_font"))
00786         {
00787             toggleFont();
00788         } else
00789         {
00790             System.out.println("input: " + s);
00791             updateJomeView();
00792         }
00793     }
00794 
00795     public void itemStateChanged(ItemEvent itemevent)
00796     {
00797 
00798         paletteCardsLayout.show(paletteCards, (String)itemevent.getItem());
00799         linearInput.requestFocus();
00800     }
00801 
00802     public void textValueChanged(TextEvent textevent)
00803     {
00804         updateJomeView();
00805     }
00806 
00807     public void windowOpened(WindowEvent windowevent)
00808     {
00809     }
00810 
00811     public void windowIconified(WindowEvent windowevent)
00812     {
00813     }
00814 
00815     public void windowDeiconified(WindowEvent windowevent)
00816     {
00817     }
00818 
00819     public void windowActivated(WindowEvent windowevent)
00820     {
00821     }
00822 
00823     public void windowDeactivated(WindowEvent windowevent)
00824     {
00825     }
00826 
00827     public void windowResized(WindowEvent windowevent)
00828     {
00829     }
00830 
00831     public void windowClosing(WindowEvent windowevent)
00832     {
00833         stop();
00834        destroy();
00835         if(!inAnApplet)
00836         {
00837             standaloneWindow.setVisible(false);
00838             standaloneWindow.dispose();
00839         }
00840     }
00841 
00842 
00843     public void windowClosed(WindowEvent windowevent)
00844     {
00845         System.exit(0);
00846     }
00847 
00848     public String getLinear(){
00849        String S=j.getLinear();     int lu;
00850        String applettext=readParams("helptext");
00851        if(applettext!=null){
00852            applettext=filter1(applettext , 1);
00853            // cut helptext/applettext from the reply string
00854            int li=applettext.length();
00855            if(li > 0){
00856               lu=S.length();
00857               S=S.substring(li,lu);
00858            }
00859        }
00860        // back to ascii wims/javascript processable syntax
00861        S=filter1(S, 0);
00862        S=root2sqrtn(S);
00863        S=removespecial(S);
00864        return S;
00865     }
00866        
00867     Jome j;
00868     JFrame standaloneWindow;
00869     JPanel controlPane;
00870     JPanel palette;
00871     JPanel paletteCards;
00872     CardLayout paletteCardsLayout;
00873     Choice paletteChoice;
00874     TextArea linearInput;
00875     /*TextField linearInput;*/
00876     JButton undoButton;
00877     JButton redoButton;
00878     boolean inAnApplet;
00879     String previousLinear;
00880     String undoBuffer[];
00881     int undoBufferCursor;
00882     int undoBufferValidSize;
00883     int undoBufferValidSizeMax;
00884     Font bigFont;
00885     Font defaultFont;
00886 }