27
28 import java.awt.AWTException;
29 import java.awt.CheckboxMenuItem;
30 import java.awt.Component;
31 import java.awt.Container;
32 import java.awt.PopupMenu;
33 import java.awt.Menu;
34 import java.awt.MenuItem;
35 import java.awt.Toolkit;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.awt.im.spi.InputMethodDescriptor;
39 import java.util.Locale;
40 import javax.swing.JCheckBoxMenuItem;
41 import javax.swing.JComponent;
42 import javax.swing.JDialog;
43 import javax.swing.JFrame;
44 import javax.swing.JPopupMenu;
45 import javax.swing.JMenu;
46 import javax.swing.JMenuItem;
47
48 /**
49 * {@code InputMethodPopupMenu} provides the popup selection menu
50 */
51
52 abstract class InputMethodPopupMenu implements ActionListener {
53
54 // Factory method to provide the menu, depending on the client, i.e.,
55 // provide Swing popup menu if client is a swing app, otherwise AWT popup
56 // is created.
57 static InputMethodPopupMenu getInstance(Component client, String title) {
58 if ((client instanceof JFrame) ||
59 (client instanceof JDialog)) {
60 return new JInputMethodPopupMenu(title);
61 } else {
62 return new AWTInputMethodPopupMenu(title);
63 }
64 }
65
66 abstract void show(Component c, int x, int y);
67
68 abstract void removeAll();
69
70 abstract void addSeparator();
71
72 abstract void addToComponent(Component c);
73
74 abstract Object createSubmenu(String label);
75
76 abstract void add(Object menuItem);
77
78 abstract void addMenuItem(String label, String command, String currentSelection);
79
80 abstract void addMenuItem(Object targetMenu, String label, String command,
81 String currentSelection);
82
83 void addOneInputMethodToMenu(InputMethodLocator locator, String currentSelection) {
84 InputMethodDescriptor descriptor = locator.getDescriptor();
85 String label = descriptor.getInputMethodDisplayName(null, Locale.getDefault());
158
159 // ActionListener implementation
160 public void actionPerformed(ActionEvent event) {
161 String choice = event.getActionCommand();
162 ((ExecutableInputMethodManager)InputMethodManager.getInstance()).changeInputMethod(choice);
163 }
164
165 }
166
167 class JInputMethodPopupMenu extends InputMethodPopupMenu {
168 static JPopupMenu delegate = null;
169
170 JInputMethodPopupMenu(String title) {
171 synchronized (this) {
172 if (delegate == null) {
173 delegate = new JPopupMenu(title);
174 }
175 }
176 }
177
178 void show(Component c, int x, int y) {
179 delegate.show(c, x, y);
180 }
181
182 void removeAll() {
183 delegate.removeAll();
184 }
185
186 void addSeparator() {
187 delegate.addSeparator();
188 }
189
190 void addToComponent(Component c) {
191 }
192
193 Object createSubmenu(String label) {
194 return new JMenu(label);
195 }
196
197 void add(Object menuItem) {
198 delegate.add((JMenuItem)menuItem);
213 menuItem.addActionListener(this);
214 menuItem.setEnabled(command != null);
215 if (targetMenu instanceof JMenu) {
216 ((JMenu)targetMenu).add(menuItem);
217 } else {
218 ((JPopupMenu)targetMenu).add(menuItem);
219 }
220 }
221
222 }
223
224 class AWTInputMethodPopupMenu extends InputMethodPopupMenu {
225 static PopupMenu delegate = null;
226
227 AWTInputMethodPopupMenu(String title) {
228 synchronized (this) {
229 if (delegate == null) {
230 delegate = new PopupMenu(title);
231 }
232 }
233 }
234
235 void show(Component c, int x, int y) {
236 delegate.show(c, x, y);
237 }
238
239 void removeAll() {
240 delegate.removeAll();
241 }
242
243 void addSeparator() {
244 delegate.addSeparator();
245 }
246
247 void addToComponent(Component c) {
248 c.add(delegate);
249 }
250
251 Object createSubmenu(String label) {
252 return new Menu(label);
|
27
28 import java.awt.AWTException;
29 import java.awt.CheckboxMenuItem;
30 import java.awt.Component;
31 import java.awt.Container;
32 import java.awt.PopupMenu;
33 import java.awt.Menu;
34 import java.awt.MenuItem;
35 import java.awt.Toolkit;
36 import java.awt.event.ActionEvent;
37 import java.awt.event.ActionListener;
38 import java.awt.im.spi.InputMethodDescriptor;
39 import java.util.Locale;
40 import javax.swing.JCheckBoxMenuItem;
41 import javax.swing.JComponent;
42 import javax.swing.JDialog;
43 import javax.swing.JFrame;
44 import javax.swing.JPopupMenu;
45 import javax.swing.JMenu;
46 import javax.swing.JMenuItem;
47 import javax.swing.JWindow;
48 import javax.swing.SwingUtilities;
49
50 /**
51 * {@code InputMethodPopupMenu} provides the popup selection menu
52 */
53
54 abstract class InputMethodPopupMenu implements ActionListener {
55
56 // Factory method to provide the menu, depending on the client, i.e.,
57 // provide Swing popup menu if client is a swing app, otherwise AWT popup
58 // is created.
59 static InputMethodPopupMenu getInstance(Component client, String title) {
60 if ((client instanceof JFrame) ||
61 (client instanceof JDialog) ||
62 (client instanceof JWindow)) {
63 return new JInputMethodPopupMenu(title);
64 } else {
65 return new AWTInputMethodPopupMenu(title);
66 }
67 }
68
69 static InputMethodPopupMenu getAWTInstance(Component client, String title) {
70 return new AWTInputMethodPopupMenu(title);
71 }
72
73 abstract boolean isVisible();
74
75 abstract void show(Component c, int x, int y);
76
77 abstract void removeAll();
78
79 abstract void addSeparator();
80
81 abstract void addToComponent(Component c);
82
83 abstract Object createSubmenu(String label);
84
85 abstract void add(Object menuItem);
86
87 abstract void addMenuItem(String label, String command, String currentSelection);
88
89 abstract void addMenuItem(Object targetMenu, String label, String command,
90 String currentSelection);
91
92 void addOneInputMethodToMenu(InputMethodLocator locator, String currentSelection) {
93 InputMethodDescriptor descriptor = locator.getDescriptor();
94 String label = descriptor.getInputMethodDisplayName(null, Locale.getDefault());
167
168 // ActionListener implementation
169 public void actionPerformed(ActionEvent event) {
170 String choice = event.getActionCommand();
171 ((ExecutableInputMethodManager)InputMethodManager.getInstance()).changeInputMethod(choice);
172 }
173
174 }
175
176 class JInputMethodPopupMenu extends InputMethodPopupMenu {
177 static JPopupMenu delegate = null;
178
179 JInputMethodPopupMenu(String title) {
180 synchronized (this) {
181 if (delegate == null) {
182 delegate = new JPopupMenu(title);
183 }
184 }
185 }
186
187 boolean isVisible() {
188 return delegate.isVisible();
189 }
190
191 void show(Component c, int x, int y) {
192 SwingUtilities.updateComponentTreeUI(delegate);
193 delegate.show(c, x, y);
194 }
195
196 void removeAll() {
197 delegate.removeAll();
198 }
199
200 void addSeparator() {
201 delegate.addSeparator();
202 }
203
204 void addToComponent(Component c) {
205 }
206
207 Object createSubmenu(String label) {
208 return new JMenu(label);
209 }
210
211 void add(Object menuItem) {
212 delegate.add((JMenuItem)menuItem);
227 menuItem.addActionListener(this);
228 menuItem.setEnabled(command != null);
229 if (targetMenu instanceof JMenu) {
230 ((JMenu)targetMenu).add(menuItem);
231 } else {
232 ((JPopupMenu)targetMenu).add(menuItem);
233 }
234 }
235
236 }
237
238 class AWTInputMethodPopupMenu extends InputMethodPopupMenu {
239 static PopupMenu delegate = null;
240
241 AWTInputMethodPopupMenu(String title) {
242 synchronized (this) {
243 if (delegate == null) {
244 delegate = new PopupMenu(title);
245 }
246 }
247 }
248
249 boolean isVisible() {
250 return false;
251 }
252
253 void show(Component c, int x, int y) {
254 delegate.show(c, x, y);
255 }
256
257 void removeAll() {
258 delegate.removeAll();
259 }
260
261 void addSeparator() {
262 delegate.addSeparator();
263 }
264
265 void addToComponent(Component c) {
266 c.add(delegate);
267 }
268
269 Object createSubmenu(String label) {
270 return new Menu(label);
|