Per Matt, fix use of FieldLabelProvider since it is not actually available to apps...
[zxing.git] / rim / src / com / google / zxing / client / rim / HistoryScreen.java
1 /*\r
2  * Copyright 2008 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.rim;\r
18 \r
19 import com.google.zxing.client.rim.persistence.history.DecodeHistory;\r
20 import com.google.zxing.client.rim.persistence.history.DecodeHistoryItem;\r
21 import com.google.zxing.client.rim.util.Log;\r
22 import net.rim.blackberry.api.browser.Browser;\r
23 import net.rim.blackberry.api.browser.BrowserSession;\r
24 import net.rim.device.api.ui.DrawStyle;\r
25 import net.rim.device.api.ui.Field;\r
26 import net.rim.device.api.ui.FieldChangeListener;\r
27 import net.rim.device.api.ui.Manager;\r
28 import net.rim.device.api.ui.component.ButtonField;\r
29 import net.rim.device.api.ui.component.LabelField;\r
30 import net.rim.device.api.ui.container.MainScreen;\r
31 import net.rim.device.api.ui.container.VerticalFieldManager;\r
32 \r
33 /**\r
34  * The screen used to display the qrcode decoding history.\r
35  *\r
36  * This code was contributed by LifeMarks.\r
37  *\r
38  * @author Matt York (matt@lifemarks.mobi)\r
39  */\r
40 final class HistoryScreen extends MainScreen {\r
41 \r
42   HistoryScreen() {\r
43     setTitle(new LabelField("ZXing - History", DrawStyle.ELLIPSIS | USE_ALL_WIDTH));\r
44     Manager vfm = new VerticalFieldManager(FIELD_HCENTER | VERTICAL_SCROLL);\r
45     Log.debug("Num history items: " + DecodeHistory.getInstance().getNumItems());\r
46     DecodeHistory history = DecodeHistory.getInstance();\r
47     FieldChangeListener itemListener = new ButtonListener();\r
48     for (int i = 0; i < history.getNumItems(); i++) {\r
49       DecodeHistoryItem item = history.getItemAt(i);\r
50       Field labelButton = new ButtonField(item.getURI(), FIELD_HCENTER | ButtonField.CONSUME_CLICK);\r
51       labelButton.setChangeListener(itemListener);\r
52       vfm.add(labelButton);\r
53     }\r
54 \r
55     Field okButton = new ButtonField("OK", FIELD_HCENTER | ButtonField.CONSUME_CLICK);\r
56     okButton.setChangeListener(itemListener);\r
57     add(vfm);\r
58   }\r
59 \r
60   /**\r
61    * Closes the screen when the OK button is pressed.\r
62    */\r
63   private static class ButtonListener implements FieldChangeListener {\r
64     public void fieldChanged(Field field, int context) {\r
65       if (field instanceof ButtonField) {\r
66         BrowserSession browserSession = Browser.getDefaultSession();\r
67         // This cannot be weakened to FieldLabelProvider -- not a public API\r
68         browserSession.displayPage(((ButtonField) field).getLabel());\r
69       }\r
70     }\r
71   }\r
72 \r
73   /**\r
74    * Overriding this method removes the save changes prompt\r
75    */\r
76   public boolean onSavePrompt() {\r
77     setDirty(false);\r
78     return true;\r
79   }\r
80 \r
81 }\r