| |
"Adding Commands to Displayable Components…"
Vol. 7, Issue 6, p. 82
Listing 1
1. import javax.microedition.lcdui.*;
2. import javax.microedition.midlet.*;
3.
4. public class ListDemo1 extends MIDlet {
5. private Display _display;
6. private Form _form = new Form("Choose a type of List:");
7.
8. private static final String[] _items
9. = {"J2ME", "J2SE", "J2EE"};
10. private static final boolean[] _flags
11. = new boolean[_items.length];
12.
13. private Command _exCmd = new Command("EXCLUSIVE",
14. Command.SCREEN, 0);
15. private Command _multCmd = new Command("MULTIPLE",
16. Command.SCREEN, 0);
17. private Command _impCmd = new Command("IMPLICIT",
18. Command.SCREEN, 0);
19. private Command _exitCmd = new Command("Exit",
20. Command.EXIT, 0);
21.
22. private Command _okCmd = new Command("OK",
23. Command.SCREEN, 0);
24. private Command _cancelCmd = new Command("Cancel",
25. Command.SCREEN, 0);
26.
27. public void startApp() {
28. _form.addCommand(_exCmd);
29. _form.addCommand(_multCmd);
30. _form.addCommand(_impCmd);
31. _form.addCommand(_exitCmd);
32. _form.setCommandListener(_formListener);
33.
34. _display = Display.getDisplay(this);
35. _display.setCurrent(_form);
36. }
37.
38. public void pauseApp() { }
39.
40. public void destroyApp(boolean unconditional) {
41. notifyDestroyed();
4. }
43.
44. private CommandListener _formListener
45. = new CommandListener() {
46. public void commandAction(Command cmd,
47. Displayable displayable) {
48. if (cmd == _exitCmd) {
49. notifyDestroyed();
50. return;
51. }
52. int listType = -1;
53. if (cmd == _exCmd) {
54. listType = Choice.EXCLUSIVE;
55. }
56. else if (cmd == _multCmd) {
57. listType = Choice.MULTIPLE;
58. }
59. else if (cmd == _impCmd) {
60. listType = Choice.IMPLICIT;
61. }
62. List list = new List("Select an item:",
63. listType, _items, null);
64. list.addCommand(_okCmd);
65. list.addCommand(_cancelCmd);
66. list.setCommandListener(_listListener);
67. _display.setCurrent(list);
68. }
69. };
70.
71. private CommandListener _listListener
72. = new CommandListener() {
73. public void commandAction(Command cmd,
74. Displayable displayable) {
75. List list = (List) displayable;
76. if (cmd == _okCmd || cmd == List.SELECT_COMMAND) {
77. list.getSelectedFlags(_flags);
78. StringBuffer msg
79. = new StringBuffer("You selected:");
80. for (int i = 0; i < _flags.length; i++) {
81. if (_flags[i]) {
82. msg.append("\nItem ");
83. msg.append(String.valueOf(i + 1));
84. msg.append(" - ");
85. msg.append(list.getString(i));
86. }
87. }
88. Alert alert = new Alert("Info",
89. msg.toString(), null, AlertType.INFO);
90. alert.setTimeout(Alert.FOREVER);
91. _display.setCurrent(alert);
92. }
93. else if (cmd == _cancelCmd) {
94. list.removeCommand(_okCmd);
95. list.removeCommand(_cancelCmd);
96. list.setCommandListener(null);
97. _display.setCurrent(_form);
98. }
99. }
100. };
101. }
Listing 2
1. import javax.microedition.lcdui.*;
2.
3. public abstract class CommandAction {
4. private Command _command;
5.
6. public CommandAction(String label, int type,
7. int priority) {
8. _command = new Command(label, type, priority);
9. }
10. public CommandAction(Command command) {
11. _command = command;
12. }
13.
14. public Command getCommand() {
15. return _command;
16. }
17. public void setCommand(Command command) {
18. _command = command;
19. }
20.
21. public abstract void execute(Displayable d);
22. }
Listing 3
1. import javax.microedition.lcdui.*;
2. import java.util.*;
3.
4. public class CommandManager implements CommandListener {
5. private Hashtable _dToC = new Hashtable();
6. private Hashtable _cToD = new Hashtable();
7. private Hashtable _commands = new Hashtable();
8.
9. public void add(CommandAction cmdAction,
10. Displayable displayable) {
11. int[] cmdCount = increment(_dToC, displayable);
12. if (cmdCount[0] == 1) {
13. displayable.setCommandListener(this);
14. }
15. increment(_cToD, cmdAction);
16.
17. Command command = cmdAction.getCommand();
18. displayable.addCommand(command);
19. _commands.put(command, cmdAction);
20. }
21.
22. public void remove(CommandAction cmdAction,
23. Displayable displayable) {
24. Command command = cmdAction.getCommand();
25. displayable.removeCommand(command);
26.
27. int[] cmdCount = (int[]) _dToC.get(displayable);
28. cmdCount[0]--;
29. if (cmdCount[0] == 0) {
30. _dToC.remove(displayable);
31. displayable.setCommandListener(null);
32. }
33.
34. int[] dispCount = (int[]) _cToD.get(cmdAction);
35. dispCount[0]--;
36. if (dispCount[0] == 0) {
37. _commands.remove(command);
38. }
39. }
40.
41. public void commandAction(Command command,
42. Displayable displayable) {
43. CommandAction cmdAction
44. = (CommandAction) _commands.get(command);
45. if (cmdAction != null) {
46. cmdAction.execute(displayable);
47. }
48. else {
49. System.err.println("No command action for command!");
50. }
51. }
52.
53. private int[] increment(Hashtable ht, Object target) {
54. int[] count = (int[]) ht.get(target);
55. if (count == null) {
56. count = new int[1];
57. ht.put(target, count);
58. }
59. count[0]++;
60. return count;
61. }
62. }
Listing 4
1. import javax.microedition.lcdui.*;
2. import javax.microedition.midlet.*;
3.
4. public class ListDemo2 extends MIDlet {
5. private Display _display;
6. private Form _form = new Form("Choose a type of List:");
7. private CommandManager _cm = new CommandManager();
8.
9. private static final String[] _items
10. = {"J2ME", "J2SE", "J2EE"};
11. private static final boolean[] _flags
12. = new boolean[_items.length];
13.
14. private class ListCommand extends CommandAction {
15. private List _list;
16. public ListCommand(int type, String label) {
17. super(label, Command.SCREEN, 0);
18. _list = new List("Select an item:",
19. type, _items, null);
20. _cm.add(new OkCommand("OK", Command.SCREEN, 0), _list);
21. _cm.add(new OkCommand(List.SELECT_COMMAND), _list);
22. _cm.add(_cancelCmd, _list);
23. }
24. public void execute(Displayable displayable) {
25. _display.setCurrent(_list);
26. }
27. }
28.
29. private CommandAction _exitCmd = new CommandAction(
30. "Exit", Command.EXIT, 0) {
31. public void execute(Displayable displayable) {
32. notifyDestroyed();
33. }
34. };
35.
36. private class OkCommand extends CommandAction {
37. public OkCommand(String label, int type, int priority) {
38. super(label, type, priority);
39. }
40. public OkCommand(Command command) {
41. super(command);
42. }
43. public void execute(Displayable displayable) {
44. List list = (List) displayable;
45. list.getSelectedFlags(_flags);
46. StringBuffer msg = new StringBuffer("You selected:");
47. for (int i = 0; i < _flags.length; i++) {
48. if (_flags[i]) {
49. msg.append("\nItem ");
50. msg.append(String.valueOf(i + 1));
51. msg.append(" - ");
52. msg.append(list.getString(i));
53. }
54. }
55. Alert alert = new Alert("Info",
56. msg.toString(), null, AlertType.INFO);
57. alert.setTimeout(Alert.FOREVER);
58. _display.setCurrent(alert);
59. }
60. };
61.
62. private CommandAction _cancelCmd = new CommandAction(
63. "Cancel", Command.SCREEN, 0) {
64. public void execute(Displayable displayable) {
65. _display.setCurrent(_form);
66. }
67. };
68.
69. public void startApp() {
70. _cm.add(new ListCommand(Choice.EXCLUSIVE, "EXCLUSIVE"), _form);
71. _cm.add(new ListCommand(Choice.MULTIPLE, "MULTIPLE"), _form);
72. _cm.add(new ListCommand(Choice.IMPLICIT, "IMPLICIT"), _form);
73. _cm.add(_exitCmd, _form);
74.
75. _display = Display.getDisplay(this);
76. _display.setCurrent(_form);
77. }
78.
79. public void pauseApp() { }
80.
81. public void destroyApp(boolean unconditional) {
82. notifyDestroyed();
83. }
84. }
|
|