diff -r 000000000000 -r c46bb04134cb src/main/java/org/vaadin/console/client/ui/VTextConsole.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/vaadin/console/client/ui/VTextConsole.java Sun May 15 17:46:06 2011 +0400 @@ -0,0 +1,252 @@ +package org.vaadin.console.client.ui; + +import org.vaadin.rpc.client.ClientSideHandler; +import org.vaadin.rpc.client.ClientSideProxy; +import org.vaadin.rpc.client.ClientSideProxy.Transcation; +import org.vaadin.rpc.client.Method; + +import com.vaadin.terminal.gwt.client.ApplicationConnection; +import com.vaadin.terminal.gwt.client.Paintable; +import com.vaadin.terminal.gwt.client.UIDL; +import com.vaadin.terminal.gwt.client.Util; + +/** + * Vaadin client-side integration to Console GWT Widget. + * + * @author Sami Ekblad / Vaadin + * + */ +public class VTextConsole extends TextConsole implements Paintable, + TextConsoleHandler, ClientSideHandler { + + private static final String CSS_CLASS_NAME = "v-console"; + + private final ClientSideProxy comm = new ClientSideProxy("VTextConsole", + this); + + /** + * The constructor should first call super() to initialize the component and + * then handle any initialization relevant to Vaadin. + */ + public VTextConsole() { + super(); + getElement().addClassName(CSS_CLASS_NAME); + setHandler(this); + + // Register all server-driven functions + registerServerCallbacks(); + } + + private void registerServerCallbacks() { + comm.register("setGreeting", new Method() { + public void invoke(final String methodName, final Object[] data) { + getConfig().setGreeting((String) data[0]); + } + }); + comm.register("setPs", new Method() { + public void invoke(final String methodName, final Object[] data) { + setPs((String) data[0]); + } + }); + comm.register("setWrap", new Method() { + public void invoke(final String methodName, final Object[] data) { + getConfig().setWrap((Boolean) data[0]); + } + }); + comm.register("setRows", new Method() { + public void invoke(final String methodName, final Object[] data) { + getConfig().setRows((Integer) data[0]); + setRows((Integer) data[0]); + } + }); + comm.register("setCols", new Method() { + public void invoke(final String methodName, final Object[] data) { + getConfig().setCols((Integer) data[0]); + setCols((Integer) data[0]); + } + }); + comm.register("print", new Method() { + public void invoke(final String methodName, final Object[] data) { + print((String) data[0]); + } + }); + comm.register("println", new Method() { + public void invoke(final String methodName, final Object[] data) { + println((String) data[0]); + } + }); + comm.register("println", new Method() { + public void invoke(final String methodName, final Object[] data) { + println((String) data[0]); + } + }); + comm.register("prompt", new Method() { + public void invoke(final String methodName, final Object[] data) { + prompt((String) (data.length > 0 ? data[0] : null)); + } + }); + comm.register("ff", new Method() { + public void invoke(final String methodName, final Object[] data) { + formFeed(); + } + }); + comm.register("cr", new Method() { + public void invoke(final String methodName, final Object[] data) { + carriageReturn(); + } + }); + comm.register("lf", new Method() { + public void invoke(final String methodName, final Object[] data) { + carriageReturn(); + } + }); + comm.register("clearBuffer", new Method() { + public void invoke(final String methodName, final Object[] data) { + clearBuffer(); + } + }); + comm.register("reset", new Method() { + public void invoke(final String methodName, final Object[] data) { + reset(); + } + }); + comm.register("newLine", new Method() { + public void invoke(final String methodName, final Object[] data) { + newLine(); + } + }); + comm.register("scrollToEnd", new Method() { + public void invoke(final String methodName, final Object[] data) { + scrollToEnd(); + } + }); + comm.register("bell", new Method() { + public void invoke(final String methodName, final Object[] data) { + bell(); + } + }); + comm.register("setMaxBufferSize", new Method() { + public void invoke(final String methodName, final Object[] data) { + getConfig().setMaxBufferSize((Integer) data[0]); + setMaxBufferSize((Integer) data[0]); + } + }); + comm.register("clearHistory", new Method() { + public void invoke(final String methodName, final Object[] data) { + clearCommandHistory(); + } + }); + } + + /** + * Called whenever an update is received from the server + */ + public void updateFromUIDL(final UIDL serverData, + final ApplicationConnection appConn) { + comm.update(this,serverData, appConn); + + } + + private void sendSizeToServer() { + if (comm.isClientInitialized()) { + final Transcation tx = comm.startTx(); + tx.send("height", getHeight()); + tx.send("width", getWidth()); + tx.commit(); + } + + } + + public void terminalInput(final TextConsole term, final String input) { + if (comm.isClientInitialized()) { + comm.call("input", input); + } + } + + @Override + protected void calculateColsFromWidth() { + final int oldCols = getCols(); + super.calculateColsFromWidth(); + + if (comm.isClientInitialized() && getCols() != oldCols) { + final Transcation tx = comm.startTx(); + tx.send("cols", getCols()); + tx.commit(); + } + } + + @Override + protected void calculateRowsFromHeight() { + final int oldRows = getRows(); + super.calculateRowsFromHeight(); + + if (comm.isClientInitialized() && getRows() != oldRows) { + final Transcation tx = comm.startTx(); + tx.send("rows", getRows()); + tx.commit(); + + } + + } + + @Override + protected void calculateHeightFromRows() { + super.calculateHeightFromRows(); + notifyPaintableSizeChange(); + sendSizeToServer(); + } + + @Override + protected void calculateWidthFromCols() { + super.calculateWidthFromCols(); + notifyPaintableSizeChange(); + sendSizeToServer(); + } + + private void notifyPaintableSizeChange() { + Util.notifyParentOfSizeChange(this, false); + } + + @Override + protected void suggest() { + if (comm.isClientInitialized()) { + comm.call("suggest", getInput()); + } + } + + @Override + public void setConfig(final TextConsoleConfig cfg) { + // Wrap everything to a single tx + final Transcation tx = comm.startTx(); + super.setConfig(cfg); + tx.commit(); + } + + /** + * Initialize the Console synchronously. + * + */ + public boolean initWidget(Object[] params) { + + // Console configuration + final TextConsoleConfig cfg = getConfig(); + int i = 0; + cfg.setCols((Integer) params[i++]); + cfg.setRows((Integer) params[i++]); + cfg.setMaxBufferSize((Integer) params[i++]); + cfg.setWrap((Boolean) params[i++]); + cfg.setGreeting((String) params[i++]); + cfg.setPs((String) params[i++]); + comm.d("init: '" + cfg.getGreeting() + "';" + cfg.getCols() + "x" + + cfg.getRows() + ""); + setConfig(cfg); + + reset(); + return false; // Not an async initialization + } + + public void handleCallFromServer(String method, Object[] params) { + comm.d("Unknown method called: " + method); + } + +}