use of org.apache.pivot.wtk.text.Document in project pivot by apache.
the class TextPane method getText.
/**
* Convenience method to get a portion of the document text into a single string.
*
* @param beginIndex The 0-based offset where to start retrieving text.
* @param endIndex The ending offset + 1 of the text to retrieve.
* @return The specified portion of the document text if there is any, or
* {@code null} if there is no document.
*/
public String getText(int beginIndex, int endIndex) {
Utils.checkTwoIndexBounds(beginIndex, endIndex, 0, getCharacterCount());
int count = endIndex - beginIndex;
if (count == 0) {
return "";
}
Document doc = getDocument();
if (doc != null) {
StringBuilder text = new StringBuilder(count);
addToText(text, doc, new Span(beginIndex, endIndex - 1));
return text.toString();
}
return null;
}
use of org.apache.pivot.wtk.text.Document in project pivot by apache.
the class TextPane method paste.
public void paste() {
if (document == null) {
setDocument(new Document());
}
Manifest clipboardContent = Clipboard.getContent();
if (clipboardContent != null && clipboardContent.containsText()) {
// Paste the string representation of the content
String text = null;
try {
// Replace \r\n with just \n and plain \r with \n instead
// so we can deal with lines uniformly below.
text = clipboardContent.getText().replace("\r\n", "\n").replace("\r", "\n");
} catch (IOException exception) {
// No-op
}
if (text != null && text.length() > 0) {
// Insert the clipboard contents
int n = 0;
int start = selectionStart;
int tabWidth = ((TextPane.Skin) getSkin()).getTabWidth();
Node currentNode = document.getDescendantAt(start);
while (!(currentNode instanceof Paragraph)) {
currentNode = currentNode.getParent();
}
int paragraphStartOffset = start - currentNode.getDocumentOffset();
bulkOperation = true;
// If there is only a line fragment here, then just insert it
int eolIndex = text.indexOf('\n');
if (eolIndex < 0) {
if (expandTabs) {
n = insertWithTabSubstitution(text, start, paragraphStartOffset, tabWidth);
} else {
insertText(text, start);
n = text.length();
}
} else {
int textOffset = 0;
// Insert each line into place, with a new paragraph following
while (eolIndex >= 0) {
String fragment = text.substring(textOffset, eolIndex);
int len;
if (expandTabs) {
len = insertWithTabSubstitution(fragment, start, paragraphStartOffset, tabWidth);
} else {
insertText(fragment, start);
len = fragment.length();
}
insertParagraph();
n += len + 1;
start += len + 1;
paragraphStartOffset = 0;
textOffset = eolIndex + 1;
eolIndex = text.indexOf('\n', textOffset);
}
// Now deal with any leftover at end of string
if (textOffset < text.length()) {
String lastFragment = text.substring(textOffset);
if (expandTabs) {
n += insertWithTabSubstitution(lastFragment, start, paragraphStartOffset, tabWidth);
} else {
insertText(lastFragment, start);
n += lastFragment.length();
}
}
}
bulkOperation = false;
textPaneCharacterListeners.charactersInserted(this, start, n);
}
}
}
use of org.apache.pivot.wtk.text.Document in project pivot by apache.
the class TextPane method setText.
public void setText(Reader textReader) throws IOException {
Utils.checkNull(textReader, "Reader");
int tabPosition = 0;
int tabWidth = ((TextPane.Skin) getSkin()).getTabWidth();
Document doc = new Document();
StringBuilder text = new StringBuilder();
int c = textReader.read();
while (c != -1) {
// Deal with the various forms of line endings: CR only, LF only or CR,LF
if (c == '\r') {
int c2 = textReader.read();
if (c2 == -1) {
break;
} else if (c2 == '\n') {
// Only add the \n (the paragraph separator)
c = c2;
} else {
// Change the paragraph separator to \n instead
// but push back the last character read
Paragraph paragraph = new Paragraph(text.toString());
doc.add(paragraph);
text.setLength(0);
tabPosition = 0;
c = c2;
continue;
}
}
if (c == '\n') {
Paragraph paragraph = new Paragraph(text.toString());
doc.add(paragraph);
text.setLength(0);
tabPosition = 0;
} else if (c == '\t') {
if (expandTabs) {
int spaces = tabWidth - (tabPosition % tabWidth);
for (int i = 0; i < spaces; i++) {
text.append(' ');
}
tabPosition += spaces;
} else {
text.append('\t');
}
} else {
text.append((char) c);
tabPosition++;
}
c = textReader.read();
}
if (text.length() != 0) {
Paragraph paragraph = new Paragraph(text.toString());
doc.add(paragraph);
}
setDocument(doc);
}
use of org.apache.pivot.wtk.text.Document in project pivot by apache.
the class TextPaneSkin method documentChanged.
// Text pane events
@Override
public void documentChanged(TextPane textPane, Document previousDocument) {
if (documentView != null) {
documentView.detach();
documentView = null;
}
Document document = textPane.getDocument();
if (document != null) {
documentView = (TextPaneSkinDocumentView) TextPaneSkinNodeView.createNodeView(this, document);
documentView.attach();
}
invalidateComponent();
}
use of org.apache.pivot.wtk.text.Document in project pivot by apache.
the class TextPaneSkin method install.
@Override
public void install(Component component) {
super.install(component);
TextPane textPane = (TextPane) component;
textPane.getTextPaneListeners().add(this);
textPane.getTextPaneSelectionListeners().add(this);
textPane.setCursor(Cursor.TEXT);
Document document = textPane.getDocument();
if (document != null) {
documentView = (TextPaneSkinDocumentView) TextPaneSkinNodeView.createNodeView(this, document);
documentView.attach();
updateSelection();
}
}
Aggregations