public void testGetBidiRootElement() { Element root = doc.getBidiRootElement(); assertNotNull(root); assertTrue(root instanceof BranchElement); assertEquals("bidi root", root.getName()); assertEquals(0, root.getStartOffset()); assertEquals(1, root.getEndOffset()); Enumeration<?> elements = ((BranchElement) root).children(); Element element = null; int count = 0; while (elements.hasMoreElements()) { count++; element = (Element) elements.nextElement(); } // if the document is empty there should be only one child assertEquals(1, count); assertTrue(element instanceof LeafElement); assertEquals("bidi level", element.getName()); assertSame(AbstractDocument.BidiElementName, element.getName()); assertEquals(0, element.getStartOffset()); assertEquals(1, element.getEndOffset()); }
@Override protected void setUp() throws Exception { super.setUp(); doc = new DefaultStyledDocument(); doc.insertString(0, "this is the test text with some long words in p2\n" // | | // 0123456789012345678901234567890123456789012345678 // 0 1 2 3 4 + "Long words: internationalization, localization", null); root = doc.getDefaultRootElement(); p1 = root.getElement(0); // Break the first paragraph into several doc.writeLock(); Element[] leaves = new Element[] { doc.createLeafElement(p1, null, 0, 17), doc.createLeafElement(p1, null, 17, 32), doc.createLeafElement(p1, null, 32, 49) }; ((BranchElement) p1).replace(0, p1.getElementCount(), leaves); doc.writeUnlock(); p1L = p1.getElement(0); // Initialize the view view = new FlowViewImplWithFactory(p1, Y_AXIS, new PartFactory()); strategy = view.strategy = new TestStrategy(); view.loadChildren(null); row = view.createRow(); view.append(row); }
/** * Test getElementIndex behavior if some elements are zero-length, * i.e. start and end offsets are the same. */ public void testGetElementIndex02() { BranchElement root = doc.new BranchElement(null, null); LeafElement[] leaves = { doc.new LeafElement(root, null, 0, 0), // [0] doc.new LeafElement(root, null, 0, 1), // [1] doc.new LeafElement(root, null, 0, 1), // [2] doc.new LeafElement(root, null, 1, 1), // [3] doc.new LeafElement(root, null, 1, 1), // [4] doc.new LeafElement(root, null, 1, 2), // [5] doc.new LeafElement(root, null, 2, 3) // [6] }; root.replace(0, 0, leaves); assertEquals(0, root.getElementIndex(-1)); assertEquals(1, root.getElementIndex(0)); assertEquals(5 /*2*/, root.getElementIndex(1)); assertEquals(6, root.getElementIndex(2)); assertEquals(6, root.getElementIndex(3)); assertEquals(6, root.getElementIndex(4)); }
/** * Tests getElementIndex behavior when there are no child elements, but * <code>getStartOffset</code> and <code>getEndOffset</code> are overridden * to prevent <code>{@link NullPointerException}</code>. */ // Regression for HARMONY-2756 public void testGetElementIndex06() { par = doc.new BranchElement(null, null) { private static final long serialVersionUID = 1L; @Override public int getStartOffset() { return 10; } @Override public int getEndOffset() { return 20; } }; assertEquals(0, par.getElementIndex(-1)); assertEquals(0, par.getElementIndex(0)); assertEquals(0, par.getElementIndex(10)); assertEquals(0, par.getElementIndex(15)); assertEquals(0, par.getElementIndex(20)); assertEquals(0, par.getElementIndex(25)); }
/** * Generic assertions upon element return by createDefaultRoot. */ public void testCreateDefaultRoot01() throws BadLocationException { Element root = doc.createDefaultRoot(); assertTrue(root instanceof BranchElement); assertEquals(AbstractDocument.ParagraphElementName, root.getName()); assertEquals(1, root.getElementCount()); checkOffsets(root.getElement(0), 0, 1); doc.insertString(0, paragraphs, null); root = doc.createDefaultRoot(); assertEquals(1, root.getElementCount()); if (BasicSwingTestCase.isHarmony()) { checkOffsets(root.getElement(0), doc.getStartPosition().getOffset(), doc .getEndPosition().getOffset()); } else { checkOffsets(root.getElement(0), 0, 1); } }
/** * Tests <code>breakView</code> uses <code>createFragment</code> to * produce the part. */ public void testBreakViewCreate() throws Exception { view = new GlyphView(root) { @Override public View createFragment(int startOffset, int endOffset) { final Element part = ((AbstractDocument) doc).new LeafElement(null, null, startOffset, endOffset); return new LabelView(part); }; }; final float width = metrics.stringWidth(FULL_TEXT.substring(0, 2)); final View fragment = view.breakView(X_AXIS, 0, 0, width); assertTrue(fragment instanceof LabelView); assertFalse(view instanceof LabelView); assertEquals(view.getStartOffset(), fragment.getStartOffset()); assertEquals(view.getStartOffset() + 2, fragment.getEndOffset()); assertTrue(fragment.getElement() instanceof LeafElement); assertTrue(view.getElement() instanceof BranchElement); }
public void setBreakpointToEOLAt(int byteOffset, int lineNumber) { final StyledDocument doc = (StyledDocument)editPane.getDocument(); final Element paragraphElement = doc.getParagraphElement(byteOffset); if (paragraphElement.getClass() == BranchElement.class) { final SimpleAttributeSet sas = new SimpleAttributeSet(); StyleConstants.setBackground(sas, Color.cyan); // Look for ending delimiter int length = 1; try { for (int i = byteOffset; ; i++) { if (i >= doc.getLength()) { length = i - byteOffset + 1; break; } else if (doc.getText(i, 1).equals("\n")) { length = i - byteOffset; break; } } } catch (BadLocationException ble) { length = 0; } if (0 < length) { doc.setCharacterAttributes(byteOffset, length, sas, false); } } }
@Override protected void setUp() throws Exception { super.setUp(); doc = new DefaultStyledDocument(); root = doc.getDefaultRootElement(); buf = new DefStyledDoc_Helpers.ElementBufferWithLogging(doc, root); doc.buffer = buf; paragraph = root.getElement(0); content = doc.getContent(); content.insertString(0, "plainbolditalic\ntext"); // Create the structure equivalent to this sequence: //doc.insertString(doc.getLength(), "plain", null); // 5 chars //doc.insertString(doc.getLength(), "bold", bold); // 4 chars //doc.insertString(doc.getLength(), "italic", italic); // 6 chars //doc.insertString(doc.getLength(), "\ntext", null); // 5 chars doc.writeLock(); // Write lock needed to modify document structure Element[] leaves = new Element[4]; leaves[0] = doc.createLeafElement(paragraph, null, 0, 5); leaves[1] = doc.createLeafElement(paragraph, bold, 5, 9); leaves[2] = doc.createLeafElement(paragraph, italic, 9, 15); leaves[3] = doc.createLeafElement(paragraph, null, 15, 16); ((BranchElement) paragraph).replace(0, 1, leaves); BranchElement branch = (BranchElement) doc.createBranchElement(root, null); leaves = new Element[1]; leaves[0] = doc.createLeafElement(branch, null, 16, 21); branch.replace(0, 0, leaves); ((BranchElement) root).replace(1, 0, new Element[] { branch }); }
public void testDefaultStyledDocumentContentStyleContext() { StyleContext styles = new StyleContext(); doc = new DefaultStyledDocument(new GapContent(10), styles); assertEquals(10, ((GapContent) doc.getContent()).getArrayLength()); Element root = doc.getDefaultRootElement(); assertTrue(root instanceof SectionElement); assertEquals(1, root.getElementCount()); Element child = root.getElement(0); assertTrue(child instanceof BranchElement); assertEquals(1, child.getElementCount()); assertTrue(child.getElement(0) instanceof LeafElement); assertSame(styles, doc.getAttributeContext()); assertSame(styles.getStyle(StyleContext.DEFAULT_STYLE), child.getAttributes() .getResolveParent()); }
public void testCreateDefaultRoot() { AbstractElement defRoot = doc.createDefaultRoot(); assertTrue(defRoot instanceof SectionElement); assertEquals(0, defRoot.getAttributeCount()); assertEquals(1, defRoot.getElementCount()); assertTrue(defRoot.getElement(0) instanceof BranchElement); }
public void testGetNameBranch() throws Exception { final String name = "Ast"; attrs.addAttribute(AbstractDocument.ElementNameAttribute, name); element = doc.new BranchElement(root, attrs); assertSame(name, element.getName()); assertSame(root, element.getParentElement()); }
public void testGetNameParent() throws Exception { final String parentName = "parentName"; attrs.addAttribute(AbstractDocument.ElementNameAttribute, parentName); BranchElement parent = doc.new BranchElement(null, attrs); AbstractElement element = new AbstractElementImpl(doc, parent, null); assertTrue(parent.isDefined(AbstractDocument.ElementNameAttribute)); assertEquals(parentName, parent.getName()); assertFalse(element.isDefined(AbstractDocument.ElementNameAttribute)); assertNull(element.getName()); }
@Override protected void setUp() throws Exception { super.setUp(); doc = new DefaultStyledDocument(); root = doc.getDefaultRootElement(); buf = new DefStyledDoc_Helpers.ElementBufferWithLogging(doc, root); doc.buffer = buf; paragraph = root.getElement(0); content = doc.getContent(); content.insertString(0, "plainbolditalic\ntext"); // Create the structure equivalent to this sequence: //doc.insertString(doc.getLength(), "plain", null); // 5 chars //doc.insertString(doc.getLength(), "bold", bold); // 4 chars //doc.insertString(doc.getLength(), "italic", italic); // 6 chars //doc.insertString(doc.getLength(), "\ntext", null); // 5 chars doc.writeLock(); // Write lock needed to modify document structure Element[] leaves = new Element[4]; leaves[0] = doc.createLeafElement(paragraph, null, 0, 5); leaves[1] = doc.createLeafElement(paragraph, bold, 5, 9); leaves[2] = doc.createLeafElement(paragraph, italic, 9, 15); leaves[3] = doc.createLeafElement(paragraph, null, 15, 16); ((BranchElement) paragraph).replace(0, 1, leaves); BranchElement branch = (BranchElement) doc.createBranchElement(root, null); leaves = new Element[1]; leaves[0] = doc.createLeafElement(branch, null, 16, 21); branch.replace(0, 0, leaves); branch.addAttributes(boldFalse); branch.addAttributes(italicFalse); // Add this branch to the root ((BranchElement) root).replace(1, 0, new Element[] { branch }); }
@Override protected void setUp() throws Exception { super.setUp(); StyleContextTest.sc = StyleContext.getDefaultStyleContext(); as = new AttributeSet[] { StyleContextTest.addAttribute(1), StyleContextTest.addAttribute(null, 2, 2), StyleContextTest.addAttribute(null, 5, 2) }; doc = new DisAbstractedDocument(new GapContent()); doc.insertString(0, "0123456789", as[0]); doc.writeLock(); BranchElement branch = doc.new BranchElement(null, as[1]); leaf1 = doc.new LeafElement(null, as[2], 0, 3); leaf2 = doc.new LeafElement(branch, as[2], 5, 8); doc.writeUnlock(); }
@Override protected void setUp() throws Exception { super.setUp(); doc = new DefaultStyledDocument(); root = doc.getDefaultRootElement(); buf = new DefStyledDoc_Helpers.ElementBufferWithLogging(doc, root); doc.buffer = buf; paragraph = root.getElement(0); content = doc.getContent(); paragraph = root.getElement(0); content.insertString(0, "plainbolditalic\ntext"); // Create the structure equivalent to this sequence: //doc.insertString(doc.getLength(), "plain", null); // 5 chars //doc.insertString(doc.getLength(), "bold", bold); // 4 chars //doc.insertString(doc.getLength(), "italic", italic); // 6 chars //doc.insertString(doc.getLength(), "\ntext", null); // 5 chars doc.writeLock(); // Write lock needed to modify document structure Element[] leaves = new Element[4]; leaves[0] = doc.createLeafElement(paragraph, null, 0, 5); leaves[1] = doc.createLeafElement(paragraph, bold, 5, 9); leaves[2] = doc.createLeafElement(paragraph, italic, 9, 15); leaves[3] = doc.createLeafElement(paragraph, null, 15, 16); ((BranchElement) paragraph).replace(0, 1, leaves); BranchElement branch = (BranchElement) doc.createBranchElement(root, null); leaves = new Element[1]; leaves[0] = doc.createLeafElement(branch, null, 16, 21); branch.replace(0, 0, leaves); // Add this branch to the root ((BranchElement) root).replace(1, 0, new Element[] { branch }); insertOffset = 5 + 2; }
@Override protected void setUp() throws Exception { super.setUp(); StyleContextTest.sc = StyleContext.getDefaultStyleContext(); as = new AttributeSet[] { StyleContextTest.addAttribute(1), StyleContextTest.addAttribute(2), StyleContextTest.addAttribute(null, 3, 2) }; doc = new PlainDocument(); doc.insertString(0, LTR + RTL + LTR + RTL + "\n01234", as[0]); bidi = (BranchElement) doc.getBidiRootElement(); leaf1 = bidi.getElement(0).getElement(0); par = (BranchElement) doc.getDefaultRootElement(); leaf2 = par.getElement(0); leaf3 = par.getElement(1); }
public void testGetStartOffsetNoChildren() { par = doc.new BranchElement(null, null); try { par.getStartOffset(); fail("NullPointerException is expected"); } catch (NullPointerException e) { // expected } }
public void testGetEndOffsetNoChildren() { par = doc.new BranchElement(null, null); try { par.getEndOffset(); fail("NullPointerException is expected"); } catch (NullPointerException e) { // expected } }
public void testBranchElement() { doc.writeLock(); bidi = doc.new BranchElement(par, as[2]); doc.writeUnlock(); assertNotSame(as[2], bidi.getAttributes()); assertEquals(as[2], bidi.getAttributes()); assertSame(par, bidi.getParentElement()); assertEquals(0, bidi.getElementCount()); assertNull(bidi.getElement(0)); Enumeration<?> elements = bidi.children(); assertNull(elements); }
/** * Creates <code>BranchElement</code> which doesn't span over the whole document. * <code>DefaultStyledDocument</code> is used to prepare the structure. */ private BranchElement createBranchElement() throws BadLocationException { final DefaultStyledDocument styledDoc = new DefaultStyledDocument(); doc = styledDoc; doc.insertString(doc.getLength(), "1 line\nonetwothree\n3 line", null); // 0123456 789012345678 901234 // 0 1 2 styledDoc.setCharacterAttributes(7, 3, SimpleAttributeSet.EMPTY, false); styledDoc.setCharacterAttributes(10, 3, SimpleAttributeSet.EMPTY, false); styledDoc.setCharacterAttributes(13, 5, SimpleAttributeSet.EMPTY, false); return (BranchElement)doc.getDefaultRootElement().getElement(1); }
private void createEmptyHTMLStructure() { doc = new DefStyledDoc_Helpers.DefStyledDocWithLogging(); root = doc.getDefaultRootElement(); buf = new DefStyledDoc_Helpers.ElementBufferWithLogging(doc, root) { private static final long serialVersionUID = 1L; @Override public void insert(int offset, int length, ElementSpec[] spec, DefaultDocumentEvent event) { super.insert(offset, length, specs = spec, event); } }; doc.buffer = buf; doc.writeLock(); try { final BranchElement html = (BranchElement) root; html.addAttribute(ELEMENT_NAME, "html"); final BranchElement body = createBranch(html); body.addAttribute(ELEMENT_NAME, "body"); final BranchElement p = createBranch(body); p.addAttribute(ELEMENT_NAME, "p"); final LeafElement content = createLeaf(p, 0, 1); content.addAttribute(ELEMENT_NAME, "leaf1"); p.replace(0, 0, new Element[] { content }); body.replace(0, 0, new Element[] { p }); html.replace(0, 1, new Element[] { body }); } finally { doc.writeUnlock(); } }
/** * Tests viewToModel(float, float, Shape, Bias[]). * For isBefore, isAfter the Y is used only. In this test either * isBefore or isAfter is true. * <p>In this test modified <code>root</code> is used so that * <code>view.getStartOffset() != 0</code> and * <code>view.getEndOffset() != doc.getLength()</code>. */ public void testViewToModel04() { // Modify the root final int oldCount = root.getElementCount(); BranchElement docRoot = (BranchElement) root; final Element[] empty = new Element[0]; docRoot.replace(0, 1, empty); docRoot.replace(docRoot.getElementCount() - 1, 1, empty); final int newCount = root.getElementCount(); assertEquals(oldCount - 2, newCount); // Re-load children view.removeAll(); view.loadChildren(factory); CompositeViewTest.useBoth = false; CompositeViewTest.useX = false; int x, y; // Before the allocation x = bounds.x; y = bounds.y - 1; assertTrue(view.isBefore(x, y, bounds)); int offset = view.viewToModel(x, y, shape, bias); assertEquals(view.getStartOffset(), offset); assertEquals(root.getElement(0).getStartOffset(), offset); // After the allocation y = bounds.y + bounds.height + 1; assertTrue(view.isAfter(x, y, bounds)); bias[0] = null; offset = view.viewToModel(x, y, shape, bias); assertEquals(view.getEndOffset() - 1, offset); assertEquals(root.getElement(root.getElementCount() - 1).getEndOffset() - 1, offset); if (BasicSwingTestCase.isHarmony()) { assertSame(Bias.Backward, bias[0]); } }
@Override protected void setUp() throws Exception { doc = new DisAbstractedDocument(new GapContent()); doc.writeLock(); BranchElement branch = doc.new BranchElement(null, null); aElement = doc.new LeafElement(null, null, 0, 3); parented = doc.new LeafElement(parent = branch, null, 5, 8); doc.writeUnlock(); }
protected void setUp() throws Exception { super.setUp(); htmlDoc = new LockableHTMLDocument(); doc = htmlDoc; doc.insertString(0, LTR + RTL + LTR + RTL + "\n01234", as[0]); bidi = (BranchElement)doc.getBidiRootElement(); leaf1 = (LeafElement)bidi.getElement(0).getElement(0); par = (BranchElement)doc.getDefaultRootElement(); leaf2 = par != null ? par.getElement(0) : null; leaf3 = leaf2; }
protected void setUp() throws Exception { super.setUp(); htmlDoc = new LockableHTMLDocument(); doc = htmlDoc; doc.insertString(0, "0123456789", as[0]); htmlDoc.lockWrite(); BranchElement branch = doc.new BranchElement(null, as[1]); leaf1 = doc.new LeafElement(null, as[2], 0, 3); leaf2 = doc.new LeafElement(branch, as[2], 5, 8); htmlDoc.unlockWrite(); }
public void testTranslateHTMLToCSSStyledDocument() throws Exception { StyledDocument doc = new DefaultStyledDocument(); doc.insertString(0, "line1\nline2", null); MutableAttributeSet mas = new SimpleAttributeSet(); mas.addAttribute(HTML.Attribute.BGCOLOR, "#ffffff"); mas.addAttribute(HTML.Attribute.TEXT, "black"); doc.setParagraphAttributes(0, 1, mas, false); AbstractElement branch = (AbstractElement)doc.getDefaultRootElement().getElement(0); assertEquals("paragraph", branch.getName()); assertTrue(branch instanceof BranchElement); assertSame(BranchElement.class, branch.getClass()); attr = ss.translateHTMLToCSS(branch); assertSame(NamedStyle.class, attr.getClass()); assertNull(((NamedStyle)attr).getName()); assertEquals(2, attr.getAttributeCount()); assertEquals("#ffffff", attr.getAttribute(CSS.Attribute.BACKGROUND_COLOR) .toString()); assertEquals("black", attr.getAttribute(CSS.Attribute.COLOR).toString()); }
@Override protected void setUp() throws Exception { super.setUp(); doc = new DefaultStyledDocument(); root = doc.getDefaultRootElement(); buf = new DefStyledDoc_Helpers.ElementBufferWithLogging(doc, root); doc.buffer = buf; paragraph = root.getElement(0); content = doc.getContent(); content.insertString(0, "plainbolditalic\ntext"); // Create the structure equivalent to this sequence: //doc.insertString(doc.getLength(), "plain", null); // 5 chars //doc.insertString(doc.getLength(), "bold", bold); // 4 chars //doc.insertString(doc.getLength(), "italic", italic); // 6 chars //doc.insertString(doc.getLength(), "\ntext", null); // 5 chars doc.writeLock(); // Write lock needed to modify document structure Element[] leaves = new Element[4]; leaves[0] = doc.createLeafElement(paragraph, null, 0, 5); leaves[1] = doc.createLeafElement(paragraph, bold, 5, 9); leaves[2] = doc.createLeafElement(paragraph, italic, 9, 15); leaves[3] = doc.createLeafElement(paragraph, null, 15, 16); ((BranchElement) paragraph).replace(0, 1, leaves); BranchElement branch = (BranchElement) doc.createBranchElement(root, null); leaves = new Element[1]; leaves[0] = doc.createLeafElement(branch, null, 16, 21); branch.replace(0, 0, leaves); // Add this branch to the root ((BranchElement) root).replace(1, 0, new Element[] { branch }); insertOffset = 5 + 2; }