OSDN Git Service

lejos_NXJ_win32_0_5_0beta.zip
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / java / classes / lejos / nxt / TextMenu.java
1 package lejos.nxt;
2 import lejos.nxt.Button;
3 import lejos.nxt.LCD;
4
5 /**
6  *Displays a list of items.  The select() method allows the user to scroll the list using the right and left keys to scroll forward and backward 
7  * through the list. The location of the list , and an optional title can be specified.
8  * @author Roger Glassey   Feb 20, 2007
9  */
10
11 public class TextMenu  
12 {
13         /**
14          * index of the list item at the top of the list; set by constructor, used by select()
15          **/
16         private int _topIndex = 0;  
17         
18         /** 
19          *      index of the currently selected item; updated by select() used by display();
20          */
21         private int _selectedIndex = 0;
22         
23         /** 
24          * number of rows displayed; set by constructor, used by display()
25          */
26         private int _size = 8;
27         
28         /**
29          *location of the top row of the list; set by constructor, used by display()
30          */
31         private int _topRow = 0;
32         
33         /**
34          *array of items to be displayed ;set by constructor, used by select();
35          */
36         private String [] _items;
37         
38         /**
39          *identifies the currently selected item
40          */
41         private String _selChar = ">";
42         
43         /**
44          * a blank line
45          */
46         public static String blank = "                ";
47         
48         /**
49          *optional menu title displayed immediately above the list of items
50          */
51         private String _title;
52         
53         /**
54          * boolean to cause select to quit 
55          */
56         private boolean _quit = false;
57         
58         /**
59          * effective length of items array  - number of items before null 
60          */
61         private int _length;
62         
63         /**
64          * This constructor sets location of the top row of the item list to row 0 of the display.
65          */
66         public TextMenu( String[] items)
67         {
68                 this.setItems(items);
69         }
70         
71         /**
72          * This constructor allows specification location of the item list .
73          */
74         public TextMenu( String[] items, int topRow)
75         {
76                 _topRow = topRow;
77                 this.setItems(items);
78         }
79         
80         /**
81          * This constuctor allows the specfication of a title (of up to 16 characters) and the location of the item list <br>
82          * The title is displayed in the row above the item lise.
83          * @param items  -  string array containing the menu items. No items beyond the first null will be displayed.
84          */     
85         public TextMenu(String[] items, int topRow, String title)
86         {
87                 _title = title;
88                 _topRow = topRow;
89                 this.setItems(items);
90         }
91         
92         /**
93          * set menu title. 
94          * @param title  the new title
95          */
96         public void setTitle(String title) 
97         {
98                 _title = title;
99                 if(_topRow == 0)_topRow = 1;
100                 if(_length <= 8)_size = _length;
101                 if(_size > 8 - _topRow) _size = 8 - _topRow;
102         }
103         
104         /**
105          * set the array of items to be displayed
106          * @param items
107          */
108         public void setItems(String[] items)
109         {
110                 _items = items;
111                 if(items == null) return;
112                 int i = 0;
113                 while(i < items.length && items[i] != null)i++;
114                 _length = i;
115 //              LCD.drawInt(_topRow,2,0,7);
116 //              LCD.drawInt(_length, 2,5,7 );
117                 _size = _length;
118                 if(_size > 8 - _topRow) _size = 8 - _topRow;
119 //              LCD.drawInt(_size,2,9,7);
120                 _quit = false;
121                 _topIndex = 0;
122         }
123         
124         /**
125          * Allows the user to scroll through the items, using the right and left buttons (forward and back)  The Enter key closes the menu <br>
126          * and returns the index of the selected item. <br>
127          * The menu display wraps items that scroll off the top will reappear on the bottom and vice versa.
128          * 
129          * The selectedIndex is set to the first menu item.
130          * 
131          * @return the index of the selected item
132          **/
133         public int select() 
134         { 
135            return select(0); 
136         } 
137
138         /**
139          * Allows the user to scroll through the items, using the right and left buttons (forward and back)  The Enter key closes the menu <br>
140          * and returns the index of the selected item. <br>
141          * The menu display wraps items that scroll off the top will reappear on the bottom and vice versa.
142          * 
143          * This version of select allows the selected index to be set when the menu is first displayed.
144          * 
145          * @param selectedIndex the idex to start the menu on
146          * @return the index of the selected item
147          **/
148         public int select(int selectedIndex) 
149         { 
150            _selectedIndex = selectedIndex;
151 //              if (_length<_size) _size = _length;
152                 int button = 0;
153                 _quit = false;
154 //              LCD.clear();
155                 display();
156                 while(!_quit)
157                 {
158                         while(Button.readButtons()>0 && !_quit)Thread.yield();// wait for release
159                         while(Button.readButtons()==0 && !_quit) Thread.yield();
160                         if (_quit) return -2; // quit by another thread
161                         try {Thread.sleep(20);} catch (InterruptedException ie) {} // wait to stabilize
162                         button=Button.readButtons();
163                         
164                         if(button == 1) return _selectedIndex;
165                         if(button == 8) return -1; //Escape
166                         if(button == 4)//scroll forward
167                         {
168                                 _selectedIndex ++;
169                                 // check for index out of bounds
170                                 if(_selectedIndex >= _length) _selectedIndex -= _length;                                
171                                 int diff = _selectedIndex - _topIndex;
172                                 if(diff < 0)diff += _length;
173                                 if(diff >= _size) _topIndex = 1+ _selectedIndex  - _size;
174                         }
175                         if(button == 2)//scroll backward
176                         {
177                                 _selectedIndex --;
178                                 // check for index out of bounds
179                                 if(_selectedIndex < 0) _selectedIndex  += _length;
180                                 int diff = _selectedIndex - _topIndex;
181                                 if(diff > _length) diff -= _length;
182                                 if(diff < 0 || diff >= _size)_topIndex = _selectedIndex;
183                         }
184                         display();
185                 }
186                 return -2;
187         }
188         
189         /**
190          * method to call from another thread to quit the menu
191          */
192     public void quit()
193     {
194         _quit = true;
195     }
196         
197         /**
198          * helper method used by select()
199          */
200         private  void display()
201         {
202                 if(_title != null)LCD.drawString(_title,0,_topRow-1);
203                 for (int i = 0;i<_size;i++)
204                 {
205                         LCD.drawString(blank,0,i + _topRow);
206                         int indx = index(i);
207                         if(_items[indx] !=null)
208                         {
209                                 LCD.drawString(_items[indx],1,i + _topRow);
210                                 if(indx == _selectedIndex) LCD.drawString(_selChar,0,i + _topRow);
211                         }
212                 }
213                 LCD.refresh();
214         }
215         
216         /**
217          * helper method used by display() to caluclate the index in the items array corresponding to a row of the 
218          * menu display
219          */
220         private int index(int row)
221         {
222                 return (_topIndex + row + _length)%_length;
223         }
224         
225 }
226