Home » openjdk-7 » javax » swing » text » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   /**
   18    * @author Alexey A. Ivanov, Roman I. Chernyatchik
   19    */
   20   package javax.swing.text;
   21   
   22   import java.util.Vector;
   23   
   24   import javax.swing.undo.AbstractUndoableEdit;
   25   
   26   abstract class AbstractContentUndoableEdit extends AbstractUndoableEdit {
   27   
   28       /*
   29        * TODO check if it is to be taken from UIDefaults
   30        */
   31       private static final String addition = "addition";
   32   
   33       /*
   34        * TODO check if it is to be taken from UIDefaults
   35        */
   36       private static final String deletion = "deletion";
   37   
   38       protected final boolean    inserted;
   39       protected final int        len;
   40       protected final int        pos;
   41       protected String     text;
   42   
   43       // Information to undo position changes.
   44       private Vector     undoPos;
   45   
   46       public AbstractContentUndoableEdit(final int where, final String chars,
   47               final boolean isInsertCommand)
   48       throws BadLocationException {
   49   
   50           pos      = where;
   51           text     = chars;
   52           len      = text.length();
   53           inserted = isInsertCommand;
   54           if (!inserted) {
   55               undoPos = getPositionsInRange(null, pos, len);
   56           } else {
   57               undoPos = new Vector();
   58           }
   59       }
   60   
   61       public void die() {
   62           super.die();
   63   
   64           text = null;
   65           undoPos = null;
   66       }
   67   
   68       public String getPresentationName() {
   69           return inserted ? addition : deletion;
   70       }
   71   
   72       public void redo() {
   73           super.redo();
   74   
   75           if (inserted) {
   76               insertText();
   77           } else {
   78               removeText();
   79           }
   80       }
   81   
   82       public void undo() {
   83           super.undo();
   84   
   85           if (inserted) {
   86               removeText();
   87           } else {
   88               insertText();
   89           }
   90       }
   91   
   92       protected abstract Vector getPositionsInRange(final Vector positions,
   93                                                     final int where,
   94                                                     final int length);
   95   
   96       protected abstract void insertItems(final int where, final String chars);
   97   
   98       protected abstract void removeItems(final int where, final int length);
   99   
  100       protected abstract void updateUndoPositions(final Vector undoPositions);
  101   
  102       private void insertText() {
  103           insertItems(pos, text);
  104           updateUndoPositions(undoPos);
  105           undoPos.clear();
  106       }
  107   
  108       private void removeText() {
  109           getPositionsInRange(undoPos, pos, len);
  110           removeItems(pos, len);
  111       }
  112   }

Home » openjdk-7 » javax » swing » text » [javadoc | source]