Source Code Cross Referenced for DefaultStyledDocument_ElementBuffer_InsertNewLineTest.java in  » Apache-Harmony-Java-SE » javax-package » javax » swing » text » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » javax package » javax.swing.text 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:        /**
018:         * @author Alexey A. Ivanov
019:         * @version $Revision$
020:         */package javax.swing.text;
021:
022:        import java.util.List;
023:        import javax.swing.event.DocumentEvent.ElementChange;
024:        import javax.swing.event.DocumentEvent.EventType;
025:        import javax.swing.text.AbstractDocument.BranchElement;
026:        import javax.swing.text.AbstractDocument.Content;
027:        import javax.swing.text.AbstractDocument.DefaultDocumentEvent;
028:        import javax.swing.text.DefaultStyledDocument.ElementBuffer;
029:        import javax.swing.text.DefaultStyledDocument.ElementSpec;
030:        import junit.framework.TestCase;
031:
032:        /**
033:         * Tests DefaultStyledDocument.ElementBuffer.insert() method with
034:         * ElementSpec array that was obtained from real-life text insertions.
035:         * The text contains only new line character.
036:         *
037:         */
038:        public class DefaultStyledDocument_ElementBuffer_InsertNewLineTest
039:                extends TestCase {
040:            private DefaultStyledDocument doc;
041:
042:            private ElementBuffer buf;
043:
044:            private Element root;
045:
046:            private Element paragraph;
047:
048:            private int insertOffset;
049:
050:            private Content content;
051:
052:            private DefaultDocumentEvent event;
053:
054:            private static final AttributeSet bold = DefStyledDoc_Helpers.bold;
055:
056:            private static final AttributeSet italic = DefStyledDoc_Helpers.italic;
057:
058:            private static final String newLine = "\n";
059:
060:            private static final int newLineLen = newLine.length();
061:
062:            @Override
063:            protected void setUp() throws Exception {
064:                super .setUp();
065:                doc = new DefaultStyledDocument();
066:                root = doc.getDefaultRootElement();
067:                buf = new DefStyledDoc_Helpers.ElementBufferWithLogging(doc,
068:                        root);
069:                doc.buffer = buf;
070:                paragraph = root.getElement(0);
071:                content = doc.getContent();
072:                content.insertString(0, "plainbolditalic\ntext");
073:                // Create the structure equivalent to this sequence:
074:                //doc.insertString(doc.getLength(), "plain", null);    // 5 chars
075:                //doc.insertString(doc.getLength(), "bold", bold);     // 4 chars
076:                //doc.insertString(doc.getLength(), "italic", italic); // 6 chars
077:                //doc.insertString(doc.getLength(), "\ntext", null);   // 5 chars
078:                doc.writeLock(); // Write lock needed to modify document structure
079:                Element[] leaves = new Element[4];
080:                leaves[0] = doc.createLeafElement(paragraph, null, 0, 5);
081:                leaves[1] = doc.createLeafElement(paragraph, bold, 5, 9);
082:                leaves[2] = doc.createLeafElement(paragraph, italic, 9, 15);
083:                leaves[3] = doc.createLeafElement(paragraph, null, 15, 16);
084:                ((BranchElement) paragraph).replace(0, 1, leaves);
085:                BranchElement branch = (BranchElement) doc.createBranchElement(
086:                        root, null);
087:                leaves = new Element[1];
088:                leaves[0] = doc.createLeafElement(branch, null, 16, 21);
089:                branch.replace(0, 0, leaves);
090:                // Add this branch to the root
091:                ((BranchElement) root).replace(1, 0, new Element[] { branch });
092:                insertOffset = 5 + 2;
093:            }
094:
095:            @Override
096:            protected void tearDown() throws Exception {
097:                super .tearDown();
098:                doc.writeUnlock();
099:            }
100:
101:            /**
102:             * Inserting text with the same attributes into the beginning of
103:             * the document.
104:             * <p>
105:             * This test is equivalent to
106:             * <code>doc.insertString(insertOffset, newLine, null)</code>,
107:             * where <code>insertOffset = 0</code>.
108:             */
109:            public void testInsertSameAttrsDocStart() throws Exception {
110:                insertOffset = 0;
111:                // doc.insertString(insertOffset, newLine, null);
112:                content.insertString(insertOffset, newLine);
113:                event = doc.new DefaultDocumentEvent(insertOffset, newLineLen,
114:                        EventType.INSERT);
115:                ElementSpec[] specs = {
116:                        new ElementSpec(null, ElementSpec.ContentType,
117:                                newLineLen),
118:                        new ElementSpec(null, ElementSpec.EndTagType),
119:                        new ElementSpec(null, ElementSpec.StartTagType) };
120:                specs[0].setDirection(ElementSpec.JoinPreviousDirection);
121:                specs[2].setDirection(ElementSpec.JoinFractureDirection);
122:                buf.insert(insertOffset, newLineLen, specs, event);
123:                List<?> edits = getEdits(event);
124:                assertEquals(2, edits.size());
125:                assertChange(edits.get(0), new int[] { 0, 6, 6, 10, 10, 16, 16,
126:                        17 }, new int[] { 0, 1 });
127:                assertChange(edits.get(1), new int[] {}, new int[] { 1, 17 });
128:                assertChildren(root.getElement(0), new int[] { 0, 1 },
129:                        new AttributeSet[] { null });
130:                assertChildren(root.getElement(1), new int[] { 1, 6, 6, 10, 10,
131:                        16, 16, 17 }, new AttributeSet[] { null, bold, italic,
132:                        null });
133:                assertEquals("\n", getText(doc
134:                        .getCharacterElement(insertOffset)));
135:                assertEquals("plain", getText(doc
136:                        .getCharacterElement(insertOffset + newLineLen)));
137:            }
138:
139:            /**
140:             * Inserting text with different attributes into the beginning of
141:             * the document.
142:             * <p>
143:             * This test is equivalent to
144:             * <code>doc.insertString(insertOffset, newLine, italic)</code>,
145:             * where <code>insertOffset = 0</code>.
146:             */
147:            public void testInsertDiffAttrsDocStart() throws Exception {
148:                insertOffset = 0;
149:                // doc.insertString(insertOffset, newLine, italic);
150:                content.insertString(insertOffset, newLine);
151:                event = doc.new DefaultDocumentEvent(insertOffset, newLineLen,
152:                        EventType.INSERT);
153:                ElementSpec[] specs = {
154:                        new ElementSpec(italic, ElementSpec.ContentType,
155:                                newLineLen),
156:                        new ElementSpec(null, ElementSpec.EndTagType),
157:                        new ElementSpec(null, ElementSpec.StartTagType) };
158:                specs[2].setDirection(ElementSpec.JoinFractureDirection);
159:                buf.insert(insertOffset, newLineLen, specs, event);
160:                List<?> edits = getEdits(event);
161:                assertEquals(2, edits.size());
162:                assertChange(edits.get(0), new int[] { 0, 6, 6, 10, 10, 16, 16,
163:                        17 }, new int[] { 0, 1 });
164:                assertChange(edits.get(1), new int[] {}, new int[] { 1, 17 });
165:                assertChildren(root.getElement(0), new int[] { 0, 1 },
166:                        new AttributeSet[] { italic });
167:                assertChildren(root.getElement(1), new int[] { 1, 6, 6, 10, 10,
168:                        16, 16, 17 }, new AttributeSet[] { null, bold, italic,
169:                        null });
170:                assertEquals("\n", getText(doc
171:                        .getCharacterElement(insertOffset)));
172:                assertEquals("plain", getText(doc
173:                        .getCharacterElement(insertOffset + newLineLen)));
174:            }
175:
176:            /**
177:             * Inserting text into middle of an element with the same attributes.
178:             * <p>
179:             * This test is equivalent to
180:             * <code>doc.insertString(insertOffset, newLine, bold)</code>,
181:             * where <code>insertOffset</code> has default value from
182:             * <code>setUp()</code>.
183:             */
184:            public void testInsertSameAttrsMiddle() throws Exception {
185:                // doc.insertString(insertOffset, newLine, bold);
186:                content.insertString(insertOffset, newLine);
187:                event = doc.new DefaultDocumentEvent(insertOffset, newLineLen,
188:                        EventType.INSERT);
189:                ElementSpec[] specs = {
190:                        new ElementSpec(null, ElementSpec.ContentType,
191:                                newLineLen),
192:                        new ElementSpec(null, ElementSpec.EndTagType),
193:                        new ElementSpec(null, ElementSpec.StartTagType) };
194:                specs[0].setDirection(ElementSpec.JoinPreviousDirection);
195:                specs[2].setDirection(ElementSpec.JoinFractureDirection);
196:                buf.insert(insertOffset, newLineLen, specs, event);
197:                List<?> edits = getEdits(event);
198:                assertEquals(2, edits.size());
199:                assertChange(edits.get(0), new int[] { 5, 10, 10, 16, 16, 17 },
200:                        new int[] { 5, 8 });
201:                assertChange(edits.get(1), new int[] {}, new int[] { 8, 17 });
202:                assertChildren(root.getElement(0), new int[] { 0, 5, 5, 8 },
203:                        new AttributeSet[] { null, bold });
204:                assertChildren(root.getElement(1), new int[] { 8, 10, 10, 16,
205:                        16, 17 }, new AttributeSet[] { bold, italic, null });
206:                assertEquals("bo\n", getText(doc
207:                        .getCharacterElement(insertOffset)));
208:                assertEquals("ld", getText(doc.getCharacterElement(insertOffset
209:                        + newLineLen)));
210:            }
211:
212:            /**
213:             * Inserting text into end of the an element with the same attributes;
214:             * the following element has different attributes.
215:             * <p>
216:             * This test is equivalent to
217:             * <code>doc.insertString(insertOffset, newLine, bold)</code>,
218:             * where 2 is added to default value of <code>insertOffset</code>.
219:             */
220:            public void testInsertSameAttrsEnd() throws Exception {
221:                insertOffset += 2;
222:                // doc.insertString(insertOffset, newLine, bold);
223:                content.insertString(insertOffset, newLine);
224:                event = doc.new DefaultDocumentEvent(insertOffset, newLineLen,
225:                        EventType.INSERT);
226:                ElementSpec[] specs = {
227:                        new ElementSpec(null, ElementSpec.ContentType,
228:                                newLineLen),
229:                        new ElementSpec(null, ElementSpec.EndTagType),
230:                        new ElementSpec(null, ElementSpec.StartTagType) };
231:                specs[0].setDirection(ElementSpec.JoinPreviousDirection);
232:                specs[2].setDirection(ElementSpec.JoinFractureDirection);
233:                // Spec [0] has wrong attributes (should be bold) but everything works
234:                // the way it supposed to.
235:                buf.insert(insertOffset, newLineLen, specs, event);
236:                List<?> edits = getEdits(event);
237:                assertEquals(2, edits.size());
238:                assertChange(edits.get(0), new int[] { 10, 16, 16, 17 },
239:                        new int[] {});
240:                assertChange(edits.get(1), new int[] {}, new int[] { 10, 17 });
241:                assertChildren(root.getElement(0), new int[] { 0, 5, 5, 10 },
242:                        new AttributeSet[] { null, bold });
243:                assertChildren(root.getElement(1),
244:                        new int[] { 10, 16, 16, 17 }, new AttributeSet[] {
245:                                italic, null });
246:                assertEquals("bold\n", getText(doc
247:                        .getCharacterElement(insertOffset)));
248:                assertEquals("italic", getText(doc
249:                        .getCharacterElement(insertOffset + newLineLen)));
250:            }
251:
252:            /**
253:             * Inserting text into the middle of an element with different attributes.
254:             * <p>
255:             * This test is equivalent to
256:             * <code>doc.insertString(insertOffset, newLine, null)</code>,
257:             * where <code>insertOffset</code> has default value from
258:             * <code>setUp()</code>.
259:             */
260:            public void testInsertDiffAttrsMiddle() throws Exception {
261:                // doc.insertString(insertOffset, newLine, null);
262:                content.insertString(insertOffset, newLine);
263:                event = doc.new DefaultDocumentEvent(insertOffset, newLineLen,
264:                        EventType.INSERT);
265:                ElementSpec[] specs = {
266:                        new ElementSpec(null, ElementSpec.ContentType,
267:                                newLineLen),
268:                        new ElementSpec(null, ElementSpec.EndTagType),
269:                        new ElementSpec(null, ElementSpec.StartTagType) };
270:                specs[2].setDirection(ElementSpec.JoinFractureDirection);
271:                buf.insert(insertOffset, newLineLen, specs, event);
272:                List<?> edits = getEdits(event);
273:                assertEquals(2, edits.size());
274:                assertChange(edits.get(0), new int[] { 5, 10, 10, 16, 16, 17 },
275:                        new int[] { 5, 7, 7, 8 });
276:                assertChange(edits.get(1), new int[] {}, new int[] { 8, 17 });
277:                assertChildren(root.getElement(0),
278:                        new int[] { 0, 5, 5, 7, 7, 8 }, new AttributeSet[] {
279:                                null, bold, null });
280:                assertChildren(root.getElement(1), new int[] { 8, 10, 10, 16,
281:                        16, 17 }, new AttributeSet[] { bold, italic, null });
282:                assertEquals("\n", getText(doc
283:                        .getCharacterElement(insertOffset)));
284:                assertEquals("ld", getText(doc.getCharacterElement(insertOffset
285:                        + newLineLen)));
286:            }
287:
288:            /**
289:             * Inserting text into element boundary; the text and both elements have
290:             * different attributes.
291:             * <p>
292:             * This test is equivalent to
293:             * <code>doc.insertString(insertOffset, newLine, null)</code>,
294:             * where 2 is added to default value of <code>insertOffset</code>.
295:             */
296:            public void testInsertDiffAttrsEnd() throws Exception {
297:                insertOffset += 2;
298:                // doc.insertString(insertOffset, newLine, null);
299:                content.insertString(insertOffset, newLine);
300:                event = doc.new DefaultDocumentEvent(insertOffset, newLineLen,
301:                        EventType.INSERT);
302:                ElementSpec[] specs = {
303:                        new ElementSpec(null, ElementSpec.ContentType,
304:                                newLineLen),
305:                        new ElementSpec(null, ElementSpec.EndTagType),
306:                        new ElementSpec(null, ElementSpec.StartTagType) };
307:                specs[2].setDirection(ElementSpec.JoinFractureDirection);
308:                buf.insert(insertOffset, newLineLen, specs, event);
309:                List<?> edits = getEdits(event);
310:                assertEquals(2, edits.size());
311:                assertChange(edits.get(0), new int[] { 5, 10, 10, 16, 16, 17 },
312:                        new int[] { 5, 9, 9, 10 });
313:                assertChange(edits.get(1), new int[] {}, new int[] { 10, 17 });
314:                assertChildren(root.getElement(0), new int[] { 0, 5, 5, 9, 9,
315:                        10 }, new AttributeSet[] { null, bold, null });
316:                assertChildren(root.getElement(1),
317:                        new int[] { 10, 16, 16, 17 }, new AttributeSet[] {
318:                                italic, null });
319:                assertEquals("\n", getText(doc
320:                        .getCharacterElement(insertOffset)));
321:                assertEquals("italic", getText(doc
322:                        .getCharacterElement(insertOffset + newLineLen)));
323:            }
324:
325:            /**
326:             * Inserting text into element boundary; the attributes of the text and
327:             * the following element are the same, the attributes of the previous
328:             * element are different.
329:             * <p>
330:             * This test is equivalent to
331:             * <code>doc.insertString(insertOffset, newLine, italic)</code>,
332:             * where 2 is added to default value of <code>insertOffset</code>.
333:             */
334:            public void testInsertDiffSameAttrsEnd() throws Exception {
335:                insertOffset += 2;
336:                // doc.insertString(insertOffset, newLine, italic);
337:                content.insertString(insertOffset, newLine);
338:                event = doc.new DefaultDocumentEvent(insertOffset, newLineLen,
339:                        EventType.INSERT);
340:                ElementSpec[] specs = {
341:                        new ElementSpec(italic, ElementSpec.ContentType,
342:                                newLineLen),
343:                        new ElementSpec(null, ElementSpec.EndTagType),
344:                        new ElementSpec(null, ElementSpec.StartTagType) };
345:                specs[2].setDirection(ElementSpec.JoinFractureDirection);
346:                buf.insert(insertOffset, newLineLen, specs, event);
347:                List<?> edits = getEdits(event);
348:                assertEquals(2, edits.size());
349:                assertChange(edits.get(0), new int[] { 5, 10, 10, 16, 16, 17 },
350:                        new int[] { 5, 9, 9, 10 });
351:                assertChange(edits.get(1), new int[] {}, new int[] { 10, 17 });
352:                assertChildren(root.getElement(0), new int[] { 0, 5, 5, 9, 9,
353:                        10 }, new AttributeSet[] { null, bold, italic });
354:                assertChildren(root.getElement(1),
355:                        new int[] { 10, 16, 16, 17 }, new AttributeSet[] {
356:                                italic, null });
357:                assertEquals("\n", getText(doc
358:                        .getCharacterElement(insertOffset)));
359:                assertEquals("italic", getText(doc
360:                        .getCharacterElement(insertOffset + newLineLen)));
361:            }
362:
363:            /**
364:             * Inserting text into the start of a paragraph with the same attributes.
365:             * <p>
366:             * This test is equivalent to
367:             * <code>doc.insertString(insertOffset, newLine, null)</code>,
368:             * where <code>insertOffset = paragraph.getEndOffset()</code>.
369:             */
370:            public void testInsertSameAttrsParStart() throws Exception {
371:                insertOffset = paragraph.getEndOffset();
372:                // doc.insertString(insertOffset, newLine, null);
373:                content.insertString(insertOffset, newLine);
374:                event = doc.new DefaultDocumentEvent(insertOffset, newLineLen,
375:                        EventType.INSERT);
376:                ElementSpec[] specs = {
377:                        new ElementSpec(null, ElementSpec.EndTagType),
378:                        new ElementSpec(null, ElementSpec.StartTagType),
379:                        new ElementSpec(null, ElementSpec.ContentType,
380:                                newLineLen),
381:                        new ElementSpec(null, ElementSpec.EndTagType),
382:                        new ElementSpec(null, ElementSpec.StartTagType) };
383:                specs[4].setDirection(ElementSpec.JoinNextDirection);
384:                buf.insert(insertOffset, newLineLen, specs, event);
385:                List<?> edits = getEdits(event);
386:                assertEquals(3, edits.size());
387:                assertChange(edits.get(0), new int[] { 15, 17 }, new int[] {
388:                        15, 16 });
389:                assertChange(edits.get(1), new int[] {}, new int[] { 16, 17 });
390:                assertChange(edits.get(2), new int[] {}, new int[] { 16, 17 });
391:                assertChildren(root.getElement(0), new int[] { 0, 5, 5, 9, 9,
392:                        15, 15, 16 }, new AttributeSet[] { null, bold, italic,
393:                        null });
394:                assertChildren(root.getElement(1), new int[] { 16, 17 },
395:                        new AttributeSet[] { null });
396:                assertChildren(root.getElement(2), new int[] { 17, 22 },
397:                        new AttributeSet[] { null });
398:                assertEquals("\n", getText(doc
399:                        .getCharacterElement(insertOffset)));
400:                assertEquals("text\n", getText(doc
401:                        .getCharacterElement(insertOffset + newLineLen)));
402:            }
403:
404:            /**
405:             * Inserting text into the start of a paragraph with different attributes.
406:             * <p>
407:             * This test is equivalent to
408:             * <code>doc.insertString(insertOffset, newLine, italic)</code>,
409:             * where <code>insertOffset = paragraph.getEndOffset()</code>.
410:             */
411:            public void testInsertDiffAttrsParStart() throws Exception {
412:                insertOffset = paragraph.getEndOffset();
413:                // doc.insertString(insertOffset, newLine, italic);
414:                content.insertString(insertOffset, newLine);
415:                event = doc.new DefaultDocumentEvent(insertOffset, newLineLen,
416:                        EventType.INSERT);
417:                ElementSpec[] specs = {
418:                        new ElementSpec(null, ElementSpec.EndTagType),
419:                        new ElementSpec(null, ElementSpec.StartTagType),
420:                        new ElementSpec(italic, ElementSpec.ContentType,
421:                                newLineLen),
422:                        new ElementSpec(null, ElementSpec.EndTagType),
423:                        new ElementSpec(null, ElementSpec.StartTagType) };
424:                specs[4].setDirection(ElementSpec.JoinNextDirection);
425:                buf.insert(insertOffset, newLineLen, specs, event);
426:                List<?> edits = getEdits(event);
427:                assertEquals(3, edits.size());
428:                assertChange(edits.get(0), new int[] { 15, 17 }, new int[] {
429:                        15, 16 });
430:                assertChange(edits.get(1), new int[] {}, new int[] { 16, 17 });
431:                assertChange(edits.get(2), new int[] {}, new int[] { 16, 17 });
432:                assertChildren(root.getElement(0), new int[] { 0, 5, 5, 9, 9,
433:                        15, 15, 16 }, new AttributeSet[] { null, bold, italic,
434:                        null });
435:                assertChildren(root.getElement(1), new int[] { 16, 17 },
436:                        new AttributeSet[] { italic });
437:                assertChildren(root.getElement(2), new int[] { 17, 22 },
438:                        new AttributeSet[] { null });
439:                assertEquals("\n", getText(doc
440:                        .getCharacterElement(insertOffset)));
441:                assertEquals("text\n", getText(doc
442:                        .getCharacterElement(insertOffset + newLineLen)));
443:            }
444:
445:            /**
446:             * Inserting text into the end of the document with the same attributes.
447:             * <p>
448:             * This test is equivalent to
449:             * <code>doc.insertString(insertOffset, newLine, null)</code>,
450:             * where <code>insertOffset = doc.getLength()</code>.
451:             */
452:            public void testInsertSameAttrsDocEnd() throws Exception {
453:                insertOffset = doc.getLength();
454:                // doc.insertString(insertOffset, newLine, null);
455:                content.insertString(insertOffset, newLine);
456:                event = doc.new DefaultDocumentEvent(insertOffset, newLineLen,
457:                        EventType.INSERT);
458:                ElementSpec[] specs = {
459:                        new ElementSpec(null, ElementSpec.ContentType,
460:                                newLineLen),
461:                        new ElementSpec(null, ElementSpec.EndTagType),
462:                        new ElementSpec(null, ElementSpec.StartTagType) };
463:                specs[0].setDirection(ElementSpec.JoinPreviousDirection);
464:                specs[2].setDirection(ElementSpec.JoinFractureDirection);
465:                buf.insert(insertOffset, newLineLen, specs, event);
466:                List<?> edits = getEdits(event);
467:                assertEquals(2, edits.size());
468:                assertChange(edits.get(0), new int[] { 16, 22 }, new int[] {
469:                        16, 21 });
470:                assertChange(edits.get(1), new int[] {}, new int[] { 21, 22 });
471:                assertChildren(root.getElement(1), new int[] { 16, 21 },
472:                        new AttributeSet[] { null });
473:                assertChildren(root.getElement(2), new int[] { 21, 22 },
474:                        new AttributeSet[] { null });
475:                assertEquals("text\n", getText(doc
476:                        .getCharacterElement(insertOffset)));
477:                assertEquals("\n", getText(doc.getCharacterElement(insertOffset
478:                        + newLineLen)));
479:            }
480:
481:            /**
482:             * Inserting text into the end of the document with different attributes.
483:             * <p>
484:             * This test is equivalent to
485:             * <code>doc.insertString(insertOffset, newLine, italic)</code>,
486:             * where <code>insertOffset = doc.getLength()</code>.
487:             */
488:            public void testInsertDiffAttrsDocEnd() throws Exception {
489:                insertOffset = doc.getLength();
490:                // doc.insertString(insertOffset, newLine, italic);
491:                content.insertString(insertOffset, newLine);
492:                event = doc.new DefaultDocumentEvent(insertOffset, newLineLen,
493:                        EventType.INSERT);
494:                ElementSpec[] specs = {
495:                        new ElementSpec(italic, ElementSpec.ContentType,
496:                                newLineLen),
497:                        new ElementSpec(null, ElementSpec.EndTagType),
498:                        new ElementSpec(null, ElementSpec.StartTagType) };
499:                specs[2].setDirection(ElementSpec.JoinFractureDirection);
500:                buf.insert(insertOffset, newLineLen, specs, event);
501:                List<?> edits = getEdits(event);
502:                assertEquals(2, edits.size());
503:                assertChange(edits.get(0), new int[] { 16, 22 }, new int[] {
504:                        16, 20, 20, 21 });
505:                assertChange(edits.get(1), new int[] {}, new int[] { 21, 22 });
506:                assertChildren(root.getElement(1),
507:                        new int[] { 16, 20, 20, 21 }, new AttributeSet[] {
508:                                null, italic });
509:                assertChildren(root.getElement(2), new int[] { 21, 22 },
510:                        new AttributeSet[] { null });
511:                assertEquals("text", getText(doc
512:                        .getCharacterElement(insertOffset - 1)));
513:                assertEquals("\n", getText(doc
514:                        .getCharacterElement(insertOffset)));
515:                assertEquals("\n", getText(doc.getCharacterElement(insertOffset
516:                        + newLineLen)));
517:            }
518:
519:            private String getText(final int offset, final int length)
520:                    throws BadLocationException {
521:                return doc.getText(offset, length);
522:            }
523:
524:            private String getText(final Element element)
525:                    throws BadLocationException {
526:                return getText(element.getStartOffset(), element.getEndOffset()
527:                        - element.getStartOffset());
528:            }
529:
530:            private static void assertChange(final Object change,
531:                    final int[] removedOffsets, final int[] addedOffsets) {
532:                DefStyledDoc_Helpers.assertChange((ElementChange) change,
533:                        removedOffsets, addedOffsets);
534:            }
535:
536:            private static void assertChildren(final Element element,
537:                    final int[] offsets, final AttributeSet[] attributes) {
538:                DefStyledDoc_Helpers.assertChildren(element, offsets,
539:                        attributes);
540:            }
541:
542:            private static List<?> getEdits(final DefaultDocumentEvent event) {
543:                return DefStyledDoc_Helpers.getEdits(event);
544:            }
545:        }
546:        /*
547:         The dump of the document after the initial set up.
548:
549:         <section>
550:         <paragraph
551:         resolver=NamedStyle:default {name=default,}
552:         >
553:         <content>
554:         [0,5][plain]
555:         <content
556:         bold=true
557:         >
558:         [5,9][bold]
559:         <content
560:         italic=true
561:         >
562:         [9,15][italic]
563:         <content>
564:         [15,16][
565:         ]
566:         <paragraph>
567:         <content>
568:         [16,21][text
569:         ]
570:         <bidi root>
571:         <bidi level
572:         bidiLevel=0
573:         >
574:         [0,21][plainbolditalic
575:         text
576:         ]
577:         */
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.