本文整理汇总了Java中javax.swing.text.Document.insertString方法的典型用法代码示例。如果您正苦于以下问题:Java Document.insertString方法的具体用法?Java Document.insertString怎么用?Java Document.insertString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.text.Document
的用法示例。
在下文中一共展示了Document.insertString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: replaceText
import javax.swing.text.Document; //导入方法依赖的package包/类
static void replaceText(JTextComponent textComponent, final int offset, final int length, final String text) {
if (!textComponent.isEditable()) {
return;
}
final Document document = textComponent.getDocument();
Runnable r = new Runnable() {
public @Override void run() {
try {
if (length > 0) {
document.remove(offset, length);
}
document.insertString(offset, text, null);
} catch (BadLocationException ble) {
ErrorManager.getDefault().notify(ble);
}
}
};
if (document instanceof BaseDocument) {
((BaseDocument)document).runAtomic(r);
} else {
r.run();
}
}
示例2: generateJavadoc
import javax.swing.text.Document; //导入方法依赖的package包/类
private void generateJavadoc(Document doc, Descriptor desc, Indent ie) throws BadLocationException {
// skip javadoc header /** and tail */ since they were already generated
String content = desc.javadoc;
if(content.endsWith("\n")) {
content = content.substring(0, content.length() - "\n".length());
}
if (content.length() == 0) {
return;
}
Position pos = desc.caret;
int startOffset = pos.getOffset();
doc.insertString(startOffset, content, null);
if (startOffset != pos.getOffset()) {
ie.reindent(startOffset + 1, pos.getOffset());
}
}
示例3: actionPerformed
import javax.swing.text.Document; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void actionPerformed(ActionEvent e) {
JTextArea currentEditor = observer.getActiveEditor().getEditor();
Document document = currentEditor.getDocument();
Caret position = currentEditor.getCaret();
int len = Math.abs(currentEditor.getCaret().getDot()
- currentEditor.getCaret().getMark());
int offset = Math.min(currentEditor.getCaret().getDot(), currentEditor
.getCaret().getMark());
try {
String text = document.getText(offset, len);
text = upperCase(text);
document.remove(offset, len);
document.insertString(offset, text, null);
position.setDot(offset);
position.moveDot(offset + len);
currentEditor.requestFocus();
} catch (BadLocationException e1) {
observer.errorMessage(e1);
}
}
示例4: setDocumentContentTo
import javax.swing.text.Document; //导入方法依赖的package包/类
public static Document setDocumentContentTo(Document doc, InputStream in) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuffer sbuf = new StringBuffer();
try {
String line = null;
while ((line = br.readLine()) != null) {
sbuf.append(line);
sbuf.append(System.getProperty("line.separator"));
}
} finally {
br.close();
}
doc.remove(0, doc.getLength());
doc.insertString(0,sbuf.toString(),null);
return doc;
}
示例5: testModDuringLongOutputStreamSave
import javax.swing.text.Document; //导入方法依赖的package包/类
public void testModDuringLongOutputStreamSave() throws Exception {
Document doc = support.openDocument();
doc.insertString(0, "a", null);
RequestProcessor.getDefault().post(new Runnable() {
@Override
public void run() {
try {
support.saveDocument();
saveDocumentFinished.set(true);
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
});
waitFor(saveToStreamStarted);
doc.insertString(1, "b", null);
docModDuringSaveStream.set(true);
waitFor(saveDocumentFinished);
// Here the document should be modified again since doc mod was made
assertTrue(isModified());
}
示例6: setDocumentContent
import javax.swing.text.Document; //导入方法依赖的package包/类
protected void setDocumentContent(Document document, String text) {
try {
document.remove(0, document.getLength());
document.insertString(0, text, null);
} catch (BadLocationException ex) {
throw new AssertionError(null, ex);
}
}
示例7: createDocument
import javax.swing.text.Document; //导入方法依赖的package包/类
private Document createDocument(String mimeType, String contents) {
Document doc = new DefaultStyledDocument();
doc.putProperty("mimeType", mimeType);
try {
doc.insertString(0, contents, null);
return doc;
} catch (BadLocationException ble) {
throw new IllegalStateException(ble);
}
}
示例8: testSaveTokensWithinLookahead
import javax.swing.text.Document; //导入方法依赖的package包/类
public void testSaveTokensWithinLookahead() throws Exception {
Document doc = new ModificationTextDocument();
String text = "aabc";
doc.insertString(0, text, null);
doc.putProperty(Language.class, TestSaveTokensInLATokenId.language());
TokenHierarchy<?> hi = TokenHierarchy.get(doc);
((AbstractDocument)doc).readLock();
try {
TokenSequence<?> ts = hi.tokenSequence();
ts.moveEnd(); // Force creation of all tokens
LexerTestUtilities.initLastTokenHierarchyEventListening(doc);
} finally {
((AbstractDocument)doc).readUnlock();
}
doc.remove(1, 1);
((AbstractDocument)doc).readLock();
try {
TokenHierarchyEvent evt = LexerTestUtilities.getLastTokenHierarchyEvent(doc);
TokenChange<?> change = evt.tokenChange();
assertEquals(1, change.addedTokenCount());
} finally {
((AbstractDocument)doc).readUnlock();
}
}
示例9: testConcurrentModifications
import javax.swing.text.Document; //导入方法依赖的package包/类
public void testConcurrentModifications() throws BadLocationException {
Document doc = createDocument(TestTokenId.language(), "NetBeans NetBeans NetBeans");
SyntaxHighlighting layer = new SyntaxHighlighting(doc);
{
HighlightsSequence hs = layer.getHighlights(Integer.MIN_VALUE, Integer.MAX_VALUE);
assertTrue("There should be some highlights", hs.moveNext());
// Modify the document
doc.insertString(0, "Hey", SimpleAttributeSet.EMPTY);
assertFalse("There should be no highlights after co-modification", hs.moveNext());
}
}
示例10: testReload
import javax.swing.text.Document; //导入方法依赖的package包/类
public void testReload() throws Exception {
final Pane[] pane = new Pane[1];
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
support.open();
pane[0] = support.getAnyEditor();
}
});
PositionRef one = support.createPositionRef(1, Bias.Forward);
Document doc = support.getDocument();
assertNotNull("Document is opened", doc);
PositionRef two = support.createPositionRef(2, Bias.Forward);
assertEquals(1, one.getOffset());
assertEquals(2, two.getOffset());
setNewTime(new Date());
Thread.sleep(2000);
assertNotNull("Document is opened", doc);
assertEquals(1, one.getOffset());
assertEquals(2, two.getOffset());
doc.insertString(0, "a", null);
assertEquals(2, one.getOffset());
assertEquals(3, two.getOffset());
}
示例11: testNullChildren
import javax.swing.text.Document; //导入方法依赖的package包/类
public void testNullChildren() throws Exception {
loggingOn();
ViewUpdatesTesting.setTestValues(ViewUpdatesTesting.NO_OP_TEST_VALUE);
JEditorPane pane = ViewUpdatesTesting.createPane();
Document doc = pane.getDocument();
final DocumentView docView = DocumentView.get(pane);
// Insert twice the length of chars that would be otherwise serviced with local views creation
int insertCount = ViewBuilder.MAX_CHARS_FOR_CREATE_LOCAL_VIEWS;
String insertText = "a\n";
for (int i = 0; i <= insertCount; i++) {
doc.insertString(0, insertText, null);
}
int docLen = doc.getLength();
int lineCount = doc.getDefaultRootElement().getElementCount();
docView.op.releaseChildren(false);
docView.ensureLayoutValidForInitedChildren();
docView.children.ensureParagraphsChildrenAndLayoutValid(docView, 0, lineCount / 2, 0, 0);
int hlStartOffset = docLen * 1 / 5;
int hlStartPIndex = doc.getDefaultRootElement().getElementIndex(hlStartOffset);
int hlEndOffset = hlStartOffset * 2;
int hlEndPIndex = hlStartPIndex * 2;
TestHighlightsViewFactory testFactory = ViewUpdatesTesting.getTestFactory(pane);
List<TestHighlight> testHlts = ViewUpdatesTesting.getHighlightsCopy(testFactory);
TestHighlight testHlt = TestHighlight.create(
ViewUtils.createPosition(doc, hlStartOffset),
ViewUtils.createPosition(doc, hlEndOffset),
ViewUpdatesTesting.FONT_ATTRS[2]
);
testHlts.add(testHlt);
testFactory.setHighlights(testHlts);
testFactory.fireChange(hlStartOffset, hlEndOffset);
docView.op.viewsRebuildOrMarkInvalid(); // Manually mark affected children as invalid
docView.children.ensureParagraphsChildrenAndLayoutValid(docView,
hlStartPIndex, hlEndPIndex, 0, 0);
ViewUpdatesTesting.setTestValues();
}
示例12: testModifyTheFileAndThenPreventItToBeSavedOnFileDisappear
import javax.swing.text.Document; //导入方法依赖的package包/类
public void testModifyTheFileAndThenPreventItToBeSavedOnFileDisappear() throws Exception {
Document doc = edit.openDocument();
assertFalse("Not Modified", edit.isModified());
doc.insertString(0, "Base change\n", null);
edit.saveDocument();
edit.open();
waitEQ();
JEditorPane[] arr = getPanes();
assertNotNull("There is one opened pane", arr);
java.awt.Component c = arr[0];
while (!(c instanceof CloneableEditor)) {
c = c.getParent();
}
CloneableEditor ce = (CloneableEditor)c;
// to change timestamps
Thread.sleep(1000);
content = "Ahoj\n";
date = new Date();
// to change timestamps
Thread.sleep(1000);
doc.remove(0, doc.getLength());
doc.insertString(0, "Internal change\n", null);
String txt = doc.getText(0, doc.getLength());
assertEquals("The right text is there", txt, "Internal change\n");
arr = getPanes();
assertNotNull("Panes are still open", arr);
assertTrue("Document is remains modified", edit.isModified());
// DD.toReturn.push(DialogDescriptor.CLOSED_OPTION);
try {
edit.saveDocument();
fail("External modification detected, expect UserQuestionException");
} catch (UserQuestionException ex) {
// rerun the action
ex.confirmed();
}
assertFalse("Editor saved", edit.isModified());
waitEQ();
String txt2 = doc.getText(0, doc.getLength());
assertEquals("The right text still remains", txt2, "Internal change\n");
assertEquals("Text is saved as well", txt2,"Internal change\n");
assertTrue("No dialog", DD.toReturn.isEmpty());
if (DD.error != null) {
fail("Error in dialog:\n" + DD.error);
}
}
示例13: testUpdateStartOffset
import javax.swing.text.Document; //导入方法依赖的package包/类
public void testUpdateStartOffset() throws Exception {
Document d = new PlainDocument();
d.putProperty(Language.class,TestTokenId.language());
d.insertString(0, "ident ident /** @see X */", null);
TokenHierarchy<?> h = TokenHierarchy.get(d);
((AbstractDocument)d).readLock();
try {
TokenSequence<?> ts = h.tokenSequence();
LexerTestUtilities.assertNextTokenEquals(ts,TestTokenId.IDENTIFIER, "ident");
assertEquals(0, ts.offset());
LexerTestUtilities.assertNextTokenEquals(ts,TestTokenId.WHITESPACE, " ");
assertEquals(5, ts.offset());
LexerTestUtilities.assertNextTokenEquals(ts,TestTokenId.IDENTIFIER, "ident");
assertEquals(6, ts.offset());
LexerTestUtilities.assertNextTokenEquals(ts,TestTokenId.WHITESPACE, " ");
assertEquals(11, ts.offset());
LexerTestUtilities.assertNextTokenEquals(ts,TestTokenId.JAVADOC_COMMENT, "/** @see X */");
assertEquals(12, ts.offset());
TokenSequence<?> inner = ts.embedded();
assertNotNull(inner);
LexerTestUtilities.assertNextTokenEquals(inner,TestJavadocTokenId.OTHER_TEXT, " ");
assertEquals(15, inner.offset());
LexerTestUtilities.assertNextTokenEquals(inner,TestJavadocTokenId.TAG, "@see");
assertEquals(16, inner.offset());
LexerTestUtilities.assertNextTokenEquals(inner,TestJavadocTokenId.OTHER_TEXT, " ");
assertEquals(20, inner.offset());
LexerTestUtilities.assertNextTokenEquals(inner,TestJavadocTokenId.IDENT, "X");
assertEquals(21, inner.offset());
LexerTestUtilities.assertNextTokenEquals(inner,TestJavadocTokenId.OTHER_TEXT, " ");
assertEquals(22, inner.offset());
} finally {
((AbstractDocument)d).readUnlock();
}
}
示例14: testDocumentChanges
import javax.swing.text.Document; //导入方法依赖的package包/类
public void testDocumentChanges() throws BadLocationException {
Document d = new PlainDocument();
d.insertString(0, "01234567890123456789012345678901234567890123456789", SimpleAttributeSet.EMPTY);
PositionsBag bag = new PositionsBag(d);
SimpleAttributeSet attribsA = new SimpleAttributeSet();
SimpleAttributeSet attribsB = new SimpleAttributeSet();
attribsA.addAttribute("set-name", "attribsA");
attribsB.addAttribute("set-name", "attribsB");
bag.addHighlight(d.createPosition(0), d.createPosition(30), attribsA);
bag.addHighlight(d.createPosition(10), d.createPosition(20), attribsB);
GapList<Position> marks = bag.getMarks();
GapList<AttributeSet> atttributes = bag.getAttributes();
assertEquals("Wrong number of highlights", 4, marks.size());
assertEquals("1. highlight - wrong start offset", 0, marks.get(0).getOffset());
assertEquals("1. highlight - wrong end offset", 10, marks.get(1).getOffset());
assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name"));
assertEquals("2. highlight - wrong start offset", 10, marks.get(1).getOffset());
assertEquals("2. highlight - wrong end offset", 20, marks.get(2).getOffset());
assertEquals("2. highlight - wrong attribs", "attribsB", atttributes.get(1).getAttribute("set-name"));
assertEquals("3. highlight - wrong start offset", 20, marks.get(2).getOffset());
assertEquals("3. highlight - wrong end offset", 30, marks.get(3).getOffset());
assertEquals("3. highlight - wrong attribs", "attribsA", atttributes.get(2).getAttribute("set-name"));
assertNull(" 3. highlight - wrong end", atttributes.get(3));
d.insertString(12, "----", SimpleAttributeSet.EMPTY);
assertEquals("Wrong number of highlights", 4, marks.size());
assertEquals("1. highlight - wrong start offset", 0, marks.get(0).getOffset());
assertEquals("1. highlight - wrong end offset", 10, marks.get(1).getOffset());
assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name"));
assertEquals("2. highlight - wrong start offset", 10, marks.get(1).getOffset());
assertEquals("2. highlight - wrong end offset", 24, marks.get(2).getOffset());
assertEquals("2. highlight - wrong attribs", "attribsB", atttributes.get(1).getAttribute("set-name"));
assertEquals("3. highlight - wrong start offset", 24, marks.get(2).getOffset());
assertEquals("3. highlight - wrong end offset", 34, marks.get(3).getOffset());
assertEquals("3. highlight - wrong attribs", "attribsA", atttributes.get(2).getAttribute("set-name"));
assertNull(" 3. highlight - wrong end", atttributes.get(3));
d.remove(1, 5);
assertEquals("Wrong number of highlights", 4, marks.size());
assertEquals("1. highlight - wrong start offset", 0, marks.get(0).getOffset());
assertEquals("1. highlight - wrong end offset", 5, marks.get(1).getOffset());
assertEquals("1. highlight - wrong attribs", "attribsA", atttributes.get(0).getAttribute("set-name"));
assertEquals("2. highlight - wrong start offset", 5, marks.get(1).getOffset());
assertEquals("2. highlight - wrong end offset", 19, marks.get(2).getOffset());
assertEquals("2. highlight - wrong attribs", "attribsB", atttributes.get(1).getAttribute("set-name"));
assertEquals("3. highlight - wrong start offset", 19, marks.get(2).getOffset());
assertEquals("3. highlight - wrong end offset", 29, marks.get(3).getOffset());
assertEquals("3. highlight - wrong attribs", "attribsA", atttributes.get(2).getAttribute("set-name"));
assertNull(" 3. highlight - wrong end", atttributes.get(3));
}
示例15: addAttribute
import javax.swing.text.Document; //导入方法依赖的package包/类
private void addAttribute(JTextComponent c, Document d, String text) throws BadLocationException {
String nsPrefix = ctx.findFxmlNsPrefix();
boolean addNsDecl = false;
// declare the namespace, if not yet present
if (nsPrefix == null) {
nsPrefix = ctx.findPrefixString(JavaFXEditorUtils.FXML_FX_NAMESPACE_CURRENT, JavaFXEditorUtils.FXML_FX_PREFIX);
addNsDecl = true;
}
int start = getStartOffset() + text.length();
// fix the position against mutation
Position startPos = NbDocument.createPosition(d, start, Position.Bias.Backward);
StringBuilder sb = new StringBuilder();
sb.append(" "); // tag-attribute separator
if (ctx.isRootElement() && addNsDecl) {
// append NS declaration
sb.append(createNsDecl(nsPrefix));
}
sb.append(ctx.createNSName(nsPrefix, getAttributeName())).append("=\"");
int l = sb.length();
sb.append("\"");
if (!ctx.isTagFinished()) {
if (shouldClose) {
sb.append("/>");
} else {
sb.append(">");
}
}
d.insertString(start, sb.toString(), null);
if (!ctx.isRootElement() && addNsDecl) {
d.insertString(
ctx.getRootAttrInsertOffset(),
createNsDecl(nsPrefix), null);
}
// position the caret inside '' for the user to enter the value
c.setCaretPosition(startPos.getOffset() + l);
}