if (editMessage.getEditType() == EditType.CREATE_CELL) {
// The create message contains a setup class of the cell setup
// information. Simply parse this stream, which will result in a
// setup class of the property type.
CellServerState setup = ((CellCreateMessage)editMessage).getCellSetup();
// Fetch the server-side cell class name and create the cell
String className = setup.getServerClassName();
logger.fine("Attempting to load cell mo: " + className);
CellMO cellMO = CellMOFactory.loadCellMO(className);
if (cellMO == null) {
/* Log a warning and move onto the next cell */
logger.warning("Unable to load cell MO: " + className );
sender.send(clientID, new ErrorMessage(editMessage.getMessageID(),
"Unable to load cell MO: " + className));
return;
}
// Find the parent cell (if any)
CellMO parent = null;
CellID parentCellID = ((CellCreateMessage)editMessage).getParentCellID();
if (parentCellID != null) {
parent = CellManagerMO.getCell(parentCellID);
}
/* Call the cell's setup method */
try {
cellMO.setServerState(setup);
if (parent == null) {
WonderlandContext.getCellManager().insertCellInWorld(cellMO);
} else {
parent.addChild(cellMO);
}
//everything worked okay. send response message
sender.send(clientID, new CellCreatedMessage(editMessage.getMessageID(), cellMO.getCellID()));
} catch (ClassCastException cce) {
logger.log(Level.WARNING, "Error setting up new cell " +
cellMO.getName() + " of type " +
cellMO.getClass() + ", it does not implement " +
"BeanSetupMO.", cce);
sender.send(clientID,
new ErrorMessage(editMessage.getMessageID(),cce));
return;
} catch (MultipleParentException excp) {
logger.log(Level.WARNING, "Error adding new cell " + cellMO.getName()
+ " of type " + cellMO.getClass() + ", has multiple parents", excp);
sender.send(clientID,
new ErrorMessage(editMessage.getMessageID(), excp));
return;
}
}
else if (editMessage.getEditType() == EditType.DELETE_CELL) {
// Find the cell object given the ID of the cell. If the ID is
// invalid, we just log an error and return.
CellID cellID = ((CellDeleteMessage)editMessage).getCellID();
CellMO cellMO = CellManagerMO.getCell(cellID);
if (cellMO == null) {
logger.warning("No cell found to delete with cell id " + cellID);
return;
}
// Find out the parent of the cell. This may be null if the cell is
// at the world root. This determines from where to remove the cell
CellMO parentMO = cellMO.getParent();
if (parentMO != null) {
parentMO.removeChild(cellMO);
}
else {
CellManagerMO.getCellManager().removeCellFromWorld(cellMO);
}
}
else if (editMessage.getEditType() == EditType.DUPLICATE_CELL) {
// Find the cell object given the ID of the cell. If the ID is
// invalid, we just log an error and return.
CellID cellID = ((CellDuplicateMessage)editMessage).getCellID();
CellMO cellMO = CellManagerMO.getCell(cellID);
if (cellMO == null) {
logger.warning("No cell found to duplicate with cell id " + cellID);
sender.send(clientID, new ErrorMessage(editMessage.getMessageID(),
"No cell found to duplicate with cell id " + cellID));
return;
}
CellMO parentCellMO = cellMO.getParent();
// We need to fetch the current state of the cell from the cell we
// wish to duplicate. We also need the name of the server-side cell
// class
CellServerState state = cellMO.getServerState(null);
String className = state.getServerClassName();
// Attempt to create the cell using the cell factory and the class
// name of the server-side cell.
CellMO newCellMO = CellMOFactory.loadCellMO(className);
if (newCellMO == null) {
/* Log a warning and move onto the next cell */
logger.warning("Unable to duplicate cell MO: " + className);
sender.send(clientID, new ErrorMessage(editMessage.getMessageID(),
"Unable to duplicate cell MO: " + className));
return;
}
// We want to modify the position of the new cell slight, so we
// offset the position by (1, 1, 1).
PositionComponentServerState position = (PositionComponentServerState)state.getComponentServerState(PositionComponentServerState.class);
if (position == null) {
logger.warning("Unable to determine the position of the cell " +
"to duplicate with id " + cellID);
sender.send(clientID, new ErrorMessage(editMessage.getMessageID(),
"Unable to determine the position of the cell " +
"to duplicate with id " + cellID));
return;
}
Vector3f offset = new Vector3f(1, 0, 1);
Vector3f origin = position.getTranslation();
position.setTranslation(offset.add(origin));
state.addComponentServerState(position);
// Set the desired name of the cell contained within the message
state.setName(((CellDuplicateMessage)editMessage).getCellName());
// Set the state of the new cell and add it to the same parent as
// the old cell. If the old parent cell is null, we just insert it
// as root.
newCellMO.setServerState(state);