001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexey A. Ivanov, Roman I. Chernyatchik
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.util.Vector;
023:
024: import javax.swing.undo.AbstractUndoableEdit;
025:
026: abstract class AbstractContentUndoableEdit extends AbstractUndoableEdit {
027:
028: /*
029: * TODO check if it is to be taken from UIDefaults
030: */
031: private static final String addition = "addition";
032:
033: /*
034: * TODO check if it is to be taken from UIDefaults
035: */
036: private static final String deletion = "deletion";
037:
038: protected final boolean inserted;
039: protected final int len;
040: protected final int pos;
041: protected String text;
042:
043: // Information to undo position changes.
044: private Vector undoPos;
045:
046: public AbstractContentUndoableEdit(final int where,
047: final String chars, final boolean isInsertCommand)
048: throws BadLocationException {
049:
050: pos = where;
051: text = chars;
052: len = text.length();
053: inserted = isInsertCommand;
054: if (!inserted) {
055: undoPos = getPositionsInRange(null, pos, len);
056: } else {
057: undoPos = new Vector();
058: }
059: }
060:
061: public void die() {
062: super .die();
063:
064: text = null;
065: undoPos = null;
066: }
067:
068: public String getPresentationName() {
069: return inserted ? addition : deletion;
070: }
071:
072: public void redo() {
073: super .redo();
074:
075: if (inserted) {
076: insertText();
077: } else {
078: removeText();
079: }
080: }
081:
082: public void undo() {
083: super .undo();
084:
085: if (inserted) {
086: removeText();
087: } else {
088: insertText();
089: }
090: }
091:
092: protected abstract Vector getPositionsInRange(
093: final Vector positions, final int where, final int length);
094:
095: protected abstract void insertItems(final int where,
096: final String chars);
097:
098: protected abstract void removeItems(final int where,
099: final int length);
100:
101: protected abstract void updateUndoPositions(
102: final Vector undoPositions);
103:
104: private void insertText() {
105: insertItems(pos, text);
106: updateUndoPositions(undoPos);
107: undoPos.clear();
108: }
109:
110: private void removeText() {
111: getPositionsInRange(undoPos, pos, len);
112: removeItems(pos, len);
113: }
114: }
|