src/main/java/org/vaadin/console/client/ui/VTextConsole.java
author indvdum
Mon, 16 May 2011 14:00:10 +0400
changeset 1 9b9d2f4b4ce0
parent 0 c46bb04134cb
child 3 3eab4f9f42a9
permissions -rw-r--r--
+ vaadin console add-on stable version 1.1.7: new client method focusInput()
     1 package org.vaadin.console.client.ui;
     2 
     3 import org.vaadin.rpc.client.ClientSideHandler;
     4 import org.vaadin.rpc.client.ClientSideProxy;
     5 import org.vaadin.rpc.client.ClientSideProxy.Transcation;
     6 import org.vaadin.rpc.client.Method;
     7 
     8 import com.vaadin.terminal.gwt.client.ApplicationConnection;
     9 import com.vaadin.terminal.gwt.client.Paintable;
    10 import com.vaadin.terminal.gwt.client.UIDL;
    11 import com.vaadin.terminal.gwt.client.Util;
    12 
    13 /**
    14  * Vaadin client-side integration to Console GWT Widget.
    15  *
    16  * @author Sami Ekblad / Vaadin
    17  *
    18  */
    19 public class VTextConsole extends TextConsole implements Paintable,
    20         TextConsoleHandler, ClientSideHandler {
    21 
    22     private static final String CSS_CLASS_NAME = "v-console";
    23 
    24     private final ClientSideProxy comm = new ClientSideProxy("VTextConsole",
    25             this);
    26 
    27     /**
    28      * The constructor should first call super() to initialize the component and
    29      * then handle any initialization relevant to Vaadin.
    30      */
    31     public VTextConsole() {
    32         super();
    33         getElement().addClassName(CSS_CLASS_NAME);
    34         setHandler(this);
    35 
    36         // Register all server-driven functions
    37         registerServerCallbacks();
    38     }
    39 
    40     private void registerServerCallbacks() {
    41         comm.register("setGreeting", new Method() {
    42             public void invoke(final String methodName, final Object[] data) {
    43                 getConfig().setGreeting((String) data[0]);
    44             }
    45         });
    46         comm.register("setPs", new Method() {
    47             public void invoke(final String methodName, final Object[] data) {
    48                 setPs((String) data[0]);
    49             }
    50         });
    51         comm.register("setWrap", new Method() {
    52             public void invoke(final String methodName, final Object[] data) {
    53                 getConfig().setWrap((Boolean) data[0]);
    54             }
    55         });
    56         comm.register("setRows", new Method() {
    57             public void invoke(final String methodName, final Object[] data) {
    58                 getConfig().setRows((Integer) data[0]);
    59                 setRows((Integer) data[0]);
    60             }
    61         });
    62         comm.register("setCols", new Method() {
    63             public void invoke(final String methodName, final Object[] data) {
    64                 getConfig().setCols((Integer) data[0]);
    65                 setCols((Integer) data[0]);
    66             }
    67         });
    68         comm.register("print", new Method() {
    69             public void invoke(final String methodName, final Object[] data) {
    70                 print((String) data[0]);
    71             }
    72         });
    73         comm.register("println", new Method() {
    74             public void invoke(final String methodName, final Object[] data) {
    75                 println((String) data[0]);
    76             }
    77         });
    78         comm.register("println", new Method() {
    79             public void invoke(final String methodName, final Object[] data) {
    80                 println((String) data[0]);
    81             }
    82         });
    83         comm.register("prompt", new Method() {
    84             public void invoke(final String methodName, final Object[] data) {
    85                 prompt((String) (data.length > 0 ? data[0] : null));
    86             }
    87         });
    88         comm.register("ff", new Method() {
    89             public void invoke(final String methodName, final Object[] data) {
    90                 formFeed();
    91             }
    92         });
    93         comm.register("cr", new Method() {
    94             public void invoke(final String methodName, final Object[] data) {
    95                 carriageReturn();
    96             }
    97         });
    98         comm.register("lf", new Method() {
    99             public void invoke(final String methodName, final Object[] data) {
   100                 carriageReturn();
   101             }
   102         });
   103         comm.register("clearBuffer", new Method() {
   104             public void invoke(final String methodName, final Object[] data) {
   105                 clearBuffer();
   106             }
   107         });
   108         comm.register("reset", new Method() {
   109             public void invoke(final String methodName, final Object[] data) {
   110                 reset();
   111             }
   112         });
   113         comm.register("newLine", new Method() {
   114             public void invoke(final String methodName, final Object[] data) {
   115                 newLine();
   116             }
   117         });
   118         comm.register("scrollToEnd", new Method() {
   119             public void invoke(final String methodName, final Object[] data) {
   120                 scrollToEnd();
   121             }
   122         });
   123         comm.register("focusInput", new Method() {
   124         	public void invoke(final String methodName, final Object[] data) {
   125         		focusInput();
   126         	}
   127         });
   128         comm.register("bell", new Method() {
   129             public void invoke(final String methodName, final Object[] data) {
   130                 bell();
   131             }
   132         });
   133         comm.register("setMaxBufferSize", new Method() {
   134             public void invoke(final String methodName, final Object[] data) {
   135                 getConfig().setMaxBufferSize((Integer) data[0]);
   136                 setMaxBufferSize((Integer) data[0]);
   137             }
   138         });
   139         comm.register("clearHistory", new Method() {
   140             public void invoke(final String methodName, final Object[] data) {
   141                 clearCommandHistory();
   142             }
   143         });
   144     }
   145 
   146     /**
   147      * Called whenever an update is received from the server
   148      */
   149     public void updateFromUIDL(final UIDL serverData,
   150             final ApplicationConnection appConn) {
   151         comm.update(this,serverData, appConn);
   152 
   153     }
   154 
   155     private void sendSizeToServer() {
   156         if (comm.isClientInitialized()) {
   157             final Transcation tx = comm.startTx();
   158             tx.send("height", getHeight());
   159             tx.send("width", getWidth());
   160             tx.commit();
   161         }
   162 
   163     }
   164 
   165     public void terminalInput(final TextConsole term, final String input) {
   166         if (comm.isClientInitialized()) {
   167             comm.call("input", input);
   168         }
   169     }
   170 
   171     @Override
   172     protected void calculateColsFromWidth() {
   173         final int oldCols = getCols();
   174         super.calculateColsFromWidth();
   175 
   176         if (comm.isClientInitialized() && getCols() != oldCols) {
   177             final Transcation tx = comm.startTx();
   178             tx.send("cols", getCols());
   179             tx.commit();
   180         }
   181     }
   182 
   183     @Override
   184     protected void calculateRowsFromHeight() {
   185         final int oldRows = getRows();
   186         super.calculateRowsFromHeight();
   187 
   188         if (comm.isClientInitialized() && getRows() != oldRows) {
   189             final Transcation tx = comm.startTx();
   190             tx.send("rows", getRows());
   191             tx.commit();
   192 
   193         }
   194 
   195     }
   196 
   197     @Override
   198     protected void calculateHeightFromRows() {
   199         super.calculateHeightFromRows();
   200         notifyPaintableSizeChange();
   201         sendSizeToServer();
   202     }
   203 
   204     @Override
   205     protected void calculateWidthFromCols() {
   206         super.calculateWidthFromCols();
   207         notifyPaintableSizeChange();
   208         sendSizeToServer();
   209     }
   210 
   211     private void notifyPaintableSizeChange() {
   212         Util.notifyParentOfSizeChange(this, false);
   213     }
   214 
   215     @Override
   216     protected void suggest() {
   217         if (comm.isClientInitialized()) {
   218             comm.call("suggest", getInput());
   219         }
   220     }
   221 
   222     @Override
   223     public void setConfig(final TextConsoleConfig cfg) {
   224         // Wrap everything to a single tx
   225         final Transcation tx = comm.startTx();
   226         super.setConfig(cfg);
   227         tx.commit();
   228     }
   229 
   230     /**
   231      * Initialize the Console synchronously.
   232      *
   233      */
   234     public boolean initWidget(Object[] params) {
   235 
   236         // Console configuration
   237         final TextConsoleConfig cfg = getConfig();
   238         int i = 0;
   239         cfg.setCols((Integer) params[i++]);
   240         cfg.setRows((Integer) params[i++]);
   241         cfg.setMaxBufferSize((Integer) params[i++]);
   242         cfg.setWrap((Boolean) params[i++]);
   243         cfg.setGreeting((String) params[i++]);
   244         cfg.setPs((String) params[i++]);
   245         comm.d("init: '" + cfg.getGreeting() + "';" + cfg.getCols() + "x"
   246                 + cfg.getRows() + "");
   247         setConfig(cfg);
   248 
   249         reset();
   250         return false; // Not an async initialization
   251     }
   252 
   253     public void handleCallFromServer(String method, Object[] params) {
   254         comm.d("Unknown method called: " + method);
   255     }
   256 
   257 }