15 * |
15 * |
16 */ |
16 */ |
17 package javax.microedition.lcdui; |
17 package javax.microedition.lcdui; |
18 |
18 |
19 import java.util.Vector; |
19 import java.util.Vector; |
|
20 import java.lang.ref.WeakReference; |
20 |
21 |
21 import javax.microedition.lcdui.EventDispatcher.LCDUIEvent; |
22 import javax.microedition.lcdui.EventDispatcher.LCDUIEvent; |
22 import com.nokia.mj.impl.nokialcdui.ItemControlStateChangeListener; |
23 import com.nokia.mj.impl.nokialcdui.ItemControlStateChangeListener; |
23 import org.eclipse.swt.graphics.Point; |
24 import org.eclipse.swt.graphics.Point; |
24 |
25 |
134 |
135 |
135 static final int LAYOUT_HORIZONTAL_MASK = LAYOUT_CENTER; // 15; |
136 static final int LAYOUT_HORIZONTAL_MASK = LAYOUT_CENTER; // 15; |
136 |
137 |
137 static final int LAYOUT_VERTICAL_MASK = LAYOUT_VCENTER; // 48; |
138 static final int LAYOUT_VERTICAL_MASK = LAYOUT_VCENTER; // 48; |
138 |
139 |
139 |
140 /** |
140 static final int UPDATE_NONE = 0; |
141 * If Item is changed, reasons for Re-layouting. |
141 |
142 */ |
142 static final int UPDATE_ADDCOMMAND = 1; |
143 static final int UPDATE_NONE = 0; |
143 |
144 |
144 static final int UPDATE_REMOVECOMMAND = 2; |
145 static final int UPDATE_ADDCOMMAND = 1; |
145 |
146 static final int UPDATE_REMOVECOMMAND = 1 << 1; |
146 /** |
147 static final int UPDATE_DEFAULTCOMMAND = 1 << 2; |
147 * Item content has changed. Re-layouting not needed. |
148 static final int UPDATE_ITEMCOMMANDLISTENER = 1 << 3; |
148 */ |
149 static final int UPDATE_LABEL = 1 << 4; |
149 static final int UPDATE_CONTENT = 3; // general update value |
150 static final int UPDATE_LAYOUT = 1 << 5; |
150 |
151 static final int UPDATE_PREFERREDSIZE = 1 << 6; |
151 static final int UPDATE_REASON_MASK = 255; |
152 |
152 |
153 static final int UPDATE_HEIGHT_CHANGED = 1 << 7; |
153 /** |
154 static final int UPDATE_WIDTH_CHANGED = 1 << 8; |
154 * Item height has changed. Re-layouting not needed. |
|
155 */ |
|
156 static final int UPDATE_HEIGHT_CHANGED = 256; |
|
157 |
|
158 /** |
|
159 * Item width is changed. Re-layouting asap. |
|
160 */ |
|
161 static final int UPDATE_WIDTH_CHANGED = 512; |
|
162 |
|
163 /** |
|
164 * Item width and height changed. Re-layouting asap. |
|
165 */ |
|
166 static final int UPDATE_SIZE_CHANGED = |
155 static final int UPDATE_SIZE_CHANGED = |
167 UPDATE_HEIGHT_CHANGED | UPDATE_WIDTH_CHANGED; |
156 UPDATE_HEIGHT_CHANGED | UPDATE_WIDTH_CHANGED; |
|
157 static final int UPDATE_SIZE_MASK = ~UPDATE_SIZE_CHANGED; |
|
158 |
|
159 static final int UPDATE_ITEM_MAX = 1 << 15; |
|
160 |
168 |
161 |
169 private String label; |
162 private String label; |
170 |
163 |
171 /** |
164 /** |
172 * Vector of commands added to this item that have eSWT Command associated |
165 * Vector of commands added to this item that have eSWT Command associated |
175 private Vector commands = new Vector(); |
168 private Vector commands = new Vector(); |
176 private ItemCommandListener itemCommandListener; |
169 private ItemCommandListener itemCommandListener; |
177 private ItemControlStateChangeListener controlListener; |
170 private ItemControlStateChangeListener controlListener; |
178 |
171 |
179 private Command defaultCommand; |
172 private Command defaultCommand; |
180 private Screen parent; |
173 private WeakReference wParent; |
181 |
174 |
182 private int layout; |
175 private int layout; |
183 private int lockedPrefWidth = -1; |
176 private int lockedPrefWidth = -1; |
184 private int lockedPrefHeight = -1; |
177 private int lockedPrefHeight = -1; |
185 |
178 |
189 private boolean focused; |
182 private boolean focused; |
190 private boolean visible; |
183 private boolean visible; |
191 |
184 |
192 /** |
185 /** |
193 * Sets the parent of this Item. |
186 * Sets the parent of this Item. |
194 * @param parent new Parent. If null, current parent is removed. |
187 * @param parent new Parent. If null, current parent is to be removed. |
195 */ |
188 */ |
196 void setParent(Screen parent) |
189 void setParent(Screen parent) |
197 { |
190 { |
198 this.parent = parent; |
191 if(parent != null) |
|
192 { |
|
193 wParent = new WeakReference(parent); |
|
194 } |
|
195 else |
|
196 { |
|
197 clearParent(); |
|
198 } |
199 } |
199 } |
200 |
200 |
201 /** |
201 /** |
202 * Gets the Item's parent. |
202 * Gets the Item's parent. |
203 * |
203 * |
204 * @return the Item's parent or null if it has none. |
204 * @return the Item's parent or null if it has none. |
205 */ |
205 */ |
206 Screen getParent() |
206 Screen getParent() |
207 { |
207 { |
208 return parent; |
208 if(wParent != null) |
|
209 { |
|
210 return (Screen)wParent.get(); |
|
211 } |
|
212 else |
|
213 { |
|
214 return null; |
|
215 } |
|
216 } |
|
217 |
|
218 /** |
|
219 * Clears the Item's parent. |
|
220 * |
|
221 */ |
|
222 void clearParent() |
|
223 { |
|
224 if(wParent != null) |
|
225 { |
|
226 wParent.clear(); |
|
227 wParent = null; |
|
228 } |
209 } |
229 } |
210 |
230 |
211 /** |
231 /** |
212 * Sets the label of the item. |
232 * Sets the label of the item. |
213 * |
233 * |
219 if(isContainedInAlert()) |
239 if(isContainedInAlert()) |
220 { |
240 { |
221 throw new IllegalStateException( |
241 throw new IllegalStateException( |
222 MsgRepository.ITEM_EXCEPTION_OWNED_BY_ALERT); |
242 MsgRepository.ITEM_EXCEPTION_OWNED_BY_ALERT); |
223 } |
243 } |
|
244 if((newLabel == null) && (label == null)) |
|
245 { |
|
246 return; |
|
247 } |
224 label = newLabel; |
248 label = newLabel; |
225 updateParent(UPDATE_SIZE_CHANGED); |
249 updateParent(UPDATE_LABEL | UPDATE_SIZE_CHANGED); |
226 } |
250 } |
227 |
251 |
228 /** |
252 /** |
229 * Gets the label of the item. |
253 * Gets the label of the item. |
230 * |
254 * |
274 throw new IllegalArgumentException( |
298 throw new IllegalArgumentException( |
275 MsgRepository.ITEM_EXCEPTION_INVALID_LAYOUT); |
299 MsgRepository.ITEM_EXCEPTION_INVALID_LAYOUT); |
276 } |
300 } |
277 layout = newLayout; |
301 layout = newLayout; |
278 Logger.method(this, "setLayout", String.valueOf(layout)); |
302 Logger.method(this, "setLayout", String.valueOf(layout)); |
279 updateParent(UPDATE_SIZE_CHANGED); |
303 updateParent(UPDATE_LAYOUT | UPDATE_SIZE_CHANGED); |
280 } |
304 } |
281 |
305 |
282 /** |
306 /** |
283 * Adds command to this item. If same command is already added to this item, |
307 * Adds command to this item. If same command is already added to this item, |
284 * nothing happens. |
308 * nothing happens. |
300 MsgRepository.ITEM_EXCEPTION_NULL_COMMAND_ADDED); |
324 MsgRepository.ITEM_EXCEPTION_NULL_COMMAND_ADDED); |
301 } |
325 } |
302 if(!commands.contains(command)) |
326 if(!commands.contains(command)) |
303 { |
327 { |
304 commands.addElement(command); |
328 commands.addElement(command); |
305 |
329 updateParent(UPDATE_ADDCOMMAND, command); |
306 int reason = UPDATE_ADDCOMMAND; |
|
307 |
|
308 if(this instanceof StringItem && commands.size() == 1) |
|
309 { |
|
310 reason |= UPDATE_SIZE_CHANGED; |
|
311 } |
|
312 if(this instanceof ImageItem && commands.size() == 1) |
|
313 { |
|
314 reason |= UPDATE_SIZE_CHANGED; |
|
315 } |
|
316 |
|
317 updateParent(reason, command); |
|
318 } |
330 } |
319 } |
331 } |
320 |
332 |
321 /** |
333 /** |
322 * Removes command from the item. If command doesn't exists in this item, |
334 * Removes command from the item. If command doesn't exists in this item, |
330 // called with null-parameter ! |
342 // called with null-parameter ! |
331 if(command != null && commands.contains(command)) |
343 if(command != null && commands.contains(command)) |
332 { |
344 { |
333 // Remove command from commands-vector |
345 // Remove command from commands-vector |
334 commands.removeElement(command); |
346 commands.removeElement(command); |
335 |
347 if(defaultCommand == command) |
336 defaultCommand = null; |
348 { |
337 |
349 defaultCommand = null; |
338 int reason = UPDATE_REMOVECOMMAND; |
350 } |
339 |
351 updateParent(UPDATE_REMOVECOMMAND, command); |
340 if(this instanceof StringItem && commands.size() == 0) |
|
341 { |
|
342 reason |= UPDATE_SIZE_CHANGED; |
|
343 } |
|
344 if(this instanceof ImageItem && commands.size() == 0) |
|
345 { |
|
346 reason |= UPDATE_SIZE_CHANGED; |
|
347 } |
|
348 |
|
349 updateParent(reason, command); |
|
350 } |
352 } |
351 } |
353 } |
352 |
354 |
353 |
355 |
354 /** |
356 /** |
517 lockedPrefHeight = h; |
519 lockedPrefHeight = h; |
518 checkLockedSizes(); |
520 checkLockedSizes(); |
519 Logger.method(this, "setPrefSize", |
521 Logger.method(this, "setPrefSize", |
520 String.valueOf(lockedPrefWidth), |
522 String.valueOf(lockedPrefWidth), |
521 String.valueOf(lockedPrefHeight)); |
523 String.valueOf(lockedPrefHeight)); |
522 updateParent(UPDATE_SIZE_CHANGED); |
524 updateParent(UPDATE_PREFERREDSIZE | UPDATE_SIZE_CHANGED); |
523 } |
525 } |
524 |
526 |
525 /** |
527 /** |
526 * Sets the default command of this item. This method doesn't remove |
528 * Sets the default command of this item. This method doesn't remove |
527 * commands from the item, it just tells which command is the default one. |
529 * commands from the item, it just tells which command is the default one. |
566 { |
568 { |
567 throw new IllegalStateException( |
569 throw new IllegalStateException( |
568 MsgRepository.ITEM_EXCEPTION_NOT_OWNED_BY_FORM); |
570 MsgRepository.ITEM_EXCEPTION_NOT_OWNED_BY_FORM); |
569 } |
571 } |
570 // Notify item state listener |
572 // Notify item state listener |
571 ((Form) parent).notifyItemStateChanged(this); |
573 ((Form) getParent()).notifyItemStateChanged(this); |
572 } |
574 } |
573 |
575 |
574 /** |
576 /** |
575 * Is this item's size locked. |
577 * Is this item's size locked. |
576 */ |
578 */ |
626 /** |
628 /** |
627 * If the item is owned by an Alert. |
629 * If the item is owned by an Alert. |
628 */ |
630 */ |
629 boolean isContainedInAlert() |
631 boolean isContainedInAlert() |
630 { |
632 { |
631 return (parent != null && parent instanceof Alert); |
633 return ((wParent != null) && (getParent() instanceof Alert)); |
632 } |
634 } |
633 |
635 |
634 /** |
636 /** |
635 * If the item is owned by an Form. |
637 * If the item is owned by an Form. |
636 */ |
638 */ |
637 boolean isContainedInForm() |
639 boolean isContainedInForm() |
638 { |
640 { |
639 return (parent != null && parent instanceof Form); |
641 return ((wParent != null) && (getParent() instanceof Form)); |
640 } |
642 } |
641 |
643 |
642 /** |
644 /** |
643 * Return internal layout with optional custom flags. |
645 * Return internal layout with optional custom flags. |
644 * |
646 * |