2 import lejos.nxt.Button;
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
14 * index of the list item at the top of the list; set by constructor, used by select()
16 private int _topIndex = 0;
19 * index of the currently selected item; updated by select() used by display();
21 private int _selectedIndex = 0;
24 * number of rows displayed; set by constructor, used by display()
26 private int _size = 8;
29 *location of the top row of the list; set by constructor, used by display()
31 private int _topRow = 0;
34 *array of items to be displayed ;set by constructor, used by select();
36 private String [] _items;
39 *identifies the currently selected item
41 private String _selChar = ">";
46 public static String blank = " ";
49 *optional menu title displayed immediately above the list of items
51 private String _title;
54 * boolean to cause select to quit
56 private boolean _quit = false;
59 * effective length of items array - number of items before null
64 * This constructor sets location of the top row of the item list to row 0 of the display.
66 public TextMenu( String[] items)
72 * This constructor allows specification location of the item list .
74 public TextMenu( String[] items, int topRow)
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.
85 public TextMenu(String[] items, int topRow, String title)
94 * @param title the new title
96 public void setTitle(String title)
99 if(_topRow == 0)_topRow = 1;
100 if(_length <= 8)_size = _length;
101 if(_size > 8 - _topRow) _size = 8 - _topRow;
105 * set the array of items to be displayed
108 public void setItems(String[] items)
111 if(items == null) return;
113 while(i < items.length && items[i] != null)i++;
115 // LCD.drawInt(_topRow,2,0,7);
116 // LCD.drawInt(_length, 2,5,7 );
118 if(_size > 8 - _topRow) _size = 8 - _topRow;
119 // LCD.drawInt(_size,2,9,7);
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.
129 * The selectedIndex is set to the first menu item.
131 * @return the index of the selected item
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.
143 * This version of select allows the selected index to be set when the menu is first displayed.
145 * @param selectedIndex the idex to start the menu on
146 * @return the index of the selected item
148 public int select(int selectedIndex)
150 _selectedIndex = selectedIndex;
151 // if (_length<_size) _size = _length;
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();
164 if(button == 1) return _selectedIndex;
165 if(button == 8) return -1; //Escape
166 if(button == 4)//scroll forward
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;
175 if(button == 2)//scroll backward
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;
190 * method to call from another thread to quit the menu
198 * helper method used by select()
200 private void display()
202 if(_title != null)LCD.drawString(_title,0,_topRow-1);
203 for (int i = 0;i<_size;i++)
205 LCD.drawString(blank,0,i + _topRow);
207 if(_items[indx] !=null)
209 LCD.drawString(_items[indx],1,i + _topRow);
210 if(indx == _selectedIndex) LCD.drawString(_selChar,0,i + _topRow);
217 * helper method used by display() to caluclate the index in the items array corresponding to a row of the
220 private int index(int row)
222 return (_topIndex + row + _length)%_length;