本文整理匯總了Java中javax.swing.text.Document.remove方法的典型用法代碼示例。如果您正苦於以下問題:Java Document.remove方法的具體用法?Java Document.remove怎麽用?Java Document.remove使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.swing.text.Document
的用法示例。
在下文中一共展示了Document.remove方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: moveLineDown
import javax.swing.text.Document; //導入方法依賴的package包/類
public void moveLineDown(RTextArea textArea, int line)
throws BadLocationException {
Document doc = textArea.getDocument();
Element root = doc.getDefaultRootElement();
Element elem = root.getElement(line);
int start = elem.getStartOffset();
int end = elem.getEndOffset();
int caret = textArea.getCaretPosition();
int caretOffset = caret - start;
String text = doc.getText(start, end-start);
doc.remove(start, end-start);
Element elem2 = root.getElement(line); // not "line+1" - removed.
//int start2 = elem2.getStartOffset();
int end2 = elem2.getEndOffset();
doc.insertString(end2, text, null);
elem = root.getElement(line+1);
textArea.setCaretPosition(elem.getStartOffset()+caretOffset);
}
示例2: testBiasSurvivesStateChanges
import javax.swing.text.Document; //導入方法依賴的package包/類
/**
* Creates a PositionRef biased backwards and verifies it behaves correctly,
* then closes and reopens the document and checks again
*/
public void testBiasSurvivesStateChanges() throws Exception {
// open the document
Document doc = support.openDocument();
PositionRef back = support.createPositionRef(3, Position.Bias.Backward);
PositionRef forw = support.createPositionRef(3, Position.Bias.Forward);
doc.insertString(3, "_", null);
assertEquals("Backwards position should not move for insert at its position",
3, back.getOffset());
assertEquals("Forwards position should move for insert at its position",
4, forw.getOffset());
// move positions at the same offset again
doc.remove(3, 1);
support.close();
doc = support.openDocument();
doc.insertString(3, "_", null);
assertEquals("Backwards position should not move for insert at its position",
3, back.getOffset());
assertEquals("Forwards position should move for insert at its position",
4, forw.getOffset());
}
示例3: 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;
}
示例4: actionPerformed
import javax.swing.text.Document; //導入方法依賴的package包/類
/**
* {@inheritDoc}
*/
@Override
public void actionPerformed(ActionEvent arg0) {
JTextArea currentEditor = observer.getActiveEditor().getEditor();
Document doc = currentEditor.getDocument();
int len = Math.abs(currentEditor.getCaret().getDot()
- currentEditor.getCaret().getMark());
int offset = Math.min(currentEditor.getCaret().getDot(), currentEditor
.getCaret().getMark());
try {
String text = doc.getText(offset, len);
text = toggleCase(text);
doc.remove(offset, len);
doc.insertString(offset, text, null);
currentEditor.getCaret().setDot(offset);
currentEditor.getCaret().moveDot(offset + len);
currentEditor.requestFocus();
} catch (BadLocationException e1) {
}
}
示例5: 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 = lowerCase(text);
document.remove(offset, len);
document.insertString(offset, text, null);
// Reselect text
position.setDot(offset);
position.moveDot(offset + len);
currentEditor.requestFocus();
} catch (BadLocationException e1) {
observer.errorMessage(e1);
}
}
示例6: handleToggleComment
import javax.swing.text.Document; //導入方法依賴的package包/類
private void handleToggleComment(Element elem, Document doc,
String[] startEnd, boolean add) throws BadLocationException {
int start = elem.getStartOffset();
int end = elem.getEndOffset() - 1;
if (add) {
doc.insertString(start, startEnd[0], null);
if (startEnd[1]!=null) {
doc.insertString(end+startEnd[0].length(), startEnd[1],
null);
}
}
else {
doc.remove(start, startEnd[0].length());
if (startEnd[1]!=null) {
int temp = startEnd[1].length();
doc.remove(end-startEnd[0].length()-temp, temp);
}
}
}
示例7: testCustomHighlightBeyondDocEnd
import javax.swing.text.Document; //導入方法依賴的package包/類
public void testCustomHighlightBeyondDocEnd() throws Exception {
loggingOn();
ViewUpdatesTesting.setTestValues(ViewUpdatesTesting.NO_OP_TEST_VALUE);
JEditorPane pane = ViewUpdatesTesting.createPane();
Document doc = pane.getDocument();
final DocumentView docView = DocumentView.get(pane);
final TestOffsetsHighlightsContainer highlights = new TestOffsetsHighlightsContainer();
ViewUpdatesTesting.getSingleHighlightingLayerCustom(pane, highlights);
highlights.setOffsetPairs(new int[] { 0, 5 });
String text = "abcd\nefgh";
doc.insertString(0, text, null);
docView.op.viewsRebuildOrMarkInvalid(); // Manually mark affected children as invalid
docView.ensureAllParagraphsChildrenAndLayoutValid();
highlights.setOffsetPairs(new int[] { 3, 9 });
doc.remove(0, doc.getLength());
}
示例8: testCuriosities
import javax.swing.text.Document; //導入方法依賴的package包/類
public void testCuriosities() throws Exception {
// Test position at offset 0 does not move after insert
Document doc = new PlainDocument();
doc.insertString(0, "test", null);
Position pos = doc.createPosition(0);
assertEquals(0, pos.getOffset());
doc.insertString(0, "a", null);
assertEquals(0, pos.getOffset());
// Test there is an extra newline above doc.getLength()
assertEquals("\n", doc.getText(doc.getLength(), 1));
assertEquals("atest\n", doc.getText(0, doc.getLength() + 1));
// Test the last line element contains the extra newline
Element lineElem = doc.getDefaultRootElement().getElement(0);
assertEquals(0, lineElem.getStartOffset());
assertEquals(doc.getLength() + 1, lineElem.getEndOffset());
// Test that once position gets to zero it won't go anywhere else (unless undo performed)
pos = doc.createPosition(1);
doc.remove(0, 1);
assertEquals(0, pos.getOffset());
doc.insertString(0, "b", null);
assertEquals(0, pos.getOffset());
}
示例9: defaultAction
import javax.swing.text.Document; //導入方法依賴的package包/類
@Override
public void defaultAction(JTextComponent component) {
//StyledDocument doc = (StyledDocument) component.getDocument();
Document doc = component.getDocument();
try {
String text = doc.getText(0, caretOffset);
int dot = text.lastIndexOf('.');
if (dot < 0) dot = 0;
else dot++;
doc.remove(dot, caretOffset - dot);
caretOffset = dot;
String insertStr;
int insertOffset = caretOffset;
if (typeElement != null) {
insertStr = ElementUtilities.getBinaryName(typeElement);
doc.remove(0, doc.getLength());
insertOffset = 0;
} else {
insertStr = ((prefix != null) ? prefix : "") + elementName;
}
doc.insertString(insertOffset, insertStr, null);
if (elementKind == ElementKind.PACKAGE) {
doc.insertString(insertOffset + insertStr.length(), ".", null);
}
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
}
if (elementKind != ElementKind.PACKAGE) {
//This statement will close the code completion box:
Completion.get().hideAll();
}
}
示例10: removeText
import javax.swing.text.Document; //導入方法依賴的package包/類
void removeText() {
if ((p0 != null) && (p1 != null) && (p0.getOffset() != p1.getOffset())) {
try {
Document doc = c.getDocument();
doc.remove(p0.getOffset(), p1.getOffset() - p0.getOffset());
} catch (BadLocationException e) {
}
}
}
示例11: deleteln
import javax.swing.text.Document; //導入方法依賴的package包/類
public synchronized Console deleteln(int lines)
{
String text = this.textContent.getText();
int newLineIndex = -1;
for(int i = 0; i < lines; ++i)
{
int j = newLineIndex == -1 ? text.lastIndexOf('\n')
: text.lastIndexOf('\n', newLineIndex - 1);
if (j == -1) break;
newLineIndex = j;
}
if (newLineIndex != -1)
{
int remaining = text.length() - newLineIndex;
Document doc = this.textContent.getDocument();
try
{
doc.remove(newLineIndex, remaining);
}
catch (BadLocationException e)
{
e.printStackTrace();
}
}
return this;
}
示例12: testRemoveNewline
import javax.swing.text.Document; //導入方法依賴的package包/類
public void testRemoveNewline() throws Exception {
loggingOn();
JEditorPane pane = ViewUpdatesTesting.createPane();
Document doc = pane.getDocument();
UndoManager undoManager = ViewUpdatesTesting.getUndoManager(doc);
doc.insertString(0, "a\nb", null);
doc.remove(1, 1);
undoManager.undo();
pane.modelToView(0);
}
示例13: testRemoveElementEvent
import javax.swing.text.Document; //導入方法依賴的package包/類
public void testRemoveElementEvent() throws DocumentModelException, BadLocationException, InterruptedException {
Document doc = new BaseDocument(DefaultEditorKit.class, false);
doc.insertString(0,"abcde|fgh|ij|k",null); //4 elements should be created
final DocumentModel model = new DocumentModel(doc, dmProvider);
DocumentModelUtils.dumpModelElements(model);
DocumentModelUtils.dumpElementStructure(model.getRootElement());
//listen to model
final Vector removedElements = new Vector();
model.addDocumentModelListener(new DocumentModelListenerAdapter() {
public void documentElementRemoved(DocumentElement de) {
removedElements.add(de);
}
});
//listen to element
final Vector removedElements2 = new Vector();
model.getRootElement().addDocumentElementListener(new DocumentElementListenerAdapter() {
public void elementRemoved(DocumentElementEvent e) {
removedElements2.add(e.getChangedChild());
}
});
model.addDocumentModelStateListener(new DocumentModelStateListenerAdapter() {
public void updateFinished() {
assertEquals(4,removedElements2.size());
assertEquals(4, removedElements.size());
assertEquals(0, model.getRootElement().getElementCount());
}
});
doc.remove(0,doc.getLength());
model.forceUpdate();
}
示例14: testTokenCountWasCalledInUpdater
import javax.swing.text.Document; //導入方法依賴的package包/類
public void testTokenCountWasCalledInUpdater() throws Exception {
Document doc = new ModificationTextDocument();
String text = "+/* */";
doc.insertString(0, text, null);
doc.putProperty(Language.class, TestTokenId.language());
TokenHierarchy<?> hi = TokenHierarchy.get(doc);
TokenSequence<?> ts;
((AbstractDocument)doc).readLock();
try {
ts = hi.tokenSequence();
assertTrue(ts.moveNext());
LexerTestUtilities.assertTokenEquals(ts,TestTokenId.PLUS, "+", -1);
} finally {
((AbstractDocument)doc).readUnlock();
}
doc.remove(1, 3); // Remove "/* "
((AbstractDocument)doc).readLock();
try {
ts = hi.tokenSequence();
ts.moveEnd();
// Extra ending '\n' of the document returned by DocumentUtilities.getText(doc) and lexed
assertTrue(ts.movePrevious());
LexerTestUtilities.assertTokenEquals(ts,TestTokenId.WHITESPACE, "\n", -1);
assertTrue(ts.movePrevious());
LexerTestUtilities.assertTokenEquals(ts,TestTokenId.DIV, "/", -1);
} finally {
((AbstractDocument)doc).readUnlock();
}
}
示例15: testEmbeddingUpdate
import javax.swing.text.Document; //導入方法依賴的package包/類
public void testEmbeddingUpdate() throws Exception {
Document doc = new ModificationTextDocument();
// Assign a language to the document
doc.putProperty(Language.class,TestTokenId.language());
doc.insertString(0, "a/*abc def*/", null);
LexerTestUtilities.initLastTokenHierarchyEventListening(doc);
TokenHierarchy<?> hi = TokenHierarchy.get(doc);
((AbstractDocument)doc).readLock();
try {
TokenSequence<?> ts = hi.tokenSequence();
assertTrue(ts.moveNext());
LexerTestUtilities.assertTokenEquals(ts,TestTokenId.IDENTIFIER, "a", 0);
assertTrue(ts.moveNext());
LexerTestUtilities.assertTokenEquals(ts,TestTokenId.BLOCK_COMMENT, "/*abc def*/", 1);
TokenSequence<?> ets = ts.embedded();
assertNotNull(ets);
assertTrue(ts.moveNext());
assertTrue(ets.moveNext());
LexerTestUtilities.assertTokenEquals(ets,TestPlainTokenId.WORD, "abc", 3);
assertTrue(ets.moveNext());
LexerTestUtilities.assertTokenEquals(ets,TestPlainTokenId.WHITESPACE, " ", 6);
assertTrue(ets.moveNext());
LexerTestUtilities.assertTokenEquals(ets,TestPlainTokenId.WORD, "def", 7);
assertFalse(ets.moveNext());
} finally {
((AbstractDocument)doc).readUnlock();
}
// Make "axbc" inside the comment
doc.insertString(4, "x", null);
((AbstractDocument)doc).readLock();
try {
TokenHierarchyEvent evt = LexerTestUtilities.getLastTokenHierarchyEvent(doc);
assertNotNull(evt);
TokenChange<?> tc = evt.tokenChange();
assertNotNull(tc);
assertEquals(1, tc.index());
assertEquals(1, tc.offset());
assertEquals(1, tc.addedTokenCount());
assertEquals(1, tc.removedTokenCount());
assertEquals(TestTokenId.language(), tc.language());
assertEquals(1, tc.embeddedChangeCount());
TokenChange<?> etc = tc.embeddedChange(0);
assertEquals(0, etc.index());
assertEquals(3, etc.offset());
assertEquals(1, etc.addedTokenCount()); // 0 to allow for lazy lexing where this would be unknowns
assertEquals(1, etc.removedTokenCount());
assertEquals(TestPlainTokenId.language(), etc.language());
assertEquals(0, etc.embeddedChangeCount());
} finally {
((AbstractDocument)doc).readUnlock();
}
doc.remove(3, 8); // there will be empty /**/ so test empty embedded sequence
doc.insertString(3, "x", null); // there will be empty /**/
}