Gridarta Editor
SyntaxDocument.java
Go to the documentation of this file.
1 /*
2  * SyntaxDocument.java - Document that can be tokenized
3  * Copyright (C) 1999 Slava Pestov
4  * Copyright (C) 2000-2015 The Gridarta Developers.
5  *
6  * You may use and modify this package for any purpose. Redistribution is
7  * permitted, in both source and binary form, provided that this notice
8  * remains intact in all source distributions of this package.
9  */
10 
11 package net.sf.gridarta.textedit.textarea;
12 
13 import javax.swing.event.DocumentEvent;
14 import javax.swing.text.BadLocationException;
15 import javax.swing.text.Element;
16 import javax.swing.text.PlainDocument;
17 import javax.swing.text.Segment;
18 import javax.swing.undo.UndoableEdit;
20 import org.jetbrains.annotations.NotNull;
21 import org.jetbrains.annotations.Nullable;
22 
28 public class SyntaxDocument extends PlainDocument {
29 
33  private static final long serialVersionUID = 1L;
34 
40  @Nullable
42  return tokenMarker;
43  }
44 
51  public void setTokenMarker(@Nullable final TokenMarker tokenMarker) {
52  this.tokenMarker = tokenMarker;
53  if (tokenMarker == null) {
54  return;
55  }
56 
57  tokenMarker.insertLines(0, getDefaultRootElement().getElementCount());
58  tokenizeLines();
59  }
60 
65  private void tokenizeLines() {
66  tokenizeLines(0, getDefaultRootElement().getElementCount());
67  }
68 
76  private void tokenizeLines(final int start, final int len) {
77  if (tokenMarker == null) {
78  return;
79  }
80 
81  final Segment lineSegment = new Segment();
82  final Element map = getDefaultRootElement();
83 
84  final int end = len + start;
85 
86  try {
87  for (int i = start; i < end; i++) {
88  final Element lineElement = map.getElement(i);
89  final int lineStart = lineElement.getStartOffset();
90  getText(lineStart, lineElement.getEndOffset() - lineStart - 1, lineSegment);
91  tokenMarker.markTokens(lineSegment, i);
92  }
93  } catch (final BadLocationException bl) {
94  bl.printStackTrace();
95  }
96  }
97 
103  public static void beginCompoundEdit() {
104  }
105 
111  public static void endCompoundEdit() {
112  }
113 
120  public static void addUndoableEdit(@NotNull final UndoableEdit edit) {
121  }
122 
123  // protected members
124 
125  @Nullable
127 
132  @Override
133  protected void fireInsertUpdate(@NotNull final DocumentEvent e) {
134  if (tokenMarker != null) {
135  final DocumentEvent.ElementChange ch = e.getChange(getDefaultRootElement());
136  if (ch != null) {
137  tokenMarker.insertLines(ch.getIndex() + 1, ch.getChildrenAdded().length - ch.getChildrenRemoved().length);
138  }
139  }
140 
141  super.fireInsertUpdate(e);
142  }
143 
148  @Override
149  protected void fireRemoveUpdate(@NotNull final DocumentEvent e) {
150  if (tokenMarker != null) {
151  final DocumentEvent.ElementChange ch = e.getChange(getDefaultRootElement());
152  if (ch != null) {
153  tokenMarker.deleteLines(ch.getIndex() + 1, ch.getChildrenRemoved().length - ch.getChildrenAdded().length);
154  }
155  }
156 
157  super.fireRemoveUpdate(e);
158  }
159 
160 }
void insertLines(final int index, final int lines)
Informs the token marker that lines have been inserted into the document.
A document implementation that can be tokenized by the syntax highlighting system.
void deleteLines(final int index, final int lines)
Informs the token marker that line have been deleted from the document.
void fireRemoveUpdate(@NotNull final DocumentEvent e)
We overwrite this method to update the token marker state immediately so that any event listeners get...
Base package of all Gridarta classes.
TokenMarker getTokenMarker()
Returns the token marker that is to be used to split lines of this document up into tokens...
static void beginCompoundEdit()
Starts a compound edit that can be undone in one operation.
This package contains the other part of the script editor.
static void endCompoundEdit()
Ends a compound edit that can be undone in one operation.
List< Token > markTokens(final Segment line, final int lineIndex)
A wrapper for the lower-level.
A token marker that splits lines of text into tokens.
static final long serialVersionUID
Serial Version UID.
void tokenizeLines()
Re-parses the document, by passing all lines to the token marker.
static void addUndoableEdit(@NotNull final UndoableEdit edit)
Adds an undoable edit to this document&#39;s undo list.
void tokenizeLines(final int start, final int len)
Re-parses the document, by passing the specified lines to the token marker.
void setTokenMarker(@Nullable final TokenMarker tokenMarker)
Sets the token marker that is to be used to split lines of this document up into tokens.
void fireInsertUpdate(@NotNull final DocumentEvent e)
We overwrite this method to update the token marker state immediately so that any event listeners get...