insertattredit.java

来自「java xml 应用开发」· Java 代码 · 共 85 行

JAVA
85
字号
import javax.swing.tree.TreePath;
import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;

import org.cip4.jdflib.core.JDFElement;
import org.cip4.jdflib.util.StringUtil;
import org.w3c.dom.Attr;

/*
 * InsertAttrEdit.java
 * @author Elena Skobchenko
 */

public class InsertAttrEdit extends AbstractUndoableEdit 
{
    private static final long serialVersionUID = -2778264565816334345L;
    
    private TreePath path;
    private TreePath attrPath;
    private Attr attrib;
    private JDFTreeNode intoNode;
    private JDFElement intoElement;
    private JDFTreeNode attrNode;
    private String attributeName;
    private JDFFrame parFrame;
    private int pos;
    private boolean success = true;
    
    public InsertAttrEdit(final JDFFrame parentFrame, final TreePath treePath, final JDFTreeNode newAttrNode) 
    {
         parFrame = parentFrame;
         path = treePath;
         attrNode = newAttrNode;
         
         try
         {
             intoNode = (JDFTreeNode) path.getLastPathComponent();
             intoElement = (JDFElement) ((Wrapper) intoNode.getUserObject()).getElement();
         }
         catch (Exception s)
         {
             intoNode = (JDFTreeNode) path.getParentPath().getLastPathComponent();
             intoElement = (JDFElement) ((Wrapper) intoNode.getUserObject()).getElement();
         }

         pos = intoNode.getIndex(attrNode);
         attributeName = StringUtil.token(attrNode.toString(), 0, "=");
         attrib = intoElement.getAttributeNode(attributeName);
         attrPath = new TreePath(attrNode.getPath());

         parFrame.updateViews(attrPath);
    }

    public void undo() throws CannotUndoException 
    { 
         success = parFrame.deleteItem(attrPath);
         attrPath = null;
         parFrame.updateViews(path);
    }

    public void redo() throws CannotRedoException 
    {
        parFrame.insertInto(intoNode, attrNode, pos);
        intoElement.setAttributeNode(attrib);
        attrPath = new TreePath(attrNode.getPath());
        parFrame.updateViews(attrPath);
    }

    public boolean canUndo() 
    {
         return success;
    }

    public boolean canRedo() 
    {
        return success;
    }

    public String getPresentationName() 
    {
         return "InsertAttribute";
    }

}

⌨️ 快捷键说明

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