Source Code Cross Referenced for UtilitiesTest.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) 


0001:        /*
0002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
0003:         *  contributor license agreements.  See the NOTICE file distributed with
0004:         *  this work for additional information regarding copyright ownership.
0005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
0006:         *  (the "License"); you may not use this file except in compliance with
0007:         *  the License.  You may obtain a copy of the License at
0008:         *
0009:         *     http://www.apache.org/licenses/LICENSE-2.0
0010:         *
0011:         *  Unless required by applicable law or agreed to in writing, software
0012:         *  distributed under the License is distributed on an "AS IS" BASIS,
0013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         *  See the License for the specific language governing permissions and
0015:         *  limitations under the License.
0016:         */
0017:        /**
0018:         * @author Evgeniya G. Maenkova
0019:         * @version $Revision$
0020:         */package javax.swing.text;
0021:
0022:        import java.awt.FontMetrics;
0023:        import java.awt.Graphics;
0024:        import java.awt.GridLayout;
0025:        import java.awt.Rectangle;
0026:        import java.text.BreakIterator;
0027:        import java.text.CharacterIterator;
0028:
0029:        import javax.swing.JFrame;
0030:        import javax.swing.JTextArea;
0031:        import javax.swing.JTextField;
0032:        import javax.swing.SwingTestCase;
0033:        import javax.swing.plaf.basic.BasicTextUI;
0034:
0035:        public class UtilitiesTest extends SwingTestCase {
0036:            JTextComponent textComponent;
0037:
0038:            TabExp te = new TabExp();
0039:
0040:            JFrame jf;
0041:
0042:            //not implemented
0043:            //JEditorPane jtp;
0044:            //not implemented
0045:            //JTextPane jtp;
0046:            JTextArea jta;
0047:
0048:            JTextField jtf;
0049:
0050:            Document doc_jta;
0051:
0052:            Document doc_jtp;
0053:
0054:            Document doc_jtf;
0055:
0056:            String sLTR = "aaaa";
0057:
0058:            String sRTL = "\u05dc" + "\u05dc" + "\u05dc" + "\u05dc";
0059:
0060:            boolean bWasException;
0061:
0062:            String message;
0063:
0064:            class TabExp implements  TabExpander {
0065:                public float nextTabStop(final float f, final int i) {
0066:                    //System.out.println(" " + f + " " + i);
0067:                    return f + i;
0068:                }
0069:            }
0070:
0071:            @Override
0072:            protected void setUp() throws Exception {
0073:                super .setUp();
0074:                timeoutDelay = 10 * DEFAULT_TIMEOUT_DELAY;
0075:                bWasException = false;
0076:                message = null;
0077:                String content = sRTL + "\t" + sLTR + "\t" + sRTL + "\t\t"
0078:                        + sLTR + "\n" + sLTR + "\t " + sLTR + " \t " + sLTR
0079:                        + sRTL + sRTL + sRTL + sRTL + "\n" + sRTL + " " + sRTL
0080:                        + "  " + sRTL + "\n" + sRTL + " " + sRTL + "; " + sRTL
0081:                        + "\n" + sRTL + ";" + sLTR + " \t" + sRTL + "\t" + sRTL
0082:                        + " " + sRTL + ",";
0083:                jf = new JFrame();
0084:                jta = new JTextArea(content);
0085:                jtf = new JTextField(content);
0086:                //jtp = new JTextPane();
0087:                //jtp.setText(content);
0088:                doc_jta = jta.getDocument();
0089:                //doc_jtp = jtp.getDocument();
0090:                doc_jtf = jtf.getDocument();
0091:                //setContent();
0092:                jf.getContentPane().setLayout(new GridLayout(2, 2));
0093:                jf.getContentPane().add(jta);
0094:                jf.getContentPane().add(jtf);
0095:                //jf.getContentPane().add(jtp);
0096:                jf.setSize(400, 500);
0097:                jf.pack();
0098:            }
0099:
0100:            protected void setContent() {
0101:                String str[] = {
0102:                        "Draws the given text, expanding any tabs",
0103:                        "that are contained using the given tab expansion technique",
0104:                        "Determines the width of the given segment of text taking",
0105:                        "Draws the given text, expanding any tabs", };
0106:                SimpleAttributeSet[] attrs = new SimpleAttributeSet[4];
0107:                attrs[0] = new SimpleAttributeSet();
0108:                StyleConstants.setFontFamily(attrs[0], "SansSerif");
0109:                StyleConstants.setFontSize(attrs[0], 16);
0110:                attrs[1] = new SimpleAttributeSet(attrs[0]);
0111:                StyleConstants.setBold(attrs[1], true);
0112:                attrs[2] = new SimpleAttributeSet(attrs[0]);
0113:                StyleConstants.setItalic(attrs[2], true);
0114:                attrs[3] = new SimpleAttributeSet(attrs[0]);
0115:                StyleConstants.setFontSize(attrs[3], 20);
0116:                try {
0117:                    for (int i = 0; i < str.length; i++) {
0118:                        doc_jtp.insertString(doc_jtp.getLength(),
0119:                                str[i] + "\n", attrs[i]);
0120:                    }
0121:                } catch (BadLocationException ble) {
0122:                    System.err.println("Error");
0123:                }
0124:            }
0125:
0126:            @Override
0127:            protected void tearDown() throws Exception {
0128:                jf.dispose();
0129:                super .tearDown();
0130:            }
0131:
0132:            void checkWasException() {
0133:                assertTrue(bWasException);
0134:                assertEquals("No more words", message);
0135:                bWasException = false;
0136:                message = null;
0137:            }
0138:
0139:            void checkWasException(final String s) {
0140:                assertTrue(bWasException);
0141:                assertEquals(s, message);
0142:                bWasException = false;
0143:                message = null;
0144:            }
0145:
0146:            int getTabbedTextOffset(final Segment s, final FontMetrics fm,
0147:                    final int start, final int end, final TabExpander t,
0148:                    final int pos) {
0149:                int res = start;
0150:                boolean lastTab = true;
0151:                int ret = 0;
0152:                int tmp1 = 0;
0153:                //TODO end < start
0154:                for (char c = s.first(); c != CharacterIterator.DONE; c = s
0155:                        .next()) {
0156:                    if (c == '\t' && t == null) {
0157:                        c = ' ';
0158:                    }
0159:                    if (c == '\t') {
0160:                        tmp1 = res;
0161:                        res = (int) t.nextTabStop(res, s.getIndex() + pos
0162:                                - s.getBeginIndex());// + s.getIndex());
0163:                        lastTab = true;
0164:                    } else {
0165:                        res += fm.charWidth(c);
0166:                        lastTab = false;
0167:                    }
0168:                    int diff = (lastTab) ? (res - tmp1) : (fm.charWidth(c));
0169:                    int tail = (diff / 2) + 1;
0170:                    if (res < end + tail) {
0171:                        ret++;
0172:                    } else {
0173:                        return ret;
0174:                    }
0175:                }
0176:                return ret;
0177:            }
0178:
0179:            int getTabbedTextWidth(final Segment s, final FontMetrics fm,
0180:                    final int x, final TabExpander t, final int pos) {
0181:                return getTabbedTextEnd(s, fm, x, t, pos) - x;
0182:            }
0183:
0184:            int getTabbedTextEnd(final Segment s, final FontMetrics fm,
0185:                    final int x, final TabExpander t, final int pos) {
0186:                int res = x;
0187:                String str = "";
0188:                boolean isNullTabExpander = (t == null);
0189:                int segmentOffset = pos - s.getBeginIndex();
0190:                boolean isTab = false;
0191:                for (char c = s.first(); c != CharacterIterator.DONE; c = s
0192:                        .next()) {
0193:                    isTab = (c == '\t');
0194:                    if (isTab && !isNullTabExpander) {
0195:                        res = (int) t.nextTabStop(fm.stringWidth(str) + res, s
0196:                                .getIndex()
0197:                                + segmentOffset);
0198:                        str = "";
0199:                    } else {
0200:                        str += (isTab) ? ' ' : c;
0201:                        isTab = false;
0202:                    }
0203:                }
0204:                return isTab ? res : (res + fm.stringWidth(str));
0205:            }
0206:
0207:            void drawTabbedTextTest(final JTextComponent c) {
0208:                //According to my experiments this is only for one line
0209:                Document doc = c.getDocument();
0210:                Graphics g = c.getGraphics();
0211:                Element root = doc.getDefaultRootElement();
0212:                Segment seg = new Segment();
0213:                for (int i = 0; i < root.getElementCount(); i++) {
0214:                    Element currentElem = root.getElement(i);
0215:                    int start = currentElem.getStartOffset();
0216:                    int end = currentElem.getEndOffset();
0217:                    for (int j = start; j < end; j++) {
0218:                        try {
0219:                            doc.getText(start, (end - start), seg);
0220:                        } catch (BadLocationException e) {
0221:                            assertTrue("Unexpected Exception: "
0222:                                    + e.getMessage(), false);
0223:                        }
0224:                        FontMetrics fm = g.getFontMetrics();
0225:                        assertEquals(Utilities.drawTabbedText(seg, 23, 24, g,
0226:                                te, j), getTabbedTextEnd(seg, fm, 23, te, j));
0227:                        assertEquals(Utilities.drawTabbedText(seg, 23, 24, g,
0228:                                null, j),
0229:                                getTabbedTextEnd(seg, fm, 23, null, j));
0230:                        assertEquals(Utilities.drawTabbedText(seg, 23, 24, g,
0231:                                te, 100),
0232:                                getTabbedTextEnd(seg, fm, 23, te, 100));
0233:                        assertEquals(Utilities.drawTabbedText(seg, 23, 24, g,
0234:                                null, 100), getTabbedTextEnd(seg, fm, 23, null,
0235:                                100));
0236:                    }
0237:                }
0238:            }
0239:
0240:            public void testDrawTabbedText() {
0241:                drawTabbedTextTest(jta);
0242:                drawTabbedTextTest(jtf);
0243:                //drawTabbedTextTest(jtp);
0244:            }
0245:
0246:            public void getTabbedTextWidthTest(final JTextComponent c) {
0247:                //According to my experiments this is only for one line
0248:                Document doc = c.getDocument();
0249:                Graphics g = c.getGraphics();
0250:                Element root = doc.getDefaultRootElement();
0251:                Segment seg = new Segment();
0252:                FontMetrics fm = g.getFontMetrics();
0253:                for (int i = 0; i < root.getElementCount(); i++) {
0254:                    Element currentElem = root.getElement(i);
0255:                    int start = currentElem.getStartOffset();
0256:                    int end = currentElem.getEndOffset();
0257:                    for (int j = start; j < end; j++) {
0258:                        try {
0259:                            doc.getText(start, end - start, seg);
0260:                        } catch (BadLocationException e) {
0261:                            assertTrue("Unexpected Exception: "
0262:                                    + e.getMessage(), false);
0263:                        }
0264:                        assertEquals(Utilities.getTabbedTextWidth(seg, fm, 23,
0265:                                te, j), getTabbedTextWidth(seg, fm, 23, te, j));
0266:                        assertEquals(Utilities.getTabbedTextWidth(seg, fm, 23,
0267:                                null, j), getTabbedTextWidth(seg, fm, 23, null,
0268:                                j));
0269:                        assertEquals(Utilities.getTabbedTextWidth(seg, fm, 23,
0270:                                te, 100), getTabbedTextWidth(seg, fm, 23, te,
0271:                                100));
0272:                        assertEquals(Utilities.getTabbedTextWidth(seg, fm, 23,
0273:                                null, 100), getTabbedTextWidth(seg, fm, 23,
0274:                                null, 100));
0275:                    }
0276:                }
0277:            }
0278:
0279:            public void testGetTabbedTextWidth() {
0280:                getTabbedTextWidthTest(jta);
0281:                getTabbedTextWidthTest(jtf);
0282:                //getTabbedTextWidthTest(jtp);
0283:            }
0284:
0285:            int getTabbedTextOffsetRound(final Segment s, final FontMetrics fm,
0286:                    final int start, final int end, final TabExpander t,
0287:                    final int pos, final boolean round) {
0288:                String str = "";
0289:                int segmentOffset = pos - s.getBeginIndex();
0290:                boolean isTab = false;
0291:                boolean isNullTabExpander = (t == null);
0292:                int currentEnd = start;
0293:                int currentIndex = 0;
0294:                int prevEnd = start;
0295:                int tabEnd = start;
0296:                for (char c = s.first(); c != CharacterIterator.DONE; c = s
0297:                        .next()) {
0298:                    isTab = (c == '\t');
0299:                    if (isTab && !isNullTabExpander) {
0300:                        tabEnd = (int) t.nextTabStop(currentEnd, s.getIndex()
0301:                                + segmentOffset);
0302:                        str = "";
0303:                    } else {
0304:                        str += (isTab) ? ' ' : c;
0305:                        isTab = false;
0306:                    }
0307:                    currentEnd = isTab ? tabEnd
0308:                            : (tabEnd + fm.stringWidth(str));
0309:                    int delta = (round) ? (currentEnd - prevEnd) / 2 : 0;
0310:                    if (currentEnd > end + delta) {
0311:                        break;
0312:                    }
0313:                    currentIndex++;
0314:                    prevEnd = currentEnd;
0315:                }
0316:                return currentIndex;
0317:            }
0318:
0319:            void getTabbedTextOffsetRoundTest_BoundaryCases(
0320:                    final JTextComponent c) {
0321:                Document doc = c.getDocument();
0322:                Graphics g = c.getGraphics();
0323:                assertNotNull(g);
0324:                Element root = doc.getDefaultRootElement();
0325:                Segment seg = new Segment();
0326:                for (int i = 0; i < root.getElementCount(); i++) {
0327:                    Element currentElem = root.getElement(i);
0328:                    int start = currentElem.getStartOffset();
0329:                    int end = currentElem.getEndOffset();
0330:                    try {
0331:                        doc.getText(start, (end - start), seg);
0332:                    } catch (BadLocationException e) {
0333:                        assertTrue("Unexpected Exception: " + e.getMessage(),
0334:                                false);
0335:                    }
0336:                    int textOffset = start + 9;
0337:                    FontMetrics fm = getFontMetrics(c.getFont(), 10);
0338:                    int textWidth = getTabbedTextWidth(seg, fm, 23, te,
0339:                            textOffset);
0340:                    for (int k = -3; k < textWidth + 4; k = (k == 0) ? k = textWidth + 1
0341:                            : k + 1) {
0342:                        int target = 23 + k;
0343:                        int offset = (k <= 0) ? 0 : seg.count;
0344:                        assertEquals(offset, Utilities.getTabbedTextOffset(seg,
0345:                                fm, 23, target, te, textOffset, false));
0346:                        assertEquals(offset, Utilities.getTabbedTextOffset(seg,
0347:                                fm, 23, target, te, textOffset, true));
0348:                        assertEquals(offset, Utilities.getTabbedTextOffset(seg,
0349:                                fm, 23, target, null, textOffset, false));
0350:                        assertEquals(offset, Utilities.getTabbedTextOffset(seg,
0351:                                fm, 23, target, null, textOffset, true));
0352:                    }
0353:                }
0354:            }
0355:
0356:            public void testGetTabbedTextOffsetRound_BoundaryCases() {
0357:                getTabbedTextOffsetRoundTest_BoundaryCases(jta);
0358:                getTabbedTextOffsetRoundTest_BoundaryCases(jtf);
0359:                //getTabbedTextOffsetRoundTest(jtp);
0360:            }
0361:
0362:            void getTabbedTextOffsetRoundTest(final JTextComponent c) {
0363:                Document doc = c.getDocument();
0364:                Graphics g = c.getGraphics();
0365:                Element root = doc.getDefaultRootElement();
0366:                Segment seg = new Segment();
0367:                FontMetrics fm = g.getFontMetrics();
0368:                for (int i = 0; i < root.getElementCount(); i++) {
0369:                    Element currentElem = root.getElement(i);
0370:                    int start = currentElem.getStartOffset();
0371:                    int end = currentElem.getEndOffset();
0372:                    try {
0373:                        doc.getText(start, (end - start), seg);
0374:                    } catch (BadLocationException e) {
0375:                        assertTrue("Unexpected Exception: " + e.getMessage(),
0376:                                false);
0377:                    }
0378:                    int offset = start + 8;
0379:                    int textWidth = Utilities.getTabbedTextWidth(seg, fm, 23,
0380:                            te, offset);
0381:                    for (int k = 0; k < textWidth; k++) {
0382:                        int target = 23 + k;
0383:                        assertEquals(getTabbedTextOffsetRound(seg, fm, 23,
0384:                                target, te, offset, false), Utilities
0385:                                .getTabbedTextOffset(seg, fm, 23, target, te,
0386:                                        offset, false));
0387:                        assertEquals(getTabbedTextOffsetRound(seg, fm, 23,
0388:                                target, te, offset, true), Utilities
0389:                                .getTabbedTextOffset(seg, fm, 23, target, te,
0390:                                        offset, true));
0391:                    }
0392:                }
0393:            }
0394:
0395:            public void testGetTabbedTextOffsetRound() {
0396:                getTabbedTextOffsetRoundTest(jta);
0397:                getTabbedTextOffsetRoundTest(jtf);
0398:                //getTabbedTextOffsetRoundTest(jtp);
0399:            }
0400:
0401:            void getTabbedTextOffsetRoundTest_NoTabExpander(
0402:                    final JTextComponent c) {
0403:                Document doc = c.getDocument();
0404:                Graphics g = c.getGraphics();
0405:                FontMetrics fm = g.getFontMetrics();
0406:                Element root = doc.getDefaultRootElement();
0407:                Segment seg = new Segment();
0408:                for (int i = 0; i < root.getElementCount(); i++) {
0409:                    Element currentElem = root.getElement(i);
0410:                    int start = currentElem.getStartOffset();
0411:                    int end = currentElem.getEndOffset();
0412:                    int offset = start + 50;
0413:                    try {
0414:                        doc.getText(start, (end - start), seg);
0415:                    } catch (BadLocationException e) {
0416:                        assertTrue("Unexpected Exception: " + e.getMessage(),
0417:                                false);
0418:                    }
0419:                    int textWidth = Utilities.getTabbedTextWidth(seg, fm, 23,
0420:                            te, offset);
0421:                    for (int k = 0; k < textWidth; k += 100) {
0422:                        int target = 23 + k;
0423:                        assertEquals(getTabbedTextOffsetRound(seg, fm, 23,
0424:                                target, null, offset, false), Utilities
0425:                                .getTabbedTextOffset(seg, fm, 23, target, null,
0426:                                        offset, false));
0427:                        assertEquals(getTabbedTextOffsetRound(seg, fm, 23,
0428:                                target, null, offset, true), Utilities
0429:                                .getTabbedTextOffset(seg, fm, 23, target, null,
0430:                                        offset, true));
0431:                    }
0432:                }
0433:            }
0434:
0435:            public void testGetTabbedTextOffsetRound_NoTabExpander() {
0436:                getTabbedTextOffsetRoundTest_NoTabExpander(jta);
0437:                getTabbedTextOffsetRoundTest_NoTabExpander(jtf);
0438:                //getTabbedTextOffsetRoundTest_NoTabExpander(jtp);
0439:            }
0440:
0441:            void getTabbedTextOffsetTest(final JTextComponent c) {
0442:                Document doc = c.getDocument();
0443:                Graphics g = c.getGraphics();
0444:                Element root = doc.getDefaultRootElement();
0445:                Segment seg = new Segment();
0446:                FontMetrics fm = g.getFontMetrics();
0447:                for (int i = 0; i < root.getElementCount(); i++) {
0448:                    Element currentElem = root.getElement(i);
0449:                    int start = currentElem.getStartOffset();
0450:                    int end = currentElem.getEndOffset();
0451:                    try {
0452:                        doc.getText(start, (end - start), seg);
0453:                    } catch (BadLocationException e) {
0454:                        assertTrue("Unexpected Exception: " + e.getMessage(),
0455:                                false);
0456:                    }
0457:                    int offset = start + 6;
0458:                    int textWidth = Utilities.getTabbedTextWidth(seg, fm, 23,
0459:                            te, offset);
0460:                    for (int k = -3; k < textWidth + 4; k++) {
0461:                        int target = 23 + k;
0462:                        assertEquals(getTabbedTextOffsetRound(seg, fm, 23,
0463:                                target, te, offset, true), Utilities
0464:                                .getTabbedTextOffset(seg, fm, 23, target, te,
0465:                                        offset));
0466:                        assertEquals(getTabbedTextOffsetRound(seg, fm, 23,
0467:                                target, null, offset, true), Utilities
0468:                                .getTabbedTextOffset(seg, fm, 23, target, null,
0469:                                        offset));
0470:                    }
0471:                }
0472:            }
0473:
0474:            public void testGetTabbedTextOffset() {
0475:                getTabbedTextOffsetTest(jta);
0476:                getTabbedTextOffsetTest(jtf);
0477:                //getTabbedTextOffsetTest(jtp);
0478:            }
0479:
0480:            int getBreakLocation(final Segment s, final FontMetrics fm,
0481:                    final int start, final int end, final TabExpander t,
0482:                    final int pos) {
0483:                int offset = s.offset;
0484:                int index = Utilities.getTabbedTextOffset(s, fm, start, end, t,
0485:                        pos, false);
0486:                int fullIndex = offset + index;
0487:                BreakIterator bi = BreakIterator.getWordInstance();
0488:                bi.setText(s);
0489:                if (bi.last() <= fullIndex) {
0490:                    return bi.last() - offset;
0491:                }
0492:                if (bi.isBoundary(fullIndex)) {
0493:                    return Character.isWhitespace(s.array[fullIndex]) ? index + 1
0494:                            : index;
0495:                }
0496:                int prev = bi.preceding(fullIndex);
0497:                if (prev == bi.first()) {
0498:                    return index;
0499:                }
0500:                return prev - offset;
0501:            }
0502:
0503:            void getBreakLocationTest(final JTextComponent c) {
0504:                Document doc = c.getDocument();
0505:                Graphics g = c.getGraphics();
0506:                FontMetrics fm = g.getFontMetrics();
0507:                Element root = doc.getDefaultRootElement();
0508:                Segment seg = new Segment();
0509:                for (int i = 0; i < root.getElementCount(); i++) {
0510:                    Element currentElem = root.getElement(i);
0511:                    int start = currentElem.getStartOffset();
0512:                    int end = currentElem.getEndOffset();
0513:                    for (int j = start; j < Math.min(start + 10, end); j++) {
0514:                        try {
0515:                            doc.getText(start, (end - start), seg);
0516:                        } catch (BadLocationException e) {
0517:                            assertTrue("Unexpected Exception: "
0518:                                    + e.getMessage(), false);
0519:                        }
0520:                        int textWidth = Utilities.getTabbedTextWidth(seg, fm,
0521:                                23, te, j);
0522:                        for (int k = 0; k < textWidth; k++) {
0523:                            int target = 23 + k;
0524:                            //TODO This test have to fail
0525:                            assertEquals(Utilities.getBreakLocation(seg, fm,
0526:                                    23, target, te, j), getBreakLocation(seg,
0527:                                    fm, 23, target, te, j));
0528:                            assertEquals(Utilities.getBreakLocation(seg, fm,
0529:                                    23, target, null, j), getBreakLocation(seg,
0530:                                    fm, 23, target, null, j));
0531:                        }
0532:                    }
0533:                }
0534:            }
0535:
0536:            public void testGetBreakLocation() {
0537:                if (isHarmony()) {
0538:                    getBreakLocationTest(jta);
0539:                }
0540:            }
0541:
0542:            private void getParagraphElementTest(final JTextComponent c) {
0543:                AbstractDocument ad = (AbstractDocument) c.getDocument();
0544:                if (ad instanceof  PlainDocument) {
0545:                    assertNull(Utilities.getParagraphElement(c, 5000));
0546:                    assertNull(Utilities.getParagraphElement(c, -5000));
0547:                } else {
0548:                    assertEquals(ad.getParagraphElement(5000), Utilities
0549:                            .getParagraphElement(c, 5000));
0550:                    assertEquals(ad.getParagraphElement(-5000), Utilities
0551:                            .getParagraphElement(c, -5000));
0552:                }
0553:                Element rootElement = ad.getDefaultRootElement();
0554:                for (int i = 0; i < rootElement.getElementCount(); i++) {
0555:                    Element elem = rootElement.getElement(i);
0556:                    int start = elem.getStartOffset();
0557:                    int end = elem.getEndOffset();
0558:                    for (int j = start; j < end; j++) {
0559:                        assertEquals(elem, Utilities.getParagraphElement(c, j));
0560:                        assertEquals(elem, ad.getParagraphElement(j));
0561:                    }
0562:                }
0563:            }
0564:
0565:            public void testGetParagraphElement() {
0566:                getParagraphElementTest(jta);
0567:                //getParagraphElementTest(jtp);
0568:                getParagraphElementTest(jtf);
0569:            }
0570:
0571:            int getPositionAbove(final JTextComponent c, final int p,
0572:                    final int x) throws BadLocationException {
0573:                int p0 = Utilities.getRowStart(c, p);
0574:                if (p0 == 0) {
0575:                    return -1;
0576:                }
0577:                int end = p0 - 1;
0578:                int diff = Integer.MAX_VALUE;
0579:                int offset = 0;
0580:                int start = Utilities.getRowStart(c, end);
0581:                for (int i = start; i <= end; i++) {
0582:                    Rectangle rect = c.modelToView(i);
0583:                    assertNotNull(rect);
0584:                    int locDiff = Math.abs(rect.x - x);
0585:                    if (locDiff <= diff) {
0586:                        diff = locDiff;
0587:                        offset = i;
0588:                    }
0589:                }
0590:                return offset;
0591:            }
0592:
0593:            int getPositionBelow(final JTextComponent c, final int p,
0594:                    final int x) throws BadLocationException {
0595:                int p0 = Utilities.getRowEnd(c, p);
0596:                int length = c.getDocument().getLength();
0597:                if (p0 == length) {
0598:                    return p;
0599:                }
0600:                int start = p0 + 1;
0601:                int diff = Integer.MAX_VALUE;
0602:                int offset = 0;
0603:                int end = Utilities.getRowEnd(c, start);
0604:                for (int i = start; i <= end; i++) {
0605:                    Rectangle rect = c.modelToView(i);
0606:                    assertNotNull(rect);
0607:                    int locDiff = Math.abs(rect.x - x);
0608:                    if (locDiff < diff) {
0609:                        diff = locDiff;
0610:                        offset = i;
0611:                    }
0612:                }
0613:                return offset;
0614:            }
0615:
0616:            void getPositionAboveBelowTest(final JTextComponent c) {
0617:                BasicTextUI ui = (BasicTextUI) c.getUI();
0618:                Document doc = c.getDocument();
0619:                int length = doc.getLength();
0620:                for (int i = 0; i < length; i++) {
0621:                    int utilAbove = 0;
0622:                    int utilAbove1 = 0;
0623:                    int utilBelow = 0;
0624:                    int utilBelow1 = 0;
0625:                    int utilAboveT = 0;
0626:                    int utilAbove1T = 0;
0627:                    int utilBelowT = 0;
0628:                    int utilBelow1T = 0;
0629:                    int appendix = 23;
0630:                    Rectangle rect = null;
0631:                    try {
0632:                        rect = ui.modelToView(c, i);
0633:                    } catch (BadLocationException e) {
0634:                    }
0635:                    assertNotNull(rect);
0636:                    try {
0637:                        utilBelow = Utilities.getPositionBelow(c, i, rect.x);
0638:                        utilBelowT = getPositionBelow(c, i, rect.x);
0639:                        utilBelow1 = Utilities.getPositionBelow(c, i, rect.x
0640:                                + appendix);
0641:                        utilBelow1T = Utilities.getPositionBelow(c, i, rect.x
0642:                                + appendix);
0643:                    } catch (BadLocationException e) {
0644:                        assertFalse("Unexpected exception: " + e.getMessage(),
0645:                                true);
0646:                    }
0647:                    assertEquals(utilAboveT, utilAbove);
0648:                    assertEquals(utilAbove1T, utilAbove1);
0649:                    assertEquals(utilBelow1, utilBelow1T);
0650:                    assertEquals(utilBelow, utilBelowT);
0651:                }
0652:                try {
0653:                    Utilities.getPositionAbove(c, -10, 100);
0654:                } catch (BadLocationException e) {
0655:                    message = e.getMessage();
0656:                    bWasException = true;
0657:                }
0658:                checkWasException("Position not represented by view");
0659:                try {
0660:                    Utilities.getPositionAbove(c, 5000, 100);
0661:                } catch (BadLocationException e) {
0662:                    message = e.getMessage();
0663:                    bWasException = true;
0664:                }
0665:                checkWasException("Position not represented by view");
0666:                try {
0667:                    Utilities.getPositionBelow(c, -10, 100);
0668:                } catch (BadLocationException e) {
0669:                    message = e.getMessage();
0670:                    bWasException = true;
0671:                }
0672:                checkWasException("Position not represented by view");
0673:                try {
0674:                    Utilities.getPositionBelow(c, 5000, 100);
0675:                } catch (BadLocationException e) {
0676:                    message = e.getMessage();
0677:                    bWasException = true;
0678:                }
0679:                checkWasException("Position not represented by view");
0680:            }
0681:
0682:            public void testGetPositionAboveBelow() {
0683:                getPositionAboveBelowTest(jta);
0684:                //getPositionAboveBelowTest(jtp);
0685:                getPositionAboveBelowTest(jtf);
0686:            }
0687:
0688:            // HARMONY-2745
0689:            public void testGetPositionAbove() throws BadLocationException {
0690:                jta = new JTextArea();
0691:                assertEquals(-1, Utilities.getPositionAbove(jta, 1, 0));
0692:            }
0693:
0694:            // HARMONY-2745
0695:            public void testGetPositionBelow() throws BadLocationException {
0696:                jta = new JTextArea();
0697:                assertEquals(-1, Utilities.getPositionBelow(jta, 1, 0));
0698:            }
0699:
0700:            void getWordStartTest(final JTextComponent c) {
0701:                AbstractDocument ad = (AbstractDocument) c.getDocument();
0702:                BreakIterator bi = BreakIterator.getWordInstance();
0703:                int length = ad.getLength();
0704:                try {
0705:                    bi.setText(ad.getText(0, ad.getLength()));
0706:                } catch (BadLocationException e) {
0707:                }
0708:                int iteratorWordStart = 0;
0709:                bi.first();
0710:                for (int i = 0; i < length; i++) {
0711:                    int utilitiesWordStart = 0;
0712:                    if (i < length - 1) {
0713:                        iteratorWordStart = bi.preceding(i + 1);
0714:                    } else {
0715:                        bi.last();
0716:                        iteratorWordStart = bi.previous();
0717:                    }
0718:                    try {
0719:                        utilitiesWordStart = Utilities.getWordStart(c, i);
0720:                    } catch (BadLocationException e) {
0721:                    }
0722:                    assertEquals(iteratorWordStart, utilitiesWordStart);
0723:                }
0724:                /* According to spec */
0725:                try {
0726:                    Utilities.getWordStart(c, length + 10);
0727:                } catch (BadLocationException e) {
0728:                    message = e.getMessage();
0729:                    bWasException = true;
0730:                }
0731:                checkWasException("No word at " + (length + 10));
0732:                try {
0733:                    Utilities.getWordStart(c, -1);
0734:                } catch (BadLocationException e) {
0735:                    message = e.getMessage();
0736:                    bWasException = true;
0737:                }
0738:                checkWasException("No word at -1");
0739:                /**/
0740:            }
0741:
0742:            public void testGetWordStart() {
0743:                getWordStartTest(jta);
0744:                //getWordStartTest(jtp);
0745:                getWordStartTest(jtf);
0746:            }
0747:
0748:            void getWordEndTest(final JTextComponent c) {
0749:                AbstractDocument ad = (AbstractDocument) c.getDocument();
0750:                BreakIterator bi = BreakIterator.getWordInstance();
0751:                int length = ad.getLength();
0752:                try {
0753:                    bi.setText(ad.getText(0, length));
0754:                } catch (BadLocationException e) {
0755:                }
0756:                bi.first();
0757:                for (int i = 0; i < length; i++) {
0758:                    int utilitiesWordEnd = 0;
0759:                    int iteratorWordEnd = bi.following(i);
0760:                    try {
0761:                        utilitiesWordEnd = Utilities.getWordEnd(c, i);
0762:                    } catch (BadLocationException e) {
0763:                    }
0764:                    assertEquals(iteratorWordEnd, utilitiesWordEnd);
0765:                }
0766:                /* According to spec */
0767:                try {
0768:                    Utilities.getWordEnd(c, length + 10);
0769:                } catch (BadLocationException e) {
0770:                    message = e.getMessage();
0771:                    bWasException = true;
0772:                }
0773:                checkWasException("No word at " + (length + 10));
0774:                try {
0775:                    Utilities.getWordEnd(c, -1);
0776:                } catch (BadLocationException e) {
0777:                    message = e.getMessage();
0778:                    bWasException = true;
0779:                }
0780:                checkWasException("No word at -1");
0781:                /**/
0782:            }
0783:
0784:            public void testGetWordEnd() {
0785:                getWordEndTest(jta);
0786:                //getWordEndTest(jtp);
0787:                getWordEndTest(jtf);
0788:            }
0789:
0790:            // may be in future...
0791:            int getRowStart(final JTextComponent c, final int pos)
0792:                    throws BadLocationException {
0793:                Rectangle r = null;
0794:                r = c.modelToView(pos);
0795:                for (int i = pos; i >= 0; i--) {
0796:                    Rectangle tmp = null;
0797:                    tmp = c.modelToView(i);
0798:                    if (tmp.y < r.y) {
0799:                        return i + 1;
0800:                    }
0801:                }
0802:                return 0;
0803:            }
0804:
0805:            int getRowEnd(final JTextComponent c, final int pos)
0806:                    throws BadLocationException {
0807:                int length = c.getDocument().getLength();
0808:                if (c instanceof  JTextField) {
0809:                    return length;
0810:                }
0811:                Rectangle r = null;
0812:                r = c.modelToView(pos);
0813:                for (int i = pos; i <= length; i++) {
0814:                    Rectangle tmp = null;
0815:                    tmp = c.modelToView(i);
0816:                    if (tmp.y > r.y) {
0817:                        return i - 1;
0818:                    }
0819:                }
0820:                return length;
0821:            }
0822:
0823:            void getRowStartEndTest(final JTextComponent c) throws Exception {
0824:                Document doc = c.getDocument();
0825:                View root = c.getUI().getRootView(c).getView(0);
0826:                assertNotNull(root);
0827:                int length = doc.getLength();
0828:                for (int i = 0; i < length; i++) {
0829:                    /*
0830:                     * int index1 = root.getViewIndex(i, Position.Bias.Forward); View
0831:                     * view1 = root.getView(index1); int index2 = view1.getViewIndex(i,
0832:                     * Position.Bias.Forward); View view2 = view1.getView(index2); View
0833:                     * view = (c instanceof JTextField) ? view1 : view2;
0834:                     *
0835:                     * int start = view.getStartOffset(); int end = view.getEndOffset() -
0836:                     * 1;
0837:                     */
0838:                    int utilitiesRowStart = 0;
0839:                    int utilitiesRowEnd = 0;
0840:                    try {
0841:                        utilitiesRowStart = Utilities.getRowStart(c, i);
0842:                        utilitiesRowEnd = Utilities.getRowEnd(c, i);
0843:                    } catch (BadLocationException e) {
0844:                    }
0845:                    assertEquals(getRowEnd(c, i), utilitiesRowEnd);
0846:                    assertEquals(getRowStart(c, i), utilitiesRowStart);
0847:                }
0848:                try {
0849:                    Utilities.getRowStart(c, 5000);
0850:                } catch (BadLocationException e) {
0851:                    bWasException = true;
0852:                    message = e.getMessage();
0853:                }
0854:                checkWasException("Position not represented by view");
0855:                try {
0856:                    Utilities.getRowEnd(c, 5000);
0857:                } catch (BadLocationException e) {
0858:                    bWasException = true;
0859:                    message = e.getMessage();
0860:                }
0861:                ;
0862:                checkWasException("Position not represented by view");
0863:                try {
0864:                    Utilities.getRowStart(c, -10);
0865:                } catch (BadLocationException e) {
0866:                    bWasException = true;
0867:                    message = e.getMessage();
0868:                }
0869:                ;
0870:                checkWasException("Position not represented by view");
0871:                try {
0872:                    Utilities.getRowEnd(c, -10);
0873:                } catch (BadLocationException e) {
0874:                    bWasException = true;
0875:                    message = e.getMessage();
0876:                }
0877:                ;
0878:                checkWasException("Position not represented by view");
0879:                textComponent = c;
0880:                textComponent.setSize(0, 0);
0881:                assertEquals(-1, Utilities.getRowStart(c, -20));
0882:                assertEquals(-1, Utilities.getRowEnd(c, 5000));
0883:                assertEquals(-1, Utilities.getRowStart(c, 5));
0884:                assertEquals(-1, Utilities.getRowEnd(c, 6));
0885:            }
0886:
0887:            public void testGetRowStartEnd() throws Exception {
0888:                getRowStartEndTest(jta);
0889:                //getRowStartEndTest(jtp);
0890:                getRowStartEndTest(jtf);
0891:            }
0892:
0893:            void getPreviousWordTest(final JTextComponent c) {
0894:                AbstractDocument ad = (AbstractDocument) c.getDocument();
0895:                BreakIterator bi = BreakIterator.getWordInstance();
0896:                int length = ad.getLength();
0897:                String content = null;
0898:                try {
0899:                    content = ad.getText(0, ad.getLength());
0900:                    bi.setText(content);
0901:                } catch (BadLocationException e) {
0902:                }
0903:                assertNotNull(content);
0904:                bi.first();
0905:                for (int i = 0; i < length; i++) {
0906:                    int utilitiesPrevWord = 0;
0907:                    int iteratorPrevWord = bi.preceding(i);
0908:                    while (iteratorPrevWord > 0
0909:                            && ((content.charAt(iteratorPrevWord) == ' ' || content
0910:                                    .charAt(iteratorPrevWord) == '\n') || content
0911:                                    .charAt(iteratorPrevWord) == '\t')) {
0912:                        iteratorPrevWord = bi.preceding(iteratorPrevWord);
0913:                    }
0914:                    try {
0915:                        utilitiesPrevWord = Utilities.getPreviousWord(c, i);
0916:                    } catch (BadLocationException e) {
0917:                    }
0918:                    if (iteratorPrevWord == -1) {
0919:                        iteratorPrevWord = 0;
0920:                    }
0921:                    assertEquals(iteratorPrevWord, utilitiesPrevWord);
0922:                }
0923:                try {
0924:                    Utilities.getPreviousWord(c, -1);
0925:                } catch (BadLocationException e) {
0926:                    bWasException = true;
0927:                    message = e.getMessage();
0928:                }
0929:                checkWasException();
0930:                try {
0931:                    Utilities.getPreviousWord(c, 5000);
0932:                } catch (BadLocationException e) {
0933:                    bWasException = true;
0934:                    message = e.getMessage();
0935:                }
0936:                checkWasException();
0937:            }
0938:
0939:            public void testGetPreviousWord() {
0940:                getPreviousWordTest(jta);
0941:                //getPreviousWordTest(jtp);
0942:                getPreviousWordTest(jtf);
0943:            }
0944:
0945:            void getNextWordTest(final JTextComponent c) {
0946:                AbstractDocument ad = (AbstractDocument) c.getDocument();
0947:                BreakIterator bi = BreakIterator.getWordInstance();
0948:                int length = ad.getLength();
0949:                String content = null;
0950:                try {
0951:                    content = ad.getText(0, ad.getLength());
0952:                    bi.setText(content);
0953:                } catch (BadLocationException e) {
0954:                }
0955:                assertNotNull(content);
0956:                bi.first();
0957:                for (int i = 0; i < length; i++) {
0958:                    int utilitiesNextWord = 0;
0959:                    int iteratorNextWord = bi.following(i);
0960:                    while (iteratorNextWord < length
0961:                            && ((content.charAt(iteratorNextWord) == ' ' || content
0962:                                    .charAt(iteratorNextWord) == '\n') || content
0963:                                    .charAt(iteratorNextWord) == '\t')) {
0964:                        iteratorNextWord = bi.following(iteratorNextWord);
0965:                    }
0966:                    try {
0967:                        utilitiesNextWord = Utilities.getNextWord(c, i);
0968:                    } catch (BadLocationException e) {
0969:                    }
0970:                    if (iteratorNextWord == length) {
0971:                        iteratorNextWord = 0;
0972:                    }
0973:                    assertEquals(iteratorNextWord, utilitiesNextWord);
0974:                }
0975:                try {
0976:                    Utilities.getNextWord(c, length + 10);
0977:                } catch (BadLocationException e) {
0978:                    message = e.getMessage();
0979:                    bWasException = true;
0980:                }
0981:                checkWasException();
0982:                bWasException = false;
0983:                message = null;
0984:                try {
0985:                    Utilities.getNextWord(c, -1);
0986:                } catch (BadLocationException e) {
0987:                    message = e.getMessage();
0988:                    bWasException = true;
0989:                }
0990:                checkWasException();
0991:            }
0992:
0993:            public void testGetNextWord() {
0994:                getNextWordTest(jta);
0995:                getNextWordTest(jtf);
0996:                //getNextWordTest(jtp);
0997:            }
0998:
0999:            // HARMONY-2744
1000:            public void testGetNextWord02() {
1001:                jta = new JTextArea("");
1002:                try {
1003:                    Utilities.getNextWord(jta, 0);
1004:                    fail("BadLocationException expected");
1005:                } catch (BadLocationException e) {
1006:                    // "No more words"
1007:                }
1008:            }
1009:
1010:            // HARMONY-2744
1011:            public void testGetNextWord03() {
1012:                jta = new JTextArea("a");
1013:                try {
1014:                    Utilities.getNextWord(jta, 0);
1015:                    fail("BadLocationException expected");
1016:                } catch (BadLocationException e) {
1017:                    // "No more words"
1018:                }
1019:
1020:                try {
1021:                    Utilities.getNextWord(jta, 1);
1022:                    fail("BadLocationException expected");
1023:                } catch (BadLocationException e) {
1024:                    // "No more words"
1025:                }
1026:            }
1027:
1028:            // HARMONY-2744
1029:            public void testGetNextWord04() throws Exception {
1030:                jta = new JTextArea("a b");
1031:                assertEquals(2, Utilities.getNextWord(jta, 0));
1032:                assertEquals(2, Utilities.getNextWord(jta, 1));
1033:                try {
1034:                    Utilities.getNextWord(jta, 2);
1035:                    fail("BadLocationException expected");
1036:                } catch (BadLocationException e) {
1037:                    // "No more words"
1038:                }
1039:            }
1040:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.