home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161a.iso / handson / files / copyjava.exe / com / sun / java / swing / undo / UndoManager.class (.txt) < prev   
Encoding:
Java Class File  |  1998-02-26  |  4.2 KB  |  224 lines

  1. package com.sun.java.swing.undo;
  2.  
  3. import com.sun.java.swing.event.UndoableEditEvent;
  4. import com.sun.java.swing.event.UndoableEditListener;
  5. import java.util.Enumeration;
  6. import java.util.Vector;
  7.  
  8. public class UndoManager extends CompoundEdit implements UndoableEditListener {
  9.    int indexOfNextAdd = 0;
  10.    int limit = 100;
  11.  
  12.    public UndoManager() {
  13.       super.edits.ensureCapacity(this.limit);
  14.    }
  15.  
  16.    public synchronized int getLimit() {
  17.       return this.limit;
  18.    }
  19.  
  20.    public synchronized void discardAllEdits() {
  21.       Enumeration var1 = super.edits.elements();
  22.  
  23.       while(var1.hasMoreElements()) {
  24.          UndoableEdit var2 = (UndoableEdit)var1.nextElement();
  25.          var2.die();
  26.       }
  27.  
  28.       super.edits = new Vector(this.limit);
  29.       this.indexOfNextAdd = 0;
  30.    }
  31.  
  32.    protected void trimForLimit() {
  33.       if (this.limit > 0) {
  34.          int var1 = super.edits.size();
  35.          if (var1 > this.limit) {
  36.             int var2 = this.limit / 2;
  37.             int var3 = this.indexOfNextAdd - 1 - var2;
  38.             int var4 = this.indexOfNextAdd - 1 + var2;
  39.             if (var4 - var3 + 1 > this.limit) {
  40.                ++var3;
  41.             }
  42.  
  43.             if (var3 < 0) {
  44.                var4 -= var3;
  45.                var3 = 0;
  46.             }
  47.  
  48.             if (var4 >= var1) {
  49.                int var5 = var1 - var4 - 1;
  50.                var4 += var5;
  51.                var3 += var5;
  52.             }
  53.  
  54.             this.trimEdits(var4 + 1, var1 - 1);
  55.             this.trimEdits(0, var3 - 1);
  56.          }
  57.       }
  58.  
  59.    }
  60.  
  61.    protected void trimEdits(int var1, int var2) {
  62.       if (var1 <= var2) {
  63.          for(int var3 = var2; var1 <= var3; --var3) {
  64.             UndoableEdit var4 = (UndoableEdit)super.edits.elementAt(var3);
  65.             var4.die();
  66.             super.edits.removeElementAt(var3);
  67.          }
  68.  
  69.          if (this.indexOfNextAdd > var2) {
  70.             this.indexOfNextAdd -= var2 - var1 + 1;
  71.             return;
  72.          }
  73.  
  74.          if (this.indexOfNextAdd >= var1) {
  75.             this.indexOfNextAdd = var1;
  76.          }
  77.       }
  78.  
  79.    }
  80.  
  81.    public synchronized void setLimit(int var1) {
  82.       this.limit = var1;
  83.       this.trimForLimit();
  84.    }
  85.  
  86.    protected UndoableEdit editToBeUndone() {
  87.       int var1 = this.indexOfNextAdd;
  88.  
  89.       while(var1 > 0) {
  90.          --var1;
  91.          UndoableEdit var2 = (UndoableEdit)super.edits.elementAt(var1);
  92.          if (var2.isSignificant()) {
  93.             return var2;
  94.          }
  95.       }
  96.  
  97.       return null;
  98.    }
  99.  
  100.    protected UndoableEdit editToBeRedone() {
  101.       int var1 = super.edits.size();
  102.       int var2 = this.indexOfNextAdd;
  103.  
  104.       while(var2 < var1) {
  105.          UndoableEdit var3 = (UndoableEdit)super.edits.elementAt(var2++);
  106.          if (var3.isSignificant()) {
  107.             return var3;
  108.          }
  109.       }
  110.  
  111.       return null;
  112.    }
  113.  
  114.    protected void undoTo(UndoableEdit var1) throws CannotUndoException {
  115.       UndoableEdit var3;
  116.       for(boolean var2 = false; !var2; var2 = var3 == var1) {
  117.          var3 = (UndoableEdit)super.edits.elementAt(--this.indexOfNextAdd);
  118.          var3.undo();
  119.       }
  120.  
  121.    }
  122.  
  123.    protected void redoTo(UndoableEdit var1) throws CannotRedoException {
  124.       UndoableEdit var3;
  125.       for(boolean var2 = false; !var2; var2 = var3 == var1) {
  126.          var3 = (UndoableEdit)super.edits.elementAt(this.indexOfNextAdd++);
  127.          var3.redo();
  128.       }
  129.  
  130.    }
  131.  
  132.    public synchronized void undoOrRedo() throws CannotRedoException, CannotUndoException {
  133.       if (this.indexOfNextAdd == super.edits.size()) {
  134.          this.undo();
  135.       } else {
  136.          this.redo();
  137.       }
  138.    }
  139.  
  140.    public synchronized boolean canUndoOrRedo() {
  141.       return this.indexOfNextAdd == super.edits.size() ? this.canUndo() : this.canRedo();
  142.    }
  143.  
  144.    public synchronized void undo() throws CannotUndoException {
  145.       if (super.inProgress) {
  146.          UndoableEdit var1 = this.editToBeUndone();
  147.          if (var1 == null) {
  148.             throw new CannotUndoException();
  149.          } else {
  150.             this.undoTo(var1);
  151.          }
  152.       } else {
  153.          super.undo();
  154.       }
  155.    }
  156.  
  157.    public synchronized boolean canUndo() {
  158.       if (super.inProgress) {
  159.          UndoableEdit var1 = this.editToBeUndone();
  160.          return var1 != null && var1.canUndo();
  161.       } else {
  162.          return super.canUndo();
  163.       }
  164.    }
  165.  
  166.    public synchronized void redo() throws CannotRedoException {
  167.       if (super.inProgress) {
  168.          UndoableEdit var1 = this.editToBeRedone();
  169.          if (var1 == null) {
  170.             throw new CannotRedoException();
  171.          } else {
  172.             this.redoTo(var1);
  173.          }
  174.       } else {
  175.          super.redo();
  176.       }
  177.    }
  178.  
  179.    public synchronized boolean canRedo() {
  180.       if (super.inProgress) {
  181.          UndoableEdit var1 = this.editToBeRedone();
  182.          return var1 != null && var1.canRedo();
  183.       } else {
  184.          return super.canRedo();
  185.       }
  186.    }
  187.  
  188.    public synchronized boolean addEdit(UndoableEdit var1) {
  189.       this.trimEdits(this.indexOfNextAdd, super.edits.size() - 1);
  190.       super.addEdit(var1);
  191.       this.indexOfNextAdd = super.edits.size();
  192.       this.trimForLimit();
  193.       return true;
  194.    }
  195.  
  196.    public synchronized String getUndoOrRedoPresentationName() {
  197.       return this.indexOfNextAdd == super.edits.size() ? this.getUndoPresentationName() : this.getRedoPresentationName();
  198.    }
  199.  
  200.    public synchronized String getUndoPresentationName() {
  201.       if (super.inProgress) {
  202.          return this.canUndo() ? this.editToBeUndone().getUndoPresentationName() : "Undo";
  203.       } else {
  204.          return super.getUndoPresentationName();
  205.       }
  206.    }
  207.  
  208.    public synchronized String getRedoPresentationName() {
  209.       if (super.inProgress) {
  210.          return this.canRedo() ? ((UndoableEdit)super.edits.elementAt(this.indexOfNextAdd)).getRedoPresentationName() : "Redo";
  211.       } else {
  212.          return super.getRedoPresentationName();
  213.       }
  214.    }
  215.  
  216.    public void undoableEditHappened(UndoableEditEvent var1) {
  217.       this.addEdit(var1.getEdit());
  218.    }
  219.  
  220.    public String toString() {
  221.       return super.toString() + " limit: " + this.limit + " indexOfNextAdd: " + this.indexOfNextAdd;
  222.    }
  223. }
  224.