KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.*;
24
25
26 /** Listener to changes in the document.
27 *
28 * @author Jaroslav Tulach
29 */

30 final class LineListener extends Object JavaDoc implements javax.swing.event.DocumentListener JavaDoc {
31     /** original count of lines */
32     private int orig;
33
34     /** document to work with */
35     public final StyledDocument doc;
36
37     /** root element of all lines */
38     private Element root;
39
40     /** last tested amount of lines */
41     private int lines;
42
43     /** operations on lines */
44     private LineStruct struct;
45
46     /** Support necessary for getting Set of lines*/
47     CloneableEditorSupport support;
48
49     /** Creates new LineListener */
50     public LineListener(StyledDocument doc, CloneableEditorSupport support) {
51         this.doc = doc;
52         this.struct = new LineStruct();
53         root = NbDocument.findLineRootElement(doc);
54         orig = lines = root.getElementCount();
55         this.support = support;
56
57         doc.addDocumentListener(org.openide.util.WeakListeners.document(this, doc));
58     }
59
60     /** Getter for amount of lines */
61     public int getOriginalLineCount() {
62         return orig;
63     }
64
65     /** Convertor between old and new line sets */
66     public int getLine(int i) {
67         return struct.convert(i, true /*originalToCurrent*/);
68     }
69
70     /** Convertor between old and new line sets */
71     public int getOld(int i) {
72         return struct.convert(i, false /*currentToOriginal*/);
73     }
74
75     public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc p0) {
76         int elem = root.getElementCount();
77         int delta = lines - elem;
78         lines = elem;
79
80         int lineNumber = NbDocument.findLineNumber(doc, p0.getOffset());
81
82         if (delta > 0) {
83             struct.deleteLines(lineNumber, delta);
84         }
85
86         if (support == null) {
87             return;
88         }
89
90         Line.Set set = support.getLineSet();
91
92         if (!(set instanceof DocumentLine.Set)) {
93             return;
94         }
95
96         // Notify lineSet there was changed range of lines.
97
((DocumentLine.Set) set).linesChanged(lineNumber, lineNumber + delta, p0);
98
99         if (delta > 0) {
100             // Notify Line.Set there was moved range of lines.
101
((DocumentLine.Set) set).linesMoved(lineNumber, elem);
102         }
103     }
104
105     public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc p0) {
106     }
107
108     public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc p0) {
109         int elem = root.getElementCount();
110
111         int delta = elem - lines;
112         lines = elem;
113
114         int lineNumber = NbDocument.findLineNumber(doc, p0.getOffset());
115
116         if (delta > 0) {
117             struct.insertLines(lineNumber, delta);
118         }
119
120         if (support == null) {
121             return;
122         }
123
124         Line.Set set = support.getLineSet();
125
126         if (!(set instanceof DocumentLine.Set)) {
127             return;
128         }
129
130         // Nptify Line.Set there was changed range of lines.
131
((DocumentLine.Set) set).linesChanged(lineNumber, lineNumber, p0);
132
133         if (delta > 0) {
134             // Notify Line.Set there was moved range of lines.
135
((DocumentLine.Set) set).linesMoved(lineNumber, elem);
136         }
137     }
138 }
139
Popular Tags