View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2005 Andres Navarro
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   *  Contributor(s):
21   *    3GLab
22   */
23  
24  package javax.microedition.lcdui;
25  
26  import org.microemu.device.DeviceFactory;
27  import org.microemu.device.ui.ListUI;
28  
29  public class List extends Screen implements Choice {
30  
31      public static final Command SELECT_COMMAND = new Command("", Command.SCREEN, 0);
32  
33      ChoiceGroup choiceGroup;
34  
35      private Command selCommand;
36  
37      private int initialPressedItem;
38  
39      public List(String title, int listType) {
40          super(title);
41          super.setUI(DeviceFactory.getDevice().getUIFactory().createListUI(this));
42  
43          if (listType != Choice.IMPLICIT && listType != Choice.MULTIPLE && listType != Choice.EXCLUSIVE)
44              throw new IllegalArgumentException("Illegal list type");
45  
46          if (listType == Choice.IMPLICIT) {
47              choiceGroup = new ChoiceGroup(null, Choice.IMPLICIT, false);
48          } else {
49              choiceGroup = new ChoiceGroup(null, listType);
50          }
51  
52          choiceGroup.setOwner(this);
53          choiceGroup.setFocus(true);
54  
55          this.selCommand = SELECT_COMMAND;
56          this.initialPressedItem = -1;
57      }
58  
59      public List(String title, int listType, String[] stringElements, Image[] imageElements) {
60          super(title);
61          super.setUI(DeviceFactory.getDevice().getUIFactory().createListUI(this));
62  
63          if (listType == Choice.IMPLICIT) {
64              choiceGroup = new ChoiceGroup(null, Choice.IMPLICIT, stringElements, imageElements, false);
65              for (int i = 0; i < size(); i++) {
66                  set(i, getString(i), null);
67              }
68          } else {
69              choiceGroup = new ChoiceGroup(null, listType, stringElements, imageElements);
70          }
71          choiceGroup.setOwner(this);
72          choiceGroup.setFocus(true);
73  
74          this.selCommand = SELECT_COMMAND;
75          this.initialPressedItem = -1;
76      }
77  
78      public int append(String stringPart, Image imagePart) {
79  		if (ui.getClass().getName().equals("org.microemu.android.device.ui.AndroidListUI")) {
80  			return ((ListUI) ui).append(stringPart, imagePart);
81  		} else {
82  			return choiceGroup.append(stringPart, imagePart);
83  		}
84      }
85  
86      public void delete(int elementNum) {
87          choiceGroup.delete(elementNum);
88      }
89  
90      public void deleteAll() {
91          choiceGroup.deleteAll();
92      }
93  
94      public int getFitPolicy() {
95          return choiceGroup.getFitPolicy();
96      }
97  
98      public Font getFont(int elementNum) {
99          return choiceGroup.getFont(elementNum);
100     }
101 
102     public Image getImage(int elementNum) {
103         return choiceGroup.getImage(elementNum);
104     }
105 
106     public int getSelectedFlags(boolean[] selectedArray_return) {
107         return choiceGroup.getSelectedFlags(selectedArray_return);
108     }
109 
110     public int getSelectedIndex() {
111 		if (ui.getClass().getName().equals("org.microemu.android.device.ui.AndroidListUI")) {
112 			return ((ListUI) ui).getSelectedIndex();
113 		} else {
114 			return choiceGroup.getSelectedIndex();
115 		}
116     }
117 
118     public String getString(int elementNum) {
119 		if (ui.getClass().getName().equals("org.microemu.android.device.ui.AndroidListUI")) {
120 			return ((ListUI) ui).getString(elementNum);
121 		} else {
122         	return choiceGroup.getString(elementNum);
123 		}
124     }
125 
126     public void insert(int elementNum, String stringPart, Image imagePart) {
127         choiceGroup.insert(elementNum, stringPart, imagePart);
128     }
129 
130     public boolean isSelected(int elementNum) {
131         return choiceGroup.isSelected(elementNum);
132     }
133 
134     public void removeCommand(Command cmd) {
135         // TODO implement
136         super.removeCommand(cmd);
137     }
138 
139     public void set(int elementNum, String stringPart, Image imagePart) {
140         choiceGroup.set(elementNum, stringPart, imagePart);
141     }
142 
143     public void setFitPolicy(int policy) {
144         choiceGroup.setFitPolicy(policy);
145     }
146 
147     public void setFont(int elementNum, Font font) {
148         choiceGroup.setFont(elementNum, font);
149     }
150 
151     public void setSelectCommand(Command command) {
152         selCommand = command;
153         
154         ((ListUI) ui).setSelectCommand(command);
155     }
156 
157     public void setSelectedFlags(boolean[] selectedArray) {
158         choiceGroup.setSelectedFlags(selectedArray);
159     }
160 
161     public void setSelectedIndex(int elementNum, boolean selected) {
162         choiceGroup.setSelectedIndex(elementNum, selected);
163     }
164 
165     public void setTicker(Ticker ticker) {
166         super.setTicker(ticker);
167         // TODO size of changed probably
168     }
169 
170     public void setTitle(String s) {
171         // TODO implement
172         super.setTitle(s);
173     }
174 
175     void keyPressed(int keyCode) {
176         if (Display.getGameAction(keyCode) == Canvas.FIRE && choiceGroup.select() && super.getCommandListener() != null
177                 && choiceGroup.choiceType == Choice.IMPLICIT) {
178             super.getCommandListener().commandAction(selCommand, this);
179         } else {
180             super.keyPressed(keyCode);
181         }
182     }
183 
184     void pointerPressed(int x, int y) {
185         Ticker ticker = getTicker();
186         if (ticker != null) {
187             y -= ticker.getHeight();
188         }
189         // TODO remove this StringComponent object when native UI is completed
190         StringComponent title = new StringComponent(getTitle());
191         y -= title.getHeight();
192         y -= 1;
193         if (y >= 0 && y < viewPortHeight) {
194             int pressedItem = choiceGroup.getItemIndexAt(x, y + viewPortY);
195             if (pressedItem != -1) {
196                 if (choiceGroup.choiceType == Choice.MULTIPLE) {
197                     setSelectedIndex(pressedItem, !isSelected(pressedItem));
198                 } else {
199                     setSelectedIndex(pressedItem, true);
200                 }
201                 initialPressedItem = pressedItem;
202             }
203         }
204     }
205 
206     void pointerReleased(int x, int y) {
207         Ticker ticker = getTicker();
208         if (ticker != null) {
209             y -= ticker.getHeight();
210         }
211         // TODO remove this StringComponent object when native UI is completed
212         StringComponent title = new StringComponent(getTitle());
213         y -= title.getHeight();
214         y -= 1;
215         if (y >= 0 && y < viewPortHeight && choiceGroup.choiceType == Choice.IMPLICIT) {
216             int releasedItem = choiceGroup.getItemIndexAt(x, y + viewPortY);
217             if (releasedItem != -1) {
218                 if (releasedItem == initialPressedItem && super.getCommandListener() != null
219                         && choiceGroup.choiceType == Choice.IMPLICIT) {
220                     super.getCommandListener().commandAction(SELECT_COMMAND, this);
221                 }
222             }
223         }
224     }
225 
226     int paintContent(Graphics g) {
227         return choiceGroup.paint(g);
228     }
229 
230     public int size() {
231         return choiceGroup.size();
232     }
233 
234     void showNotify() {
235         super.showNotify();
236 
237         int selectedItemIndex = getSelectedIndex();
238         int heightToItem = choiceGroup.getHeightToItem(selectedItemIndex);
239         int heightAfterItem = heightToItem;
240         if (selectedItemIndex >= 0) {
241             heightAfterItem += choiceGroup.getItemHeight(selectedItemIndex);
242         }
243         if (viewPortY > heightToItem) {
244             viewPortY = heightToItem;
245         } else if ((viewPortY + viewPortHeight) < heightAfterItem) {
246             viewPortY = heightAfterItem - viewPortHeight;
247         }
248     }
249 
250     int traverse(int gameKeyCode, int top, int bottom) {
251         int traverse = choiceGroup.traverse(gameKeyCode, top, bottom, true);
252         if (traverse == Item.OUTOFITEM) {
253             return 0;
254         } else {
255             return traverse;
256         }
257     }
258 
259 }