package com.nexirius.ulc.example1.controller;
import com.nexirius.framework.datamodel.DataModelAdaptor;
import com.nexirius.framework.datamodel.DataModelEvent;
import com.nexirius.framework.application.DialogManager;
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 SwingController extends DataModelAdaptor {
private ItemListModel listModel;
public SwingController(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 && DialogManager.getPopupEditorAdaptor().popupEdit(editItem)) {
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();
if (DialogManager.getPopupEditorAdaptor().popupEdit(newItem)) {
int index = listModel.getArray().sortInsert(newItem);
listModel.getArray().setHighlightedItem(index);
try {
PersistenceManager.saveItem(newItem) ;
} catch (Exception e) {
e.printStackTrace(); //TODO
}
}
} else if (event.isMethodCall(ItemListModel.DELETE_ITEM_COMMAND)) {
ItemModel removeItem = (ItemModel) listModel.getArray().getHighlightedItem();
if (removeItem != null && DialogManager.ask("reallyDelete", "Yes", "No", true)) {
try {
PersistenceManager.remove(removeItem);
listModel.getArray().removeItem(removeItem);
} catch (Exception e) {
e.printStackTrace(); //TODO
}
}
}
}
}