📄 gapcontent.java
字号:
/* GapContent.java -- Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.text;import java.io.Serializable;import java.util.ArrayList;import java.util.Collections;import java.util.Iterator;import java.util.ListIterator;import java.util.Vector;import javax.swing.undo.AbstractUndoableEdit;import javax.swing.undo.CannotRedoException;import javax.swing.undo.CannotUndoException;import javax.swing.undo.UndoableEdit;/** * This implementation of {@link AbstractDocument.Content} uses a gapped buffer. * This takes advantage of the fact that text area content is mostly inserted * sequentially. The buffer is a char array that maintains a gap at the current * insertion point. If characters a inserted at gap boundaries, the cost is * minimal (simple array access). The array only has to be shifted around when * the insertion point moves (then the gap also moves and one array copy is * necessary) or when the gap is filled up and the buffer has to be enlarged. * * TODO: Implement UndoableEdit support stuff */public class GapContent implements AbstractDocument.Content, Serializable{ /** * A {@link Position} implementation for <code>GapContent</code>. */ class GapContentPosition implements Position, Comparable { /** The index within the buffer array. */ int mark; /** * Creates a new GapContentPosition object. * * @param mark the mark of this Position */ GapContentPosition(int mark) { this.mark = mark; } /** * Comparable interface implementation. This is used to store all * positions in an ordered fashion. * * @param o the object to be compared to this * * @return a negative integer if this is less than <code>o</code>, zero * if both are equal or a positive integer if this is greater than * <code>o</code> * * @throws ClassCastException if <code>o</code> is not a * GapContentPosition or Integer object */ public int compareTo(Object o) { if (o instanceof Integer) { int otherMark = ((Integer) o).intValue(); return mark - otherMark; } else { GapContentPosition other = (GapContentPosition) o; return mark - other.mark; } } /** * Returns the current offset of this Position within the content. * * @return the current offset of this Position within the content. */ public int getOffset() { // Check precondition. assert mark <= gapStart || mark >= gapEnd : "mark: " + mark + ", gapStart: " + gapStart + ", gapEnd: " + gapEnd; if (mark <= gapStart) return mark; else return mark - (gapEnd - gapStart); } } class UndoInsertString extends AbstractUndoableEdit { public int where, length; String text; public UndoInsertString(int start, int len) { where = start; length = len; } public void undo () throws CannotUndoException { super.undo(); try { text = getString(where, length); remove(where, length); } catch (BadLocationException ble) { throw new CannotUndoException(); } } public void redo () throws CannotUndoException { super.redo(); try { insertString(where, text); } catch (BadLocationException ble) { throw new CannotRedoException(); } } } class UndoRemove extends AbstractUndoableEdit { public int where; String text; public UndoRemove(int start, String removedText) { where = start; text = removedText; } public void undo () throws CannotUndoException { super.undo(); try { insertString(where, text); } catch (BadLocationException ble) { throw new CannotUndoException(); } } public void redo () throws CannotUndoException { super.redo(); try { remove(where, text.length()); } catch (BadLocationException ble) { throw new CannotRedoException(); } } } /** The serialization UID (compatible with JDK1.5). */ private static final long serialVersionUID = -6226052713477823730L; /** * This is the default buffer size and the amount of bytes that a buffer is * extended if it is full. */ static final int DEFAULT_BUFSIZE = 10; /** * The text buffer. */ char[] buffer; /** * The index of the first character of the gap. */ int gapStart; /** * The index of the character after the last character of the gap. */ int gapEnd; /** * The positions generated by this GapContent. They are kept in an ordered * fashion, so they can be looked up easily. */ ArrayList positions; /** * Creates a new GapContent object. */ public GapContent() { this(DEFAULT_BUFSIZE); } /** * Creates a new GapContent object with a specified initial size. * * @param size the initial size of the buffer */ public GapContent(int size) { buffer = (char[]) allocateArray(size); gapStart = 1; gapEnd = size; buffer[0] = '\n'; positions = new ArrayList(); } /** * Allocates an array of the specified length that can then be used as * buffer. * * @param size the size of the array to be allocated * * @return the allocated array */ protected Object allocateArray(int size) { return new char[size]; } /** * Returns the length of the allocated buffer array. * * @return the length of the allocated buffer array */ protected int getArrayLength() { return buffer.length; } /** * Returns the length of the content. * * @return the length of the content */ public int length() { return buffer.length - (gapEnd - gapStart); } /** * Inserts a string at the specified position. * * @param where the position where the string is inserted * @param str the string that is to be inserted * * @return an UndoableEdit object * * @throws BadLocationException if <code>where</code> is not a valid * location in the buffer */ public UndoableEdit insertString(int where, String str) throws BadLocationException { // check arguments int length = length(); int strLen = str.length(); if (where >= length) throw new BadLocationException("the where argument cannot be greater" + " than the content length", where); replace(where, 0, str.toCharArray(), strLen); return new UndoInsertString(where, strLen); } /** * Removes a piece of content at th specified position. * * @param where the position where the content is to be removed * @param nitems number of characters to be removed * * @return an UndoableEdit object * * @throws BadLocationException if <code>where</code> is not a valid * location in the buffer */ public UndoableEdit remove(int where, int nitems) throws BadLocationException { // check arguments int length = length(); if (where >= length) throw new BadLocationException("the where argument cannot be greater" + " than the content length", where); if ((where + nitems) > length) throw new BadLocationException("where + nitems cannot be greater" + " than the content length", where + nitems); String removedText = getString(where, nitems); replace(where, nitems, null, 0); return new UndoRemove(where, removedText); } /** * Returns a piece of content as String. * * @param where the start location of the fragment * @param len the length of the fragment * * @throws BadLocationException if <code>where</code> or * <code>where + len</code> are no valid locations in the buffer */ public String getString(int where, int len) throws BadLocationException { Segment seg = new Segment(); try { getChars(where, len, seg); return new String(seg.array, seg.offset, seg.count); } catch (StringIndexOutOfBoundsException ex) { int invalid = 0; if (seg.offset < 0 || seg.offset >= seg.array.length) invalid = seg.offset; else invalid = seg.offset + seg.count; throw new BadLocationException("Illegal location: array.length = " + seg.array.length + ", offset = " + seg.offset + ", count = " + seg.count, invalid); } } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -