⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utilundoer.java

📁 JTREE的例子
💻 JAVA
字号:
package util2;import javax.swing.undo.*;import java.util.*;import util3.*;import util4.*;public class UtilUndoer extends UndoManager implements java.io.Serializable{  int index = -1;  int limit = 10000;  int capacity = 0;  Vector vector;  boolean gotToSave;  boolean sign_set;  UndoRedoListener listener;  UndoControlListener listener1;  //a variable edits inherintanted  public UtilUndoer(){    super();    this.setLimit(10000);    this.vector=new Vector(1000,200);  }  public void addUndoRedoListener(UndoRedoListener l){    this.listener=l;  }  public void addUndoControlListener(UndoControlListener l){    this.listener1=l;  }  public int getCapacity(){return this.capacity;}/*  protected UndoableEdit editToBeUndone() {    if(!this.canUndo()){return null;}    return      (UndoableEdit)edits.elementAt(((Integer)vector.elementAt(index--)).intValue());  }  protected UndoableEdit editToBeRedone() {    if(!this.canRedo()) return null;    return      (UndoableEdit)edits.elementAt(((Integer)vector.elementAt(index++)).intValue());  }*/  public boolean addEdit(UndoableEdit anEdit) {    //int size=vector.size();    //if((this.index+1)<size){this.trim(size+1-index);}    if(super.addEdit(anEdit)){      this.capacity=edits.size();      this.gotToSave=true;      if(this.sign_set){sign_set=false; this.listener.update();}      return true;    }    return false;  }  private void trim(int from,int to){//System.out.println("trim:"+from+" "+to);    boolean trimed=false;    for(int i=from;i>=to;i--){      this.vector.removeElementAt(i);      trimed=true;    }//System.out.println(vector.size());    if(trimed)index--;  }  public void setSignificant(int dot){//System.out.println(vector.size()+" , "+index);    int size=vector.size();    if((this.index+1)<size){this.trim(size-1,index);}    //System.out.println("setSignificant():"+this.capacity+" "+dot);    //System.out.println("setSignificant():  "+this.index);    this.vector.addElement(new UtilSaves(this.capacity,dot));    this.index++;    this.gotToSave=false;    this.sign_set=true;    //this.listener.update();  }  public void undo() throws CannotUndoException{    if(!this.canUndo()) return;    if(this.gotToSave){this.setSignificant(this.listener1.getCaretDot()); }    //System.out.print(" "+(this.index-1)+" ");    UtilSaves us=(UtilSaves)(vector.elementAt(--index));    int ind=us.getSignifican();    //System.out.print("c:"+capacity+"  "+"i:"+ind+"  ");    for(int i=this.capacity;i>ind;i--){      super.undo();    }    this.capacity=ind;    this.listener1.setCaretDot(us.getDot());    this.listener1.setModified(true);  }  public void redo() throws CannotUndoException{    if(!this.canRedo()) return;    //int ind=((Integer) this.vector.elementAt(++index)).intValue();    UtilSaves us=(UtilSaves)(vector.elementAt(++index));    int ind=us.getSignifican();    for(int i=this.capacity;i<ind;i++){      super.redo();    }    this.capacity=ind;    this.listener1.setCaretDot(us.getDot());    this.listener1.setModified(true);  }  public boolean canRedo() {    if(super.canRedo() && this.index<this.vector.size()){return true;}    return false;  }  public boolean canUndo(){//System.out.print(super.canUndo()+" index : "+index);    if(super.canUndo() && this.index>=0) return true;    return false;  }  public void discardAllEdits() {    super.discardAllEdits();    this.vector=new Vector(1000,200);    this.capacity=0;    this.index=-1;    this.listener.update();  }  class UtilSaves  { int significan,dot;    UtilSaves(int significan,int dot){      this.significan=significan;      this.dot=dot;    }    public int getSignifican(){return this.significan;}    public int getDot(){return this.dot;}  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -