Source Code Cross Referenced for JTextComponentTest.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.Color;
0023:        import java.awt.ComponentOrientation;
0024:        import java.awt.Graphics;
0025:        import java.awt.Insets;
0026:        import java.awt.Point;
0027:        import java.awt.Rectangle;
0028:        import java.awt.Robot;
0029:        import java.awt.Shape;
0030:        import java.awt.Toolkit;
0031:        import java.awt.datatransfer.Clipboard;
0032:        import java.awt.datatransfer.DataFlavor;
0033:        import java.awt.datatransfer.StringSelection;
0034:        import java.awt.datatransfer.UnsupportedFlavorException;
0035:        import java.awt.event.ActionEvent;
0036:        import java.awt.event.InputEvent;
0037:        import java.awt.event.InputMethodEvent;
0038:        import java.awt.event.InputMethodListener;
0039:        import java.awt.event.KeyEvent;
0040:        import java.awt.event.MouseEvent;
0041:        import java.awt.font.TextAttribute;
0042:        import java.awt.font.TextHitInfo;
0043:        import java.beans.PropertyChangeEvent;
0044:        import java.beans.PropertyChangeListener;
0045:        import java.io.IOException;
0046:        import java.io.StringReader;
0047:        import java.io.StringWriter;
0048:        import java.util.Vector;
0049:        import javax.swing.Action;
0050:        import javax.swing.JFrame;
0051:        import javax.swing.JTextArea;
0052:        import javax.swing.JTextField;
0053:        import javax.swing.KeyStroke;
0054:        import javax.swing.SwingTestCase;
0055:        import javax.swing.TransferHandler;
0056:        import javax.swing.UIDefaults;
0057:        import javax.swing.UIManager;
0058:        import javax.swing.event.CaretEvent;
0059:        import javax.swing.event.CaretListener;
0060:
0061:        public class JTextComponentTest extends SwingTestCase {
0062:            JFrame jf;
0063:
0064:            JTextArea jtc;
0065:
0066:            SimplePropertyChangeListener pChListener;
0067:
0068:            boolean bWasException;
0069:
0070:            String s;
0071:
0072:            Color color;
0073:
0074:            String strOrderFireCaretUpdate;
0075:
0076:            String strOrderProcessInputMethodEventCaret;
0077:
0078:            String strOrderProcessInputMethodEventText;
0079:
0080:            String sRTL = "\u05DC";
0081:
0082:            String sLTR = "\u0061";
0083:
0084:            NavigationFilter navFilter;
0085:
0086:            JTextComp jtComp;
0087:
0088:            AbstractDocument docXXX;
0089:
0090:            JTextField jep;
0091:
0092:            Robot robot;
0093:
0094:            Rectangle rect;
0095:
0096:            String pattern = "@[^,}]*";
0097:
0098:            class SimpleInputMethodListener implements  InputMethodListener {
0099:                String name;
0100:
0101:                public SimpleInputMethodListener(final String s) {
0102:                    name = s;
0103:                }
0104:
0105:                public void caretPositionChanged(final InputMethodEvent e) {
0106:                    strOrderProcessInputMethodEventCaret += name;
0107:                }
0108:
0109:                public void inputMethodTextChanged(final InputMethodEvent e) {
0110:                    strOrderProcessInputMethodEventText += name;
0111:                }
0112:            }
0113:
0114:            void isElement(final Object[] a, final Object b, final int count) {
0115:                assertNotNull(a);
0116:                boolean cond = false;
0117:                int k = 0;
0118:                for (int i = 0; i < a.length; i++) {
0119:                    if (a[i] == b) {
0120:                        cond = true;
0121:                        k += 1;
0122:                    }
0123:                }
0124:                assertTrue(cond);
0125:                assertEquals(count, k);
0126:            }
0127:
0128:            void assertEqualsPropertyChangeEvent(final String name,
0129:                    final Object oldValue, final Object newValue,
0130:                    final PropertyChangeEvent e) {
0131:                assertEquals(name, e.getPropertyName());
0132:                assertEquals(oldValue, e.getOldValue());
0133:                assertEquals(newValue, e.getNewValue());
0134:            }
0135:
0136:            class SimpleKeyMap implements  Keymap {
0137:                String name;
0138:
0139:                Keymap parent;
0140:
0141:                public SimpleKeyMap(final String s) {
0142:                    name = s;
0143:                }
0144:
0145:                public void addActionForKeyStroke(final KeyStroke k,
0146:                        final Action a) {
0147:                }
0148:
0149:                public Action getAction(final KeyStroke k) {
0150:                    return null;
0151:                }
0152:
0153:                public Action[] getBoundActions() {
0154:                    return null;
0155:                }
0156:
0157:                public Action getDefaultAction() {
0158:                    return null;
0159:                }
0160:
0161:                public KeyStroke[] getKeyStrokesForAction(final Action arg0) {
0162:                    return null;
0163:                }
0164:
0165:                public Keymap getResolveParent() {
0166:                    return parent;
0167:                }
0168:
0169:                public KeyStroke[] getBoundKeyStrokes() {
0170:                    return null;
0171:                }
0172:
0173:                public void removeBindings() {
0174:                }
0175:
0176:                public String getName() {
0177:                    return name;
0178:                }
0179:
0180:                public void setDefaultAction(final Action arg0) {
0181:                }
0182:
0183:                public void setResolveParent(final Keymap keymap) {
0184:                    parent = keymap;
0185:                }
0186:
0187:                public boolean isLocallyDefined(final KeyStroke arg0) {
0188:                    return false;
0189:                }
0190:
0191:                public void removeKeyStrokeBinding(final KeyStroke arg0) {
0192:                }
0193:            }
0194:
0195:            class JTextComp extends JTextComponent {
0196:                private static final long serialVersionUID = 1L;
0197:
0198:                String UIClassId = "TextCompUIFirst";
0199:
0200:                @Override
0201:                public String getUIClassID() {
0202:                    return (UIClassId != null) ? UIClassId : "TextCompUIFirst";
0203:                }
0204:            }
0205:
0206:            class SimpleCaretListener implements  CaretListener {
0207:                String name;
0208:
0209:                SimpleCaretListener(final String s) {
0210:                    name = s;
0211:                }
0212:
0213:                public void caretUpdate(final CaretEvent ce) {
0214:                    strOrderFireCaretUpdate = strOrderFireCaretUpdate + name;
0215:                }
0216:            }
0217:
0218:            class SimplePropertyChangeListener implements 
0219:                    PropertyChangeListener {
0220:                PropertyChangeEvent event;
0221:
0222:                public void propertyChange(final PropertyChangeEvent e) {
0223:                    if (e.getPropertyName() != "ancestor") {
0224:                        event = e;
0225:                    }
0226:                }
0227:
0228:                PropertyChangeEvent getEvent() {
0229:                    PropertyChangeEvent e = event;
0230:                    event = null;
0231:                    return e;
0232:                }
0233:            }
0234:
0235:            class SimpleTransferHandler extends TransferHandler {
0236:                private static final long serialVersionUID = 1L;
0237:            }
0238:
0239:            private class SimpleTextAction extends TextAction {
0240:                private static final long serialVersionUID = 1L;
0241:
0242:                public SimpleTextAction(final String name) {
0243:                    super (name);
0244:                }
0245:
0246:                public void actionPerformed(final ActionEvent e) {
0247:                }
0248:            }
0249:
0250:            public JTextComponentTest() {
0251:                setIgnoreNotImplemented(true);
0252:            }
0253:
0254:            @Override
0255:            protected void setUp() throws Exception {
0256:                jf = new JFrame();
0257:                bWasException = false;
0258:                s = null;
0259:                navFilter = new NavigationFilter();
0260:                strOrderFireCaretUpdate = "";
0261:                strOrderProcessInputMethodEventCaret = "";
0262:                strOrderProcessInputMethodEventText = "";
0263:                pChListener = new SimplePropertyChangeListener();
0264:                color = new Color(15, 15, 16);
0265:                jtc = new JTextArea();
0266:                jtc.addPropertyChangeListener(pChListener);
0267:                jf.getContentPane().add(jtc);
0268:                jf.setLocation(200, 300);
0269:                jf.setSize(300, 200);
0270:                jf.pack();
0271:            }
0272:
0273:            @Override
0274:            protected void tearDown() throws Exception {
0275:                jf.dispose();
0276:                super .tearDown();
0277:            }
0278:
0279:            public void testRemoveNotify() throws Exception {
0280:            }
0281:
0282:            public void testGetToolTipTextMouseEvent() throws Exception {
0283:                jf.dispose();
0284:                jf = new JFrame();
0285:                jtc = new JTextArea("just for prefSize");
0286:                jf.getContentPane().add(jtc);
0287:                jf.setVisible(true);
0288:                jf.pack();
0289:                rect = null;
0290:                try {
0291:                    rect = jtc.modelToView(0);
0292:                } catch (BadLocationException e) {
0293:                }
0294:                assertNotNull(rect);
0295:                MouseEvent me = new MouseEvent(jtc, MouseEvent.MOUSE_PRESSED,
0296:                        0, InputEvent.BUTTON1_MASK, jtc.getX() + rect.x, jtc
0297:                                .getY()
0298:                                + rect.y, 0, false, MouseEvent.BUTTON1);
0299:                assertEquals(jtc.getUI().getToolTipText(jtc,
0300:                        new Point(rect.x, rect.y)), jtc.getToolTipText(me));
0301:                String s = "ToolTipText";
0302:                jtc.setToolTipText(s);
0303:                assertEquals(s, jtc.getToolTipText(me));
0304:            }
0305:
0306:            public void testSetGetNavigationFilter() throws Exception {
0307:                assertNull(jtc.getNavigationFilter());
0308:                jtc.setNavigationFilter(navFilter);
0309:            }
0310:
0311:            public void testSetGetKeymap() throws Exception {
0312:                Keymap keyMap1 = jtc.getKeymap();
0313:                assertNotNull(keyMap1);
0314:                SimpleKeyMap keyMap2 = new SimpleKeyMap("Second");
0315:                jtc.setKeymap(keyMap2);
0316:                assertEqualsPropertyChangeEvent("keymap", keyMap1, keyMap2,
0317:                        pChListener.event);
0318:                assertEquals(keyMap2, jtc.getKeymap());
0319:            }
0320:
0321:            public void testSetGetHighlighter() throws Exception {
0322:                DefaultHighlighter dh1 = (DefaultHighlighter) jtc
0323:                        .getHighlighter();
0324:                DefaultHighlighter dh2 = new DefaultHighlighter();
0325:                assertTrue(jtc.getHighlighter() instanceof  DefaultHighlighter);
0326:                jtc.setHighlighter(dh2);
0327:                assertEqualsPropertyChangeEvent("highlighter", dh1, dh2,
0328:                        pChListener.event);
0329:                assertEquals(dh2, jtc.getHighlighter());
0330:            }
0331:
0332:            public void testSetGetDocument() throws Exception {
0333:                assertNotNull(jtc.getDocument());
0334:                jtc.setText("testSetGetDocument");
0335:                Highlighter highlighter = jtc.getHighlighter();
0336:                Highlighter.HighlightPainter painter = new Highlighter.HighlightPainter() {
0337:                    public void paint(Graphics g, int p1, int p2, Shape shape,
0338:                            JTextComponent c) {
0339:                    }
0340:                };
0341:                highlighter.addHighlight(0, 3, painter);
0342:                highlighter.addHighlight(2, 5, painter);
0343:                Document oldDoc = jtc.getDocument();
0344:                PlainDocument doc = new PlainDocument();
0345:                jtc.setDocument(doc);
0346:                if (isHarmony()) {
0347:                    assertEquals(0, jtc.getHighlighter().getHighlights().length);
0348:                }
0349:                assertEqualsPropertyChangeEvent("document", oldDoc, doc,
0350:                        pChListener.event);
0351:                assertEquals(doc, jtc.getDocument());
0352:            }
0353:
0354:            public void testSetGetCaret() throws Exception {
0355:                DefaultCaret dc1 = (DefaultCaret) jtc.getCaret();
0356:                DefaultCaret dc2 = new DefaultCaret();
0357:                assertTrue(jtc.getCaret() instanceof  DefaultCaret);
0358:                jtc.setCaret(dc2);
0359:                assertEqualsPropertyChangeEvent("caret", dc1, dc2,
0360:                        pChListener.event);
0361:                assertEquals(dc2, jtc.getCaret());
0362:            }
0363:
0364:            public void assertEquals(
0365:                    final Vector<SimpleCaretListener> listeners1,
0366:                    final CaretListener[] listeners2) {
0367:                assertNotNull(listeners1);
0368:                assertNotNull(listeners2);
0369:                assertEquals(listeners1.size(), listeners2.length);
0370:                for (int i = 0; i < listeners1.size(); i++) {
0371:                    assertEquals(listeners1.get(i), listeners2[i]);
0372:                }
0373:            }
0374:
0375:            public void testCaretListeners() {
0376:                jtc.addCaretListener(null);
0377:                SimpleCaretListener listener1 = new SimpleCaretListener("1");
0378:                SimpleCaretListener listener2 = new SimpleCaretListener("2");
0379:                SimpleCaretListener listener3 = new SimpleCaretListener("3");
0380:                Vector<SimpleCaretListener> listeners = new Vector<SimpleCaretListener>();
0381:                jtc.addCaretListener(listener1);
0382:                listeners.add(listener1);
0383:                assertEquals(listeners, jtc.getCaretListeners());
0384:                jtc.addCaretListener(listener2);
0385:                listeners.add(0, listener2);
0386:                assertEquals(listeners, jtc.getCaretListeners());
0387:                jtc.addCaretListener(listener3);
0388:                listeners.add(0, listener3);
0389:                assertEquals(listeners, jtc.getCaretListeners());
0390:                jtc.removeCaretListener(listener2);
0391:                listeners.remove(listener2);
0392:                assertEquals(listeners, jtc.getCaretListeners());
0393:                jtc.removeCaretListener(listener3);
0394:                listeners.remove(listener3);
0395:                assertEquals(listeners, jtc.getCaretListeners());
0396:                jtc.removeCaretListener(listener1);
0397:                listeners.remove(listener1);
0398:                assertEquals(listeners, jtc.getCaretListeners());
0399:            }
0400:
0401:            public void testFireCaretUpdate() throws Exception {
0402:                jtc.setText("JTextComponent");
0403:                SimpleCaretListener listener1 = new SimpleCaretListener("1");
0404:                SimpleCaretListener listener2 = new SimpleCaretListener("2");
0405:                SimpleCaretListener listener3 = new SimpleCaretListener("3");
0406:                jtc.addCaretListener(listener1);
0407:                jtc.addCaretListener(listener2);
0408:                jtc.addCaretListener(listener3);
0409:                jtc.addCaretListener(listener1);
0410:                assertEquals(strOrderFireCaretUpdate, "");
0411:                jtc.setCaretPosition(5);
0412:                assertEquals("1321", strOrderFireCaretUpdate);
0413:            }
0414:
0415:            // Regression for HARMONY-2819
0416:            public void testFireCaretUpdateNull() throws Exception {
0417:                new JTextArea().fireCaretUpdate(null);
0418:                // no exception is expected
0419:            }
0420:
0421:            public void testSetGetText() throws Exception {
0422:                assertTrue(jtc.getText().equals(""));
0423:                jtc.setText("JTextComponent");
0424:                assertTrue(jtc.getText().equals("JTextComponent"));
0425:                String str = "";
0426:                try {
0427:                    str = jtc.getText(5, 3);
0428:                } catch (BadLocationException e) {
0429:                    bWasException = true;
0430:                    s = e.getMessage();
0431:                }
0432:                assertFalse("Unexpected exception: " + s, bWasException);
0433:                assertTrue(str.equals("Com"));
0434:            }
0435:
0436:            public void testReplaceSelection() throws Exception {
0437:                jtc.setText("JTextComponent");
0438:                jtc.select(5, 8);
0439:                jtc.replaceSelection(null);
0440:                assertEquals("JTextponent", jtc.getText());
0441:                assertNull(jtc.getSelectedText());
0442:                jtc.select(5, 8);
0443:                jtc.replaceSelection("XXX");
0444:                assertNull(jtc.getSelectedText());
0445:                assertEquals("JTextXXXent", jtc.getText());
0446:                jtc.setText("JTextComponent");
0447:                jtc.setCaretPosition(2);
0448:                jtc.replaceSelection("XXX");
0449:                assertNull(jtc.getSelectedText());
0450:                assertEquals("JTXXXextComponent", jtc.getText());
0451:            }
0452:
0453:            public void testReadWrite() throws Exception {
0454:                String s = "JTextComponent\nRead\nWrite\n";
0455:                String sProperty = "CurrentStreamDescriptionProperty";
0456:                StringWriter writer = new StringWriter();
0457:                jtc.setText(s);
0458:                try {
0459:                    jtc.write(writer);
0460:                } catch (IOException e) {
0461:                }
0462:                jtc.setText("temporary");
0463:                assertEquals("temporary", jtc.getText());
0464:                StringReader reader = new StringReader(writer.toString());
0465:                try {
0466:                    jtc.read(reader, sProperty);
0467:                } catch (IOException e) {
0468:                }
0469:                assertEquals(s, jtc.getText());
0470:                assertEquals(sProperty, jtc.getDocument().getProperty(
0471:                        Document.StreamDescriptionProperty));
0472:                assertTrue(jtc.getDocument() instanceof  PlainDocument);
0473:            }
0474:
0475:            public void testAddInputMethodListener() {
0476:                SimpleInputMethodListener listener1 = new SimpleInputMethodListener(
0477:                        "1");
0478:                SimpleInputMethodListener listener2 = new SimpleInputMethodListener(
0479:                        "2");
0480:                SimpleInputMethodListener listener3 = new SimpleInputMethodListener(
0481:                        "3");
0482:                jtc.addInputMethodListener(listener1);
0483:                jtc.addInputMethodListener(listener2);
0484:                jtc.addInputMethodListener(listener3);
0485:                InputMethodListener listeners[] = jtc.getInputMethodListeners();
0486:                assertEquals(listener1, listeners[0]);
0487:                assertEquals(listener2, listeners[1]);
0488:                assertEquals(listener3, listeners[2]);
0489:                assertEquals(3, listeners.length);
0490:            }
0491:
0492:            void imEventTest(final int id, final String s1, final String s2) {
0493:                InputMethodEvent event = new InputMethodEvent(jtc, id, null,
0494:                        null);
0495:                jtc.processInputMethodEvent(event);
0496:                assertEquals(s1, strOrderProcessInputMethodEventCaret);
0497:                assertEquals(s2, strOrderProcessInputMethodEventText);
0498:                strOrderProcessInputMethodEventCaret = "";
0499:                strOrderProcessInputMethodEventText = "";
0500:            }
0501:
0502:            public void testProcessInputMethodEventInputMethodEvent()
0503:                    throws Exception {
0504:                SimpleInputMethodListener listener1 = new SimpleInputMethodListener(
0505:                        "1");
0506:                SimpleInputMethodListener listener2 = new SimpleInputMethodListener(
0507:                        "2");
0508:                SimpleInputMethodListener listener3 = new SimpleInputMethodListener(
0509:                        "3");
0510:                jtc.addInputMethodListener(listener1);
0511:                jtc.addInputMethodListener(listener2);
0512:                jtc.addInputMethodListener(listener3);
0513:                TextHitInfo textHitInfo = TextHitInfo.afterOffset(0);
0514:                assertNotNull(textHitInfo);
0515:                jtc.setText("JTextComponent");
0516:                jtc.enableInputMethods(true);
0517:                imEventTest(InputMethodEvent.CARET_POSITION_CHANGED, "123", "");
0518:                imEventTest(InputMethodEvent.INPUT_METHOD_TEXT_CHANGED, "",
0519:                        "123");
0520:                imEventTest(InputMethodEvent.INPUT_METHOD_FIRST, "", "123");
0521:                imEventTest(InputMethodEvent.INPUT_METHOD_LAST, "123", "");
0522:            }
0523:
0524:            public void testSetGetMargin() throws Exception {
0525:                Insets insets1 = new Insets(0, 0, 0, 0);
0526:                assertEquals(insets1, jtc.getMargin());
0527:                Insets insets2 = new Insets(10, 20, 30, 40);
0528:                jtc.setMargin(insets2);
0529:                assertEqualsPropertyChangeEvent("margin", insets1, insets2,
0530:                        pChListener.event);
0531:                assertEquals(insets2, jtc.getMargin());
0532:            }
0533:
0534:            public void testSetGetComponentOrientation() throws Exception {
0535:                assertEquals(ComponentOrientation.UNKNOWN, jtc
0536:                        .getComponentOrientation());
0537:                docXXX = (AbstractDocument) jtc.getDocument();
0538:                jtc.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
0539:                assertEquals(new Boolean(true), docXXX
0540:                        .getProperty(TextAttribute.RUN_DIRECTION));
0541:                jtc.setText("JTextComponent");
0542:                assertEquals(2, docXXX.getBidiRootElement().getElementCount());
0543:                assertEquals(ComponentOrientation.RIGHT_TO_LEFT, jtc
0544:                        .getComponentOrientation());
0545:                jtc.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
0546:                assertEquals(new Boolean(false), docXXX
0547:                        .getProperty(TextAttribute.RUN_DIRECTION));
0548:                jtc.setText("JTextComponent");
0549:                assertEquals(ComponentOrientation.LEFT_TO_RIGHT, jtc
0550:                        .getComponentOrientation());
0551:                jtc.setComponentOrientation(ComponentOrientation.UNKNOWN);
0552:                assertEquals(new Boolean(false), docXXX
0553:                        .getProperty(TextAttribute.RUN_DIRECTION));
0554:                jtc.setText("JTextComponent");
0555:                assertEquals(ComponentOrientation.UNKNOWN, jtc
0556:                        .getComponentOrientation());
0557:            }
0558:
0559:            public void testSetGetColors() throws Exception {
0560:                Color color1 = getColorProperty("caretForeground");
0561:                assertEquals(color1, jtc.getCaretColor());
0562:                jtc.setCaretColor(color);
0563:                assertEqualsPropertyChangeEvent("caretColor", color1, color,
0564:                        pChListener.event);
0565:                assertEquals(color, jtc.getCaretColor());
0566:                color1 = getColorProperty("selectionBackground");
0567:                assertEquals(color1, jtc.getSelectionColor());
0568:                jtc.setSelectionColor(color);
0569:                assertEqualsPropertyChangeEvent("selectionColor", color1,
0570:                        color, pChListener.event);
0571:                assertEquals(color, jtc.getSelectionColor());
0572:                color1 = getColorProperty("inactiveForeground");
0573:                assertEquals(color1, jtc.getDisabledTextColor());
0574:                jtc.setDisabledTextColor(color);
0575:                assertEqualsPropertyChangeEvent("disabledTextColor", color1,
0576:                        color, pChListener.event);
0577:                assertEquals(color, jtc.getDisabledTextColor());
0578:                color1 = getColorProperty("selectionForeground");
0579:                assertEquals(color1, jtc.getSelectedTextColor());
0580:                jtc.setSelectedTextColor(color);
0581:                assertEqualsPropertyChangeEvent("selectedTextColor", color1,
0582:                        color, pChListener.event);
0583:                assertEquals(color, jtc.getSelectedTextColor());
0584:            }
0585:
0586:            public void testSetIsEditable() throws Exception {
0587:                assertTrue(jtc.isEditable());
0588:                jtc.setEditable(false);
0589:                assertEqualsPropertyChangeEvent("editable", new Boolean(true),
0590:                        new Boolean(false), pChListener.event);
0591:                assertFalse(jtc.isEditable());
0592:            }
0593:
0594:            public void testSetGetDragEnabled() throws Exception {
0595:                /*
0596:                 * class RunnableCase0 extends RunnableWrap { public void run() {
0597:                 * assertFalse(jtc.getDragEnabled());
0598:                 * assertNotNull(jtc.getTransferHandler()); TransferHandler th =
0599:                 * jtc.getTransferHandler(); jtc.setDragEnabled(true);
0600:                 * assertTrue(jtc.getDragEnabled());
0601:                 * assertNotNull(jtc.getTransferHandler());
0602:                 * assertEquals(th,jtc.getTransferHandler());
0603:                 * jtc.setText("JTextComponent"); jtc.select(5,8); } }
0604:                 *
0605:                 * SwingUtilities.invokeAndWait(new RunnableCase0());
0606:                 * InternalTests.isRealized(jf); throwEx(afe);
0607:                 *
0608:                 * robot = null; try { robot = new Robot(); } catch(AWTException e){}
0609:                 *
0610:                 * try { rect = jtc.modelToView(8); }catch(BadLocationException e){}
0611:                 *
0612:                 * Point p = jtc.getLocationOnScreen();
0613:                 *
0614:                 * robot.mouseMove(rect.x -14 + p.x,rect.y +2 + p.y);
0615:                 * robot.mousePress(InputEvent.BUTTON1_MASK); try { Thread.sleep(500); }
0616:                 * catch(Exception e){}
0617:                 *
0618:                 * try { rect = jtc.modelToView(4); }catch(BadLocationException e){}
0619:                 *
0620:                 * robot.mouseMove(rect.x + p.x,rect.y+2 + p.y); robot.waitForIdle();
0621:                 *
0622:                 * try { rect = jtc.modelToView(2); }catch(BadLocationException e){}
0623:                 *
0624:                 *
0625:                 * robot.mouseMove(rect.x + p.x,rect.y+2 + p.y);
0626:                 * robot.mouseRelease(InputEvent.BUTTON1_MASK);
0627:                 *
0628:                 * try { Thread.sleep(500); } catch(Exception e){}
0629:                 *
0630:                 *
0631:                 * assertEquals(5,jtc.getCaretPosition());
0632:                 * assertNull(jtc.getSelectedText());
0633:                 * assertEquals("JTComextponent",jtc.getText());
0634:                 *
0635:                 * throwEx(afe);
0636:                 */
0637:            }
0638:
0639:            public void testSelect() throws Exception {
0640:                jtc.setText("JTextComponent");
0641:                jtc.select(5, 8);
0642:                assertEquals("Com", jtc.getSelectedText());
0643:                assertEquals(5, jtc.getCaret().getMark());
0644:                assertEquals(8, jtc.getCaret().getDot());
0645:                jtc.select(-2, -1);
0646:                assertNull(jtc.getSelectedText());
0647:                assertEquals(0, jtc.getCaret().getMark());
0648:                assertEquals(0, jtc.getCaret().getDot());
0649:                jtc.select(-5, 16);
0650:                assertEquals("JTextComponent", jtc.getSelectedText());
0651:                assertEquals(0, jtc.getCaret().getMark());
0652:                assertEquals(14, jtc.getCaret().getDot());
0653:                jtc.select(17, 18);
0654:                assertNull(jtc.getSelectedText());
0655:                assertEquals(14, jtc.getCaret().getMark());
0656:                assertEquals(14, jtc.getCaret().getDot());
0657:                jtc.select(8, 5);
0658:                assertNull(jtc.getSelectedText());
0659:                assertEquals(8, jtc.getCaret().getMark());
0660:                assertEquals(8, jtc.getCaret().getDot());
0661:                jtc.select(8, 8);
0662:                assertNull(jtc.getSelectedText());
0663:                assertEquals(8, jtc.getCaret().getMark());
0664:                assertEquals(8, jtc.getCaret().getDot());
0665:            }
0666:
0667:            public void testSetGetSelectionStartEnd() throws Exception {
0668:                if (!isHarmony()) {
0669:                    return;
0670:                }
0671:                assertEquals(0, jtc.getSelectionStart());
0672:                assertEquals(0, jtc.getSelectionEnd());
0673:                jtc.setText("JTextComponent");
0674:                assertEquals(14, jtc.getSelectionStart());
0675:                assertEquals(14, jtc.getSelectionEnd());
0676:                jtc.setSelectionStart(5);
0677:                jtc.setSelectionEnd(8);
0678:                assertEquals("Com", jtc.getSelectedText());
0679:                assertEquals(5, jtc.getSelectionStart());
0680:                assertEquals(8, jtc.getSelectionEnd());
0681:                assertEquals(8, jtc.getCaret().getDot());
0682:                assertEquals(5, jtc.getCaret().getMark());
0683:                jtc.setSelectionStart(8);
0684:                jtc.setSelectionEnd(5);
0685:                assertNull(jtc.getSelectedText());
0686:                assertEquals(5, jtc.getSelectionStart());
0687:                assertEquals(5, jtc.getSelectionEnd());
0688:                assertEquals(5, jtc.getCaret().getDot());
0689:                assertEquals(5, jtc.getCaret().getMark());
0690:                jtc.setCaretPosition(4);
0691:                jtc.moveCaretPosition(6);
0692:                assertEquals(4, jtc.getSelectionStart());
0693:                assertEquals(6, jtc.getSelectionEnd());
0694:                jtc.setCaretPosition(6);
0695:                jtc.moveCaretPosition(3);
0696:                assertEquals(3, jtc.getSelectionStart());
0697:                assertEquals(6, jtc.getSelectionEnd());
0698:                jtc.setSelectionStart(4);
0699:                assertEquals(4, jtc.getCaret().getDot());
0700:                assertEquals(6, jtc.getCaret().getMark());
0701:                assertEquals(4, jtc.getSelectionStart());
0702:                assertEquals(6, jtc.getSelectionEnd());
0703:                jtc.setSelectionEnd(3);
0704:                assertEquals(3, jtc.getCaret().getDot());
0705:                assertEquals(3, jtc.getCaret().getMark());
0706:                assertEquals(3, jtc.getSelectionStart());
0707:                assertEquals(3, jtc.getSelectionEnd());
0708:                assertNull(jtc.getSelectedText());
0709:                jtc.setSelectionStart(8);
0710:                assertEquals(8, jtc.getCaret().getDot());
0711:                assertEquals(8, jtc.getCaret().getMark());
0712:                assertEquals(8, jtc.getSelectionStart());
0713:                assertEquals(8, jtc.getSelectionEnd());
0714:                jtc.setSelectionEnd(10);
0715:                assertEquals(10, jtc.getCaret().getDot());
0716:                assertEquals(8, jtc.getCaret().getMark());
0717:                assertEquals(8, jtc.getSelectionStart());
0718:                assertEquals(10, jtc.getSelectionEnd());
0719:                jtc.setSelectionStart(9);
0720:                assertEquals(9, jtc.getCaret().getDot());
0721:                assertEquals(10, jtc.getCaret().getMark());
0722:                assertEquals(9, jtc.getSelectionStart());
0723:                assertEquals(10, jtc.getSelectionEnd());
0724:                jtc.setCaretPosition(8);
0725:                jtc.moveCaretPosition(4);
0726:                assertEquals("tCom", jtc.getSelectedText());
0727:                jtc.setSelectionEnd(6);
0728:                assertEquals(6, jtc.getCaret().getDot());
0729:                assertEquals(4, jtc.getCaret().getMark());
0730:                assertEquals(4, jtc.getSelectionStart());
0731:                assertEquals(6, jtc.getSelectionEnd());
0732:            }
0733:
0734:            public void testSetGetCaretPosition() throws Exception {
0735:                assertEquals(0, jtc.getCaretPosition());
0736:                jtc.setText("JTextComponent");
0737:                assertEquals(14, jtc.getCaretPosition());
0738:                jtc.setCaretPosition(5);
0739:                assertEquals(5, jtc.getCaretPosition());
0740:                bWasException = false;
0741:                s = null;
0742:                try {
0743:                    jtc.setCaretPosition(-2);
0744:                } catch (IllegalArgumentException e) {
0745:                    bWasException = true;
0746:                    s = e.getMessage();
0747:                }
0748:                assertTrue(bWasException);
0749:                assertEquals("bad position: -2", s);
0750:                bWasException = false;
0751:                s = null;
0752:                try {
0753:                    jtc.setCaretPosition(20);
0754:                } catch (IllegalArgumentException e) {
0755:                    bWasException = true;
0756:                    s = e.getMessage();
0757:                }
0758:                assertTrue(bWasException);
0759:                assertEquals("bad position: 20", s);
0760:            }
0761:
0762:            public void testMoveCaretPosition() throws Exception {
0763:                jtc.setText("JTextComponent");
0764:                jtc.setCaretPosition(5);
0765:                jtc.moveCaretPosition(8);
0766:                assertEquals(8, jtc.getCaretPosition());
0767:                bWasException = false;
0768:                s = null;
0769:                try {
0770:                    jtc.moveCaretPosition(-2);
0771:                } catch (IllegalArgumentException e) {
0772:                    bWasException = true;
0773:                    s = e.getMessage();
0774:                }
0775:                assertTrue(bWasException);
0776:                assertEquals("bad position: -2", s);
0777:                bWasException = false;
0778:                s = null;
0779:                try {
0780:                    jtc.moveCaretPosition(20);
0781:                } catch (IllegalArgumentException e) {
0782:                    bWasException = true;
0783:                    s = e.getMessage();
0784:                }
0785:                assertTrue(bWasException);
0786:                assertEquals("bad position: 20", s);
0787:            }
0788:
0789:            public void testSetGetFocusAccelerator() throws Exception {
0790:                //TODO It's very strange but in 1.5.0 PropertyChangeEvent's
0791:                //name doesn't equal JTextComponent.FOCUS_ACCELERATOR_KEY
0792:                String name = JTextComponent.FOCUS_ACCELERATOR_KEY;
0793:                //String name = "focusAccelerator";
0794:                assertEquals('\0', jtc.getFocusAccelerator());
0795:                jtc.setFocusAccelerator('a');
0796:                assertSame(name, pChListener.event.getPropertyName());
0797:                assertEqualsPropertyChangeEvent(name, new Character('\0'),
0798:                        new Character('A'), pChListener.event);
0799:                assertEquals('A', jtc.getFocusAccelerator());
0800:                jtc.setFocusAccelerator('B');
0801:                assertEqualsPropertyChangeEvent(name, new Character('A'),
0802:                        new Character('B'), pChListener.event);
0803:                assertEquals('B', jtc.getFocusAccelerator());
0804:            }
0805:
0806:            public void testSelectAll() throws Exception {
0807:                jtc.selectAll();
0808:                assertNull(jtc.getSelectedText());
0809:                jtc.setText("JTextComponent");
0810:                jtc.selectAll();
0811:                assertEquals("JTextComponent", jtc.getSelectedText());
0812:                assertEquals(14, jtc.getCaret().getDot());
0813:                assertEquals(0, jtc.getCaret().getMark());
0814:                jtc.setText("\u05DC" + "\u05DC" + "\u05DC" + "\u05DC");
0815:                jtc.selectAll();
0816:                assertEquals(4, jtc.getCaret().getDot());
0817:                assertEquals(0, jtc.getCaret().getMark());
0818:            }
0819:
0820:            public void testPaste() throws Exception {
0821:                // TODO: uncomment when System clipboard is properly supported
0822:                //        if (jtc.getToolkit().getSystemClipboard() == null)
0823:                //            return;
0824:                //        jtc.setText("JTextComponent");
0825:                //        setClipboardString(jtc, "XXX");
0826:                //        jtc.setCaretPosition(5);
0827:                //        jtc.paste();
0828:                //        assertEquals("XXX", getClipboardString(jtc));
0829:                //        assertNull(jtc.getSelectedText());
0830:                //        assertEquals("JTextXXXComponent", jtc.getText());
0831:                //
0832:                //        jtc.select(10, 14);
0833:                //        setClipboardString(jtc, "YYY");
0834:                //        jtc.paste();
0835:                //        assertEquals("YYY", getClipboardString(jtc));
0836:                //        assertNull(jtc.getSelectedText());
0837:                //        assertEquals("JTextXXXCoYYYent", jtc.getText());
0838:                //
0839:                //        jtc.select(14, 16);
0840:                //        setClipboardString(jtc, "");
0841:                //        jtc.paste();
0842:                //        assertEquals("", getClipboardString(jtc));
0843:                //        //TODO ??
0844:                //        assertNull(jtc.getSelectedText());
0845:                //        assertEquals("JTextXXXCoYYYe", jtc.getText());
0846:            }
0847:
0848:            public void testCut() throws Exception {
0849:                // TODO: uncomment when System clipboard is properly supported
0850:                //        if (jtc.getToolkit().getSystemClipboard() == null)
0851:                //            return;
0852:                //        jtc.setText("JTextComponent");
0853:                //        setClipboardString(jtc, "XXX");
0854:                //        jtc.cut();
0855:                //        assertEquals("XXX", getClipboardString(jtc));
0856:                //        assertNull(jtc.getSelectedText());
0857:                //        assertEquals("JTextComponent", jtc.getText());
0858:                //
0859:                //        jtc.select(4, 8);
0860:                //        jtc.cut();
0861:                //        assertEquals("tCom", getClipboardString(jtc));
0862:                //        assertNull(jtc.getSelectedText());
0863:                //        assertEquals("JTexponent", jtc.getText());
0864:            }
0865:
0866:            String getClipboardString(final JTextComponent jtc) {
0867:                String content = null;
0868:                Toolkit toolkit = jtc.getToolkit();
0869:                Clipboard clipboard = toolkit.getSystemClipboard();
0870:                DataFlavor dataFlavor = DataFlavor.stringFlavor;
0871:                try {
0872:                    content = (String) clipboard.getContents(null)
0873:                            .getTransferData(dataFlavor);
0874:                } catch (UnsupportedFlavorException e) {
0875:                } catch (IOException e) {
0876:                }
0877:                return content;
0878:            }
0879:
0880:            void setClipboardString(final JTextComponent jtc,
0881:                    final String content) {
0882:                Toolkit toolkit = jtc.getToolkit();
0883:                Clipboard clipboard = toolkit.getSystemClipboard();
0884:                StringSelection dataFlavor = new StringSelection(content);
0885:                clipboard.setContents(dataFlavor, dataFlavor);
0886:            }
0887:
0888:            public void testCopy() throws Exception {
0889:                // TODO: uncomment when System clipboard is properly supported
0890:                //        if (jtc.getToolkit().getSystemClipboard() == null)
0891:                //            return;
0892:                //        jtc.setText("JTextComponent");
0893:                //        setClipboardString(jtc, "XXX");
0894:                //        jtc.copy();
0895:                //        assertEquals("XXX", getClipboardString(jtc));
0896:                //        assertNull(jtc.getSelectedText());
0897:                //        assertEquals("JTextComponent", jtc.getText());
0898:                //
0899:                //        jtc.select(4, 8);
0900:                //        jtc.copy();
0901:                //        assertEquals("tCom", getClipboardString(jtc));
0902:                //        assertEquals("tCom", jtc.getSelectedText());
0903:                //        assertEquals("JTextComponent", jtc.getText());
0904:            }
0905:
0906:            public void testAddRemoveKeymaps() throws Exception {
0907:                Keymap keyMap = jtc.getKeymap();
0908:                Keymap keyMap1 = JTextComponent.addKeymap("First", keyMap);
0909:                assertEquals("First", keyMap1.getName());
0910:                Keymap keyMap2 = JTextComponent.addKeymap("Second", keyMap1);
0911:                assertEquals("Second", keyMap2.getName());
0912:                Keymap keyMap3 = JTextComponent.addKeymap("Third", null);
0913:                Keymap keyMap4 = JTextComponent.addKeymap(null, null);
0914:                assertEquals("Third", keyMap3.getName());
0915:                Keymap keyMap5 = JTextComponent.addKeymap("Fifth", keyMap2);
0916:                assertNotNull(keyMap5);
0917:                assertEquals(keyMap, keyMap1.getResolveParent());
0918:                assertEquals(keyMap2.getResolveParent(), keyMap1);
0919:                assertNull(keyMap3.getResolveParent());
0920:                assertNull(keyMap4.getResolveParent());
0921:                assertEquals(keyMap1, JTextComponent.getKeymap("First"));
0922:                assertEquals(keyMap2, JTextComponent.getKeymap("Second"));
0923:                assertEquals(keyMap3, JTextComponent.getKeymap("Third"));
0924:                assertNull(JTextComponent.getKeymap("Fourth"));
0925:                JTextComponent.removeKeymap("First");
0926:                assertNull(JTextComponent.getKeymap("First"));
0927:                //assertEquals(keyMap,JTextComponent.getKeymap("Second").//TODO
0928:                //      getResolveParent());
0929:                JTextComponent.removeKeymap("Second");
0930:                JTextComponent.removeKeymap("Third");
0931:                JTextComponent.removeKeymap("Fifth");
0932:                assertNull(JTextComponent.getKeymap("Second"));
0933:                assertNull(JTextComponent.getKeymap("Third"));
0934:                assertNull(JTextComponent.getKeymap("Fifth"));
0935:            }
0936:
0937:            public void testLoadKeymap() throws Exception {
0938:                JTextArea jta = new JTextArea();
0939:                Keymap k = jta.getKeymap();
0940:                KeyStroke keyStroke1 = KeyStroke.getKeyStroke(KeyEvent.VK_A,
0941:                        InputEvent.CTRL_MASK);
0942:                KeyStroke keyStroke2 = KeyStroke.getKeyStroke(KeyEvent.VK_B,
0943:                        InputEvent.CTRL_MASK);
0944:                KeyStroke keyStroke3 = KeyStroke.getKeyStroke(KeyEvent.VK_C,
0945:                        InputEvent.CTRL_MASK);
0946:                KeyStroke keyStroke4 = KeyStroke.getKeyStroke(KeyEvent.VK_D,
0947:                        InputEvent.CTRL_MASK);
0948:                KeyStroke keyStroke5 = KeyStroke.getKeyStroke(KeyEvent.VK_E,
0949:                        InputEvent.CTRL_MASK);
0950:                KeyStroke keyStroke6 = KeyStroke.getKeyStroke(KeyEvent.VK_F,
0951:                        InputEvent.CTRL_MASK);
0952:                JTextComponent.KeyBinding keyBindings[] = {
0953:                        new JTextComponent.KeyBinding(keyStroke1, "first"),
0954:                        new JTextComponent.KeyBinding(keyStroke2,
0955:                                DefaultEditorKit.copyAction),
0956:                        new JTextComponent.KeyBinding(keyStroke3, "second"),
0957:                        new JTextComponent.KeyBinding(keyStroke4,
0958:                                DefaultEditorKit.pasteAction),
0959:                        new JTextComponent.KeyBinding(keyStroke6, "first"),
0960:                        new JTextComponent.KeyBinding(keyStroke5, "first") };
0961:                Action actions[] = { new SimpleTextAction("first"),
0962:                        new SimpleTextAction("second"),
0963:                        new DefaultEditorKit.CopyAction(),
0964:                        new DefaultEditorKit.CutAction() };
0965:                JTextComponent.loadKeymap(k, keyBindings, actions);
0966:                assertEquals(5, k.getBoundActions().length);
0967:                assertEquals(5, k.getBoundKeyStrokes().length);
0968:                isElement(k.getBoundActions(), actions[0], 3);
0969:                isElement(k.getBoundActions(), actions[1], 1);
0970:                isElement(k.getBoundActions(), actions[2], 1);
0971:                isElement(k.getBoundKeyStrokes(), keyStroke1, 1);
0972:                isElement(k.getBoundKeyStrokes(), keyStroke2, 1);
0973:                isElement(k.getBoundKeyStrokes(), keyStroke3, 1);
0974:                isElement(k.getBoundKeyStrokes(), keyStroke5, 1);
0975:                isElement(k.getBoundKeyStrokes(), keyStroke6, 1);
0976:                assertEquals(actions[0], k.getAction(keyStroke1));
0977:                assertEquals(actions[2], k.getAction(keyStroke2));
0978:                assertEquals(actions[1], k.getAction(keyStroke3));
0979:                assertEquals(actions[0], k.getAction(keyStroke5));
0980:                assertEquals(actions[0], k.getAction(keyStroke6));
0981:                assertNull(k.getAction(keyStroke4));
0982:                assertEquals(3, k.getKeyStrokesForAction(actions[0]).length);
0983:                assertEquals(1, k.getKeyStrokesForAction(actions[1]).length);
0984:                assertEquals(1, k.getKeyStrokesForAction(actions[2]).length);
0985:                isElement(k.getKeyStrokesForAction(actions[0]), keyStroke1, 1);
0986:                isElement(k.getKeyStrokesForAction(actions[0]), keyStroke5, 1);
0987:                isElement(k.getKeyStrokesForAction(actions[0]), keyStroke6, 1);
0988:                isElement(k.getKeyStrokesForAction(actions[1]), keyStroke3, 1);
0989:                isElement(k.getKeyStrokesForAction(actions[2]), keyStroke2, 1);
0990:                assertNull(k.getKeyStrokesForAction(actions[3]));
0991:            }
0992:
0993:            public void testConstants() {
0994:                assertEquals("default", JTextComponent.DEFAULT_KEYMAP);
0995:                assertEquals("focusAcceleratorKey",
0996:                        JTextComponent.FOCUS_ACCELERATOR_KEY);
0997:            }
0998:
0999:            public void testJTextComponent_KeyBinding() {
1000:                KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_F,
1001:                        InputEvent.CTRL_MASK);
1002:                JTextComponent.KeyBinding keyBinding = new JTextComponent.KeyBinding(
1003:                        keyStroke, s);
1004:                assertNotNull(keyBinding);
1005:                assertEquals(keyStroke, keyBinding.key);
1006:                assertEquals(s, keyBinding.actionName);
1007:            }
1008:
1009:            // Serialization is not supported now by Swing
1010:            /*public void testSerialization() throws Exception {
1011:             jtc.setText("JTextComponent");
1012:             jtc.setCaretColor(color);
1013:             jtc.setSelectionColor(color);
1014:             jtc.setSelectedTextColor(color);
1015:             jtc.setDisabledTextColor(color);
1016:             //jtc.select(5,8);
1017:             jtc.setMargin(new Insets(10, 20, 30, 40));
1018:             //jtc.setKeymap(new SimpleKeyMap("KeyMap"));
1019:             jtc.setKeymap(JTextComponent.addKeymap("KeyMap", null));
1020:             jtc.setEditable(false);
1021:             jtc.setDragEnabled(true);
1022:             jtc.addInputMethodListener(new SimpleInputMethodListener("1"));
1023:             jtc.addCaretListener(new SimpleCaretListener("1"));
1024:             jtc.setFocusAccelerator('a');
1025:             jtc.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
1026:
1027:             JTextArea jtc1 = new JTextArea();
1028:
1029:             try {
1030:             FileOutputStream fo = new FileOutputStream("tmp");
1031:             ObjectOutputStream so = new ObjectOutputStream(fo);
1032:             so.writeObject(jtc);
1033:             so.flush();
1034:             so.close();
1035:             FileInputStream fi = new FileInputStream("tmp");
1036:             ObjectInputStream si = new ObjectInputStream(fi);
1037:             jtc1 = (JTextArea) si.readObject();
1038:             si.close();
1039:             } catch (Exception e) {
1040:             assertTrue("seralization failed" + e.getMessage(),false);
1041:             }
1042:
1043:             assertEquals("JTextComponent", jtc1.getText());
1044:             assertEquals(color, jtc1.getCaretColor());
1045:             assertEquals(color, jtc1.getSelectionColor());
1046:             assertEquals(color, jtc1.getSelectedTextColor());
1047:             assertEquals(color, jtc1.getDisabledTextColor());
1048:             assertNull(jtc1.getSelectedText());
1049:             assertEquals(0, jtc1.getCaretPosition());
1050:             assertEquals(new Insets(10, 20, 30, 40), jtc1.getMargin());
1051:             assertNotSame("KeyMap", jtc1.getKeymap().getName());
1052:             assertFalse(jtc1.isEditable());
1053:             assertTrue(jtc1.getDragEnabled());
1054:             assertEquals(0, jtc1.getCaretListeners().length);
1055:             assertEquals(0, jtc1.getInputMethodListeners().length);
1056:             assertEquals('A', jtc1.getFocusAccelerator());
1057:             assertNotSame(ComponentOrientation.RIGHT_TO_LEFT, jtc1
1058:             .getComponentOrientation());
1059:             assertEquals(0, jtc1.getHighlighter().getHighlights().length);
1060:             } */
1061:            public void testKeyMap() throws Exception {
1062:                KeyStroke keyStrokeA = KeyStroke.getKeyStroke(KeyEvent.VK_A,
1063:                        InputEvent.CTRL_MASK);
1064:                KeyStroke keyStrokeB = KeyStroke.getKeyStroke(KeyEvent.VK_B,
1065:                        InputEvent.CTRL_MASK);
1066:                KeyStroke keyStrokeC = KeyStroke.getKeyStroke(KeyEvent.VK_C,
1067:                        InputEvent.CTRL_MASK);
1068:                TextAction A = new DefaultEditorKit.CopyAction();
1069:                TextAction B = new DefaultEditorKit.CutAction();
1070:                TextAction C = new DefaultEditorKit.PasteAction();
1071:                TextAction D = new DefaultEditorKit.DefaultKeyTypedAction();
1072:                Keymap grand_parent = JTextComponent.addKeymap("grand_parent",
1073:                        null);
1074:                grand_parent.addActionForKeyStroke(keyStrokeA, A);
1075:                Keymap parent = JTextComponent
1076:                        .addKeymap("parent", grand_parent);
1077:                parent.addActionForKeyStroke(keyStrokeB, B);
1078:                parent.setDefaultAction(D);
1079:                Keymap keymap = JTextComponent.addKeymap("test", parent);
1080:                keymap.addActionForKeyStroke(keyStrokeC, C);
1081:                assertFalse(keymap.isLocallyDefined(keyStrokeB));
1082:                assertFalse(keymap.isLocallyDefined(keyStrokeA));
1083:                assertTrue(keymap.isLocallyDefined(keyStrokeC));
1084:                assertEquals(A, keymap.getAction(keyStrokeA));
1085:                assertEquals(B, keymap.getAction(keyStrokeB));
1086:                assertEquals(C, keymap.getAction(keyStrokeC));
1087:                assertEquals(D, keymap.getDefaultAction());
1088:            }
1089:
1090:            public void testKeymapToString() {
1091:                KeyStroke keyStrokeX = KeyStroke.getKeyStroke(KeyEvent.VK_X,
1092:                        InputEvent.CTRL_MASK);
1093:                KeyStroke keyStrokeY = KeyStroke.getKeyStroke(KeyEvent.VK_Y,
1094:                        InputEvent.CTRL_MASK);
1095:                KeyStroke keyStrokeZ = KeyStroke.getKeyStroke(KeyEvent.VK_Z,
1096:                        InputEvent.CTRL_MASK);
1097:                Action a = new SimpleTextAction("a");//DefaultEditorKit.selectWordDoing;
1098:                Action b = new SimpleTextAction("b");//DefaultEditorKit.selectLineDoing;
1099:                jtc.getKeymap().addActionForKeyStroke(keyStrokeX, a);
1100:                jtc.getKeymap().addActionForKeyStroke(keyStrokeY, a);
1101:                jtc.getKeymap().addActionForKeyStroke(keyStrokeZ, b);
1102:                String sample1 = (keyStrokeY + "=" + a)
1103:                        .replaceFirst("[/$]", "");
1104:                String sample2 = (keyStrokeX + "=" + a)
1105:                        .replaceFirst("[/$]", "");
1106:                String sample3 = (keyStrokeZ + "=" + b)
1107:                        .replaceFirst("[/$]", "");
1108:                String test = jtc.getKeymap().toString().replaceAll("[/$]", "");
1109:                assertTrue(test.indexOf(sample1) > 0);
1110:                assertTrue(test.indexOf(sample2) > 0);
1111:                assertTrue(test.indexOf(sample3) > 0);
1112:                test = test.replaceFirst(sample1, "").replaceFirst(sample2, "")
1113:                        .replaceFirst(sample3, "");
1114:                assertEquals("Keymap[BasicTextAreaUI]{, , }", test);
1115:                jtc.getKeymap().removeKeyStrokeBinding(keyStrokeX);
1116:                jtc.getKeymap().removeKeyStrokeBinding(keyStrokeY);
1117:                jtc.getKeymap().removeKeyStrokeBinding(keyStrokeZ);
1118:            }
1119:
1120:            private Color getColorProperty(final String key) {
1121:                final UIDefaults uiDefaults = UIManager
1122:                        .getLookAndFeelDefaults();
1123:                return uiDefaults.getColor("TextArea." + key);
1124:            }
1125:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.