undoabletreeedit.java

来自「《深入浅出设计模式》的完整源代码」· Java 代码 · 共 91 行

JAVA
91
字号
package taxonomy;import javax.swing.event.TreeModelEvent;import javax.swing.undo.*;/**  A class whose instances represent undoable edits to a tree  @see TaxTreeModel  @see Node  @see <u>Java Swing</u> pp 647*/public class UndoableTreeEdit extends AbstractUndoableEdit {  /**    The tree model on which this edit was made  */  private TaxTreeModel tm;  /**    The tree model event generated by this edit  */  private TreeModelEvent tme;  /**    The parent element in the tree affected by this edit  */  private Object parent;  /**    The new child element added to the tree by this edit  */  private Object child;  /**    Construct a new undoable tree edit.    @param aTM The tree model on which this edit was made    @param aTME The tree model event generated by this edit    @param pVal The parent element in the tree affected by this edit    @param cVal The new child element added to the tree by this edit  */  public UndoableTreeEdit(TaxTreeModel aTM,                          TreeModelEvent aTME,                          Object pVal, Object cVal) {    tm = aTM;    tme = aTME;    parent = pVal;    child = cVal;  }  /**    Return a reasonable/descriptive name for this edit.    @return A string representation for this edit  */  public String getPresentationName() {    return ("added " + child.toString() + " to " + parent.toString());  }  /**    Redo this edit.  */  public void redo() throws CannotRedoException {    // call redo in the super class    // to ensure this edit is in the appropriate state    super.redo();    // reinsert the element into the tree    // build a new tree model event capturing the insertion    // notify any listeners in the table model    ((Node)tm.find(parent)).addChild(child);    TreeModelEvent e = tm.buildTreeModelEvent(tm.find(parent));    tme = e;    tm.notifyTreeModelListeners(e);  }  /**    Undo this edit.  */  public void undo() throws CannotUndoException {    // call undo in the super class    // to ensure this edit is in the appropriate state    super.undo();    // remove the (child) element from the tree    // notify any listeners in the table model    tm.find(parent).removeChild(tm.find(child));    //TreeModelEvent e = new TreeModelEvent((tm.find(parent));    //tme = e;    tm.notifyTreeModelListeners(tme);  }}

⌨️ 快捷键说明

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