20 package net.sf.gridarta.textedit.textarea;
22 import javax.swing.text.BadLocationException;
23 import javax.swing.text.Element;
24 import javax.swing.text.Segment;
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
114 public void setSelection(
final int newStart,
final int newEnd,
final int newStartLine,
final int newEndLine,
final boolean newBias) {
115 selectionStart = newStart;
116 selectionEnd = newEnd;
117 selectionStartLine = newStartLine;
118 selectionEndLine = newEndLine;
127 if (selectionStart == selectionEnd) {
131 if (rectangleSelect) {
134 final Element map = document.getDefaultRootElement();
136 int start = selectionStart - map.getElement(selectionStartLine).getStartOffset();
137 int end = selectionEnd - map.getElement(selectionEndLine).getStartOffset();
146 final StringBuilder buf =
new StringBuilder();
147 final Segment seg =
new Segment();
150 final Element lineElement = map.getElement(i);
151 int lineStart = lineElement.getStartOffset();
152 final int lineEnd = lineElement.getEndOffset() - 1;
154 lineStart = Math.min(lineStart + start, lineEnd);
155 final int lineLen = Math.min(end - start, lineEnd - lineStart);
157 getText(lineStart, lineLen, seg);
158 buf.append(seg.array, seg.offset, seg.count);
160 if (i != selectionEndLine) {
165 return buf.toString();
167 return getText(selectionStart, selectionEnd - selectionStart);
173 if (selectionStart == selectionEnd) {
174 rectangleSelect =
false;
185 public String
getText(
final int start,
final int len) {
187 return document.getText(start, len);
188 }
catch (
final BadLocationException bl) {
189 bl.printStackTrace();
201 public void getText(
final int start,
final int len, @NotNull
final Segment segment) {
203 document.getText(start, len, segment);
204 }
catch (
final BadLocationException bl) {
205 bl.printStackTrace();
219 if (rectangleSelect) {
220 final Element map = document.getDefaultRootElement();
222 int start = selectionStart - map.getElement(selectionStartLine).getStartOffset();
223 int end = selectionEnd - map.getElement(selectionEndLine).getStartOffset();
236 final Element lineElement = map.getElement(i);
237 final int lineStart = lineElement.getStartOffset();
238 final int lineEnd = lineElement.getEndOffset() - 1;
239 final int rectangleStart = Math.min(lineEnd, lineStart + start);
241 document.remove(rectangleStart, Math.min(lineEnd - rectangleStart, end - start));
243 if (selectedText == null) {
247 currNewline = selectedText.indexOf(
'\n', lastNewline);
248 if (currNewline == -1) {
249 currNewline = selectedText.length();
252 document.insertString(rectangleStart, selectedText.substring(lastNewline, currNewline), null);
254 lastNewline = Math.min(selectedText.length(), currNewline + 1);
257 if (selectedText != null && currNewline != selectedText.length()) {
258 final int offset = map.getElement(selectionEndLine).getEndOffset() - 1;
259 document.insertString(offset,
"\n", null);
260 document.insertString(offset + 1, selectedText.substring(currNewline + 1), null);
263 document.remove(selectionStart, selectionEnd - selectionStart);
264 if (selectedText != null) {
265 document.insertString(selectionStart, selectedText, null);
268 }
catch (
final BadLocationException bl) {
269 bl.printStackTrace();
270 throw new InternalError(
"Cannot replace selection");
316 return document.getLength();
323 return document.getDefaultRootElement().getElementCount();
331 return document.getDefaultRootElement().getElementIndex(offset);
341 final Element lineElement = document.getDefaultRootElement().getElement(line);
342 if (lineElement == null) {
345 return lineElement.getStartOffset();
356 final Element lineElement = document.getDefaultRootElement().getElement(line);
357 if (lineElement == null) {
360 return lineElement.getEndOffset();
369 final Element lineElement = document.getDefaultRootElement().getElement(line);
370 if (lineElement == null) {
373 return lineElement.getEndOffset() - lineElement.getStartOffset() - 1;
383 return document.getText(0, document.getLength());
384 }
catch (
final BadLocationException bl) {
385 bl.printStackTrace();
393 public void setText(@NotNull
final String text) {
396 document.remove(0, document.getLength());
397 document.insertString(0, text, null);
398 }
catch (
final BadLocationException bl) {
399 bl.printStackTrace();
421 public void getLineText(
final int lineIndex, @NotNull
final Segment segment) {
int getCaretLine()
Returns the caret line.
A document implementation that can be tokenized by the syntax highlighting system.
final SyntaxDocument document
The SyntaxDocument this selection is part of.
int getSelectionEnd()
Returns the selection end offset.
int getCaretPosition()
Returns the caret position.
int getSelectionStart()
Returns the selection start offset.
TextAreaSelection(@NotNull final SyntaxDocument document)
Creates a new instance.
int getLineCount()
Returns the number of lines in the document.
void getLineText(final int lineIndex, @NotNull final Segment segment)
Copies the text on the specified line into a segment.
int getLineLength(final int line)
Returns the length of the specified line.
TokenMarker getTokenMarker()
Returns the document's token marker.
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...
void disableSelectionIfEmpty()
CharSequence getLineText(final int lineIndex)
Returns the text on the specified line.
void setSelection(final int newStart, final int newEnd, final int newStartLine, final int newEndLine, final boolean newBias)
SyntaxDocument getDocument()
Returns the document this text area is editing.
static void beginCompoundEdit()
Starts a compound edit that can be undone in one operation.
void setText(@NotNull final String text)
Sets the entire text of this text area.
int getMarkPosition()
Returns the mark position.
int getSelectionStartLine()
Returns the selection start line.
This package contains the other part of the script editor.
static void endCompoundEdit()
Ends a compound edit that can be undone in one operation.
void setSelectionRectangular(final boolean rectangleSelect)
Sets if the selection should be rectangular.
String getText(final int start, final int len)
Returns the specified substring of the document.
void getText(final int start, final int len, @NotNull final Segment segment)
Copies the specified substring of the document into a segment.
int getLineEndOffset(final int line)
Returns the end offset of the specified line.
A token marker that splits lines of text into tokens.
boolean isSelectionRectangular()
Returns true if the selection is rectangular, false otherwise.
int getLineStartOffset(final int line)
Returns the start offset of the specified line.
String getText()
Returns the entire text of this text area.
int getSelectionEndLine()
Returns the selection end line.
int getLineOfOffset(final int offset)
Returns the line containing the specified offset.
int getDocumentLength()
Returns the length of the document.
String getSelectedText()
Returns the selected text, or null if no selection is active.
void setSelectedText(@NotNull final String selectedText)
Replaces the selection with the specified text.