1 7 8 package org.jdesktop.swing; 9 10 import java.awt.BorderLayout ; 11 import java.awt.Dimension ; 12 import java.awt.GraphicsEnvironment ; 13 14 import java.awt.event.ActionEvent ; 15 16 import java.io.IOException ; 17 18 import java.net.URL ; 19 20 import javax.swing.*; 21 22 import javax.swing.text.html.HTMLDocument ; 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 38 public class JXEditorPaneTest extends TestCase { 39 40 private static String testText = "This is an example of some text"; 41 42 private static boolean DEBUG = false; 43 44 public void testInitialization() throws IOException { 45 URL 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 ); 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 ); 57 assertNotNull(editor.getCaretListener()); 58 assertNotNull(editor.getUndoableEditListener()); 59 60 editor = new JXEditorPane(); 61 assertFalse(editor.getDocument() instanceof HTMLDocument ); 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 79 public void testCutPasteHtml() { 80 JXEditorPane editor = new JXEditorPane("text/html", testText); 81 editorCutPaste(editor); 82 } 83 84 public void editorCutPaste(JEditorPane editor) { 85 if (GraphicsEnvironment.isHeadless()) { 87 return; 88 } 89 90 if (DEBUG) { 91 System.out.println("Document: " + editor.getDocument()); 92 } 93 94 editor.select(4, 15); 96 97 String selected = editor.getSelectedText(); 98 if (DEBUG) { 99 System.out.println("Selected: \"" + selected + "\" length: " + selected.length()); 100 } 101 102 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 before = editor.getText(); 112 113 if (DEBUG) { 114 System.out.println("Before cut: " + before); 115 } 116 117 cut.actionPerformed(new ActionEvent (editor, 0, 118 (String )cut.getValue(Action.ACTION_COMMAND_KEY))); 119 120 if (DEBUG) { 121 System.out.println("After cut: " + editor.getText()); 122 } 123 124 editor.setCaretPosition(4); 126 127 paste.actionPerformed(new ActionEvent (editor, 0, 128 (String )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 [] args) throws Exception { 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 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 url = JXEditorPaneTest.class.getResource("resources/test.html"); 174 JXEditorPane editor = new JXEditorPane(url); 175 editor.setPreferredSize(new Dimension (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 |