]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/swingui/SwingMenuBuilder.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / swingui / SwingMenuBuilder.java
1 /*\r
2  * (C) Copyright IBM Corp. 1998-2008.  All Rights Reserved.\r
3  *\r
4  * The program is provided "as is" without any warranty express or\r
5  * implied, including the warranty of non-infringement and the implied\r
6  * warranties of merchantibility and fitness for a particular purpose.\r
7  * IBM will not be liable for any damages suffered by you as a result\r
8  * of using the Program. In no event will IBM be liable for any\r
9  * special, indirect or consequential damages or lost profits even if\r
10  * IBM has been advised of the possibility of their occurrence. IBM\r
11  * will not be liable for any third party claims against you.\r
12  */\r
13 package com.ibm.richtext.swingui;\r
14 \r
15 import java.awt.Frame;\r
16 import java.awt.Window;\r
17 \r
18 import javax.swing.JMenu;\r
19 import javax.swing.JMenuBar;\r
20 \r
21 import com.ibm.richtext.textpanel.MTextPanel;\r
22 \r
23 import com.ibm.richtext.uiimpl.resources.FrameResources;\r
24 \r
25 import com.ibm.richtext.uiimpl.*;\r
26 import com.ibm.richtext.uiimpl.DialogItem.DialogFactory;\r
27 \r
28 /**\r
29  * SwingMenuBuilder provides a method for creating a set of Swing menus for interacting\r
30  * with an MTextPanel.  Future versions of this class may provide greater control\r
31  * over the menu contents.\r
32  * @see MTextPanel\r
33  */\r
34 public final class SwingMenuBuilder extends MenuBuilder {\r
35     \r
36     static final String COPYRIGHT =\r
37                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
38     private static final SwingMenuBuilder INSTANCE = new SwingMenuBuilder();\r
39     \r
40     /**\r
41      * Id for an Edit menu.  The Edit menu has the following items:\r
42      * <ul>\r
43      * <li><b>Undo</b> - invoke undo() on the MTextPanel</li>\r
44      * <li><b>Redo</b> - invoke redo() on the MTextPanel</li>\r
45      * <li><b>Cut</b> - invoke cut() on the MTextPanel</li>\r
46      * <li><b>Copy</b> - invoke copy() on the MTextPanel</li>\r
47      * <li><b>Paste</b> - invoke paste() on the MTextPanel</li>\r
48      * <li><b>Clear</b> - invoke clear() on the MTextPanel</li>\r
49      * <li><b>Select All</b> - invoke selectAll() on the MTextPanel</li>\r
50      * </ul>\r
51      */\r
52     public static final int EDIT = MenuBuilder.EDIT;\r
53     /**\r
54      * Id for the point sizes menu.  The menu has items that set the size of a character\r
55      * in a typeface.\r
56      */\r
57     public static final int SIZE = MenuBuilder.SIZE;\r
58     /**\r
59      * Id for a Style menu.  The Style menu has the following items:\r
60      * <ul>\r
61      * <li><b>Plain</b> - remove <code>WEIGHT</code>,\r
62      *                    <code>POSTURE</code>,\r
63      *                    <code>UNDERLINE</code> and\r
64      *                    <code>STRIKETHROUGH</code> attributes from the\r
65      * current selection</li>\r
66      * <li><b>Bold</b> - add <code>{WEIGHT,WEIGHT_BOLD}</code> to\r
67      * the current selection</li>\r
68      * <li><b>Italic</b> - add <code>{POSTURE,POSTURE_ITALIC}</code> to\r
69      * the current selection</li>\r
70      * <li><b>Underline</b> - add <code>{UNDERLINE,UNDERLINE_ON}</code> to\r
71      * the current selection</li>\r
72      * <li><b>Strikethrough</b> - add <code>{STRIKETHROUGH,STRIKETHROUGH_ON}</code>\r
73      * to the current selection</li>\r
74      * <li><b>Font...</b> - display a dialog allowing the user to\r
75      * select a typeface (font family) for the current selection</li>\r
76      * <li><b>Forecolor...</b> - display a dialog allowing the user to\r
77      * select a foreground color for the current selection</li>\r
78      * <li><b>Backcolor...</b> - display a dialog allowing the user to\r
79      * select a background color for the current selection</li>\r
80      * </ul>\r
81      */\r
82     public static final int STYLE = MenuBuilder.STYLE;\r
83     /**\r
84      * Id for a paragraph alignment menu.  The menu has the following items:\r
85      * <ul>\r
86      * <li><b>Leading</b> - give selected paragraph(s) LEADING flush</li>\r
87      * <li><b>Center</b> - give selected paragraph(s) CENTER flush</li>\r
88      * <li><b>Trailing</b> - give selected paragraph(s) TRAILING flush</li>\r
89      * <li><b>Justified</b> - give selected paragraph(s) full justification</li>\r
90      * </ul>\r
91      */\r
92     public static final int FLUSH = MenuBuilder.FLUSH;\r
93     /**\r
94      * Id for a menu that sets the KeyRemap\r
95      * on an MTextPanel.  The menu has the following items:\r
96      * <ul>\r
97      * <li><b>Default</b> - set KeyRemap to identity remap</li>\r
98      * <li><b>Arabic</b> - set KeyRemap to Arabic transliteration</li>\r
99      * <li><b>Hebrew</b> - set KeyRemap to Hebrew transliteration</li>\r
100      * <li><b>Israel Nikud</b> - set KeyRemap to Israel Nikud</li>\r
101      * <li><b>Thai Ketmanee</b> - set KeyRemap to Thai Ketmanee</li>\r
102      * </ul>\r
103      */\r
104     public static final int KEYMAP = MenuBuilder.KEYMAP;\r
105     /**\r
106      * Id for a menu that sets\r
107      * the primary run direction for a paragraph.  Run direction can be left-to-right,\r
108      * right-to-left, or can use the default run direction from the Unicode bidi algorithm.\r
109      */\r
110     public static final int BIDI = MenuBuilder.BIDI;\r
111     /**\r
112      * Id for a menu with an <b>About</b> item.  When selected, \r
113      * the item displays a Frame containing some\r
114      * self-promotional text.\r
115      */\r
116     public static final int ABOUT = MenuBuilder.ABOUT;\r
117     /**\r
118      * Return an instance of SwingMenuBuilder.\r
119      */\r
120      \r
121     public static SwingMenuBuilder getInstance() {\r
122         \r
123         return INSTANCE;\r
124     }\r
125     \r
126     private JMenuBar fMenuBar;\r
127     \r
128     private SwingMenuBuilder() {\r
129     }\r
130     \r
131     /**\r
132      * Add a standard set of menus to the given menu bar.  The menus \r
133      * will interact with the given MTextPanel.\r
134      * @param menuBar the MenuBar to which menus are added\r
135      * @param textPanel the MTextPanel with which the menus interact\r
136      * @param frame a Frame to use as the parent of any dialogs created by a \r
137      *   a menu item.  If null, menu items which create dialogs will be omitted.\r
138      */\r
139     public void createMenus(JMenuBar menuBar, \r
140                             MTextPanel textPanel,\r
141                             Frame frame) {\r
142 \r
143         createMenus(menuBar, textPanel, frame, defaultMenus);\r
144     }\r
145     \r
146     /**\r
147      * Add a set of menus to the given menu bar.  The menus \r
148      * will interact with the given MTextPanel.\r
149      * @param menuBar the MenuBar to which menus are added\r
150      * @param textPanel the MTextPanel with which the menus interact\r
151      * @param frame a Frame to use as the parent of any dialogs created by a \r
152      *   a menu item.  If null, menu items which create dialogs will be omitted.\r
153      * @param menus an array of integer menu id's.  Each element of the\r
154      *   array must be one of this class's menu id constants.  If null,\r
155      *   the default menus are created.\r
156      */\r
157     public void createMenus(JMenuBar menuBar,\r
158                             MTextPanel textPanel,\r
159                             Frame frame,\r
160                             int[] menus) {\r
161         \r
162         if (menus == null) {\r
163             menus = defaultMenus;\r
164         }\r
165         \r
166         synchronized (MItem.LOCK) {\r
167             \r
168             fMenuBar = menuBar;\r
169             doCreateMenus(textPanel, frame, menus);\r
170             fMenuBar = null;\r
171         }\r
172     }\r
173     \r
174     protected void handleAddMenu(String key) {\r
175         \r
176         JMenu menu = new JMenu(ResourceUtils.getResourceString(key));\r
177         fMenuBar.add(menu);\r
178         MItem.setItemFactory(new SwingMenuFactory(menu));\r
179     }\r
180 \r
181     protected DialogFactory createObjectDialogFactory(final String dialogTitle,\r
182                                                       final String dialogMessage,\r
183                                                       final Object key,\r
184                                                       final boolean character,\r
185                                                       final String[] names,\r
186                                                       final Object[] values) {\r
187         \r
188         final Frame dialogParent = fDialogParent;\r
189 \r
190         return new DialogFactory() {    \r
191             public Window createDialog(MTextPanel textPanel) {\r
192                 return new JObjectDialog(dialogParent,\r
193                                         dialogTitle,\r
194                                         dialogMessage,\r
195                                         textPanel,\r
196                                         key,\r
197                                         character,\r
198                                         names,\r
199                                         values);\r
200             }\r
201         };\r
202     }\r
203     \r
204     protected DialogFactory createNumberDialogFactory(final String dialogTitle,\r
205                                                       final String dialogMessage,\r
206                                                       final Object key,\r
207                                                       final boolean character) {\r
208         \r
209         final Frame dialogParent = fDialogParent;\r
210         // variable not used final MTextPanel textPanel = fTextPanel;\r
211             \r
212         return new DialogFactory() {\r
213             public Window createDialog(MTextPanel fTxtPanel) {\r
214                 return new JNumberDialog(dialogParent,\r
215                                          dialogTitle,\r
216                                          dialogMessage,\r
217                                          fTxtPanel,\r
218                                          key,\r
219                                          character,\r
220                                          1);\r
221             }\r
222         };\r
223     }    \r
224     \r
225     protected DialogFactory createAboutDialogFactory() {\r
226         \r
227         return new DialogFactory() {\r
228             public Window createDialog(MTextPanel textPanel) {\r
229                 String title = ResourceUtils.getResourceString(FrameResources.ABOUT_TITLE);\r
230                 return new JMessageDialog(title, AboutText.getAboutText());\r
231             }\r
232         };\r
233     }\r
234 }