KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > JXEditorPaneTest


1 /*
2  * $Id: JXEditorPaneTest.java,v 1.3 2005/01/12 18:24:20 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing;
9
10 import java.awt.BorderLayout JavaDoc;
11 import java.awt.Dimension JavaDoc;
12 import java.awt.GraphicsEnvironment JavaDoc;
13
14 import java.awt.event.ActionEvent JavaDoc;
15
16 import java.io.IOException JavaDoc;
17
18 import java.net.URL JavaDoc;
19
20 import javax.swing.*;
21
22 import javax.swing.text.html.HTMLDocument JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.jdesktop.swing.JXEditorPane;
27
28 import org.jdesktop.swing.Application;
29 import org.jdesktop.swing.actions.ActionManager;
30 import org.jdesktop.swing.actions.ActionFactory;
31 import org.jdesktop.swing.actions.ActionContainerFactory;
32
33 /**
34  * A unit test for the JXEditorPane
35  *
36  * @author Mark Davidson
37  */

38 public class JXEditorPaneTest extends TestCase {
39
40     private static String JavaDoc testText = "This is an example of some text";
41
42     private static boolean DEBUG = false;
43
44     public void testInitialization() throws IOException JavaDoc {
45         URL JavaDoc url = JXEditorPaneTest.class.getResource("resources/test.html");
46         JXEditorPane editor = new JXEditorPane();
47         editor.setContentType("text/html");
48         editor.setPage(url);
49
50         assertTrue(editor.getDocument() instanceof HTMLDocument JavaDoc);
51         assertNotNull(editor.getCaretListener());
52         assertNotNull(editor.getUndoableEditListener());
53
54         editor = new JXEditorPane("text/html", "");
55         editor.setPage(url);
56         assertTrue(editor.getDocument() instanceof HTMLDocument JavaDoc);
57         assertNotNull(editor.getCaretListener());
58         assertNotNull(editor.getUndoableEditListener());
59
60         editor = new JXEditorPane();
61         assertFalse(editor.getDocument() instanceof HTMLDocument JavaDoc);
62         assertNull(editor.getCaretListener());
63     }
64
65     public void testRegistration() {
66
67     }
68
69     public void testCutPastePlain() {
70         JXEditorPane editor = new JXEditorPane("text/plain", testText);
71         editorCutPaste(editor);
72     }
73
74     /**
75      * XXX currently the html cut and paste is broken. A work around has been
76      * implemented to use only plain text on the clip. This test will fail
77      * if the text contains some markup like <b>foo</b>
78      */

79     public void testCutPasteHtml() {
80         JXEditorPane editor = new JXEditorPane("text/html", testText);
81         editorCutPaste(editor);
82     }
83
84     public void editorCutPaste(JEditorPane editor) {
85         // This test will not work in a headless configuration.
86
if (GraphicsEnvironment.isHeadless()) {
87             return;
88         }
89
90         if (DEBUG) {
91             System.out.println("Document: " + editor.getDocument());
92         }
93
94         // set selection markers
95
editor.select(4, 15);
96
97         String JavaDoc selected = editor.getSelectedText();
98         if (DEBUG) {
99             System.out.println("Selected: \"" + selected + "\" length: " + selected.length());
100         }
101
102         // get cut and paste actions and execute them simultaniously
103

104         ActionMap map = editor.getActionMap();
105         Action cut = map.get("cut-to-clipboard");
106         Action paste = map.get("paste-from-clipboard");
107
108         assertNotNull(cut);
109         assertNotNull(paste);
110
111         String JavaDoc before = editor.getText();
112
113         if (DEBUG) {
114             System.out.println("Before cut: " + before);
115         }
116
117         cut.actionPerformed(new ActionEvent JavaDoc(editor, 0,
118                                             (String JavaDoc)cut.getValue(Action.ACTION_COMMAND_KEY)));
119
120         if (DEBUG) {
121             System.out.println("After cut: " + editor.getText());
122         }
123
124         // XXX caret position should be moved in the cut operation
125
editor.setCaretPosition(4);
126
127         paste.actionPerformed(new ActionEvent JavaDoc(editor, 0,
128                                               (String JavaDoc)paste.getValue(Action.ACTION_COMMAND_KEY)));
129         if (DEBUG) {
130             System.out.println("After paste: " + editor.getText());
131         }
132         assertEquals(before, editor.getText());
133     }
134
135     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
136         Action[] actions = new Action[14];
137
138         actions[0] = ActionFactory.createTargetableAction("cut-to-clipboard", "Cut", "C");
139         actions[1] = ActionFactory.createTargetableAction("copy-to-clipboard", "Copy", "P");
140         actions[2] = ActionFactory.createTargetableAction("paste-from-clipboard", "Paste", "T");
141
142         actions[3] = ActionFactory.createTargetableAction("undo", "Undo", "U");
143         actions[4] = ActionFactory.createTargetableAction("redo", "Redo", "R");
144
145         actions[5] = ActionFactory.createTargetableAction("left-justify", "Left", "L", true,
146                                                           "position-group");
147         actions[6] = ActionFactory.createTargetableAction("center-justify", "Center", "C", true,
148                                                           "position-group");
149         actions[7] = ActionFactory.createTargetableAction("right-justify", "Right", "R", true,
150                                                           "position-group");
151
152         actions[8] = ActionFactory.createTargetableAction("font-bold", "Bold", "B", true);
153         actions[9] = ActionFactory.createTargetableAction("font-italic", "Italic", "I", true);
154         actions[10] = ActionFactory.createTargetableAction("font-underline", "Underline", "U", true);
155
156         actions[11] = ActionFactory.createTargetableAction("InsertUnorderedList", "UL", "U", true);
157         actions[12] = ActionFactory.createTargetableAction("InsertOrderedList", "OL", "O", true);
158         actions[13] = ActionFactory.createTargetableAction("InsertHR", "HR", "H");
159
160         Application app = Application.getInstance();
161         ActionManager manager = app.getActionManager();
162
163         // Populate the toolbar. Must use the ActionContainerFactory to ensure
164
// that toggle actions are supported.
165
ActionContainerFactory factory = manager.getFactory();
166
167         JToolBar toolbar = new JToolBar();
168         for (int i = 0; i < actions.length; i++) {
169             manager.addAction(actions[i]);
170             toolbar.add(factory.createButton(actions[i]));
171         }
172
173         URL JavaDoc url = JXEditorPaneTest.class.getResource("resources/test.html");
174         JXEditorPane editor = new JXEditorPane(url);
175         editor.setPreferredSize(new Dimension JavaDoc(600, 400));
176
177         toolbar.add(editor.getParagraphSelector());
178
179         JFrame frame = new JFrame("Editor tester");
180         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
181         frame.getContentPane().add(toolbar, BorderLayout.NORTH);
182         frame.getContentPane().add(new JScrollPane(editor), BorderLayout.CENTER);
183
184         frame.pack();
185         frame.setVisible(true);
186     }
187 }
188
Popular Tags