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

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