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

  1. package webeq3.app;
  2.  
  3. import java.awt.Component;
  4. import java.awt.Event;
  5. import java.awt.Graphics;
  6. import java.util.Hashtable;
  7. import java.util.Vector;
  8. import webeq3.editor.Cursor;
  9. import webeq3.editor.InputHandler;
  10. import webeq3.editor.Template;
  11. import webeq3.schema.Box;
  12. import webeq3.schema.ContentBox;
  13. import webeq3.schema.MRow;
  14. import webeq3.schema.MTd;
  15. import webeq3.schema.MTr;
  16.  
  17. public class EEquation extends Equation {
  18.    private Cursor post_paint_box;
  19.    public static Hashtable minChildren = new Hashtable();
  20.    public static Hashtable maxChildren = new Hashtable();
  21.    public static final int INSERT_MODE = 1;
  22.    public static final int OVERWRITE_MODE = 2;
  23.    public Cursor cursor;
  24.    public boolean tainted = false;
  25.    public int text_mode = 1;
  26.    // $FF: renamed from: ih webeq3.editor.InputHandler
  27.    public InputHandler field_0 = null;
  28.    public Vector templates = new Vector();
  29.    public int current_template = -1;
  30.    public boolean atTop = false;
  31.    Box lastRoot;
  32.    Box lastCursor;
  33.    boolean lastTainted;
  34.    int lastTextMode;
  35.    Vector lastTemplates;
  36.    boolean lastAtTop;
  37.    int lastCurrentTemplate;
  38.    boolean lastCursorAtEnd;
  39.    boolean lastCursorAtBegin;
  40.    int lastInBox;
  41.    boolean lastCursorAddTemplate;
  42.    boolean lastHaveSelection;
  43.    boolean safeToRestore;
  44.  
  45.    public EEquation() {
  46.    }
  47.  
  48.    public EEquation(Handler var1) {
  49.       super.handler = var1;
  50.       ((Equation)this).setRoot(new Box(this), new ContentBox(this));
  51.       this.tainted = false;
  52.       this.atTop = true;
  53.       super.padding = 3;
  54.    }
  55.  
  56.    public EEquation(Handler var1, boolean var2) {
  57.       super.handler = var1;
  58.       ((Equation)this).setRoot(new Box(this), new ContentBox(this));
  59.       this.tainted = false;
  60.       this.atTop = var2;
  61.       super.padding = 3;
  62.    }
  63.  
  64.    public void postPaint(Graphics var1) {
  65.       if (!super.haveSelection && this.post_paint_box != null) {
  66.          this.post_paint_box.paint(var1);
  67.       }
  68.  
  69.    }
  70.  
  71.    public Box findMatching(Box var1, Box var2, Box var3) {
  72.       if (var3 == var1) {
  73.          return var2;
  74.       } else {
  75.          for(int var7 = 0; var7 < var1.getNumChildren(); ++var7) {
  76.             Box var5 = var1.getChild(var7);
  77.  
  78.             Box var6;
  79.             try {
  80.                var6 = var2.getChild(var7);
  81.             } catch (NullPointerException var9) {
  82.                var6 = null;
  83.             }
  84.  
  85.             Box var4 = this.findMatching(var5, var6, var3);
  86.             if (var4 != null) {
  87.                return var4;
  88.             }
  89.          }
  90.  
  91.          return null;
  92.       }
  93.    }
  94.  
  95.    public boolean handleEvent(Event var1) {
  96.       if (super.allow_cut && var1.id == 501 && super.oh != null && (var1.modifiers & 2) != 0) {
  97.          super.root.printSelected("", super.oh);
  98.       } else {
  99.          if (var1.id == 501) {
  100.             ((Component)this).requestFocus();
  101.             super.root.clearSelection();
  102.             if (super.allow_cut) {
  103.                super.cutstartx = var1.x;
  104.                super.cutstarty = var1.y;
  105.             }
  106.  
  107.             int[] var5 = ((Equation)this).normalVirtualCoords(super.cutstartx, super.cutstarty);
  108.             if (this.cursor.move(var5[0], var5[1], super.root)) {
  109.                if (this.field_0 != null) {
  110.                   this.field_0.setState(0);
  111.                }
  112.  
  113.                ((Equation)this).redraw();
  114.                ((Component)this).repaint();
  115.                super.handler.getComponent().repaint();
  116.             }
  117.  
  118.             return true;
  119.          }
  120.  
  121.          if (super.allow_cut && var1.id == 506) {
  122.             super.cutendx = var1.x;
  123.             super.cutendy = var1.y;
  124.             int[] var2 = ((Equation)this).normalVirtualCoords(super.cutstartx, super.cutstarty);
  125.             super.rectleft = var2[0];
  126.             super.recttop = var2[1];
  127.             var2 = ((Equation)this).normalVirtualCoords(super.cutendx, super.cutendy);
  128.             super.rectright = var2[0];
  129.             super.rectbottom = var2[1];
  130.             if (super.rectright < super.rectleft) {
  131.                int var3 = super.rectleft;
  132.                super.rectleft = super.rectright;
  133.                super.rectright = var3;
  134.             }
  135.  
  136.             if (super.rectbottom < super.recttop) {
  137.                int var7 = super.rectbottom;
  138.                super.rectbottom = super.recttop;
  139.                super.recttop = var7;
  140.             }
  141.  
  142.             super.hilight_change = false;
  143.             super.haveSelection = false;
  144.             super.leftmostSelection = null;
  145.             super.rightmostSelection = null;
  146.             ((Equation)this).hilight(super.root);
  147.             if (super.hilight_change) {
  148.                ((Equation)this).redraw();
  149.                ((Component)this).repaint();
  150.                super.handler.getComponent().repaint();
  151.             }
  152.  
  153.             return true;
  154.          }
  155.  
  156.          if (var1.key == 9 && var1.id == 401) {
  157.             this.tab_template_boxes();
  158.             if (this.field_0 != null) {
  159.                this.field_0.setState(0);
  160.             }
  161.  
  162.             return true;
  163.          }
  164.       }
  165.  
  166.       boolean var6 = super.ah.handleAction(var1);
  167.       if (!var6) {
  168.          super.handler.showDefaultStatus();
  169.       }
  170.  
  171.       return false;
  172.    }
  173.  
  174.    public void InsertBox(Box var1) {
  175.       Box var2;
  176.       if (this.atTop) {
  177.          var2 = this.cursor.getBox();
  178.       } else if (this.cursor.getParent() == null) {
  179.          var2 = this.cursor.getBox();
  180.       } else {
  181.          var2 = this.cursor.getParent();
  182.       }
  183.  
  184.       if (var2.getClass().getName().equals("webeq3.schema.MTable") && !var1.getClass().getName().equals("webeq3.schema.MTr")) {
  185.          MTr var3 = new MTr(var2);
  186.          if (!var1.getClass().getName().equals("webeq3.schema.MTd")) {
  187.             MTd var4 = new MTd(var2);
  188.             ((Box)var4).insertChildAt((Box)var1, 0);
  189.             ((Box)var3).insertChildAt(var4, 0);
  190.             var1 = var3;
  191.          } else {
  192.             ((Box)var3).insertChildAt((Box)var1, 0);
  193.             var1 = var3;
  194.          }
  195.       }
  196.  
  197.       if (var2.getClass().getName().equals("webeq3.schema.MTr") && !var1.getClass().getName().equals("webeq3.schema.MTd")) {
  198.          MTd var5 = new MTd(var2);
  199.          ((Box)var5).insertChildAt((Box)var1, 0);
  200.          var1 = var5;
  201.       }
  202.  
  203.       if (this.text_mode == 1) {
  204.          if (!this.atTop && !this.cursor.isAtBegin()) {
  205.             var2.insertChildAt((Box)var1, this.cursor.getBox().box_id + 1);
  206.             this.cursor.toRight();
  207.          } else {
  208.             var2.insertChildAt((Box)var1, this.cursor.getBox().box_id);
  209.             this.cursor.setBox((Box)var1);
  210.             this.cursor.setAtBegin(false);
  211.          }
  212.       } else if (this.text_mode == 2) {
  213.          Object var6 = null;
  214.          int var8 = this.cursor.getBox().box_id;
  215.          Box var7 = var2.removeChildAt(var8);
  216.          var2.insertChildAt((Box)var1, var8);
  217.          this.cursor.setBox((Box)var1);
  218.          if (this.cursor.isAtBegin()) {
  219.             this.cursor.setAtBegin(false);
  220.          }
  221.  
  222.          if (var7 != null && var7.getClass().getName().equals("webeq3.editor.Template")) {
  223.             if (this.templates.indexOf(var7) <= this.current_template) {
  224.                --this.current_template;
  225.             }
  226.  
  227.             this.templates.removeElement(var7);
  228.          }
  229.       }
  230.  
  231.       if (this.atTop) {
  232.          this.atTop = false;
  233.       }
  234.  
  235.       this.tainted = true;
  236.    }
  237.  
  238.    public void splitBox() {
  239.       Box var1 = this.cursor.getBox();
  240.       int var2 = this.cursor.inBox();
  241.       if ((var1.kind == 1 || var1.kind == 2) && var1.getDataLength() > var2 && var2 > 0) {
  242.          Box var3 = var1.getCopy();
  243.          Box var4 = var1.getCopy();
  244.          int var5 = 0;
  245.          String var7 = var1.adata;
  246.          String var8 = var1.udata;
  247.  
  248.          for(int var9 = 0; var9 < var2; ++var9) {
  249.             char var6 = var7.charAt(var5);
  250.             if (var6 == '&') {
  251.                var5 = var7.indexOf(59, var5);
  252.             }
  253.  
  254.             ++var5;
  255.          }
  256.  
  257.          var3.setAData(var7.substring(0, var5));
  258.          var4.setAData(var7.substring(var5));
  259.          var3.setUData(var8.substring(0, var2));
  260.          var4.setUData(var8.substring(var2));
  261.          var3.setData();
  262.          var4.setData();
  263.          Box var10 = var1.getParent();
  264.          int var11 = var1.box_id;
  265.          var10.removeChildAt(var11);
  266.          var10.insertChildAt(var3, var11);
  267.          var10.insertChildAt(var4, var11 + 1);
  268.          this.cursor.setBox(var3);
  269.       }
  270.  
  271.    }
  272.  
  273.    public boolean mergeBoxes() {
  274.       Box var1 = this.cursor.getBox();
  275.       Box var2 = var1.getParent();
  276.       int var3 = var1.getBoxID();
  277.       Box var4 = var2.getChild(var3 + 1);
  278.       Box var5 = var2.getChild(var3 - 1);
  279.       Box var6 = null;
  280.       if (var4 != null && var1.kind == var4.kind && (var1.kind == 1 || var1.kind == 2)) {
  281.          var6 = var1.getCopy();
  282.          var6.addData(var4.adata);
  283.          var6.setData();
  284.          var2.removeChildAt(var3);
  285.          var2.removeChildAt(var3);
  286.          var2.insertChildAt(var6, var3);
  287.          int var7 = this.cursor.inBox();
  288.          this.cursor.setBox(var6);
  289.          this.cursor.setInBox(var7);
  290.          var1 = var6;
  291.       }
  292.  
  293.       if (var5 != null && var1.kind == var5.kind && (var1.kind == 1 || var1.kind == 2)) {
  294.          var6 = var5.getCopy();
  295.          var6.addData(var1.adata);
  296.          var6.setData();
  297.          var2.removeChildAt(var3 - 1);
  298.          var2.removeChildAt(var3 - 1);
  299.          var2.insertChildAt(var6, var3 - 1);
  300.          int var8 = this.cursor.inBox();
  301.          this.cursor.setBox(var6);
  302.          this.cursor.setInBox(var8 + var5.getDataLength());
  303.       }
  304.  
  305.       if (var6 == null) {
  306.          return false;
  307.       } else {
  308.          var6.layout();
  309.          return true;
  310.       }
  311.    }
  312.  
  313.    public void gotoCurrentTemplate() {
  314.       if (this.templates.size() != 0) {
  315.          if (this.current_template < 0) {
  316.             this.current_template = 0;
  317.          }
  318.  
  319.          if (this.current_template >= this.templates.size()) {
  320.             this.current_template = this.templates.size() - 1;
  321.          }
  322.  
  323.          this.cursor.setBox((Box)this.templates.elementAt(this.current_template));
  324.          this.cursor.tunnel();
  325.          ((Equation)this).redraw();
  326.          ((Component)this).repaint();
  327.       }
  328.    }
  329.  
  330.    private void tab_template_boxes() {
  331.       if (this.templates.size() != 0) {
  332.          if (this.current_template >= this.templates.size() - 1) {
  333.             this.current_template = -1;
  334.          }
  335.  
  336.          ++this.current_template;
  337.          this.cursor.setBox((Box)this.templates.elementAt(this.current_template));
  338.          this.cursor.tunnel();
  339.          ((Equation)this).redraw();
  340.          ((Component)this).repaint();
  341.       }
  342.    }
  343.  
  344.    public void recursiveRmTemplate(Box var1) {
  345.       for(int var2 = 0; var2 < var1.getNumChildren(); ++var2) {
  346.          Box var3 = var1.getChild(var2);
  347.          if (var3 instanceof Template) {
  348.             this.templates.removeElement(var3);
  349.          }
  350.  
  351.          this.recursiveRmTemplate(var3);
  352.       }
  353.  
  354.    }
  355.  
  356.    public void setCurrentTemplate(Template var1) {
  357.       this.current_template = this.templates.indexOf(var1);
  358.    }
  359.  
  360.    public void setCurrentTemplate(int var1) {
  361.       this.current_template = var1;
  362.    }
  363.  
  364.    public void removeTemplate(Template var1) {
  365.       this.setCurrentTemplate(var1);
  366.       this.templates.removeElement(var1);
  367.    }
  368.  
  369.    public Template insertNecessaryTemplates(Box var1) {
  370.       Template var2 = null;
  371.       Box var3 = var1.getParent();
  372.       String var4 = var3.getClass().getName();
  373.       int var5 = (Integer)minChildren.get(var4);
  374.       int var6 = var5 - var3.getNumChildren();
  375.       if (var6 > 0) {
  376.          if (!"webeq3.schema.Box".equals(var4) && !"webeq3.schema.MFrac".equals(var4) && !"webeq3.schema.MSqrt".equals(var4) && !"webeq3.schema.MRoot".equals(var4) && !"webeq3.schema.MSub".equals(var4) && !"webeq3.schema.MSup".equals(var4) && !"webeq3.schema.MSubsup".equals(var4) && !"webeq3.schema.MUnder".equals(var4) && !"webeq3.schema.MOver".equals(var4) && !"webeq3.schema.MUnderover".equals(var4)) {
  377.             var2 = new Template(var3);
  378.             var3.insertChildAt(var2, var1.box_id);
  379.             this.addTemplate(var2);
  380.             this.setCurrentTemplate(var2);
  381.          } else {
  382.             for(int var7 = 0; var7 < var6; ++var7) {
  383.                MRow var8 = new MRow(var3);
  384.                var3.insertChildAt(var8, var1.box_id + var7);
  385.                var2 = new Template(var8);
  386.                ((Box)var8).addChild(var2);
  387.                this.addTemplate(var2);
  388.                this.setCurrentTemplate(var2);
  389.             }
  390.          }
  391.       }
  392.  
  393.       return var2;
  394.    }
  395.  
  396.    public int getNumTemplates() {
  397.       return this.templates.size();
  398.    }
  399.  
  400.    public void addTemplate(Template var1) {
  401.       this.templates.addElement(var1);
  402.       this.setCurrentTemplate(var1);
  403.    }
  404.  
  405.    public boolean isTainted() {
  406.       return this.tainted;
  407.    }
  408.  
  409.    public void setAllowCut(boolean var1) {
  410.       super.allow_cut = var1;
  411.    }
  412.  
  413.    public void setCursor(Cursor var1) {
  414.       this.cursor = var1;
  415.       this.post_paint_box = var1;
  416.    }
  417.  
  418.    public void setInputHandler(InputHandler var1) {
  419.       this.field_0 = var1;
  420.    }
  421.  
  422.    public void setTainted(boolean var1) {
  423.       this.tainted = var1;
  424.    }
  425.  
  426.    public void setTextMode(int var1) {
  427.       this.text_mode = var1;
  428.    }
  429.  
  430.    public Cursor getECursor() {
  431.       return this.cursor;
  432.    }
  433.  
  434.    public int getTextMode() {
  435.       return this.text_mode;
  436.    }
  437.  
  438.    public void storeUndoInfo() {
  439.       this.lastRoot = super.root.getCopy();
  440.       this.lastCursor = this.findMatching(super.root, this.lastRoot, this.cursor.getBox());
  441.       this.lastTainted = this.tainted;
  442.       this.lastTextMode = this.text_mode;
  443.       this.lastAtTop = this.atTop;
  444.       this.lastHaveSelection = super.haveSelection;
  445.       this.lastCursorAtEnd = this.cursor.isAtEnd();
  446.       this.lastCursorAtBegin = this.cursor.isAtBegin();
  447.       this.lastInBox = this.cursor.inBox();
  448.       this.lastCursorAddTemplate = this.cursor.isAddTemplate();
  449.       this.lastTemplates = new Vector();
  450.  
  451.       for(int var1 = 0; var1 < this.templates.size(); ++var1) {
  452.          Box var2 = this.findMatching(super.root, this.lastRoot, (Box)this.templates.elementAt(var1));
  453.          if (var2 != null) {
  454.             this.lastTemplates.addElement(var2);
  455.          }
  456.       }
  457.  
  458.       this.lastCurrentTemplate = this.current_template;
  459.       this.field_0.saveState(super.root, this.lastRoot);
  460.       this.safeToRestore = true;
  461.    }
  462.  
  463.    public void undo() {
  464.       if (this.safeToRestore) {
  465.          super.root = this.lastRoot;
  466.          this.cursor = new Cursor(this.lastCursor);
  467.          this.cursor.setEEquation(this);
  468.          this.setCursor(this.cursor);
  469.          this.cursor.setAtEnd(this.lastCursorAtEnd);
  470.          this.cursor.setAtBegin(this.lastCursorAtBegin);
  471.          this.cursor.setInBox(this.lastInBox);
  472.          this.cursor.setAddTemplate(this.lastCursorAddTemplate);
  473.          this.tainted = this.lastTainted;
  474.          super.haveSelection = this.lastHaveSelection;
  475.          this.text_mode = this.lastTextMode;
  476.          this.templates = this.lastTemplates;
  477.          this.current_template = this.lastCurrentTemplate;
  478.          this.atTop = this.lastAtTop;
  479.          this.field_0.restoreState();
  480.       }
  481.    }
  482.  
  483.    static {
  484.       minChildren.put("webeq3.schema.MI", new Integer(1));
  485.       minChildren.put("webeq3.schema.MN", new Integer(1));
  486.       minChildren.put("webeq3.schema.MO", new Integer(1));
  487.       minChildren.put("webeq3.schema.MText", new Integer(1));
  488.       minChildren.put("webeq3.schema.MS", new Integer(1));
  489.       minChildren.put("webeq3.schema.MFrac", new Integer(2));
  490.       minChildren.put("webeq3.schema.MSqrt", new Integer(1));
  491.       minChildren.put("webeq3.schema.MRoot", new Integer(2));
  492.       minChildren.put("webeq3.schema.MRow", new Integer(1));
  493.       minChildren.put("webeq3.schema.MStyle", new Integer(1));
  494.       minChildren.put("webeq3.schema.MError", new Integer(1));
  495.       minChildren.put("webeq3.schema.MPhantom", new Integer(1));
  496.       minChildren.put("webeq3.schema.MFenced", new Integer(1));
  497.       minChildren.put("webeq3.schema.MSub", new Integer(3));
  498.       minChildren.put("webeq3.schema.MSup", new Integer(3));
  499.       minChildren.put("webeq3.schema.MSubsup", new Integer(3));
  500.       minChildren.put("webeq3.schema.MUnder", new Integer(3));
  501.       minChildren.put("webeq3.schema.MOver", new Integer(3));
  502.       minChildren.put("webeq3.schema.MUnderover", new Integer(3));
  503.       minChildren.put("webeq3.schema.MMultiscripts", new Integer(1));
  504.       minChildren.put("webeq3.schema.Box", new Integer(1));
  505.       minChildren.put("webeq3.schema.MTable", new Integer(0));
  506.       minChildren.put("webeq3.schema.MTr", new Integer(1));
  507.       minChildren.put("webeq3.schema.MTd", new Integer(1));
  508.       minChildren.put("webeq3.schema.MAction", new Integer(1));
  509.       maxChildren.put("webeq3.schema.MI", new Integer(1));
  510.       maxChildren.put("webeq3.schema.MN", new Integer(1));
  511.       maxChildren.put("webeq3.schema.MO", new Integer(1));
  512.       maxChildren.put("webeq3.schema.MText", new Integer(1));
  513.       maxChildren.put("webeq3.schema.MS", new Integer(1));
  514.       maxChildren.put("webeq3.schema.MFrac", new Integer(2));
  515.       maxChildren.put("webeq3.schema.MSqrt", new Integer(1));
  516.       maxChildren.put("webeq3.schema.MRoot", new Integer(2));
  517.       maxChildren.put("webeq3.schema.MRow", new Integer(256));
  518.       maxChildren.put("webeq3.schema.MStyle", new Integer(256));
  519.       maxChildren.put("webeq3.schema.MError", new Integer(256));
  520.       maxChildren.put("webeq3.schema.MPhantom", new Integer(256));
  521.       maxChildren.put("webeq3.schema.MFenced", new Integer(256));
  522.       maxChildren.put("webeq3.schema.MSub", new Integer(2));
  523.       maxChildren.put("webeq3.schema.MSup", new Integer(2));
  524.       maxChildren.put("webeq3.schema.MSubsup", new Integer(3));
  525.       maxChildren.put("webeq3.schema.MUnder", new Integer(2));
  526.       maxChildren.put("webeq3.schema.MOver", new Integer(2));
  527.       maxChildren.put("webeq3.schema.MUnderover", new Integer(3));
  528.       maxChildren.put("webeq3.schema.MMultiscripts", new Integer(256));
  529.       maxChildren.put("webeq3.schema.Box", new Integer(256));
  530.       maxChildren.put("webeq3.schema.MTable", new Integer(256));
  531.       maxChildren.put("webeq3.schema.MTr", new Integer(256));
  532.       maxChildren.put("webeq3.schema.MTd", new Integer(256));
  533.       maxChildren.put("webeq3.schema.MAction", new Integer(256));
  534.    }
  535. }
  536.