home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2001 December / dppcpro1201.iso / Extras / maple / Viewer / WebEQ / MMLViewerInstall.cab / MMLViewerApplet.cab / webeq3 / Main.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-05-24  |  9.4 KB  |  402 lines

  1. package webeq3;
  2.  
  3. import java.applet.Applet;
  4. import java.awt.Color;
  5. import java.awt.Component;
  6. import java.awt.Event;
  7. import java.awt.Graphics;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12. import java.util.Vector;
  13. import webeq3.app.Handler;
  14. import webeq3.app.PEquation;
  15. import webeq3.constants.ColorDictionary;
  16. import webeq3.dom.MathMLDOMApplet;
  17. import webeq3.fonts.FontBroker;
  18. import webeq3.parser.Parser;
  19. import webeq3.schema.Box;
  20. import webeq3.schema.ContentBox;
  21. import webeq3.util.ErrorHandler;
  22.  
  23. public class Main extends MathMLDOMApplet {
  24.    // $FF: renamed from: eq webeq3.app.PEquation
  25.    public PEquation field_0 = null;
  26.    ErrorHandler err = new ErrorHandler();
  27.    Handler handler = new Handler();
  28.    String equation = null;
  29.    String src_file = null;
  30.    // $FF: renamed from: in java.lang.String
  31.    String field_1 = null;
  32.    String macros = null;
  33.    String macrofile = null;
  34.    public String use_parser = "webtex";
  35.    URL src_URL = null;
  36.    int pointsize = 18;
  37.    boolean controls = false;
  38.    boolean linebreak = false;
  39.    boolean allow_cut = false;
  40.    String align = "center";
  41.    String valign = "baseline";
  42.    int padding = 1;
  43.    public boolean isBehavior = false;
  44.    public boolean readyToFire = false;
  45.    public boolean sizingDone = false;
  46.    private Vector m_listeners;
  47.  
  48.    public void init() {
  49.       super.init();
  50.       this.m_listeners = new Vector();
  51.       this.isBehavior = "true".equals(((Applet)this).getParameter("FiresScriptEvents"));
  52.       this.handler.setDefaultStatus(Key.getStatusLine());
  53.       this.handler.setParameters(this);
  54.       this.equation = ((Applet)this).getParameter("eq");
  55.       this.macros = ((Applet)this).getParameter("macros");
  56.       this.macrofile = ((Applet)this).getParameter("macrofile");
  57.       this.src_file = ((Applet)this).getParameter("src");
  58.       String var1 = ((Applet)this).getParameter("align");
  59.       if (var1 != null) {
  60.          this.align = var1.toLowerCase();
  61.       }
  62.  
  63.       var1 = ((Applet)this).getParameter("valign");
  64.       if (var1 != null) {
  65.          this.valign = var1.toLowerCase();
  66.       }
  67.  
  68.       var1 = ((Applet)this).getParameter("padding");
  69.       if (var1 != null) {
  70.          this.padding = Integer.parseInt(var1);
  71.       }
  72.  
  73.       var1 = ((Applet)this).getParameter("controls");
  74.       if (var1 != null) {
  75.          this.controls = Boolean.valueOf(var1.toLowerCase());
  76.       }
  77.  
  78.       var1 = ((Applet)this).getParameter("linebreak");
  79.       if (var1 != null) {
  80.          this.linebreak = Boolean.valueOf(var1.toLowerCase());
  81.       }
  82.  
  83.       var1 = ((Applet)this).getParameter("selection");
  84.       if (var1 == null) {
  85.          var1 = ((Applet)this).getParameter("allow_cut");
  86.       }
  87.  
  88.       if (var1 != null) {
  89.          this.allow_cut = Boolean.valueOf(var1.toLowerCase());
  90.       }
  91.  
  92.       var1 = ((Applet)this).getParameter("size");
  93.       if (var1 != null) {
  94.          this.pointsize = Integer.parseInt(var1);
  95.       }
  96.  
  97.       var1 = ((Applet)this).getParameter("background");
  98.       if (var1 == null) {
  99.          var1 = ((Applet)this).getParameter("color");
  100.       }
  101.  
  102.       if (var1 == null) {
  103.          var1 = "white";
  104.       }
  105.  
  106.       Color var2 = this.resolveColor(var1);
  107.       if (var2 != null) {
  108.          ((Component)this).setBackground(var2);
  109.       }
  110.  
  111.       var1 = ((Applet)this).getParameter("foreground");
  112.       if (var1 == null) {
  113.          var1 = "black";
  114.       }
  115.  
  116.       var2 = this.resolveColor(var1);
  117.       if (var2 != null) {
  118.          ((Component)this).setForeground(var2);
  119.       }
  120.  
  121.       if (this.equation != null && !this.equation.equals("")) {
  122.          this.field_1 = this.equation;
  123.       } else if (this.src_file == null) {
  124.          System.out.println("WebEQ Error: A WebEQ Applet call must include either an EQ or an SRC parameter.");
  125.          this.field_1 = "error";
  126.       } else {
  127.          try {
  128.             this.src_URL = new URL(((Applet)this).getDocumentBase(), this.src_file);
  129.          } catch (MalformedURLException var16) {
  130.             System.out.println("Bad URL for equation source: " + var16);
  131.          }
  132.  
  133.          this.field_1 = this.convertToString(this.src_URL);
  134.       }
  135.  
  136.       var1 = ((Applet)this).getParameter("parser");
  137.       if (var1 != null) {
  138.          this.use_parser = var1.toLowerCase();
  139.       } else {
  140.          String var3 = this.field_1 + "                    ";
  141.          StringBuffer var4 = new StringBuffer(var3.substring(0, 20));
  142.          StringBuffer var5 = new StringBuffer();
  143.  
  144.          for(int var6 = 0; var6 < var4.length(); ++var6) {
  145.             char var7 = var4.charAt(var6);
  146.             if (var7 != '\n' && var7 != ' ' && var7 != '\t' && var7 != 160) {
  147.                var5.append(var7);
  148.             }
  149.          }
  150.  
  151.          var3 = var5.toString();
  152.          if (var3.startsWith("<math")) {
  153.             this.use_parser = "mathml";
  154.          } else if (var3.startsWith("<")) {
  155.             int var31 = var3.indexOf(":");
  156.             int var8 = var3.indexOf(">");
  157.             int var9 = var3.indexOf("math");
  158.             if (var31 < var9 && var9 < var8) {
  159.                this.use_parser = "mathml";
  160.             }
  161.          }
  162.       }
  163.  
  164.       if (this.macrofile != null && !this.macrofile.equals("")) {
  165.          URL var28 = null;
  166.  
  167.          try {
  168.             var28 = new URL(((Applet)this).getDocumentBase(), this.macrofile);
  169.          } catch (MalformedURLException var15) {
  170.             System.out.println("Error reading macro data: " + var15);
  171.          }
  172.  
  173.          this.macros = this.macros + this.convertToString(var28);
  174.       }
  175.  
  176.       super.document = new PEquation(this.handler);
  177.       super.dom_children.addNode(super.document);
  178.       this.field_0 = (PEquation)super.document;
  179.       this.field_0.setSize(((Component)this).size().width, ((Component)this).size().height);
  180.       this.field_0.setPadding(this.padding);
  181.       this.field_0.setPointSize(this.pointsize);
  182.       this.field_0.setLinebreak(this.linebreak);
  183.       this.field_0.setControls(this.controls);
  184.       this.field_0.setSelection(this.allow_cut);
  185.       this.field_0.setAlign(this.align);
  186.       this.field_0.setVAlign(this.valign);
  187.       this.field_0.setBackground(((Component)this).getBackground());
  188.       this.field_0.setForeground(((Component)this).getForeground());
  189.       this.field_0.initBG();
  190.       this.field_0.initFG();
  191.       FontBroker.initialize(true);
  192.  
  193.       try {
  194.          Parser var29 = (Parser)Class.forName("webeq3.parser." + this.use_parser + "." + this.use_parser).newInstance();
  195.          var29.init(this.handler);
  196.  
  197.          try {
  198.             if (this.use_parser.equals("webtex")) {
  199.                var29.parse(this.field_1, this.macros, this.field_0.root, this.err);
  200.             } else {
  201.                var29.parse(this.field_1, this.macros, this.field_0.markupRoot, this.err);
  202.             }
  203.          } catch (Exception var11) {
  204.             System.out.println(var11);
  205.             ((Throwable)var11).printStackTrace();
  206.          }
  207.  
  208.          this.field_0.registerActions();
  209.       } catch (InstantiationException var12) {
  210.          System.out.println(var12);
  211.       } catch (IllegalAccessException var13) {
  212.          System.out.println(var13);
  213.       } catch (ClassNotFoundException var14) {
  214.          System.out.println(var14);
  215.          return;
  216.       }
  217.  
  218.       MathMLDOMApplet var30 = (MathMLDOMApplet)this.field_0.getParent();
  219.       this.field_0.applet = this;
  220.       if (this.isBehavior) {
  221.          try {
  222.             this.field_0.root.layout();
  223.             this.readyToFire = true;
  224.             this.fireEvent();
  225.          } catch (Exception var10) {
  226.             System.out.println("layout errors -- rendering failed\n" + var10);
  227.             ((Throwable)var10).printStackTrace();
  228.          }
  229.       } else {
  230.          this.field_0.redraw();
  231.       }
  232.  
  233.    }
  234.  
  235.    public void paint(Graphics var1) {
  236.       this.field_0.resize(this.handler.size().width, this.handler.size().height);
  237.       this.field_0.paint(var1);
  238.    }
  239.  
  240.    public void start() {
  241.       this.field_0.registerControls();
  242.    }
  243.  
  244.    public void stop() {
  245.       this.field_0.unregisterControls();
  246.    }
  247.  
  248.    public void destroy() {
  249.       super.destroy();
  250.    }
  251.  
  252.    public boolean handleEvent(Event var1) {
  253.       return this.field_0 != null ? this.field_0.handleEvent(var1) : false;
  254.    }
  255.  
  256.    public void refresh() {
  257.       this.field_0.redraw();
  258.       ((Component)this).repaint();
  259.    }
  260.  
  261.    public void removeMainListener(MainListener var1) {
  262.       this.m_listeners.removeElement(var1);
  263.    }
  264.  
  265.    public void addMainListener(MainListener var1) {
  266.       if (this.m_listeners == null) {
  267.          this.m_listeners = new Vector();
  268.       }
  269.  
  270.       this.m_listeners.addElement(var1);
  271.       if (this.readyToFire) {
  272.          this.fireEvent();
  273.       }
  274.  
  275.    }
  276.  
  277.    private void fireEvent() {
  278.       Vector var2 = this.m_listeners;
  279.       MainListener[] var1;
  280.       synchronized(var2) {
  281.          var1 = new MainListener[this.m_listeners.size()];
  282.          this.m_listeners.copyInto(var1);
  283.       }
  284.  
  285.       for(int var3 = 0; var3 < var1.length; ++var3) {
  286.          var1[var3].webeqResize(((Applet)this).getParameter("id"));
  287.       }
  288.  
  289.    }
  290.  
  291.    private String convertToString(URL var1) {
  292.       String var2 = "";
  293.       byte[] var4 = new byte[1024];
  294.       InputStream var5 = null;
  295.  
  296.       try {
  297.          var5 = var1.openStream();
  298.       } catch (IOException var7) {
  299.          System.out.println("Error opening " + var1);
  300.       }
  301.  
  302.       while(true) {
  303.          try {
  304.             int var3 = var5.read(var4);
  305.             if (var3 == -1) {
  306.                return var2;
  307.             }
  308.  
  309.             var2 = var2 + new String(var4, 0, 0, var3);
  310.          } catch (IOException var8) {
  311.             System.out.println("Error reading " + var1);
  312.          }
  313.       }
  314.    }
  315.  
  316.    private Color resolveColor(String var1) {
  317.       if (var1 == null) {
  318.          return null;
  319.       } else {
  320.          Color var2 = null;
  321.  
  322.          try {
  323.             if (var1.length() == 7 && var1.startsWith("#")) {
  324.                var2 = new Color(Integer.parseInt(var1.substring(1), 16));
  325.             } else if (var1.length() == 4 && var1.startsWith("#")) {
  326.                String var3 = var1.charAt(1) + "0" + var1.charAt(2) + "0" + var1.charAt(3) + "0";
  327.                var2 = new Color(Integer.parseInt(var3, 16));
  328.             } else if (ColorDictionary.ColorTable.containsKey(var1.toLowerCase())) {
  329.                var2 = new Color(Integer.parseInt((String)ColorDictionary.ColorTable.get(var1.toLowerCase()), 16));
  330.             }
  331.          } catch (Exception var4) {
  332.             System.out.println("Color Format Error: use color=#rrggbb or color=keyword");
  333.          }
  334.  
  335.          return var2;
  336.       }
  337.    }
  338.  
  339.    public int preferredAscent() {
  340.       return this.field_0.root.getAscent();
  341.    }
  342.  
  343.    public int preferredHeight() {
  344.       return this.field_0.root.getHeight();
  345.    }
  346.  
  347.    public int preferredWidth() {
  348.       return this.field_0.root.getWidth();
  349.    }
  350.  
  351.    public void setAllSizes(int var1) {
  352.       this.field_0.controls.setAllSizes(var1);
  353.    }
  354.  
  355.    public void setColor(String var1) {
  356.       Color var2 = this.resolveColor(var1);
  357.       ((Component)this).setBackground(var2);
  358.       this.field_0.initBG(var2);
  359.       this.field_0.redraw();
  360.       ((Component)this).repaint();
  361.    }
  362.  
  363.    public void setEquation(String var1) {
  364.       this.field_0.setRoot(new Box(this.field_0), new ContentBox(this.field_0));
  365.       this.field_0.initBG();
  366.  
  367.       try {
  368.          Parser var2 = (Parser)Class.forName("webeq3.parser." + this.use_parser + "." + this.use_parser).newInstance();
  369.          var2.init(this.handler);
  370.  
  371.          try {
  372.             if (this.use_parser.equals("webtex")) {
  373.                var2.parse(this.field_1, this.macros, this.field_0.root, this.err);
  374.             } else {
  375.                var2.parse(this.field_1, this.macros, this.field_0.markupRoot, this.err);
  376.             }
  377.          } catch (Exception var5) {
  378.             System.out.println(var5);
  379.             ((Throwable)var5).printStackTrace();
  380.          }
  381.  
  382.          this.field_0.registerActions();
  383.       } catch (InstantiationException var6) {
  384.          System.out.println(var6);
  385.       } catch (IllegalAccessException var7) {
  386.          System.out.println(var7);
  387.       } catch (ClassNotFoundException var8) {
  388.          System.out.println(var8);
  389.          return;
  390.       }
  391.  
  392.       this.field_0.redraw();
  393.       ((Component)this).repaint();
  394.    }
  395.  
  396.    public void setSize(int var1) {
  397.       this.field_0.setPointSize(var1);
  398.       this.field_0.redraw();
  399.       ((Component)this).repaint();
  400.    }
  401. }
  402.