home *** CD-ROM | disk | FTP | other *** search
/ S283 Planetary Science &n…he Search for Life DVD 2 / DVD-ROM.iso / install / jre1_3 / lib / rt.jar / javax / swing / undo / UndoManager.class (.txt) < prev   
Encoding:
Java Class File  |  1979-12-31  |  3.5 KB  |  237 lines

  1. package javax.swing.undo;
  2.  
  3. import java.util.Enumeration;
  4. import java.util.Vector;
  5. import javax.swing.event.UndoableEditEvent;
  6. import javax.swing.event.UndoableEditListener;
  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.          } else if (this.indexOfNextAdd >= var1) {
  72.             this.indexOfNextAdd = var1;
  73.          }
  74.       }
  75.  
  76.    }
  77.  
  78.    public synchronized void setLimit(int var1) {
  79.       if (!super.inProgress) {
  80.          throw new RuntimeException("Attempt to call UndoManager.setLimit() after UndoManager.end() has been called");
  81.       } else {
  82.          this.limit = var1;
  83.          this.trimForLimit();
  84.       }
  85.    }
  86.  
  87.    protected UndoableEdit editToBeUndone() {
  88.       int var1 = this.indexOfNextAdd;
  89.  
  90.       while(var1 > 0) {
  91.          --var1;
  92.          UndoableEdit var2 = (UndoableEdit)super.edits.elementAt(var1);
  93.          if (var2.isSignificant()) {
  94.             return var2;
  95.          }
  96.       }
  97.  
  98.       return null;
  99.    }
  100.  
  101.    protected UndoableEdit editToBeRedone() {
  102.       int var1 = super.edits.size();
  103.       int var2 = this.indexOfNextAdd;
  104.  
  105.       while(var2 < var1) {
  106.          UndoableEdit var3 = (UndoableEdit)super.edits.elementAt(var2++);
  107.          if (var3.isSignificant()) {
  108.             return var3;
  109.          }
  110.       }
  111.  
  112.       return null;
  113.    }
  114.  
  115.    protected void undoTo(UndoableEdit var1) throws CannotUndoException {
  116.       UndoableEdit var3;
  117.       for(boolean var2 = false; !var2; var2 = var3 == var1) {
  118.          var3 = (UndoableEdit)super.edits.elementAt(--this.indexOfNextAdd);
  119.          var3.undo();
  120.       }
  121.  
  122.    }
  123.  
  124.    protected void redoTo(UndoableEdit var1) throws CannotRedoException {
  125.       UndoableEdit var3;
  126.       for(boolean var2 = false; !var2; var2 = var3 == var1) {
  127.          var3 = (UndoableEdit)super.edits.elementAt(this.indexOfNextAdd++);
  128.          var3.redo();
  129.       }
  130.  
  131.    }
  132.  
  133.    public synchronized void undoOrRedo() throws CannotRedoException, CannotUndoException {
  134.       if (this.indexOfNextAdd == super.edits.size()) {
  135.          this.undo();
  136.       } else {
  137.          this.redo();
  138.       }
  139.  
  140.    }
  141.  
  142.    public synchronized boolean canUndoOrRedo() {
  143.       return this.indexOfNextAdd == super.edits.size() ? this.canUndo() : this.canRedo();
  144.    }
  145.  
  146.    public synchronized void undo() throws CannotUndoException {
  147.       if (super.inProgress) {
  148.          UndoableEdit var1 = this.editToBeUndone();
  149.          if (var1 == null) {
  150.             throw new CannotUndoException();
  151.          }
  152.  
  153.          this.undoTo(var1);
  154.       } else {
  155.          super.undo();
  156.       }
  157.  
  158.    }
  159.  
  160.    public synchronized boolean canUndo() {
  161.       if (!super.inProgress) {
  162.          return super.canUndo();
  163.       } else {
  164.          UndoableEdit var1 = this.editToBeUndone();
  165.          return var1 != null && var1.canUndo();
  166.       }
  167.    }
  168.  
  169.    public synchronized void redo() throws CannotRedoException {
  170.       if (super.inProgress) {
  171.          UndoableEdit var1 = this.editToBeRedone();
  172.          if (var1 == null) {
  173.             throw new CannotRedoException();
  174.          }
  175.  
  176.          this.redoTo(var1);
  177.       } else {
  178.          super.redo();
  179.       }
  180.  
  181.    }
  182.  
  183.    public synchronized boolean canRedo() {
  184.       if (!super.inProgress) {
  185.          return super.canRedo();
  186.       } else {
  187.          UndoableEdit var1 = this.editToBeRedone();
  188.          return var1 != null && var1.canRedo();
  189.       }
  190.    }
  191.  
  192.    public synchronized boolean addEdit(UndoableEdit var1) {
  193.       this.trimEdits(this.indexOfNextAdd, super.edits.size() - 1);
  194.       boolean var2 = super.addEdit(var1);
  195.       if (super.inProgress) {
  196.          var2 = true;
  197.       }
  198.  
  199.       this.indexOfNextAdd = super.edits.size();
  200.       this.trimForLimit();
  201.       return var2;
  202.    }
  203.  
  204.    public synchronized void end() {
  205.       super.end();
  206.       this.trimEdits(this.indexOfNextAdd, super.edits.size() - 1);
  207.    }
  208.  
  209.    public synchronized String getUndoOrRedoPresentationName() {
  210.       return this.indexOfNextAdd == super.edits.size() ? this.getUndoPresentationName() : this.getRedoPresentationName();
  211.    }
  212.  
  213.    public synchronized String getUndoPresentationName() {
  214.       if (super.inProgress) {
  215.          return this.canUndo() ? this.editToBeUndone().getUndoPresentationName() : "Undo";
  216.       } else {
  217.          return super.getUndoPresentationName();
  218.       }
  219.    }
  220.  
  221.    public synchronized String getRedoPresentationName() {
  222.       if (super.inProgress) {
  223.          return this.canRedo() ? this.editToBeRedone().getRedoPresentationName() : "Redo";
  224.       } else {
  225.          return super.getRedoPresentationName();
  226.       }
  227.    }
  228.  
  229.    public void undoableEditHappened(UndoableEditEvent var1) {
  230.       this.addEdit(var1.getEdit());
  231.    }
  232.  
  233.    public String toString() {
  234.       return super.toString() + " limit: " + this.limit + " indexOfNextAdd: " + this.indexOfNextAdd;
  235.    }
  236. }
  237.