package net.sourceforge.pain.logic.event.console.command.builder;
import net.sourceforge.pain.*;
import net.sourceforge.pain.data.*;
import net.sourceforge.pain.data.type.*;
import net.sourceforge.pain.logic.fn.*;
import net.sourceforge.pain.network.console.*;
/**
* PAiN Date: 05.06.2003 Time: 1:40:02
*/
public final class BC_Space extends BuilderCommand {
private Space space;
private int state = STATE_INITIAL;
private static final int STATE_INITIAL = 0;
private static final int STATE_ASK_CREATE_NEW = 1;
private static final int STATE_ENTER_NAME = 2;
private static final int STATE_ENTER_DESC = 3;
private static final int STATE_ENTER_CAPACITY = 4;
private String space_id;
private String newName;
private String newDesc;
private int newCapcacity;
public void processBuilderCommand(BuilderShell p, String args) throws Exception {
if (state > STATE_INITIAL) {
processReply(p, args);
} else if (args == null || args.length() == 0) {
showUsage(p.console);
} else {
String id = args;
IndexedSpacesRegistry reg = Core.getWorld().getIndexedSpacesRegistry();
IndexedSpace is = reg.getSpace(id);
p.activeCommand = this;
space_id = id;
if (is == null) {
askToCreateNew(p);
} else {
space = is.asSpace();
MessageOutFn.outln(p.console, "Space found!");
MessageOutFn.outln(p.console, "{W" + space.getName() + "{x");
MessageOutFn.outln(p.console, space.getDesc());
MessageOutFn.outln(p.console, "Capaciy :" + space.getCapacity());
askName(p);
}
}
}
private void processReply(BuilderShell p, String reply) throws Exception {
switch (state) {
case STATE_ASK_CREATE_NEW:
if ("Y".equalsIgnoreCase(reply.substring(0, 1))) {
askName(p);
} else {
cancelCommand(p);
}
break;
case STATE_ENTER_NAME:
reply = reply.trim();
if (reply.length() == 0) {
if (space == null) {
MessageOutFn.outln(p.console, "Name is too short!");
askName(p);
} else {
newName = space.getName();
MessageOutFn.outln(p.console, "Reusing old name:" + newName);
askSpaceDesc(p);
}
} else if (reply.equalsIgnoreCase("x")) {
cancelCommand(p);
} else if (reply.length() == 1) {
MessageOutFn.outln(p.console, "Name is too short!");
askName(p);
} else {
newName = reply;
askSpaceDesc(p);
}
break;
case STATE_ENTER_DESC:
reply = reply.trim();
if (reply.length() == 0) {
if (space == null) {
MessageOutFn.outln(p.console, "Description is too short!");
askSpaceDesc(p);
} else {
newDesc = space.getDesc();
MessageOutFn.outln(p.console, "Reusing old desc.");
askSpaceCapacity(p);
}
} else if (reply.equalsIgnoreCase("x")) {
cancelCommand(p);
} else if (reply.length() < 10) {
MessageOutFn.outln(p.console, "Description is too short!");
askSpaceDesc(p);
} else {
newDesc = reply;
askSpaceCapacity(p);
}
break;
case STATE_ENTER_CAPACITY:
reply = reply.trim();
if (reply.length() == 0) {
if (space == null) {
askSpaceCapacity(p);
} else {
newCapcacity = space.getCapacity();
MessageOutFn.outln(p.console, "Reusing old capacity.");
finish(p);
}
} else if (reply.equalsIgnoreCase("x")) {
cancelCommand(p);
} else {
try {
newCapcacity = Integer.parseInt(reply);
if (newCapcacity < 0) {
throw new IllegalArgumentException("specified capacity < 0 :" + newCapcacity);
}
} catch (Exception e) {
MessageOutFn.outln(p.console, "{R" + e.getClass().getName() + ":" + e.getMessage() + "{x");
newCapcacity = 0;
askSpaceCapacity(p);
}
if (newCapcacity > 0) {
finish(p);
}
}
break;
}
}
private void finish(BuilderShell p) throws Exception {
if (space == null) {
IndexedSpace is = (IndexedSpace) ObjectFactory.create(IndexedSpace.class);
is.setSpaceUniqueId(space_id);
space = is.asSpace();
fillSpace();
Core.getWorld().getIndexedSpacesRegistry().registerSpace(is);
} else {
fillSpace();
}
MessageOutFn.outln(p.console, "Done.");
p.activeCommand = null;
}
private void fillSpace() {
space.setName(newName);
space.setDesc(newDesc);
space.setCapacity(newCapcacity);
}
private void cancelCommand(BuilderShell p) {
p.activeCommand = null;
}
private void askName(BuilderShell p) {
MessageOutFn.out(p.console, "Enter new space name (" + (space == null ? "" : "ENTER - leave old, ") + "'X' to stop command) :");
state = STATE_ENTER_NAME;
}
private void askSpaceDesc(BuilderShell p) {
MessageOutFn.out(p.console, "Enter new space desc (" + (space == null ? "" : "ENTER - leave old, ") + "'X' to stop command) :");
state = STATE_ENTER_DESC;
}
private void askSpaceCapacity(BuilderShell p) {
MessageOutFn.out(p.console, "Enter new space capacity (" + (space == null ? "" : "ENTER - leave old, ") + "'X' to stop command) :");
state = STATE_ENTER_CAPACITY;
}
private void askToCreateNew(BuilderShell p) {
MessageOutFn.outln(p.console, "Space not found:" + space_id);
MessageOutFn.out(p.console, "Would you like to craete new? (Y/N):");
state = STATE_ASK_CREATE_NEW;
}
public void showUsage(Console console) {
MessageOutFn.outln(console, "Space:Command syntax <SPACE_ID>\n");
}
public void showHelp(Console console) {
MessageOutFn.outln(console, "Builder command SPACE is used to create or modify indexed space");
MessageOutFn.outln(console, "Usage: space <SPACE_ID>");
}
public String getName() {
return "Space";
}
}