KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > EditorDemo


1 /*
2  * $Id: EditorDemo.java,v 1.3 2004/10/29 04:39:23 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 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9
10 import javax.swing.JMenuBar JavaDoc;
11 import javax.swing.JToolBar JavaDoc;
12 import javax.swing.KeyStroke JavaDoc;
13 import javax.swing.SwingUtilities JavaDoc;
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 /**
26  * This is a demo of the editor component and the actions framework. This
27  * is the programmatic way of creating the editorDemo.jdnc xml application.
28  *
29  * @author Mark Davidson
30  */

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     /**
43      * Load all the managed actions into the action manager. This is property
44      * oriented and kind of tedious. This is the sort of problem that that xml is good for.
45      */

46     protected void initActions() {
47         ActionManager manager = Application.getInstance().getActionManager();
48
49         // Construct all the actions in this application. This is tedious but
50
// the xml version is a little less so. Perhaps it would be nice to have
51
// a mini action xml parser.
52

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         // Text editing commands.
100

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         // These justification actions are part of a button group.
114
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     /**
132      * Convenience method to create actions with all the attributes. A factory
133      * method for other factory methods.
134      */

135     private AbstractActionExt action(String JavaDoc id, String JavaDoc name, String JavaDoc mnemonic, String JavaDoc desc,
136                           String JavaDoc smicon, String JavaDoc lgicon, String JavaDoc 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 JavaDoc list = new ArrayList JavaDoc();
152
153         // Create toolbar
154
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         // Use the factory to create the toolbar.
173
ActionManager manager = Application.getInstance().getActionManager();
174         JToolBar JavaDoc toolbar = manager.getFactory().createToolBar(list);
175
176         // Build the menu bar. Menus are lists of lists of action ids.
177
List JavaDoc file = new ArrayList JavaDoc();
178         file.add("file-menu");
179         file.add("new-command");
180         file.add("open-command");
181
182         List JavaDoc edit = new ArrayList JavaDoc();
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 JavaDoc format = new ArrayList JavaDoc();
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 JavaDoc();
202         list.add(file);
203         list.add(edit);
204         list.add(format);
205
206         JMenuBar JavaDoc 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 JavaDoc 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 JavaDoc[] args) {
227         SwingUtilities.invokeLater(new Runnable JavaDoc() {
228                 public void run() {
229                     EditorDemo demo = new EditorDemo();
230                 }
231             });
232
233     }
234
235 }
236
Popular Tags