KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jedit > syntax > SyntaxDocument


1 package org.jedit.syntax;
2
3 /*
4  * SyntaxDocument.java - Document that can be tokenized
5  * Copyright (C) 1999 Slava Pestov
6  *
7  * You may use and modify this package for any purpose. Redistribution is
8  * permitted, in both source and binary form, provided that this notice
9  * remains intact in all source distributions of this package.
10  */

11
12 import javax.swing.event.*;
13 import javax.swing.text.*;
14 import javax.swing.undo.UndoableEdit JavaDoc;
15
16 /**
17  * A document implementation that can be tokenized by the syntax highlighting
18  * system.
19  *
20  * @author Slava Pestov
21  * @version $Id: SyntaxDocument.java,v 1.1 2003/12/14 16:29:49 daggerrz Exp $
22  */

23 public class SyntaxDocument extends PlainDocument
24 {
25    /**
26     * Returns the token marker that is to be used to split lines
27     * of this document up into tokens. May return null if this
28     * document is not to be colorized.
29     */

30    public TokenMarker getTokenMarker()
31    {
32       return tokenMarker;
33    }
34
35    /**
36     * Sets the token marker that is to be used to split lines of
37     * this document up into tokens. May throw an exception if
38     * this is not supported for this type of document.
39     * @param tm The new token marker
40     */

41    public void setTokenMarker(TokenMarker tm)
42    {
43       tokenMarker = tm;
44       if(tm == null)
45          return;
46       tokenMarker.insertLines(0,getDefaultRootElement()
47          .getElementCount());
48       tokenizeLines();
49    }
50
51    /**
52     * Reparses the document, by passing all lines to the token
53     * marker. This should be called after the document is first
54     * loaded.
55     */

56    public void tokenizeLines()
57    {
58       tokenizeLines(0,getDefaultRootElement().getElementCount());
59    }
60
61    /**
62     * Reparses the document, by passing the specified lines to the
63     * token marker. This should be called after a large quantity of
64     * text is first inserted.
65     * @param start The first line to parse
66     * @param len The number of lines, after the first one to parse
67     */

68    public void tokenizeLines(int start, int len)
69    {
70       if(tokenMarker == null || !tokenMarker.supportsMultilineTokens())
71          return;
72
73       Segment lineSegment = new Segment();
74       Element map = getDefaultRootElement();
75
76       len += start;
77
78       try
79       {
80          for(int i = start; i < len; i++)
81          {
82             Element lineElement = map.getElement(i);
83             int lineStart = lineElement.getStartOffset();
84             getText(lineStart,lineElement.getEndOffset()
85                - lineStart - 1,lineSegment);
86             tokenMarker.markTokens(lineSegment,i);
87          }
88       }
89       catch(BadLocationException bl)
90       {
91          bl.printStackTrace();
92       }
93    }
94
95    /**
96     * Starts a compound edit that can be undone in one operation.
97     * Subclasses that implement undo should override this method;
98     * this class has no undo functionality so this method is
99     * empty.
100     */

101    public void beginCompoundEdit() {}
102
103    /**
104     * Ends a compound edit that can be undone in one operation.
105     * Subclasses that implement undo should override this method;
106     * this class has no undo functionality so this method is
107     * empty.
108     */

109    public void endCompoundEdit() {}
110
111    /**
112     * Adds an undoable edit to this document's undo list. The edit
113     * should be ignored if something is currently being undone.
114     * @param edit The undoable edit
115     *
116     * @since jEdit 2.2pre1
117     */

118    public void addUndoableEdit(UndoableEdit JavaDoc edit) {}
119
120    // protected members
121
protected TokenMarker tokenMarker;
122
123    /**
124     * We overwrite this method to update the token marker
125     * state immediately so that any event listeners get a
126     * consistent token marker.
127     */

128    protected void fireInsertUpdate(DocumentEvent evt)
129    {
130       if(tokenMarker != null)
131       {
132          DocumentEvent.ElementChange ch = evt.getChange(
133             getDefaultRootElement());
134          if(ch != null)
135          {
136             tokenMarker.insertLines(ch.getIndex() + 1,
137                ch.getChildrenAdded().length -
138                ch.getChildrenRemoved().length);
139          }
140       }
141
142       super.fireInsertUpdate(evt);
143    }
144    
145    /**
146     * We overwrite this method to update the token marker
147     * state immediately so that any event listeners get a
148     * consistent token marker.
149     */

150    protected void fireRemoveUpdate(DocumentEvent evt)
151    {
152       if(tokenMarker != null)
153       {
154          DocumentEvent.ElementChange ch = evt.getChange(
155             getDefaultRootElement());
156          if(ch != null)
157          {
158             tokenMarker.deleteLines(ch.getIndex() + 1,
159                ch.getChildrenRemoved().length -
160                ch.getChildrenAdded().length);
161          }
162       }
163
164       super.fireRemoveUpdate(evt);
165    }
166 }
167
Popular Tags