src/main/java/org/vaadin/console/client/ui/VTextConsole.java
changeset 0 c46bb04134cb
child 1 9b9d2f4b4ce0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main/java/org/vaadin/console/client/ui/VTextConsole.java	Sun May 15 17:46:06 2011 +0400
     1.3 @@ -0,0 +1,252 @@
     1.4 +package org.vaadin.console.client.ui;
     1.5 +
     1.6 +import org.vaadin.rpc.client.ClientSideHandler;
     1.7 +import org.vaadin.rpc.client.ClientSideProxy;
     1.8 +import org.vaadin.rpc.client.ClientSideProxy.Transcation;
     1.9 +import org.vaadin.rpc.client.Method;
    1.10 +
    1.11 +import com.vaadin.terminal.gwt.client.ApplicationConnection;
    1.12 +import com.vaadin.terminal.gwt.client.Paintable;
    1.13 +import com.vaadin.terminal.gwt.client.UIDL;
    1.14 +import com.vaadin.terminal.gwt.client.Util;
    1.15 +
    1.16 +/**
    1.17 + * Vaadin client-side integration to Console GWT Widget.
    1.18 + *
    1.19 + * @author Sami Ekblad / Vaadin
    1.20 + *
    1.21 + */
    1.22 +public class VTextConsole extends TextConsole implements Paintable,
    1.23 +        TextConsoleHandler, ClientSideHandler {
    1.24 +
    1.25 +    private static final String CSS_CLASS_NAME = "v-console";
    1.26 +
    1.27 +    private final ClientSideProxy comm = new ClientSideProxy("VTextConsole",
    1.28 +            this);
    1.29 +
    1.30 +    /**
    1.31 +     * The constructor should first call super() to initialize the component and
    1.32 +     * then handle any initialization relevant to Vaadin.
    1.33 +     */
    1.34 +    public VTextConsole() {
    1.35 +        super();
    1.36 +        getElement().addClassName(CSS_CLASS_NAME);
    1.37 +        setHandler(this);
    1.38 +
    1.39 +        // Register all server-driven functions
    1.40 +        registerServerCallbacks();
    1.41 +    }
    1.42 +
    1.43 +    private void registerServerCallbacks() {
    1.44 +        comm.register("setGreeting", new Method() {
    1.45 +            public void invoke(final String methodName, final Object[] data) {
    1.46 +                getConfig().setGreeting((String) data[0]);
    1.47 +            }
    1.48 +        });
    1.49 +        comm.register("setPs", new Method() {
    1.50 +            public void invoke(final String methodName, final Object[] data) {
    1.51 +                setPs((String) data[0]);
    1.52 +            }
    1.53 +        });
    1.54 +        comm.register("setWrap", new Method() {
    1.55 +            public void invoke(final String methodName, final Object[] data) {
    1.56 +                getConfig().setWrap((Boolean) data[0]);
    1.57 +            }
    1.58 +        });
    1.59 +        comm.register("setRows", new Method() {
    1.60 +            public void invoke(final String methodName, final Object[] data) {
    1.61 +                getConfig().setRows((Integer) data[0]);
    1.62 +                setRows((Integer) data[0]);
    1.63 +            }
    1.64 +        });
    1.65 +        comm.register("setCols", new Method() {
    1.66 +            public void invoke(final String methodName, final Object[] data) {
    1.67 +                getConfig().setCols((Integer) data[0]);
    1.68 +                setCols((Integer) data[0]);
    1.69 +            }
    1.70 +        });
    1.71 +        comm.register("print", new Method() {
    1.72 +            public void invoke(final String methodName, final Object[] data) {
    1.73 +                print((String) data[0]);
    1.74 +            }
    1.75 +        });
    1.76 +        comm.register("println", new Method() {
    1.77 +            public void invoke(final String methodName, final Object[] data) {
    1.78 +                println((String) data[0]);
    1.79 +            }
    1.80 +        });
    1.81 +        comm.register("println", new Method() {
    1.82 +            public void invoke(final String methodName, final Object[] data) {
    1.83 +                println((String) data[0]);
    1.84 +            }
    1.85 +        });
    1.86 +        comm.register("prompt", new Method() {
    1.87 +            public void invoke(final String methodName, final Object[] data) {
    1.88 +                prompt((String) (data.length > 0 ? data[0] : null));
    1.89 +            }
    1.90 +        });
    1.91 +        comm.register("ff", new Method() {
    1.92 +            public void invoke(final String methodName, final Object[] data) {
    1.93 +                formFeed();
    1.94 +            }
    1.95 +        });
    1.96 +        comm.register("cr", new Method() {
    1.97 +            public void invoke(final String methodName, final Object[] data) {
    1.98 +                carriageReturn();
    1.99 +            }
   1.100 +        });
   1.101 +        comm.register("lf", new Method() {
   1.102 +            public void invoke(final String methodName, final Object[] data) {
   1.103 +                carriageReturn();
   1.104 +            }
   1.105 +        });
   1.106 +        comm.register("clearBuffer", new Method() {
   1.107 +            public void invoke(final String methodName, final Object[] data) {
   1.108 +                clearBuffer();
   1.109 +            }
   1.110 +        });
   1.111 +        comm.register("reset", new Method() {
   1.112 +            public void invoke(final String methodName, final Object[] data) {
   1.113 +                reset();
   1.114 +            }
   1.115 +        });
   1.116 +        comm.register("newLine", new Method() {
   1.117 +            public void invoke(final String methodName, final Object[] data) {
   1.118 +                newLine();
   1.119 +            }
   1.120 +        });
   1.121 +        comm.register("scrollToEnd", new Method() {
   1.122 +            public void invoke(final String methodName, final Object[] data) {
   1.123 +                scrollToEnd();
   1.124 +            }
   1.125 +        });
   1.126 +        comm.register("bell", new Method() {
   1.127 +            public void invoke(final String methodName, final Object[] data) {
   1.128 +                bell();
   1.129 +            }
   1.130 +        });
   1.131 +        comm.register("setMaxBufferSize", new Method() {
   1.132 +            public void invoke(final String methodName, final Object[] data) {
   1.133 +                getConfig().setMaxBufferSize((Integer) data[0]);
   1.134 +                setMaxBufferSize((Integer) data[0]);
   1.135 +            }
   1.136 +        });
   1.137 +        comm.register("clearHistory", new Method() {
   1.138 +            public void invoke(final String methodName, final Object[] data) {
   1.139 +                clearCommandHistory();
   1.140 +            }
   1.141 +        });
   1.142 +    }
   1.143 +
   1.144 +    /**
   1.145 +     * Called whenever an update is received from the server
   1.146 +     */
   1.147 +    public void updateFromUIDL(final UIDL serverData,
   1.148 +            final ApplicationConnection appConn) {
   1.149 +        comm.update(this,serverData, appConn);
   1.150 +
   1.151 +    }
   1.152 +
   1.153 +    private void sendSizeToServer() {
   1.154 +        if (comm.isClientInitialized()) {
   1.155 +            final Transcation tx = comm.startTx();
   1.156 +            tx.send("height", getHeight());
   1.157 +            tx.send("width", getWidth());
   1.158 +            tx.commit();
   1.159 +        }
   1.160 +
   1.161 +    }
   1.162 +
   1.163 +    public void terminalInput(final TextConsole term, final String input) {
   1.164 +        if (comm.isClientInitialized()) {
   1.165 +            comm.call("input", input);
   1.166 +        }
   1.167 +    }
   1.168 +
   1.169 +    @Override
   1.170 +    protected void calculateColsFromWidth() {
   1.171 +        final int oldCols = getCols();
   1.172 +        super.calculateColsFromWidth();
   1.173 +
   1.174 +        if (comm.isClientInitialized() && getCols() != oldCols) {
   1.175 +            final Transcation tx = comm.startTx();
   1.176 +            tx.send("cols", getCols());
   1.177 +            tx.commit();
   1.178 +        }
   1.179 +    }
   1.180 +
   1.181 +    @Override
   1.182 +    protected void calculateRowsFromHeight() {
   1.183 +        final int oldRows = getRows();
   1.184 +        super.calculateRowsFromHeight();
   1.185 +
   1.186 +        if (comm.isClientInitialized() && getRows() != oldRows) {
   1.187 +            final Transcation tx = comm.startTx();
   1.188 +            tx.send("rows", getRows());
   1.189 +            tx.commit();
   1.190 +
   1.191 +        }
   1.192 +
   1.193 +    }
   1.194 +
   1.195 +    @Override
   1.196 +    protected void calculateHeightFromRows() {
   1.197 +        super.calculateHeightFromRows();
   1.198 +        notifyPaintableSizeChange();
   1.199 +        sendSizeToServer();
   1.200 +    }
   1.201 +
   1.202 +    @Override
   1.203 +    protected void calculateWidthFromCols() {
   1.204 +        super.calculateWidthFromCols();
   1.205 +        notifyPaintableSizeChange();
   1.206 +        sendSizeToServer();
   1.207 +    }
   1.208 +
   1.209 +    private void notifyPaintableSizeChange() {
   1.210 +        Util.notifyParentOfSizeChange(this, false);
   1.211 +    }
   1.212 +
   1.213 +    @Override
   1.214 +    protected void suggest() {
   1.215 +        if (comm.isClientInitialized()) {
   1.216 +            comm.call("suggest", getInput());
   1.217 +        }
   1.218 +    }
   1.219 +
   1.220 +    @Override
   1.221 +    public void setConfig(final TextConsoleConfig cfg) {
   1.222 +        // Wrap everything to a single tx
   1.223 +        final Transcation tx = comm.startTx();
   1.224 +        super.setConfig(cfg);
   1.225 +        tx.commit();
   1.226 +    }
   1.227 +
   1.228 +    /**
   1.229 +     * Initialize the Console synchronously.
   1.230 +     *
   1.231 +     */
   1.232 +    public boolean initWidget(Object[] params) {
   1.233 +
   1.234 +        // Console configuration
   1.235 +        final TextConsoleConfig cfg = getConfig();
   1.236 +        int i = 0;
   1.237 +        cfg.setCols((Integer) params[i++]);
   1.238 +        cfg.setRows((Integer) params[i++]);
   1.239 +        cfg.setMaxBufferSize((Integer) params[i++]);
   1.240 +        cfg.setWrap((Boolean) params[i++]);
   1.241 +        cfg.setGreeting((String) params[i++]);
   1.242 +        cfg.setPs((String) params[i++]);
   1.243 +        comm.d("init: '" + cfg.getGreeting() + "';" + cfg.getCols() + "x"
   1.244 +                + cfg.getRows() + "");
   1.245 +        setConfig(cfg);
   1.246 +
   1.247 +        reset();
   1.248 +        return false; // Not an async initialization
   1.249 +    }
   1.250 +
   1.251 +    public void handleCallFromServer(String method, Object[] params) {
   1.252 +        comm.d("Unknown method called: " + method);
   1.253 +    }
   1.254 +
   1.255 +}