1/* 2 * Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25package sun.jvm.hotspot.ui; 26 27import javax.swing.text.*; 28 29/** This class implements a special type of document in which edits 30 can only be performed at the end, from "mark" to the end of the 31 document. Thanks to Scott Violet for suggesting to subclass a 32 Document implementation for this purpose. (Can't do it with 33 DocumentEvents or UndoableEditEvents; however, in 1.4, there will 34 be a DocumentFilter which will allow this kind of functionality.) */ 35 36public class EditableAtEndDocument extends PlainDocument { 37 private int mark; 38 39 public void insertString(int offset, String text, AttributeSet a) 40 throws BadLocationException { 41 int len = getLength(); 42 super.insertString(len, text, a); 43 } 44 45 public void remove(int offs, int len) throws BadLocationException { 46 int start = offs; 47 int end = offs + len; 48 49 int markStart = mark; 50 int markEnd = getLength(); 51 52 if ((end < markStart) || (start > markEnd)) { 53 // no overlap 54 return; 55 } 56 57 // Determine interval intersection 58 int cutStart = Math.max(start, markStart); 59 int cutEnd = Math.min(end, markEnd); 60 super.remove(cutStart, cutEnd - cutStart); 61 } 62 63 public void setMark() { 64 mark = getLength(); 65 } 66 67 public String getMarkedText() throws BadLocationException { 68 return getText(mark, getLength() - mark); 69 } 70 71 /** Used to reset the contents of this document */ 72 public void clear() { 73 try { 74 super.remove(0, getLength()); 75 setMark(); 76 } 77 catch (BadLocationException e) { 78 } 79 } 80} 81