KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > general > EditorKitsRegistryTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package general;
21
22 import javax.swing.JEditorPane JavaDoc;
23 import javax.swing.SwingUtilities JavaDoc;
24 import javax.swing.text.DefaultEditorKit JavaDoc;
25 import javax.swing.text.EditorKit JavaDoc;
26 import javax.swing.text.html.HTMLEditorKit JavaDoc;
27 import javax.swing.text.rtf.RTFEditorKit JavaDoc;
28 import org.netbeans.junit.NbTestCase;
29 import org.openide.text.CloneableEditorSupport;
30
31 /**
32  *
33  * @author Vita Stejskal
34  */

35 public class EditorKitsRegistryTest extends NbTestCase {
36     
37     /** Creates a new instance of EditorKitsRegistryTest */
38     public EditorKitsRegistryTest(String JavaDoc name) {
39         super(name);
40     }
41     
42     public void testHTMLEditorKits() {
43         JEditorPane JavaDoc pane = new JEditorPane JavaDoc();
44         setContentTypeInAwt(pane, "text/html");
45         
46         // Test JDK kit
47
EditorKit JavaDoc kitFromJdk = pane.getEditorKit();
48         assertNotNull("Can't find JDK kit for text/html", kitFromJdk);
49         assertTrue("Wrong JDK kit for text/html", kitFromJdk instanceof HTMLEditorKit JavaDoc);
50         
51         // Test Netbeans kit
52
EditorKit JavaDoc kitFromNb = CloneableEditorSupport.getEditorKit("text/html");
53         assertNotNull("Can't find Nb kit for text/html", kitFromNb);
54         assertEquals("Wrong Nb kit for text/html",
55             "org.netbeans.modules.editor.html.HTMLKit", kitFromNb.getClass().getName());
56     }
57
58     public void testPlainEditorKits() {
59         // VIS: JEditorPane when constructed contains javax.swing.JEditorPane$PlainEditorKit
60
// and calling JEP.setContenetType("text/plain") has no effect. IMO this is probably
61
// a defect in JDK, becuase JEP should always honour its EditorKit registry.
62
JEditorPane JavaDoc pane = new JEditorPane JavaDoc();
63         pane.setEditorKit(new DefaultEditorKit JavaDoc() {
64             public String JavaDoc getContentType() {
65                 return "text/whatever";
66             }
67         });
68         setContentTypeInAwt(pane, "text/plain");
69         
70         // Test JDK kit
71
EditorKit JavaDoc kitFromJdk = pane.getEditorKit();
72         assertNotNull("Can't find JDK kit for text/plain", kitFromJdk);
73         assertEquals("The kit for text/plain should not be from JDK",
74             "org.netbeans.modules.editor.plain.PlainKit", kitFromJdk.getClass().getName());
75
76         // Test Netbeans kit
77
EditorKit JavaDoc kitFromNb = CloneableEditorSupport.getEditorKit("text/plain");
78         assertNotNull("Can't find Nb kit for text/plain", kitFromNb);
79         assertEquals("Wrong Nb kit for text/plain",
80             "org.netbeans.modules.editor.plain.PlainKit", kitFromNb.getClass().getName());
81     }
82
83     public void testTextRtfEditorKits() {
84         JEditorPane JavaDoc pane = new JEditorPane JavaDoc();
85         setContentTypeInAwt(pane, "text/rtf");
86         
87         // Test JDK kit
88
EditorKit JavaDoc kitFromJdk = pane.getEditorKit();
89         assertNotNull("Can't find JDK kit for text/rtf", kitFromJdk);
90         assertTrue("Wrong JDK kit for application/rtf", kitFromJdk instanceof RTFEditorKit JavaDoc);
91     }
92
93     public void testApplicationRtfEditorKits() {
94         JEditorPane JavaDoc pane = new JEditorPane JavaDoc();
95         setContentTypeInAwt(pane, "application/rtf");
96         
97         // Test JDK kit
98
EditorKit JavaDoc kitFromJdk = pane.getEditorKit();
99         assertNotNull("Can't find JDK kit for application/rtf", kitFromJdk);
100         assertTrue("Wrong JDK kit for application/rtf", kitFromJdk instanceof RTFEditorKit JavaDoc);
101     }
102     
103     private void setContentTypeInAwt(final JEditorPane JavaDoc pane, final String JavaDoc mimeType) {
104         try {
105             SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
106                 public void run() {
107                     pane.setContentType(mimeType);
108                 }
109             });
110         } catch (Exception JavaDoc e) {
111             e.printStackTrace();
112             fail("Can't set content type in AWT: " + e.getMessage());
113         }
114     }
115 }
116
Popular Tags