Source Code Cross Referenced for ComponentViewTest.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 Roman I. Chernyatchik
019:         * @version $Revision$
020:         */package javax.swing.text;
021:
022:        import java.awt.Component;
023:        import java.awt.Dimension;
024:        import java.awt.Graphics;
025:        import java.awt.Rectangle;
026:        import java.awt.Shape;
027:        import java.awt.geom.Ellipse2D;
028:        import java.awt.geom.Rectangle2D;
029:        import javax.swing.JButton;
030:        import javax.swing.JComponent;
031:        import javax.swing.JPanel;
032:        import javax.swing.JTextArea;
033:        import javax.swing.JTextPane;
034:        import javax.swing.SwingTestCase;
035:
036:        public class ComponentViewTest extends SwingTestCase {
037:            StyledDocument document;
038:
039:            JTextPane textPane;
040:
041:            Element componentElement;
042:
043:            JButton insertedComponent;
044:
045:            ComponentView view;
046:
047:            @Override
048:            protected void setUp() throws Exception {
049:                super .setUp();
050:                insertedComponent = new JButton();
051:                textPane = new JTextPane();
052:                document = textPane.getStyledDocument();
053:                document.insertString(0, "Hello\n word!!!",
054:                        new SimpleAttributeSet());
055:                textPane.setCaretPosition(3);
056:                textPane.insertComponent(insertedComponent);
057:                componentElement = document.getDefaultRootElement().getElement(
058:                        0).getElement(1);
059:                view = new ComponentView(componentElement);
060:            }
061:
062:            public void testComponentView() {
063:                assertNotNull(StyleConstants.getComponent(componentElement
064:                        .getAttributes()));
065:                componentElement = document.getDefaultRootElement();
066:                assertNull(view.getParent());
067:                assertNull(view.getComponent());
068:            }
069:
070:            public void testCreateComponent() {
071:                final Marker createComponentCalled = new Marker();
072:                view = new ComponentView(componentElement) {
073:                    @Override
074:                    public void setParent(View parent) {
075:                        createComponentCalled.setOccurred();
076:                        super .setParent(parent);
077:                    }
078:                };
079:                assertEquals(insertedComponent, view.createComponent());
080:                JPanel panel = new JPanel();
081:                MutableAttributeSet attrs = new SimpleAttributeSet();
082:                StyleConstants.setComponent(attrs, panel);
083:                document.setCharacterAttributes(3, 1, attrs, true);
084:                assertSame(panel, view.createComponent());
085:                createComponentCalled.reset();
086:                view.setParent(textPane.getUI().getRootView(textPane));
087:                assertTrue(createComponentCalled.isOccurred());
088:                assertSame(panel, view.createComponent());
089:                JTextArea textArea = new JTextArea();
090:                createComponentCalled.reset();
091:                view.setParent(textArea.getUI().getRootView(textArea));
092:                assertSame(panel, view.createComponent());
093:                assertTrue(createComponentCalled.isOccurred());
094:                createComponentCalled.reset();
095:                view.setParent(null);
096:                assertTrue(createComponentCalled.isOccurred());
097:                createComponentCalled.reset();
098:                view.setParent(textArea.getUI().getRootView(textArea));
099:                assertTrue(createComponentCalled.isOccurred());
100:                assertSame(panel, view.createComponent());
101:            }
102:
103:            public void testGetPreferredSpan() {
104:                Component c;
105:                assertTrue(0 == view.getPreferredSpan(View.X_AXIS));
106:                assertTrue(0 == view.getPreferredSpan(View.Y_AXIS));
107:                testExceptionalCase(new IllegalArgumentCase() {
108:                    @Override
109:                    public void exceptionalAction() throws Exception {
110:                        view.getPreferredSpan(2);
111:                    }
112:                });
113:                testExceptionalCase(new IllegalArgumentCase() {
114:                    @Override
115:                    public void exceptionalAction() throws Exception {
116:                        view.getPreferredSpan(-1);
117:                    }
118:                });
119:                insertedComponent.setPreferredSize(new Dimension(100, 200));
120:                view.setParent(textPane.getUI().getRootView(textPane));
121:                c = view.getComponent();
122:                if (isHarmony()) {
123:                    assertTrue(c.getPreferredSize().width + 2 == view
124:                            .getPreferredSpan(View.X_AXIS));
125:                } else {
126:                    assertTrue(c.getPreferredSize().width == view
127:                            .getPreferredSpan(View.X_AXIS));
128:                }
129:                assertTrue(c.getPreferredSize().height == view
130:                        .getPreferredSpan(View.Y_AXIS));
131:                testExceptionalCase(new IllegalArgumentCase() {
132:                    @Override
133:                    public void exceptionalAction() throws Exception {
134:                        view.getPreferredSpan(2);
135:                    }
136:                });
137:                testExceptionalCase(new IllegalArgumentCase() {
138:                    @Override
139:                    public void exceptionalAction() throws Exception {
140:                        view.getPreferredSpan(-1);
141:                    }
142:                });
143:                c.setPreferredSize(new Dimension(20, 30));
144:                if (isHarmony()) {
145:                    assertTrue(22 == view.getPreferredSpan(View.X_AXIS));
146:                    assertTrue(30 == view.getPreferredSpan(View.Y_AXIS));
147:                } else {
148:                    assertTrue(100 == view.getPreferredSpan(View.X_AXIS));
149:                    assertTrue(200 == view.getPreferredSpan(View.Y_AXIS));
150:                }
151:                view.setParent(null);
152:                testExceptionalCase(new IllegalArgumentCase() {
153:                    @Override
154:                    public void exceptionalAction() throws Exception {
155:                        view.getPreferredSpan(2);
156:                    }
157:                });
158:                testExceptionalCase(new IllegalArgumentCase() {
159:                    @Override
160:                    public void exceptionalAction() throws Exception {
161:                        view.getPreferredSpan(-1);
162:                    }
163:                });
164:                c = view.getComponent();
165:                if (isHarmony()) {
166:                    assertTrue(0 == view.getPreferredSpan(View.X_AXIS));
167:                    assertTrue(0 == view.getPreferredSpan(View.Y_AXIS));
168:                } else {
169:                    assertTrue(100 == view.getPreferredSpan(View.X_AXIS));
170:                    assertTrue(200 == view.getPreferredSpan(View.Y_AXIS));
171:                }
172:                view.getComponent().setPreferredSize(new Dimension(20, 30));
173:                if (isHarmony()) {
174:                    assertTrue(0 == view.getPreferredSpan(View.X_AXIS));
175:                    assertTrue(0 == view.getPreferredSpan(View.Y_AXIS));
176:                } else {
177:                    assertTrue(100 == view.getPreferredSpan(View.X_AXIS));
178:                    assertTrue(200 == view.getPreferredSpan(View.Y_AXIS));
179:                }
180:            }
181:
182:            public void testGetMinimumSpan() {
183:                Component c;
184:                assertTrue(0 == view.getMinimumSpan(View.X_AXIS));
185:                assertTrue(0 == view.getMinimumSpan(View.Y_AXIS));
186:                testExceptionalCase(new IllegalArgumentCase() {
187:                    @Override
188:                    public void exceptionalAction() throws Exception {
189:                        view.getMinimumSpan(2);
190:                    }
191:                });
192:                testExceptionalCase(new IllegalArgumentCase() {
193:                    @Override
194:                    public void exceptionalAction() throws Exception {
195:                        view.getMinimumSpan(-1);
196:                    }
197:                });
198:                insertedComponent.setMinimumSize(new Dimension(100, 200));
199:                view.setParent(textPane.getUI().getRootView(textPane));
200:                c = view.getComponent();
201:                if (isHarmony()) {
202:                    assertTrue(c.getMinimumSize().width + 2 == view
203:                            .getMinimumSpan(View.X_AXIS));
204:                } else {
205:                    assertTrue(c.getMinimumSize().width == view
206:                            .getMinimumSpan(View.X_AXIS));
207:                }
208:                assertTrue(c.getMinimumSize().height == view
209:                        .getMinimumSpan(View.Y_AXIS));
210:                testExceptionalCase(new IllegalArgumentCase() {
211:                    @Override
212:                    public void exceptionalAction() throws Exception {
213:                        view.getMinimumSpan(2);
214:                    }
215:                });
216:                testExceptionalCase(new IllegalArgumentCase() {
217:                    @Override
218:                    public void exceptionalAction() throws Exception {
219:                        view.getMinimumSpan(-1);
220:                    }
221:                });
222:                c.setMinimumSize(new Dimension(20, 30));
223:                if (isHarmony()) {
224:                    assertTrue(22 == view.getMinimumSpan(View.X_AXIS));
225:                    assertTrue(30 == view.getMinimumSpan(View.Y_AXIS));
226:                } else {
227:                    assertTrue(100 == view.getMinimumSpan(View.X_AXIS));
228:                    assertTrue(200 == view.getMinimumSpan(View.Y_AXIS));
229:                }
230:                view.setParent(null);
231:                testExceptionalCase(new IllegalArgumentCase() {
232:                    @Override
233:                    public void exceptionalAction() throws Exception {
234:                        view.getMinimumSpan(2);
235:                    }
236:                });
237:                testExceptionalCase(new IllegalArgumentCase() {
238:                    @Override
239:                    public void exceptionalAction() throws Exception {
240:                        view.getMinimumSpan(-1);
241:                    }
242:                });
243:                c = view.getComponent();
244:                if (isHarmony()) {
245:                    assertTrue(0 == view.getMinimumSpan(View.X_AXIS));
246:                    assertTrue(0 == view.getMinimumSpan(View.Y_AXIS));
247:                } else {
248:                    assertTrue(100 == view.getMinimumSpan(View.X_AXIS));
249:                    assertTrue(200 == view.getMinimumSpan(View.Y_AXIS));
250:                }
251:                view.getComponent().setMinimumSize(new Dimension(20, 30));
252:                if (isHarmony()) {
253:                    assertTrue(0 == view.getMinimumSpan(View.X_AXIS));
254:                    assertTrue(0 == view.getMinimumSpan(View.Y_AXIS));
255:                } else {
256:                    assertTrue(100 == view.getMinimumSpan(View.X_AXIS));
257:                    assertTrue(200 == view.getMinimumSpan(View.Y_AXIS));
258:                }
259:            }
260:
261:            public void testGetMaximumSpan() {
262:                Component c;
263:                assertTrue(0 == view.getMaximumSpan(View.X_AXIS));
264:                assertTrue(0 == view.getMaximumSpan(View.Y_AXIS));
265:                testExceptionalCase(new IllegalArgumentCase() {
266:                    @Override
267:                    public void exceptionalAction() throws Exception {
268:                        view.getMaximumSpan(2);
269:                    }
270:                });
271:                testExceptionalCase(new IllegalArgumentCase() {
272:                    @Override
273:                    public void exceptionalAction() throws Exception {
274:                        view.getMaximumSpan(-1);
275:                    }
276:                });
277:                insertedComponent.setMaximumSize(new Dimension(100, 200));
278:                view.setParent(textPane.getUI().getRootView(textPane));
279:                c = view.getComponent();
280:                if (isHarmony()) {
281:                    assertTrue(c.getMaximumSize().width + 2 == view
282:                            .getMaximumSpan(View.X_AXIS));
283:                } else {
284:                    assertTrue(c.getMaximumSize().width == view
285:                            .getMaximumSpan(View.X_AXIS));
286:                }
287:                assertTrue(c.getMaximumSize().height == view
288:                        .getMaximumSpan(View.Y_AXIS));
289:                testExceptionalCase(new IllegalArgumentCase() {
290:                    @Override
291:                    public void exceptionalAction() throws Exception {
292:                        view.getMaximumSpan(2);
293:                    }
294:                });
295:                testExceptionalCase(new IllegalArgumentCase() {
296:                    @Override
297:                    public void exceptionalAction() throws Exception {
298:                        view.getMaximumSpan(-1);
299:                    }
300:                });
301:                c.setMaximumSize(new Dimension(20, 30));
302:                if (isHarmony()) {
303:                    assertTrue(22 == view.getMaximumSpan(View.X_AXIS));
304:                    assertTrue(30 == view.getMaximumSpan(View.Y_AXIS));
305:                } else {
306:                    assertTrue(100 == view.getMaximumSpan(View.X_AXIS));
307:                    assertTrue(200 == view.getMaximumSpan(View.Y_AXIS));
308:                }
309:                view.setParent(null);
310:                testExceptionalCase(new IllegalArgumentCase() {
311:                    @Override
312:                    public void exceptionalAction() throws Exception {
313:                        view.getMaximumSpan(2);
314:                    }
315:                });
316:                testExceptionalCase(new IllegalArgumentCase() {
317:                    @Override
318:                    public void exceptionalAction() throws Exception {
319:                        view.getMaximumSpan(-1);
320:                    }
321:                });
322:                c = view.getComponent();
323:                if (isHarmony()) {
324:                    assertTrue(0 == view.getMaximumSpan(View.X_AXIS));
325:                    assertTrue(0 == view.getMaximumSpan(View.Y_AXIS));
326:                } else {
327:                    assertTrue(100 == view.getMaximumSpan(View.X_AXIS));
328:                    assertTrue(200 == view.getMaximumSpan(View.Y_AXIS));
329:                }
330:                view.getComponent().setMaximumSize(new Dimension(20, 30));
331:                if (isHarmony()) {
332:                    assertTrue(0 == view.getMaximumSpan(View.X_AXIS));
333:                    assertTrue(0 == view.getMaximumSpan(View.Y_AXIS));
334:                } else {
335:                    assertTrue(100 == view.getMaximumSpan(View.X_AXIS));
336:                    assertTrue(200 == view.getMaximumSpan(View.Y_AXIS));
337:                }
338:            }
339:
340:            public void testGetAlignment() {
341:                JComponent jc;
342:                assertTrue(View.ALIGN_CENTER == view.getAlignment(View.X_AXIS));
343:                assertTrue(View.ALIGN_CENTER == view.getAlignment(View.Y_AXIS));
344:                assertTrue(View.ALIGN_CENTER == view.getAlignment(2));
345:                assertTrue(View.ALIGN_CENTER == view.getAlignment(-1));
346:                insertedComponent.setAlignmentX(0.3f);
347:                insertedComponent.setAlignmentY(0.6f);
348:                view.setParent(textPane.getUI().getRootView(textPane));
349:                jc = (JComponent) view.getComponent();
350:                assertEquals(jc.getAlignmentX(),
351:                        view.getAlignment(View.X_AXIS), 0.001);
352:                assertEquals(jc.getAlignmentY(),
353:                        view.getAlignment(View.Y_AXIS), 0.001);
354:                assertTrue(View.ALIGN_CENTER == view.getAlignment(2));
355:                assertTrue(View.ALIGN_CENTER == view.getAlignment(-1));
356:                jc.setAlignmentX(0.8f);
357:                jc.setAlignmentY(0.9f);
358:                if (isHarmony()) {
359:                    assertEquals(0.8f, view.getAlignment(View.X_AXIS), 0.001);
360:                    assertEquals(0.9f, view.getAlignment(View.Y_AXIS), 0.001);
361:                } else {
362:                    assertEquals(0.3f, view.getAlignment(View.X_AXIS), 0.001);
363:                    assertEquals(0.6f, view.getAlignment(View.Y_AXIS), 0.001);
364:                }
365:                jc.setAlignmentX(0.9f);
366:                jc.setAlignmentY(1.0f);
367:                if (isHarmony()) {
368:                    assertEquals(0.9f, view.getAlignment(View.X_AXIS), 0.001);
369:                    assertEquals(1.0f, view.getAlignment(View.Y_AXIS), 0.001);
370:                } else {
371:                    assertEquals(0.3f, view.getAlignment(View.X_AXIS), 0.001);
372:                    assertEquals(0.6f, view.getAlignment(View.Y_AXIS), 0.001);
373:                }
374:                view.setParent(null);
375:                if (isHarmony()) {
376:                    assertEquals(0.9f, view.getAlignment(View.X_AXIS), 0.001);
377:                    assertEquals(1.0f, view.getAlignment(View.Y_AXIS), 0.001);
378:                } else {
379:                    assertEquals(0.3f, view.getAlignment(View.X_AXIS), 0.001);
380:                    assertEquals(0.6f, view.getAlignment(View.Y_AXIS), 0.001);
381:                }
382:                ((JComponent) view.getComponent()).setAlignmentX(0.1f);
383:                ((JComponent) view.getComponent()).setAlignmentY(0.2f);
384:                if (isHarmony()) {
385:                    assertEquals(0.1f, view.getAlignment(View.X_AXIS), 0.001);
386:                    assertEquals(0.2f, view.getAlignment(View.Y_AXIS), 0.001);
387:                } else {
388:                    assertEquals(0.3f, view.getAlignment(View.X_AXIS), 0.001);
389:                    assertEquals(0.6f, view.getAlignment(View.Y_AXIS), 0.001);
390:                }
391:            }
392:
393:            public void testGetComponent() {
394:                assertNull(view.getComponent());
395:                assertNotNull(view.createComponent());
396:                assertNull(view.getComponent());
397:                view.setParent(null);
398:                assertNull(view.getComponent());
399:                assertNotNull(view.createComponent());
400:                assertNull(view.getComponent());
401:                view.setParent(textPane.getUI().getRootView(textPane));
402:                Component c = view.createComponent();
403:                assertSame(c, view.getComponent());
404:                JPanel panel = new JPanel();
405:                MutableAttributeSet attrs = new SimpleAttributeSet();
406:                StyleConstants.setComponent(attrs, panel);
407:                document.setCharacterAttributes(3, 1, attrs, true);
408:                assertSame(c, view.getComponent());
409:                view.setParent(null);
410:                assertSame(c, view.getComponent());
411:                JTextArea textArea = new JTextArea();
412:                view.setParent(textArea.getUI().getRootView(textArea));
413:                assertSame(c, view.getComponent());
414:            }
415:
416:            public void testModelToView() throws BadLocationException {
417:                testExceptionalCase(new BadLocationCase() {
418:                    @Override
419:                    public void exceptionalAction() throws Exception {
420:                        view.modelToView(1, new Rectangle(),
421:                                Position.Bias.Backward);
422:                    }
423:                });
424:                testExceptionalCase(new BadLocationCase() {
425:                    @Override
426:                    public void exceptionalAction() throws Exception {
427:                        view.modelToView(5, new Rectangle(),
428:                                Position.Bias.Forward);
429:                    }
430:                });
431:                testExceptionalCase(new BadLocationCase() {
432:                    @Override
433:                    public void exceptionalAction() throws Exception {
434:                        view.modelToView(500, new Rectangle(),
435:                                Position.Bias.Forward);
436:                    }
437:                });
438:                Shape box;
439:                box = view.modelToView(3, new Rectangle(2, 5, 20, 30),
440:                        Position.Bias.Backward);
441:                assertNotNull(box);
442:                checkBounds(2, 5, 30, box);
443:                box = view.modelToView(3, new Rectangle(2, 3, insertedComponent
444:                        .getWidth(), insertedComponent.getHeight()),
445:                        Position.Bias.Backward);
446:                assertNotNull(box);
447:                checkBounds(2, 3, insertedComponent.getHeight(), box);
448:                box = view.modelToView(4, new Rectangle(1, 1, 16, 7),
449:                        Position.Bias.Forward);
450:                assertNotNull(box);
451:                checkBounds(17, 1, 7, box);
452:                box = view.modelToView(4, new Rectangle(0, 0, 1, 1),
453:                        Position.Bias.Forward);
454:                assertNotNull(box);
455:                checkBounds(1, 0, 1, box);
456:                Ellipse2D ellipse = new Ellipse2D.Float(25, 3, 30, 40);
457:                box = view.modelToView(4, ellipse, Position.Bias.Forward);
458:                checkBounds(55, 3, 40, box);
459:                box = view.modelToView(4, ellipse, Position.Bias.Backward);
460:                assertNotNull(box);
461:                checkBounds(55, 3, 40, box);
462:                Rectangle2D rect = new Rectangle2D() {
463:                    @Override
464:                    public void setRect(double x, double y, double width,
465:                            double height) {
466:                    }
467:
468:                    @Override
469:                    public int outcode(double x, double y) {
470:                        return 0;
471:                    }
472:
473:                    @Override
474:                    public Rectangle2D createIntersection(Rectangle2D r) {
475:                        return null;
476:                    }
477:
478:                    @Override
479:                    public Rectangle2D createUnion(Rectangle2D r) {
480:                        return null;
481:                    }
482:
483:                    @Override
484:                    public double getX() {
485:                        return 1;
486:                    }
487:
488:                    @Override
489:                    public double getY() {
490:                        return 2;
491:                    }
492:
493:                    @Override
494:                    public double getWidth() {
495:                        return 50;
496:                    }
497:
498:                    @Override
499:                    public double getHeight() {
500:                        return 60;
501:                    }
502:
503:                    @Override
504:                    public boolean isEmpty() {
505:                        return false;
506:                    }
507:                };
508:                box = view.modelToView(4, rect, Position.Bias.Backward);
509:                assertNotNull(box);
510:                checkBounds(51, 2, 60, box);
511:            }
512:
513:            public void testViewToModel() {
514:                Shape shape;
515:                shape = new Rectangle(4, 3, 20, 30);
516:                checkViewToModel(shape, 1, 1);
517:                checkViewToModel(shape, 1, 5);
518:                checkViewToModel(shape, 1, 15);
519:                checkViewToModel(shape, 1, -5);
520:                checkViewToModel(shape, 12, 5);
521:                checkViewToModel(shape, 15, 5);
522:                checkViewToModel(shape, 16, 5);
523:                checkViewToModel(shape, 30, 5);
524:                checkViewToModel(shape, 46, 5);
525:                final Ellipse2D floatEllipse = new Ellipse2D.Float(25, 3, 3, 4);
526:                checkViewToModelWithEllipse(floatEllipse);
527:                Ellipse2D intEllipse = new Ellipse2D() {
528:                    @Override
529:                    public double getX() {
530:                        return 25;
531:                    }
532:
533:                    @Override
534:                    public double getY() {
535:                        return 3;
536:                    }
537:
538:                    @Override
539:                    public double getWidth() {
540:                        return 3;
541:                    }
542:
543:                    @Override
544:                    public double getHeight() {
545:                        return 4;
546:                    }
547:
548:                    @Override
549:                    public boolean isEmpty() {
550:                        return false;
551:                    }
552:
553:                    @Override
554:                    public void setFrame(double x, double y, double w, double h) {
555:                        return;
556:                    }
557:
558:                    public Rectangle2D getBounds2D() {
559:                        return null;
560:                    }
561:                };
562:                checkViewToModelWithEllipse(intEllipse);
563:            }
564:
565:            public void testPaint() {
566:                final Marker paintCalled = new Marker();
567:                JPanel panel = new JPanel() {
568:                    private static final long serialVersionUID = 1L;
569:
570:                    @Override
571:                    public void paint(Graphics graphics) {
572:                        throw new UnsupportedOperationException();
573:                    }
574:
575:                    @Override
576:                    public void paintImmediately(Rectangle rect) {
577:                        throw new UnsupportedOperationException();
578:                    }
579:
580:                    @Override
581:                    public void paintImmediately(int x, int y, int width,
582:                            int height) {
583:                        throw new UnsupportedOperationException();
584:                    }
585:
586:                    @Override
587:                    public void paintComponents(Graphics g) {
588:                        throw new UnsupportedOperationException();
589:                    }
590:
591:                    @Override
592:                    public void paintAll(Graphics g) {
593:                        throw new UnsupportedOperationException();
594:                    }
595:                };
596:                MutableAttributeSet attrs = new SimpleAttributeSet();
597:                StyleConstants.setComponent(attrs, panel);
598:                document.setCharacterAttributes(3, 1, attrs, true);
599:                view = new ComponentView(componentElement);
600:                assertNull(view.getParent());
601:                assertNull(view.getComponent());
602:                paintCalled.reset();
603:                view.paint(createTestGraphics(), new Rectangle(10, 10));
604:                assertFalse(paintCalled.isOccurred());
605:                view.setParent(textPane.getUI().getRootView(textPane));
606:                paintCalled.reset();
607:                view.paint(createTestGraphics(), new Rectangle(100, 100));
608:                assertFalse(paintCalled.isOccurred());
609:                assertSame(panel, view.getComponent());
610:                view.setParent(null);
611:                paintCalled.reset();
612:                view.paint(createTestGraphics(), new Rectangle(10, 10));
613:                assertFalse(paintCalled.isOccurred());
614:            }
615:
616:            public void testSetParent() {
617:                final Marker getViewCountCalled = new Marker();
618:                view = new ComponentView(componentElement) {
619:                    @Override
620:                    public int getViewCount() {
621:                        getViewCountCalled.setOccurred();
622:                        return super .getViewCount();
623:                    }
624:                };
625:                view.setParent(null);
626:                assertNull(view.getComponent());
627:                assertNull(view.getParent());
628:                assertNull(view.getContainer());
629:                view.setParent(textPane.getUI().getRootView(textPane));
630:                if (isHarmony()) {
631:                    assertSame(textPane, view.getComponent().getParent());
632:                } else {
633:                    assertSame(textPane, view.getComponent().getParent()
634:                            .getParent());
635:                }
636:                JTextArea textArea = new JTextArea();
637:                view.setParent(textArea.getUI().getRootView(textArea));
638:                assertNotNull(view.getParent());
639:                assertNotNull(view.getContainer());
640:                if (isHarmony()) {
641:                    assertSame(textPane, view.getComponent().getParent());
642:                } else {
643:                    assertNotSame(textPane, view.getComponent().getParent());
644:                    assertFalse(textPane
645:                            .equals(view.getComponent().getParent()));
646:                    assertSame(textPane, view.getComponent().getParent()
647:                            .getParent());
648:                }
649:                getViewCountCalled.reset();
650:                view.setParent(null);
651:                assertTrue(getViewCountCalled.isOccurred());
652:                assertNotNull(view.getComponent());
653:                assertNull(view.getContainer());
654:                assertNull(view.getParent());
655:                if (isHarmony()) {
656:                    assertNull(view.getComponent().getParent());
657:                } else {
658:                    assertNotNull(view.getComponent().getParent());
659:                    assertNull(view.getComponent().getParent().getParent());
660:                }
661:                Component c = view.getComponent();
662:                JPanel panel = new JPanel();
663:                MutableAttributeSet attrs = new SimpleAttributeSet();
664:                StyleConstants.setComponent(attrs, panel);
665:                document.setCharacterAttributes(3, 1, attrs, true);
666:                view.setParent(textPane.getUI().getRootView(textPane));
667:                assertSame(c, view.getComponent());
668:                if (isHarmony()) {
669:                    assertSame(textPane, view.getComponent().getParent());
670:                } else {
671:                    assertNotSame(textPane, view.getComponent().getParent());
672:                    assertFalse(textPane
673:                            .equals(view.getComponent().getParent()));
674:                    assertSame(textPane, view.getComponent().getParent()
675:                            .getParent());
676:                }
677:            }
678:
679:            private void checkBounds(final int x, final int y,
680:                    final int height, final Shape box) {
681:                Rectangle bounds = box.getBounds();
682:                assertEquals(x, bounds.x);
683:                assertEquals(y, bounds.y);
684:                assertEquals(0, bounds.width);
685:                assertEquals(height, bounds.height);
686:            }
687:
688:            private void checkViewToModelWithEllipse(final Ellipse2D ellipse) {
689:                if (isHarmony()) {
690:                    checkViewToModel(ellipse, 25, 15);
691:                    checkViewToModel(ellipse, 26, 5);
692:                } else {
693:                    testExceptionalCase(new ClassCastCase() {
694:                        @Override
695:                        public void exceptionalAction() throws Exception {
696:                            checkViewToModel(ellipse, 25, 15);
697:                        }
698:                    });
699:                    testExceptionalCase(new ClassCastCase() {
700:                        @Override
701:                        public void exceptionalAction() throws Exception {
702:                            checkViewToModel(ellipse, 26, 5);
703:                        }
704:                    });
705:                }
706:            }
707:
708:            private void checkViewToModel(final Shape shape, final int x,
709:                    final int y) {
710:                Position.Bias[] bias = new Position.Bias[1];
711:                int position = view.viewToModel(x, y, shape, bias);
712:                if (x > shape.getBounds().width / 2 + shape.getBounds().x - 1) {
713:                    assertEquals(Position.Bias.Backward, bias[0]);
714:                    assertEquals(position, componentElement.getEndOffset());
715:                } else {
716:                    assertEquals(Position.Bias.Forward, bias[0]);
717:                    assertEquals(position, componentElement.getStartOffset());
718:                }
719:            }
720:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.