001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.text.edits;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.jface.text.BadLocationException;
016: import org.eclipse.jface.text.IDocument;
017:
018: /**
019: * This class encapsulates the reverse changes of an executed text
020: * edit tree. To apply an undo memento to a document use method
021: * <code>apply(IDocument)</code>.
022: * <p>
023: * Clients can't add additional children to an undo edit nor can they
024: * add an undo edit as a child to another edit. Doing so results in
025: * both cases in a <code>MalformedTreeException<code>.
026: *
027: * @since 3.0
028: */
029: public final class UndoEdit extends TextEdit {
030:
031: UndoEdit() {
032: super (0, Integer.MAX_VALUE);
033: }
034:
035: private UndoEdit(UndoEdit other) {
036: super (other);
037: }
038:
039: /*
040: * @see org.eclipse.text.edits.TextEdit#internalAdd(org.eclipse.text.edits.TextEdit)
041: */
042: void internalAdd(TextEdit child) throws MalformedTreeException {
043: throw new MalformedTreeException(null, this , TextEditMessages
044: .getString("UndoEdit.no_children")); //$NON-NLS-1$
045: }
046:
047: /*
048: * @see org.eclipse.text.edits.MultiTextEdit#aboutToBeAdded(org.eclipse.text.edits.TextEdit)
049: */
050: void aboutToBeAdded(TextEdit parent) {
051: throw new MalformedTreeException(parent, this , TextEditMessages
052: .getString("UndoEdit.can_not_be_added")); //$NON-NLS-1$
053: }
054:
055: UndoEdit dispatchPerformEdits(TextEditProcessor processor)
056: throws BadLocationException {
057: return processor.executeUndo();
058: }
059:
060: void dispatchCheckIntegrity(TextEditProcessor processor)
061: throws MalformedTreeException {
062: processor.checkIntegrityUndo();
063: }
064:
065: /*
066: * @see org.eclipse.text.edits.TextEdit#doCopy()
067: */
068: protected TextEdit doCopy() {
069: return new UndoEdit(this );
070: }
071:
072: /*
073: * @see TextEdit#accept0
074: */
075: protected void accept0(TextEditVisitor visitor) {
076: boolean visitChildren = visitor.visit(this );
077: if (visitChildren) {
078: acceptChildren(visitor);
079: }
080: }
081:
082: /*
083: * @see TextEdit#performDocumentUpdating
084: */
085: int performDocumentUpdating(IDocument document)
086: throws BadLocationException {
087: fDelta = 0;
088: return fDelta;
089: }
090:
091: void add(ReplaceEdit edit) {
092: List children = internalGetChildren();
093: if (children == null) {
094: children = new ArrayList(2);
095: internalSetChildren(children);
096: }
097: children.add(edit);
098: }
099:
100: void defineRegion(int offset, int length) {
101: internalSetOffset(offset);
102: internalSetLength(length);
103: }
104:
105: boolean deleteChildren() {
106: return false;
107: }
108: }
|