package com.nexirius.ulc.example1.controller;
import com.nexirius.framework.datamodel.DataModelAdaptor;
import com.nexirius.framework.datamodel.DataModelEvent;
import com.nexirius.framework.datamodel.DataModel;
import com.nexirius.ulc.application.ULCDialogManager;
import com.nexirius.ulc.application.ULCDialogListener;
import com.nexirius.ulc.example1.datamodel.ItemListModel;
import com.nexirius.ulc.example1.datamodel.ItemModel;
import com.nexirius.ulc.example1.persistence.PersistenceManager;
import java.sql.SQLException;
public class ULCController extends DataModelAdaptor {
private static DialogListener dialogListener = new DialogListener();
private ItemListModel listModel;
public ULCController(ItemListModel listModel) {
this.listModel = listModel;
}
public void dataModelEdit(DataModelEvent event) {
if (event.isMethodCall(ItemListModel.EDIT_ITEM_COMMAND)) {
ItemModel editItem = (ItemModel) listModel.getArray().getHighlightedItem();
if (editItem != null) {
ULCDialogManager.instance().popup(editItem, true, dialogListener);
listModel.getArray().sort();
try {
PersistenceManager.saveItem(editItem) ;
int index = listModel.getArray().getChildIndex(editItem);
listModel.getArray().setHighlightedItem(index);
} catch (Exception e) {
//ignore
}
}
} else if (event.isMethodCall(ItemListModel.NEW_ITEM_COMMAND)) {
ItemModel newItem = new ItemModel();
ULCDialogManager.instance().popup(newItem, true, dialogListener);
try {
PersistenceManager.saveItem(newItem) ;
} catch (Exception e) {
e.printStackTrace(); //TODO
}
int index = listModel.getArray().sortInsert(newItem);
listModel.getArray().setHighlightedItem(index);
} else if (event.isMethodCall(ItemListModel.DELETE_ITEM_COMMAND)) {
ItemModel removeItem = (ItemModel) listModel.getArray().getHighlightedItem();
if (removeItem != null) {
try {
PersistenceManager.remove(removeItem);
listModel.getArray().removeItem(removeItem);
} catch (Exception e) {
e.printStackTrace(); //TODO
}
}
}
}
static class DialogListener implements ULCDialogListener {
public void ok(DataModel model) {
try {
PersistenceManager.saveItem((ItemModel) model);
} catch (Exception e) {
e.printStackTrace();
}
}
public void cancel(DataModel model) {
}
}
}