KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > BackwardPosition


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.text;
20
21 import org.openide.util.WeakListeners;
22
23 import javax.swing.event.*;
24 import javax.swing.text.*;
25
26
27 /** Position that stays at the same place if someone inserts
28 * directly to its offset.
29 *
30 * @author Jaroslav Tulach
31 */

32 class BackwardPosition extends Object JavaDoc implements Position, DocumentListener {
33     /** positions current offset */
34     private int offset;
35
36     /** Constructor.
37     */

38     private BackwardPosition(int offset) {
39         this.offset = offset;
40     }
41
42     /** @param doc document
43     * @param offset offset
44     * @return new instance of the position
45     */

46     public static Position create(Document doc, int offset) {
47         BackwardPosition p = new BackwardPosition(offset);
48         doc.addDocumentListener(org.openide.util.WeakListeners.document(p, doc));
49
50         return p;
51     }
52
53     //
54
// Position
55
//
56

57     /** @return the offset
58     */

59     public int getOffset() {
60         return offset;
61     }
62
63     //
64
// document listener
65
//
66

67     /** Updates */
68     public void insertUpdate(DocumentEvent e) {
69         // less, not less and equal
70
if (e.getOffset() < offset) {
71             offset += e.getLength();
72         }
73     }
74
75     /** Updates */
76     public void removeUpdate(DocumentEvent e) {
77         int o = e.getOffset();
78
79         if (o < offset) {
80             offset -= e.getLength();
81
82             // was the position in deleted range? => go to its beginning
83
if (offset < o) {
84                 offset = o;
85             }
86         }
87     }
88
89     /** Nothing */
90     public void changedUpdate(DocumentEvent e) {
91     }
92 }
93
Popular Tags