void doInsert(final int offset, final String text, final AttributeSet attrs) throws BadLocationException { final DefaultDocumentEvent event = new DefaultDocumentEvent(offset, text.length(), EventType.INSERT); final UndoableEdit contentEdit = content.insertString(offset, text); if (contentEdit != null) { event.addEdit(contentEdit); } insertUpdate(event, attrs); event.end(); fireInsertUpdate(event); if (contentEdit != null) { fireUndoableEditUpdate(new UndoableEditEvent(this, event)); } }
protected void updateDamage(final DocumentEvent event, final Shape shape, final ViewFactory factory) { if (shape == null) { return; } if (metrics == null) { updateMetrics(); preferenceChanged(null, true, true); return; } final ElementChange change = event.getChange(getElement()); if (event.getType() == EventType.INSERT) { updateDamageOnInsert(event, change, shape); } else { updateDamageOnRemove(event, change, shape); } }
private void removeDefaultBody() { Element impliedLF = getCharacterElement(getLength() - 1); try { remove(getLength() - 1, 1); } catch (BadLocationException e) { } BranchElement root = (BranchElement)getDefaultRootElement(); final int oddBodyIndex = root.getElementCount() - 1; Element oddBody = root.getElement(oddBodyIndex); final Element[] emptyArray = new Element[0]; root.replace(oddBodyIndex, 1, emptyArray); Element lf = getCharacterElement(getLength()); writeLock(); try { ((MutableAttributeSet)lf).removeAttributes(lf.getAttributes().getAttributeNames()); ((MutableAttributeSet)lf).addAttributes(impliedLF.getAttributes()); } finally { writeUnlock(); } final DefaultDocumentEvent removeEvent = new DefaultDocumentEvent(oddBody.getStartOffset(), oddBody.getEndOffset() - oddBody.getStartOffset(), EventType.REMOVE); removeEvent.addEdit(new ElementEdit(root, oddBodyIndex, new Element[] {oddBody}, emptyArray)); fireRemoveUpdate(removeEvent); }
private void removeElements(final Element parent, final int startRemoveIndex, final int numRemoved) { final Element[] emptyArray = new Element[0]; final Element[] removedArray = new Element[numRemoved]; final BranchElement branch = (BranchElement)parent; final int numElements = branch.getElementCount(); for (int i = 0; i < numRemoved; i++) { removedArray[i] = branch.getElement(startRemoveIndex + i); } branch.replace(startRemoveIndex, numRemoved, emptyArray); final int eventLength = removedArray[numRemoved - 1].getEndOffset() - removedArray[0].getStartOffset(); DefaultDocumentEvent removeEvent = new DefaultDocumentEvent(removedArray[0].getStartOffset(), eventLength, EventType.REMOVE); removeEvent.addEdit(new ElementEdit(parent, startRemoveIndex, removedArray, emptyArray)); fireRemoveUpdate(removeEvent); }
/** * As if attributes are changed in the range 7-18: * the second paragraph (6-15), and * the third one (15, 19). * <code>updateChilren</code> returns <code>true</code> * (child views represent entire elements). */ public void testChangedUpdate04() throws BadLocationException { hasChildren = true; changeDocument(); updateChildrenReturn = true; Element prevLastLine = root.getElement(root.getElementCount() - 2); docEvent = ((AbstractDocument) doc).new DefaultDocumentEvent(line.getStartOffset() + 1, prevLastLine.getEndOffset() - 2 - line.getStartOffset(), EventType.CHANGE); view.changedUpdate(docEvent, rect, viewFactory); assertFalse(updateChildrenCalled); assertTrue(forwardUpdateCalled); assertTrue(forwardUpdateToViewCalled); assertEquals(2, viewsForwardedTo.size()); for (int i = 0; i < viewsForwardedTo.size(); i++) { assertSame("@ " + i, view.getView(i + 1), viewsForwardedTo.get(i)); } assertTrue(updateLayoutCalled); }
/** * As if attributes are changed in the range 7-18: * the second paragraph (6-15), and * the third one (15, 19). * <code>updateChilren</code> returns <code>false</code> * (child views represent entire elements). */ public void testChangedUpdate05() throws BadLocationException { hasChildren = true; changeDocument(); updateChildrenReturn = false; Element prevLastLine = root.getElement(root.getElementCount() - 2); docEvent = ((AbstractDocument) doc).new DefaultDocumentEvent(line.getStartOffset() + 1, prevLastLine.getEndOffset() - 2 - line.getStartOffset(), EventType.CHANGE); view.changedUpdate(docEvent, rect, viewFactory); assertFalse(updateChildrenCalled); assertTrue(forwardUpdateCalled); assertTrue(forwardUpdateToViewCalled); assertEquals(2, viewsForwardedTo.size()); for (int i = 0; i < viewsForwardedTo.size(); i++) { assertSame("@ " + i, view.getView(i + 1), viewsForwardedTo.get(i)); } assertTrue(updateLayoutCalled); }
/** * Inserting text into the start of a paragraph with the same attributes. * <p> * This test is equivalent to * <code>doc.insertString(insertOffset, caps, null)</code>, * where <code>insertOffset = paragraph.getEndOffset()</code>. */ public void testInsertSameAttrsParStart() throws Exception { insertOffset = paragraph.getEndOffset(); // doc.insertString(insertOffset, caps, null); content.insertString(insertOffset, caps); event = doc.new DefaultDocumentEvent(insertOffset, capsLen, EventType.INSERT); ElementSpec[] specs = { new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType), new ElementSpec(null, ElementSpec.ContentType, capsLen), }; specs[1].setDirection(ElementSpec.JoinNextDirection); specs[2].setDirection(ElementSpec.JoinNextDirection); buf.insert(insertOffset, capsLen, specs, event); List<?> edits = getEdits(event); assertEquals(2, edits.size()); assertChange(edits.get(0), new int[] { 15, 19 }, new int[] { 15, 16 }); assertChange(edits.get(1), new int[] { 19, 24 }, new int[] { 16, 24 }); assertChildren(paragraph, new int[] { 0, 5, 5, 9, 9, 15, 15, 16 }, new AttributeSet[] { null, bold, italic, null }); assertChildren(root.getElement(1), new int[] { 16, 24 }, new AttributeSet[] { null }); assertEquals("^^^text\n", getText(doc.getCharacterElement(insertOffset))); }
/** * Inserting text into the start of a paragraph with different attributes. * <p> * This test is equivalent to * <code>doc.insertString(insertOffset, caps, italic)</code>, * where <code>insertOffset = paragraph.getEndOffset()</code>. */ public void testInsertDiffAttrsParStart() throws Exception { insertOffset = paragraph.getEndOffset(); // doc.insertString(insertOffset, caps, italic); content.insertString(insertOffset, caps); event = doc.new DefaultDocumentEvent(insertOffset, capsLen, EventType.INSERT); ElementSpec[] specs = { new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType), new ElementSpec(italic, ElementSpec.ContentType, capsLen), }; specs[1].setDirection(ElementSpec.JoinNextDirection); buf.insert(insertOffset, capsLen, specs, event); List<?> edits = getEdits(event); assertEquals(2, edits.size()); assertChange(edits.get(0), new int[] { 15, 19 }, new int[] { 15, 16 }); assertChange(edits.get(1), new int[] {}, new int[] { 16, 19 }); assertChildren(paragraph, new int[] { 0, 5, 5, 9, 9, 15, 15, 16 }, new AttributeSet[] { null, bold, italic, null }); assertChildren(root.getElement(1), new int[] { 16, 19, 19, 24 }, new AttributeSet[] { italic, null }); assertEquals("^^^", getText(doc.getCharacterElement(insertOffset))); assertEquals("text\n", getText(doc.getCharacterElement(insertOffset + capsLen))); }
/** * Inserting text into the end of the document with different attributes. * <p> * This test is equivalent to * <code>doc.insertString(insertOffset, caps, italic)</code>, * where <code>insertOffset = doc.getLength()</code>. */ public void testInsertDiffAttrsDocEnd() throws Exception { insertOffset = doc.getLength(); // doc.insertString(insertOffset, caps, italic); content.insertString(insertOffset, caps); event = doc.new DefaultDocumentEvent(insertOffset, capsLen, EventType.INSERT); ElementSpec spec = new ElementSpec(italic, ElementSpec.ContentType, capsLen); buf.insert(insertOffset, capsLen, new ElementSpec[] { spec }, event); List<?> edits = getEdits(event); assertEquals(1, edits.size()); assertChange(edits.get(0), new int[] { 16, 24 }, new int[] { 16, 20, 20, 23, 23, 24 }); assertChildren(root.getElement(1), new int[] { 16, 20, 20, 23, 23, 24 }, new AttributeSet[] { null, italic, null }); assertEquals("text", getText(doc.getCharacterElement(insertOffset - 1))); assertEquals("^^^", getText(doc.getCharacterElement(insertOffset))); assertEquals("\n", getText(doc.getCharacterElement(insertOffset + capsLen))); }
public void testInsertAfterEnd_Events() throws Exception { Element root = htmlDoc.getDefaultRootElement(); Element body = root.getElement(0); Element p = body.getElement(0); addElement(); htmlDoc.setParser(new ParserDelegator()); DocumentController listener = new DocumentController(); htmlDoc.addDocumentListener(listener); htmlDoc.insertAfterEnd(p, "<a>link</a><b>bold</b>"); assertEquals(1, listener.getNumEvents()); checkEvent(body, listener.getEvent(0), DocumentEvent.EventType.INSERT, 3, 8, 4); listener.reset(); htmlDoc.insertAfterEnd(body, "<a>link</a><b>bold</b>"); assertEquals(1, listener.getNumEvents()); checkEvent(root, listener.getEvent(0), DocumentEvent.EventType.INSERT, 3, 8, 12); }
public void testInsertAfterStart_Events() throws Exception { Element root = htmlDoc.getDefaultRootElement(); Element body = root.getElement(0); Element p = body.getElement(0); addElement(); htmlDoc.setParser(new ParserDelegator()); DocumentController listener = new DocumentController(); htmlDoc.addDocumentListener(listener); htmlDoc.insertAfterStart(p, "<a>link</a><b>bold</b>"); assertEquals(1, listener.getNumEvents()); checkEvent(p, listener.getEvent(0), DocumentEvent.EventType.INSERT, 3, 8, 0); listener.reset(); htmlDoc.insertAfterStart(body, "<a>link</a><b>bold</b>"); assertEquals(1, listener.getNumEvents()); if (!isHarmony()) { checkEvent(body, listener.getEvent(0), DocumentEvent.EventType.INSERT, 3, 8, 0); } else { checkEvent(body, listener.getEvent(0), DocumentEvent.EventType.INSERT, 2, 8, 0); } }
public void testInsertBeforeEnd_Events() throws Exception { Element root = htmlDoc.getDefaultRootElement(); Element body = root.getElement(0); Element p = body.getElement(0); addElement(); htmlDoc.setParser(new ParserDelegator()); DocumentController listener = new DocumentController(); htmlDoc.addDocumentListener(listener); htmlDoc.insertBeforeEnd(p, "<a>link</a><b>bold</b>"); assertEquals(1, listener.getNumEvents()); checkEvent(p, listener.getEvent(0), DocumentEvent.EventType.INSERT, 3, 8, 4); listener.reset(); htmlDoc.insertBeforeEnd(body, "<a>link</a><b>bold</b>"); assertEquals(1, listener.getNumEvents()); checkEvent(body, listener.getEvent(0), DocumentEvent.EventType.INSERT, 2, 8, 13); }
public void testInsertBeforeStart_Events() throws Exception { Element root = htmlDoc.getDefaultRootElement(); Element branch1 = root.getElement(0); Element branch2 = branch1.getElement(0); addElement(); htmlDoc.setParser(new ParserDelegator()); DocumentController listener = new DocumentController(); htmlDoc.addDocumentListener(listener); htmlDoc.insertBeforeStart(branch2, "<a>link</a><b>bold</b>"); assertEquals(1, listener.getNumEvents()); if (!isHarmony()) { checkEvent(branch1, listener.getEvent(0), DocumentEvent.EventType.INSERT, 3, 8, 0); } else { checkEvent(branch1, listener.getEvent(0), DocumentEvent.EventType.INSERT, 2, 8, 0); } listener.reset(); htmlDoc.insertBeforeStart(branch1, "<a>link</a><b>bold</b>"); assertEquals(1, listener.getNumEvents()); if (!isHarmony()) { checkEvent(root, listener.getEvent(0), DocumentEvent.EventType.INSERT, 3, 8, 0); } else { checkEvent(root, listener.getEvent(0), DocumentEvent.EventType.INSERT, 2, 8, 0); } }
public void testSetInnerHTML_Events() throws Exception { addElements(); Element root = htmlDoc.getDefaultRootElement(); Element branch1 = root.getElement(0); final Element branch2 = branch1.getElement(0); Element branch3 = branch2.getElement(0); htmlDoc.setParser(new ParserDelegator()); DocumentController listener = new DocumentController(); htmlDoc.addDocumentListener(listener); htmlDoc.setInnerHTML(branch2, "<a>link</a><b>bold</b>"); assertEquals(2, listener.getNumEvents()); checkEvent(branch2, listener.getEvent(0), DocumentEvent.EventType.INSERT, 4, 9, 0); checkEvent(branch2, listener.getEvent(1), DocumentEvent.EventType.REMOVE, 4, 14, 8); listener.reset(); htmlDoc.setInnerHTML(branch1, "<a>link</a><b>bold</b>"); assertEquals(2, listener.getNumEvents()); checkEvent(branch1, listener.getEvent(0), DocumentEvent.EventType.INSERT, 4, 9, 0); checkEvent(branch1, listener.getEvent(1), DocumentEvent.EventType.REMOVE, 2, 10, 8); }
/** * Inserting text with the same attributes into the beginning of * the document. * <p> * This test is equivalent to * <code>doc.insertString(insertOffset, newLine, null)</code>, * where <code>insertOffset = 0</code>. */ public void testInsertSameAttrsDocStart() throws Exception { insertOffset = 0; // doc.insertString(insertOffset, newLine, null); content.insertString(insertOffset, newLine); event = doc.new DefaultDocumentEvent(insertOffset, newLineLen, EventType.INSERT); ElementSpec[] specs = { new ElementSpec(null, ElementSpec.ContentType, newLineLen), new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType) }; specs[0].setDirection(ElementSpec.JoinPreviousDirection); specs[2].setDirection(ElementSpec.JoinFractureDirection); buf.insert(insertOffset, newLineLen, specs, event); List<?> edits = getEdits(event); assertEquals(2, edits.size()); assertChange(edits.get(0), new int[] { 0, 6, 6, 10, 10, 16, 16, 17 }, new int[] { 0, 1 }); assertChange(edits.get(1), new int[] {}, new int[] { 1, 17 }); assertChildren(root.getElement(0), new int[] { 0, 1 }, new AttributeSet[] { null }); assertChildren(root.getElement(1), new int[] { 1, 6, 6, 10, 10, 16, 16, 17 }, new AttributeSet[] { null, bold, italic, null }); assertEquals("\n", getText(doc.getCharacterElement(insertOffset))); assertEquals("plain", getText(doc.getCharacterElement(insertOffset + newLineLen))); }
/** * Inserting text with different attributes into the beginning of * the document. * <p> * This test is equivalent to * <code>doc.insertString(insertOffset, newLine, italic)</code>, * where <code>insertOffset = 0</code>. */ public void testInsertDiffAttrsDocStart() throws Exception { insertOffset = 0; // doc.insertString(insertOffset, newLine, italic); content.insertString(insertOffset, newLine); event = doc.new DefaultDocumentEvent(insertOffset, newLineLen, EventType.INSERT); ElementSpec[] specs = { new ElementSpec(italic, ElementSpec.ContentType, newLineLen), new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType) }; specs[2].setDirection(ElementSpec.JoinFractureDirection); buf.insert(insertOffset, newLineLen, specs, event); List<?> edits = getEdits(event); assertEquals(2, edits.size()); assertChange(edits.get(0), new int[] { 0, 6, 6, 10, 10, 16, 16, 17 }, new int[] { 0, 1 }); assertChange(edits.get(1), new int[] {}, new int[] { 1, 17 }); assertChildren(root.getElement(0), new int[] { 0, 1 }, new AttributeSet[] { italic }); assertChildren(root.getElement(1), new int[] { 1, 6, 6, 10, 10, 16, 16, 17 }, new AttributeSet[] { null, bold, italic, null }); assertEquals("\n", getText(doc.getCharacterElement(insertOffset))); assertEquals("plain", getText(doc.getCharacterElement(insertOffset + newLineLen))); }
/** * Inserting text into middle of an element with the same attributes. * <p> * This test is equivalent to * <code>doc.insertString(insertOffset, newLine, bold)</code>, * where <code>insertOffset</code> has default value from * <code>setUp()</code>. */ public void testInsertSameAttrsMiddle() throws Exception { // doc.insertString(insertOffset, newLine, bold); content.insertString(insertOffset, newLine); event = doc.new DefaultDocumentEvent(insertOffset, newLineLen, EventType.INSERT); ElementSpec[] specs = { new ElementSpec(null, ElementSpec.ContentType, newLineLen), new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType) }; specs[0].setDirection(ElementSpec.JoinPreviousDirection); specs[2].setDirection(ElementSpec.JoinFractureDirection); buf.insert(insertOffset, newLineLen, specs, event); List<?> edits = getEdits(event); assertEquals(2, edits.size()); assertChange(edits.get(0), new int[] { 5, 10, 10, 16, 16, 17 }, new int[] { 5, 8 }); assertChange(edits.get(1), new int[] {}, new int[] { 8, 17 }); assertChildren(root.getElement(0), new int[] { 0, 5, 5, 8 }, new AttributeSet[] { null, bold }); assertChildren(root.getElement(1), new int[] { 8, 10, 10, 16, 16, 17 }, new AttributeSet[] { bold, italic, null }); assertEquals("bo\n", getText(doc.getCharacterElement(insertOffset))); assertEquals("ld", getText(doc.getCharacterElement(insertOffset + newLineLen))); }
/** * Inserting text into end of the an element with the same attributes; * the following element has different attributes. * <p> * This test is equivalent to * <code>doc.insertString(insertOffset, newLine, bold)</code>, * where 2 is added to default value of <code>insertOffset</code>. */ public void testInsertSameAttrsEnd() throws Exception { insertOffset += 2; // doc.insertString(insertOffset, newLine, bold); content.insertString(insertOffset, newLine); event = doc.new DefaultDocumentEvent(insertOffset, newLineLen, EventType.INSERT); ElementSpec[] specs = { new ElementSpec(null, ElementSpec.ContentType, newLineLen), new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType) }; specs[0].setDirection(ElementSpec.JoinPreviousDirection); specs[2].setDirection(ElementSpec.JoinFractureDirection); // Spec [0] has wrong attributes (should be bold) but everything works // the way it supposed to. buf.insert(insertOffset, newLineLen, specs, event); List<?> edits = getEdits(event); assertEquals(2, edits.size()); assertChange(edits.get(0), new int[] { 10, 16, 16, 17 }, new int[] {}); assertChange(edits.get(1), new int[] {}, new int[] { 10, 17 }); assertChildren(root.getElement(0), new int[] { 0, 5, 5, 10 }, new AttributeSet[] { null, bold }); assertChildren(root.getElement(1), new int[] { 10, 16, 16, 17 }, new AttributeSet[] { italic, null }); assertEquals("bold\n", getText(doc.getCharacterElement(insertOffset))); assertEquals("italic", getText(doc.getCharacterElement(insertOffset + newLineLen))); }
/** * Inserting text into the middle of an element with different attributes. * <p> * This test is equivalent to * <code>doc.insertString(insertOffset, newLine, null)</code>, * where <code>insertOffset</code> has default value from * <code>setUp()</code>. */ public void testInsertDiffAttrsMiddle() throws Exception { // doc.insertString(insertOffset, newLine, null); content.insertString(insertOffset, newLine); event = doc.new DefaultDocumentEvent(insertOffset, newLineLen, EventType.INSERT); ElementSpec[] specs = { new ElementSpec(null, ElementSpec.ContentType, newLineLen), new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType) }; specs[2].setDirection(ElementSpec.JoinFractureDirection); buf.insert(insertOffset, newLineLen, specs, event); List<?> edits = getEdits(event); assertEquals(2, edits.size()); assertChange(edits.get(0), new int[] { 5, 10, 10, 16, 16, 17 }, new int[] { 5, 7, 7, 8 }); assertChange(edits.get(1), new int[] {}, new int[] { 8, 17 }); assertChildren(root.getElement(0), new int[] { 0, 5, 5, 7, 7, 8 }, new AttributeSet[] { null, bold, null }); assertChildren(root.getElement(1), new int[] { 8, 10, 10, 16, 16, 17 }, new AttributeSet[] { bold, italic, null }); assertEquals("\n", getText(doc.getCharacterElement(insertOffset))); assertEquals("ld", getText(doc.getCharacterElement(insertOffset + newLineLen))); }
/** * Inserting text into element boundary; the text and both elements have * different attributes. * <p> * This test is equivalent to * <code>doc.insertString(insertOffset, newLine, null)</code>, * where 2 is added to default value of <code>insertOffset</code>. */ public void testInsertDiffAttrsEnd() throws Exception { insertOffset += 2; // doc.insertString(insertOffset, newLine, null); content.insertString(insertOffset, newLine); event = doc.new DefaultDocumentEvent(insertOffset, newLineLen, EventType.INSERT); ElementSpec[] specs = { new ElementSpec(null, ElementSpec.ContentType, newLineLen), new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType) }; specs[2].setDirection(ElementSpec.JoinFractureDirection); buf.insert(insertOffset, newLineLen, specs, event); List<?> edits = getEdits(event); assertEquals(2, edits.size()); assertChange(edits.get(0), new int[] { 5, 10, 10, 16, 16, 17 }, new int[] { 5, 9, 9, 10 }); assertChange(edits.get(1), new int[] {}, new int[] { 10, 17 }); assertChildren(root.getElement(0), new int[] { 0, 5, 5, 9, 9, 10 }, new AttributeSet[] { null, bold, null }); assertChildren(root.getElement(1), new int[] { 10, 16, 16, 17 }, new AttributeSet[] { italic, null }); assertEquals("\n", getText(doc.getCharacterElement(insertOffset))); assertEquals("italic", getText(doc.getCharacterElement(insertOffset + newLineLen))); }
/** * Inserting text into element boundary; the attributes of the text and * the following element are the same, the attributes of the previous * element are different. * <p> * This test is equivalent to * <code>doc.insertString(insertOffset, newLine, italic)</code>, * where 2 is added to default value of <code>insertOffset</code>. */ public void testInsertDiffSameAttrsEnd() throws Exception { insertOffset += 2; // doc.insertString(insertOffset, newLine, italic); content.insertString(insertOffset, newLine); event = doc.new DefaultDocumentEvent(insertOffset, newLineLen, EventType.INSERT); ElementSpec[] specs = { new ElementSpec(italic, ElementSpec.ContentType, newLineLen), new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType) }; specs[2].setDirection(ElementSpec.JoinFractureDirection); buf.insert(insertOffset, newLineLen, specs, event); List<?> edits = getEdits(event); assertEquals(2, edits.size()); assertChange(edits.get(0), new int[] { 5, 10, 10, 16, 16, 17 }, new int[] { 5, 9, 9, 10 }); assertChange(edits.get(1), new int[] {}, new int[] { 10, 17 }); assertChildren(root.getElement(0), new int[] { 0, 5, 5, 9, 9, 10 }, new AttributeSet[] { null, bold, italic }); assertChildren(root.getElement(1), new int[] { 10, 16, 16, 17 }, new AttributeSet[] { italic, null }); assertEquals("\n", getText(doc.getCharacterElement(insertOffset))); assertEquals("italic", getText(doc.getCharacterElement(insertOffset + newLineLen))); }
/** * Inserting text into the start of a paragraph with the same attributes. * <p> * This test is equivalent to * <code>doc.insertString(insertOffset, newLine, null)</code>, * where <code>insertOffset = paragraph.getEndOffset()</code>. */ public void testInsertSameAttrsParStart() throws Exception { insertOffset = paragraph.getEndOffset(); // doc.insertString(insertOffset, newLine, null); content.insertString(insertOffset, newLine); event = doc.new DefaultDocumentEvent(insertOffset, newLineLen, EventType.INSERT); ElementSpec[] specs = { new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType), new ElementSpec(null, ElementSpec.ContentType, newLineLen), new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType) }; specs[4].setDirection(ElementSpec.JoinNextDirection); buf.insert(insertOffset, newLineLen, specs, event); List<?> edits = getEdits(event); assertEquals(3, edits.size()); assertChange(edits.get(0), new int[] { 15, 17 }, new int[] { 15, 16 }); assertChange(edits.get(1), new int[] {}, new int[] { 16, 17 }); assertChange(edits.get(2), new int[] {}, new int[] { 16, 17 }); assertChildren(root.getElement(0), new int[] { 0, 5, 5, 9, 9, 15, 15, 16 }, new AttributeSet[] { null, bold, italic, null }); assertChildren(root.getElement(1), new int[] { 16, 17 }, new AttributeSet[] { null }); assertChildren(root.getElement(2), new int[] { 17, 22 }, new AttributeSet[] { null }); assertEquals("\n", getText(doc.getCharacterElement(insertOffset))); assertEquals("text\n", getText(doc.getCharacterElement(insertOffset + newLineLen))); }
/** * Inserting text into the start of a paragraph with different attributes. * <p> * This test is equivalent to * <code>doc.insertString(insertOffset, newLine, italic)</code>, * where <code>insertOffset = paragraph.getEndOffset()</code>. */ public void testInsertDiffAttrsParStart() throws Exception { insertOffset = paragraph.getEndOffset(); // doc.insertString(insertOffset, newLine, italic); content.insertString(insertOffset, newLine); event = doc.new DefaultDocumentEvent(insertOffset, newLineLen, EventType.INSERT); ElementSpec[] specs = { new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType), new ElementSpec(italic, ElementSpec.ContentType, newLineLen), new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType) }; specs[4].setDirection(ElementSpec.JoinNextDirection); buf.insert(insertOffset, newLineLen, specs, event); List<?> edits = getEdits(event); assertEquals(3, edits.size()); assertChange(edits.get(0), new int[] { 15, 17 }, new int[] { 15, 16 }); assertChange(edits.get(1), new int[] {}, new int[] { 16, 17 }); assertChange(edits.get(2), new int[] {}, new int[] { 16, 17 }); assertChildren(root.getElement(0), new int[] { 0, 5, 5, 9, 9, 15, 15, 16 }, new AttributeSet[] { null, bold, italic, null }); assertChildren(root.getElement(1), new int[] { 16, 17 }, new AttributeSet[] { italic }); assertChildren(root.getElement(2), new int[] { 17, 22 }, new AttributeSet[] { null }); assertEquals("\n", getText(doc.getCharacterElement(insertOffset))); assertEquals("text\n", getText(doc.getCharacterElement(insertOffset + newLineLen))); }
/** * Inserting text into the end of the document with the same attributes. * <p> * This test is equivalent to * <code>doc.insertString(insertOffset, newLine, null)</code>, * where <code>insertOffset = doc.getLength()</code>. */ public void testInsertSameAttrsDocEnd() throws Exception { insertOffset = doc.getLength(); // doc.insertString(insertOffset, newLine, null); content.insertString(insertOffset, newLine); event = doc.new DefaultDocumentEvent(insertOffset, newLineLen, EventType.INSERT); ElementSpec[] specs = { new ElementSpec(null, ElementSpec.ContentType, newLineLen), new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType) }; specs[0].setDirection(ElementSpec.JoinPreviousDirection); specs[2].setDirection(ElementSpec.JoinFractureDirection); buf.insert(insertOffset, newLineLen, specs, event); List<?> edits = getEdits(event); assertEquals(2, edits.size()); assertChange(edits.get(0), new int[] { 16, 22 }, new int[] { 16, 21 }); assertChange(edits.get(1), new int[] {}, new int[] { 21, 22 }); assertChildren(root.getElement(1), new int[] { 16, 21 }, new AttributeSet[] { null }); assertChildren(root.getElement(2), new int[] { 21, 22 }, new AttributeSet[] { null }); assertEquals("text\n", getText(doc.getCharacterElement(insertOffset))); assertEquals("\n", getText(doc.getCharacterElement(insertOffset + newLineLen))); }
/** * Inserting text into the end of the document with different attributes. * <p> * This test is equivalent to * <code>doc.insertString(insertOffset, newLine, italic)</code>, * where <code>insertOffset = doc.getLength()</code>. */ public void testInsertDiffAttrsDocEnd() throws Exception { insertOffset = doc.getLength(); // doc.insertString(insertOffset, newLine, italic); content.insertString(insertOffset, newLine); event = doc.new DefaultDocumentEvent(insertOffset, newLineLen, EventType.INSERT); ElementSpec[] specs = { new ElementSpec(italic, ElementSpec.ContentType, newLineLen), new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType) }; specs[2].setDirection(ElementSpec.JoinFractureDirection); buf.insert(insertOffset, newLineLen, specs, event); List<?> edits = getEdits(event); assertEquals(2, edits.size()); assertChange(edits.get(0), new int[] { 16, 22 }, new int[] { 16, 20, 20, 21 }); assertChange(edits.get(1), new int[] {}, new int[] { 21, 22 }); assertChildren(root.getElement(1), new int[] { 16, 20, 20, 21 }, new AttributeSet[] { null, italic }); assertChildren(root.getElement(2), new int[] { 21, 22 }, new AttributeSet[] { null }); assertEquals("text", getText(doc.getCharacterElement(insertOffset - 1))); assertEquals("\n", getText(doc.getCharacterElement(insertOffset))); assertEquals("\n", getText(doc.getCharacterElement(insertOffset + newLineLen))); }
/** * Whenever an UndoableEdit happens the edit will either be absorbed by the * current compound edit or a new compound edit will be started */ @Override public void undoableEditHappened(final UndoableEditEvent e) { final boolean prog = document.isProgrammatic(); final DefaultDocumentEvent edit = (DefaultDocumentEvent)e.getEdit(); if (compoundEdit == null) // start a new compound edit compoundEdit = startCompoundEdit(edit); else if (edit.getType() == EventType.CHANGE) compoundEdit.addEdit(edit); else if (lastProgrammatic && prog || !lastProgrammatic && !prog && isIncremental(edit)) // append to existing edit compoundEdit.addEdit(edit, document.getTextPane() .getCaretPosition()); else { // close this compound edit and start a new one compoundEdit.end(); compoundEdit = startCompoundEdit(edit); } lastProgrammatic = prog; updateCursorPosition(); }