Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/ui/EditableAtEndDocument.java
41161 views
/*1* Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.ui;2526import javax.swing.text.*;2728/** This class implements a special type of document in which edits29can only be performed at the end, from "mark" to the end of the30document. Thanks to Scott Violet for suggesting to subclass a31Document implementation for this purpose. (Can't do it with32DocumentEvents or UndoableEditEvents; however, in 1.4, there will33be a DocumentFilter which will allow this kind of functionality.) */3435public class EditableAtEndDocument extends PlainDocument {36private int mark;3738public void insertString(int offset, String text, AttributeSet a)39throws BadLocationException {40int len = getLength();41super.insertString(len, text, a);42}4344public void remove(int offs, int len) throws BadLocationException {45int start = offs;46int end = offs + len;4748int markStart = mark;49int markEnd = getLength();5051if ((end < markStart) || (start > markEnd)) {52// no overlap53return;54}5556// Determine interval intersection57int cutStart = Math.max(start, markStart);58int cutEnd = Math.min(end, markEnd);59super.remove(cutStart, cutEnd - cutStart);60}6162public void setMark() {63mark = getLength();64}6566public String getMarkedText() throws BadLocationException {67return getText(mark, getLength() - mark);68}6970/** Used to reset the contents of this document */71public void clear() {72try {73super.remove(0, getLength());74setMark();75}76catch (BadLocationException e) {77}78}79}808182