Added simplistic ability to rotate an LCDUI image (once), to help out some devices...
[zxing.git] / javame / src / com / google / zxing / client / j2me / Menu.java
1 /*\r
2  * Copyright 2009 ZXing authors\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package com.google.zxing.client.j2me;\r
18 \r
19 import javax.microedition.lcdui.Command;\r
20 import javax.microedition.lcdui.CommandListener;\r
21 import javax.microedition.lcdui.Display;\r
22 import javax.microedition.lcdui.Displayable;\r
23 import javax.microedition.lcdui.List;\r
24 \r
25 /**\r
26  * The Menu form simply adds Command Listener functionality\r
27  * to the standard List User Interface component.\r
28  *\r
29  * @author Simon Flannery (simon.flannery@gmail.com)\r
30  */\r
31 final class Menu extends List implements CommandListener {\r
32 \r
33   private final ZXingMIDlet zXingMIDlet;\r
34   private final Command cancelCommand;\r
35   private final Command barcodeCommand;\r
36 \r
37   /**\r
38    * Creates a new Search List and initialises all components.\r
39    *\r
40    * @param parent The Parent ZXing MIDlet.\r
41    * @param title The title of the List.\r
42    * @param item The caption of the item action Command.\r
43    */\r
44   Menu(ZXingMIDlet parent, String title, String item) {\r
45     super(title, IMPLICIT); // Set the title of the form\r
46     zXingMIDlet = parent;\r
47     // Build the UI components\r
48     cancelCommand  = new Command("Cancel", Command.CANCEL, 0);\r
49     barcodeCommand = new Command(item, Command.ITEM, 0);\r
50     addCommand(cancelCommand);\r
51     addCommand(barcodeCommand);\r
52     setCommandListener(this);\r
53   }\r
54 \r
55   /**\r
56    * A convenience method for getting the selected option item.\r
57    *\r
58    * @return The selected option represented as a String. If no option is\r
59    *         selected, then the empty string is returned.\r
60    */\r
61   public String getSelectedString() {\r
62     String result = "";\r
63     if (getSelectedIndex() != -1) {\r
64       result = getString(getSelectedIndex());\r
65     }\r
66     return result;\r
67   }\r
68 \r
69   /**\r
70    * A convenience method for removing all items from the list.\r
71    * While the size of the list does not equal zero, the first item of the list is deleted.\r
72    */\r
73   public void clear() {\r
74     while (size() != 0) { // Delete the first-most element until there is no first-most element\r
75       delete(0);\r
76     }\r
77   }\r
78 \r
79   /**\r
80    * CommandListener Required Implementation for capturing soft key presses.\r
81    * This is where all call back methods (of the MIDlet) are serviced.\r
82    *\r
83    * @param command The command requiring attention.\r
84    * @param displayable The Display.\r
85    */\r
86   public void commandAction(Command command, Displayable displayable) {\r
87     if (command == cancelCommand) { /* Detecting the soft key press. */\r
88       Display.getDisplay(zXingMIDlet).setCurrent(zXingMIDlet.getCanvas());\r
89     } else if (command == barcodeCommand || command == SELECT_COMMAND) {\r
90       if (getSelectedIndex() != -1) {\r
91         zXingMIDlet.itemRequest();\r
92       }\r
93     }\r
94   }\r
95 \r
96 }\r