package net.homeip.mleclerc.omnilinkbbclient;
import net.homeip.mleclerc.omnilinkbbclient.category.Category;
import net.homeip.mleclerc.omnilinkbbclient.resources.OmniLinkClientResource;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.ChoiceField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.util.Arrays;
class OmniLinkBBClientScreen extends MainScreen {
private Category[] categories = new Category[0];
private Field[] fields = new Field[0];
private Category selectedCategory;
public OmniLinkBBClientScreen() {
LabelField title = new LabelField(OmniLinkBBClient.getString(OmniLinkClientResource.APPLICATION_TITLE), LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
}
public void addCategory(Category category) {
Arrays.add(categories, category);
}
protected void onUiEngineAttached(boolean attached) {
super.onUiEngineAttached(attached);
ObjectChoiceField categoryChoiceField = new ObjectChoiceField("", categories, 0, Field.FIELD_LEFT);
categoryChoiceField.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if (context == ChoiceField.CONTEXT_CHANGE_OPTION) {
ChoiceField choiceField = (ChoiceField) field;
int index = choiceField.getSelectedIndex();
if (index >= 0) {
Category category = (Category) choiceField.getChoice(index);
displayCategory(category);
}
}
}
});
add(categoryChoiceField);
add(new SeparatorField());
if (categories.length > 0) {
displayCategory(categories[0]);
}
}
protected boolean onSavePrompt() {
return true;
}
private void displayCategory(final Category category) {
selectedCategory = category;
category.reset();
for (int i = 0; i < this.fields.length; i++) {
Field field = this.fields[i];
delete(field);
}
// Execute in a separate thread to avoid blocking the event thread for too long
Runnable exec = new Runnable() {
public void run() {
// Get the fields to display
final Field[] fields = category.getFields();
OmniLinkBBClientScreen.this.fields = fields;
// Add UI elements to the event thread
Runnable exec = new Runnable() {
public void run() {
// Add all the fields to this screen
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
add(field);
}
}
};
OmniLinkBBClient.getUiApplication().invokeLater(exec);
}
};
new Thread(exec).start();
}
protected void makeMenu(Menu menu, int type) {
menu.add(new MenuItem(OmniLinkBBClient.getString(OmniLinkClientResource.CATEGORY_REFRESH), 110, 10) {
public void run() {
displayCategory(selectedCategory);
}
});
super.makeMenu(menu, type);
}
}