Source Code Cross Referenced for GlyphViewTest.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.awt.Font;
023:        import java.awt.FontMetrics;
024:        import java.awt.Graphics;
025:        import java.awt.Rectangle;
026:        import java.awt.Shape;
027:        import java.awt.Toolkit;
028:        import javax.swing.BasicSwingTestCase;
029:        import javax.swing.SwingConstants;
030:        import javax.swing.text.GlyphView.GlyphPainter;
031:        import javax.swing.text.Position.Bias;
032:
033:        /**
034:         * Tests GlyphView class.
035:         *
036:         */
037:        public class GlyphViewTest extends BasicSwingTestCase {
038:            protected static class EmptyPainter extends GlyphPainter {
039:                @Override
040:                public float getSpan(GlyphView v, int p0, int p1,
041:                        TabExpander e, float x) {
042:                    return 0;
043:                }
044:
045:                @Override
046:                public float getHeight(GlyphView v) {
047:                    return 0;
048:                }
049:
050:                @Override
051:                public float getAscent(GlyphView v) {
052:                    return 0;
053:                }
054:
055:                @Override
056:                public float getDescent(GlyphView v) {
057:                    return 0;
058:                }
059:
060:                @Override
061:                public void paint(GlyphView v, Graphics g, Shape a, int p0,
062:                        int p1) {
063:                }
064:
065:                @Override
066:                public Shape modelToView(GlyphView v, int pos, Bias bias,
067:                        Shape a) throws BadLocationException {
068:                    return null;
069:                }
070:
071:                @Override
072:                public int viewToModel(GlyphView v, float x, float y, Shape a,
073:                        Bias[] biasRet) {
074:                    return 0;
075:                }
076:
077:                @Override
078:                public int getBoundedPosition(GlyphView v, int p0, float x,
079:                        float len) {
080:                    return 0;
081:                }
082:
083:                @Override
084:                public String toString() {
085:                    return "GlyphViewTest.EmptyPainter";
086:                }
087:            }
088:
089:            private static final float ACCURACY = 0.00001f;
090:
091:            private GlyphView view;
092:
093:            private StyledDocument doc;
094:
095:            private Element root;
096:
097:            private Element leaf;
098:
099:            private static final String TEXT = "012345";
100:
101:            @Override
102:            protected void setUp() throws Exception {
103:                super .setUp();
104:                doc = new DefaultStyledDocument();
105:                root = doc.getDefaultRootElement();
106:                doc.insertString(0, TEXT, null);
107:                doc.setCharacterAttributes(0, TEXT.length() - 1,
108:                        SimpleAttributeSet.EMPTY, false);
109:                leaf = root.getElement(0).getElement(0);
110:                view = new GlyphView(leaf);
111:            }
112:
113:            public void testGlyphView() {
114:                view = new GlyphView(root);
115:                assertSame(root, view.getElement());
116:                assertNull(view.getGlyphPainter());
117:            }
118:
119:            @SuppressWarnings("deprecation")
120:            public void testGetPreferredSpan() {
121:                assertNull(view.getGlyphPainter());
122:                Font font = view.getFont();
123:                FontMetrics metrics = Toolkit.getDefaultToolkit()
124:                        .getFontMetrics(font);
125:                assertEquals(metrics.stringWidth(TEXT.substring(view
126:                        .getStartOffset(), view.getEndOffset())), view
127:                        .getPreferredSpan(View.X_AXIS), ACCURACY);
128:                assertEquals(metrics.getHeight(), view
129:                        .getPreferredSpan(View.Y_AXIS), ACCURACY);
130:                assertNotNull(view.getGlyphPainter());
131:            }
132:
133:            public void testGetPreferredSpanCustomPainter() {
134:                view.setGlyphPainter(new EmptyPainter() {
135:                    @Override
136:                    public float getSpan(GlyphView v, int p0, int p1,
137:                            TabExpander e, float x) {
138:                        return (p1 - p0) * 0.4261f;
139:                    }
140:
141:                    @Override
142:                    public float getHeight(GlyphView v) {
143:                        return 0.524f;
144:                    }
145:                });
146:                assertEquals(
147:                        (view.getEndOffset() - view.getStartOffset()) * 0.4261f,
148:                        view.getPreferredSpan(View.X_AXIS), ACCURACY);
149:                assertEquals(0.524f, view.getPreferredSpan(View.Y_AXIS),
150:                        ACCURACY);
151:            }
152:
153:            public void testGetStartOffset() {
154:                assertEquals(leaf.getStartOffset(), view.getStartOffset());
155:            }
156:
157:            public void testGetEndOffset() {
158:                assertEquals(leaf.getEndOffset(), view.getEndOffset());
159:            }
160:
161:            public void testSetGetGlyphPainter() {
162:                assertNull(view.getGlyphPainter());
163:                GlyphPainter painter = new EmptyPainter();
164:                view.setGlyphPainter(painter);
165:                assertSame(painter, view.getGlyphPainter());
166:            }
167:
168:            public void testGetText() {
169:                Segment docText = view.getText(0, doc.getLength() + 1);
170:                assertEquals(TEXT + "\n", docText.toString());
171:                Segment text = view.getText(2, leaf.getEndOffset());
172:                assertNotSame(docText, text);
173:                assertEquals(TEXT.substring(2, TEXT.length() - 1), text
174:                        .toString());
175:                try {
176:                    view.getText(3, 2).toString();
177:                    fail("an Error is expected");
178:                } catch (Error e) {
179:                    //            System.out.println(e.getMessage());
180:                }
181:            }
182:
183:            public void testGetTabExpander() throws BadLocationException {
184:                assertNull(view.getTabExpander());
185:                View plainView = new ParagraphView(root.getElement(0));
186:                assertTrue(plainView instanceof  TabExpander);
187:                plainView.replace(0, 0, new View[] { view });
188:                assertTrue(view.getParent() instanceof  TabExpander);
189:                if (isHarmony()) {
190:                    assertSame(plainView, view.getTabExpander());
191:                } else {
192:                    assertNull(view.getTabExpander());
193:                }
194:                doc.insertString(1, "\t", null);
195:                if (isHarmony()) {
196:                    assertSame(plainView, view.getTabExpander());
197:                } else {
198:                    assertNull(view.getTabExpander());
199:                }
200:            }
201:
202:            @SuppressWarnings("deprecation")
203:            public void testGetTabbedSpan() throws BadLocationException {
204:                final Font font = view.getFont();
205:                final FontMetrics metrics = Toolkit.getDefaultToolkit()
206:                        .getFontMetrics(font);
207:                TabExpander expander = new TabExpander() {
208:                    public float nextTabStop(float x, int tabOffset) {
209:                        return (x / 10 + 1) * 10;
210:                    }
211:                };
212:                final String leafText = view.getText(view.getStartOffset(),
213:                        view.getEndOffset()).toString();
214:                assertEquals(metrics.stringWidth(leafText), (int) view
215:                        .getTabbedSpan(0, expander));
216:                doc.insertString(2, "\t", null); // leafText doesn't contain tab
217:                final int beforeTab = metrics.stringWidth(leafText.substring(0,
218:                        2));
219:                assertEquals(metrics.stringWidth(leafText.substring(2))
220:                        + (int) expander.nextTabStop(beforeTab, 2), (int) view
221:                        .getTabbedSpan(0, expander));
222:            }
223:
224:            @SuppressWarnings("deprecation")
225:            public void testGetPartialSpan() {
226:                final Font font = view.getFont();
227:                final FontMetrics metrics = Toolkit.getDefaultToolkit()
228:                        .getFontMetrics(font);
229:                final String leafText = view.getText(view.getStartOffset(),
230:                        view.getEndOffset()).toString();
231:                assertEquals(metrics.stringWidth(leafText), (int) view
232:                        .getPartialSpan(view.getStartOffset(), view
233:                                .getEndOffset()));
234:                final String subText = leafText.substring(2, 4);
235:                assertEquals(metrics.stringWidth(subText), (int) view
236:                        .getPartialSpan(2, 4));
237:            }
238:
239:            public void testCheckPainter() {
240:                assertNull(view.getGlyphPainter());
241:                view.checkPainter();
242:                assertNotNull(view.getGlyphPainter());
243:                GlyphView other = new GlyphView(root);
244:                other.checkPainter();
245:                assertSame(view.getGlyphPainter(), other.getGlyphPainter());
246:            }
247:
248:            /**
249:             * Shape modelToView(int, Shape, Bias)
250:             * This assures a default GlyphPainter is created.
251:             */
252:            public void testModelToView() throws BadLocationException {
253:                assertNull(view.getGlyphPainter());
254:                view.modelToView(0, new Rectangle(), Bias.Forward);
255:                assertNotNull(view.getGlyphPainter());
256:            }
257:
258:            /**
259:             * int viewToModel(float, float, Shape, Bias[])
260:             * This assures a default GlyphPainter is created.
261:             */
262:            public void testViewToModel() {
263:                assertNull(view.getGlyphPainter());
264:                view.viewToModel(0, 0, new Rectangle(), new Bias[1]);
265:                assertNotNull(view.getGlyphPainter());
266:            }
267:
268:            /**
269:             * This assures a default GlyphPainter is created.
270:             */
271:            public void testGetNextVisualPositionFrom()
272:                    throws BadLocationException {
273:                assertNull(view.getGlyphPainter());
274:                try {
275:                    view.getNextVisualPositionFrom(0, Bias.Forward,
276:                            new Rectangle(), SwingConstants.EAST, new Bias[1]);
277:                    fail("No default painter is installed when this method is called");
278:                } catch (NullPointerException e) {
279:                }
280:                view.setGlyphPainter(new EmptyPainter());
281:                assertNotNull(view.getGlyphPainter());
282:                // Causes no exception
283:                view.getNextVisualPositionFrom(0, Bias.Forward,
284:                        new Rectangle(), SwingConstants.EAST, new Bias[1]);
285:            }
286:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.