1 7 import java.util.ArrayList ; 8 import java.util.List ; 9 10 import javax.swing.JMenuBar ; 11 import javax.swing.JToolBar ; 12 import javax.swing.KeyStroke ; 13 import javax.swing.SwingUtilities ; 14 15 import org.jdesktop.swing.Application; 16 import org.jdesktop.swing.JXEditorPane; 17 import org.jdesktop.swing.JXFrame; 18 import org.jdesktop.swing.JXRootPane; 19 import org.jdesktop.swing.JXStatusBar; 20 21 import org.jdesktop.swing.actions.AbstractActionExt; 22 import org.jdesktop.swing.actions.ActionFactory; 23 import org.jdesktop.swing.actions.ActionManager; 24 25 31 public class EditorDemo { 32 33 private JXFrame frame; 34 35 public EditorDemo() { 36 initActions(); 37 initUI(); 38 39 frame.setVisible(true); 40 } 41 42 46 protected void initActions() { 47 ActionManager manager = Application.getInstance().getActionManager(); 48 49 53 manager.addAction(ActionFactory.createTargetableAction("file-menu", "File", "F")); 54 manager.addAction(ActionFactory.createTargetableAction("edit-menu", "Edit", "E")); 55 manager.addAction(ActionFactory.createTargetableAction("format-menu", "Format", "O")); 56 57 AbstractActionExt action; 58 59 action("new-command", "New", "N", "Create a new document", 60 "/toolbarButtonGraphics/general/New16.gif", 61 "/toolbarButtonGraphics/general/New24.gif", null, false); 62 63 action("open-command", "Open", "O", "Open a new document", 64 "/toolbarButtonGraphics/general/Open16.gif", 65 "/toolbarButtonGraphics/general/Open24.gif", null, false); 66 67 action("cut-to-clipboard", "Cut", "C", "Cut to clipboard", 68 "/toolbarButtonGraphics/general/Cut16.gif", 69 "/toolbarButtonGraphics/general/Cut24.gif", null, false); 70 71 action("copy-to-clipboard", "Copy", "P", "Copy to clipboard", 72 "/toolbarButtonGraphics/general/Copy16.gif", 73 "/toolbarButtonGraphics/general/Copy24.gif", null, false); 74 75 action("paste-from-clipboard", "Paste", "T", "Paste to clipboard", 76 "/toolbarButtonGraphics/general/Paste16.gif", 77 "/toolbarButtonGraphics/general/Paste24.gif", null, false); 78 79 action("undo", "Undo", "U", "Reverse a transaction", 80 "/toolbarButtonGraphics/general/Undo16.gif", 81 "/toolbarButtonGraphics/general/Undo24.gif", null, false); 82 83 action("redo", "Redo", "R", "Restore an undone transaction", 84 "/toolbarButtonGraphics/general/Redo16.gif", 85 "/toolbarButtonGraphics/general/Redo24.gif", null, false); 86 87 action("find", "Find", "F", "Find an item", 88 "/toolbarButtonGraphics/general/Find16.gif", 89 "/toolbarButtonGraphics/general/Find24.gif", null, false); 90 91 action("print", "Print", "P", "Print the document", 92 "/toolbarButtonGraphics/general/Print16.gif", 93 "/toolbarButtonGraphics/general/Print24.gif", null, false); 94 95 action("about", "About", "A", "About this application", 96 "/toolbarButtonGraphics/general/About16.gif", 97 "/toolbarButtonGraphics/general/About24.gif", null, false); 98 99 101 action("font-bold", "Bold", "B", "Bold current selection", 102 "/toolbarButtonGraphics/text/Bold16.gif", 103 "/toolbarButtonGraphics/text/Bold24.gif", null, true); 104 105 action("font-italic", "Italic", "I", "Italicize current selection", 106 "/toolbarButtonGraphics/text/Italic16.gif", 107 "/toolbarButtonGraphics/text/Italic24.gif", null, true); 108 109 action("font-underline", "Underline", "U", "Underline current selection", 110 "/toolbarButtonGraphics/text/Underline16.gif", 111 "/toolbarButtonGraphics/text/Underline24.gif", null, true); 112 113 action = action("left-justify", "Left Align", "L", "Left alignment", 115 "/toolbarButtonGraphics/text/AlignLeft16.gif", 116 "/toolbarButtonGraphics/text/AlignLeft24.gif", null, true); 117 action.setGroup("paragraph-align"); 118 119 action = action("center-justify", "Center", "N", "Center alignment", 120 "/toolbarButtonGraphics/text/AlignCenter16.gif", 121 "/toolbarButtonGraphics/text/AlignCenter24.gif", null, true); 122 action.setGroup("paragraph-align"); 123 124 action = action("right-justify", "Right Align", "R", "Right alignment", 125 "/toolbarButtonGraphics/text/AlignRight16.gif", 126 "/toolbarButtonGraphics/text/AlignRight24.gif", null, true); 127 action.setGroup("paragraph-align"); 128 129 } 130 131 135 private AbstractActionExt action(String id, String name, String mnemonic, String desc, 136 String smicon, String lgicon, String keystroke, boolean toggle) { 137 AbstractActionExt action = ActionFactory.createTargetableAction(id, 138 name, mnemonic, toggle); 139 140 ActionFactory.decorateAction(action, desc, desc, 141 Application.getIcon(smicon, this), 142 Application.getIcon(lgicon, this), 143 KeyStroke.getKeyStroke(keystroke)); 144 145 ActionManager manager = Application.getInstance().getActionManager(); 146 return (AbstractActionExt)manager.addAction(action); 147 } 148 149 150 protected void initUI() { 151 List list = new ArrayList (); 152 153 list.add("font-bold"); 155 list.add("font-italic"); 156 list.add("font-underline"); 157 list.add(null); 158 list.add("cut-to-clipboard"); 159 list.add("copy-to-clipboard"); 160 list.add("paste-from-clipboard"); 161 list.add(null); 162 list.add("undo"); 163 list.add("redo"); 164 list.add(null); 165 list.add("find"); 166 list.add("print"); 167 list.add(null); 168 list.add("left-justify"); 169 list.add("center-justify"); 170 list.add("right-justify"); 171 172 ActionManager manager = Application.getInstance().getActionManager(); 174 JToolBar toolbar = manager.getFactory().createToolBar(list); 175 176 List file = new ArrayList (); 178 file.add("file-menu"); 179 file.add("new-command"); 180 file.add("open-command"); 181 182 List edit = new ArrayList (); 183 edit.add("edit-menu"); 184 edit.add("cut-to-clipboard"); 185 edit.add("copy-to-clipboard"); 186 edit.add("paste-from-clipboard"); 187 edit.add(null); 188 edit.add("undo"); 189 edit.add("redo"); 190 191 List format = new ArrayList (); 192 format.add("format-menu"); 193 format.add("font-bold"); 194 format.add("font-italic"); 195 format.add("font-underline"); 196 format.add(null); 197 format.add("left-justify"); 198 format.add("center-justify"); 199 format.add("right-justify"); 200 201 list = new ArrayList (); 202 list.add(file); 203 list.add(edit); 204 list.add(format); 205 206 JMenuBar menu = manager.getFactory().createMenuBar(list); 207 208 frame = new JXFrame("Editor Demo Application"); 209 210 JXEditorPane editor; 211 try { 212 editor = new JXEditorPane(EditorDemo.class.getResource("resources/jabberwock.html")); 213 } catch (Exception ex) { 214 editor = new JXEditorPane(); 215 } 216 JXRootPane root = frame.getRootPaneExt(); 217 218 toolbar.add(editor.getParagraphSelector()); 219 220 root.addComponent(editor); 221 root.setJMenuBar(menu); 222 root.setToolBar(toolbar); 223 root.setStatusBar(new JXStatusBar()); 224 } 225 226 public static void main(String [] args) { 227 SwingUtilities.invokeLater(new Runnable () { 228 public void run() { 229 EditorDemo demo = new EditorDemo(); 230 } 231 }); 232 233 } 234 235 } 236 | Popular Tags |