Source code: com/barteo/emulator/CommandManager.java
1 /*
2 * MicroEmulator
3 * Copyright (C) 2001 Bartek Teodorczyk <barteo@it.pl>
4 * Copyright (C) 2002 3G Lab http://www.3glab.com
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 package com.barteo.emulator;
22
23 import java.util.Vector;
24
25 import javax.microedition.lcdui.Choice;
26 import javax.microedition.lcdui.Command;
27 import javax.microedition.lcdui.CommandListener;
28 import javax.microedition.lcdui.Displayable;
29 import javax.microedition.lcdui.List;
30 import com.barteo.emulator.device.DeviceFactory;
31 import com.barteo.emulator.device.SoftButton;
32
33
34 public class CommandManager
35 {
36 static CommandManager instance = new CommandManager();
37
38 final static Command MENU_COMMAND = new Command("Menu", Command.SCREEN, 0);
39 final static Command BACK_COMMAND = new Command("Back", Command.BACK, 0);
40 final static Command SELECT_COMMAND = new Command("Select", Command.OK, 0);
41 final static List commandList = new List("Menu", Choice.IMPLICIT);
42 static CommandListener commandManagerListener;
43 static Displayable previous;
44
45 static Vector menuCommands = new Vector();
46
47
48 public static CommandManager getInstance()
49 {
50 return instance;
51 }
52
53
54 public void commandAction(Command cmd)
55 {
56 if (cmd == MENU_COMMAND) {
57 previous = MIDletBridge.getMIDletAccess().getDisplayAccess().getCurrent();
58 MIDletBridge.getMIDletAccess().getDisplayAccess().setCurrent(commandList);
59 } else {
60 MIDletBridge.getMIDletAccess().getDisplayAccess().commandAction(cmd);
61 }
62 }
63
64
65 /**
66 * Updates the commands on the soft buttons.
67 *
68 * Requires that the command vector passed in is in priority order.
69 *
70 * A menu is created if there are more commands than the number of soft
71 * buttons. The function of one of the soft buttons will be to display the
72 * menu. The commands with the highest priorities are mapped to the soft
73 * buttons, while the rest will appear in the menu.
74 */
75 public void updateCommands(Vector commands)
76 {
77 // Verify that the list is ordered
78 // Really an assert condition leave till all working
79 if (commands != null) {
80 for (int i=0; i<commands.size()-1; i++) {
81 Command cmda = (Command)commands.elementAt(i);
82 Command cmdb = (Command)commands.elementAt((i+1));
83 if (cmda.getPriority() > cmdb.getPriority()) {
84 System.err.println("Assert: CommandManager.updateCommands commands out of order");
85 }
86 }
87 }
88
89 // Find all the soft buttons we have
90 Vector devButtons = DeviceFactory.getDevice().getSoftButtons();
91 Vector softButtons = new Vector(3);
92
93 for (int i=0; i < devButtons.size(); i++) {
94 SoftButton button = (SoftButton) devButtons.elementAt(i);
95 if (button instanceof SoftButton) {
96 softButtons.addElement(button);
97 button.setCommand(null);
98 }
99 }
100
101 if (commands == null) {
102 return;
103 }
104
105 Vector cmds = commands;
106
107 // Insert a MENU button if a menu is needed so that all the commands can be
108 // displayed.
109 int nSoftButtons = softButtons.size();
110 if (commands.size() > nSoftButtons) {
111 cmds = (Vector)commands.clone();
112 cmds.insertElementAt(MENU_COMMAND, 0);
113 }
114
115 Vector unmappedCmds = new Vector();
116
117 // First try to map commands to buttons which have the command's type as
118 // one of their preferred types.
119 for (int i = 0; i < nSoftButtons && i < cmds.size(); ++i) {
120 Command cmd = (Command)cmds.elementAt(i);
121
122 boolean commandAssignedToAButton = false;
123 for (int j = 0; j < softButtons.size(); ++j) {
124 SoftButton softButton = (SoftButton)softButtons.elementAt(j);
125 if (softButton.preferredCommandType(cmd)) {
126 // Note: there may be several buttons which cmd could be mapped to. We
127 // take the first one, even though it may be "better" to take another.
128 softButton.setCommand(cmd);
129 softButtons.removeElementAt(j);
130 commandAssignedToAButton = true;
131 break;
132 }
133 }
134 if (!commandAssignedToAButton) {
135 unmappedCmds.addElement(cmd);
136 }
137 }
138
139 // Now map all the unassigned commands to buttons.
140 // Note: at this point softButtons contains only buttons which have no cmd.
141 for (int i = 0; i < unmappedCmds.size(); ++i) {
142 ((SoftButton)softButtons.elementAt(i)).setCommand((Command)
143 unmappedCmds.elementAt(i));
144 }
145
146
147 if (cmds.size() > nSoftButtons) {
148 // Create menu for items that can't be mapped to buttons.
149
150 while (commandList.size() > 0) {
151 commandList.delete(0);
152 }
153 menuCommands.removeAllElements();
154
155 for (int i = nSoftButtons; i < cmds.size(); ++i) {
156 Command cmd = (Command)cmds.elementAt(i);
157 commandList.append(cmd.getLabel(), null);
158 menuCommands.addElement(cmd);
159 }
160 }
161 }
162
163
164 static class CommandManagerListener implements CommandListener
165 {
166 /**
167 * Description of the Method
168 *
169 *@param cmd Description of Parameter
170 *@param d Description of Parameter
171 */
172 public void commandAction(Command cmd, Displayable d)
173 {
174 // Get the selection. This must be done before calling setCurrent which
175 // recreates the commandList (recreating the commandList makes the
176 // "selection" the "default selection").
177 Command selection = (Command) menuCommands.elementAt(commandList.getSelectedIndex());
178
179 MIDletBridge.getMIDletAccess().getDisplayAccess().setCurrent(previous);
180
181 if ((cmd == SELECT_COMMAND) || cmd == List.SELECT_COMMAND) {
182 MIDletBridge.getMIDletAccess().getDisplayAccess().commandAction(selection);
183 }
184 }
185 }
186
187
188 static {
189 commandManagerListener = new CommandManagerListener();
190 commandList.addCommand(BACK_COMMAND);
191 commandList.addCommand(SELECT_COMMAND);
192 commandList.setCommandListener(commandManagerListener);
193 }
194
195 }