src/main/java/org/vaadin/console/client/ui/VTextConsole.java
author indvdum
Tue, 17 May 2011 18:50:45 +0400
changeset 4 27bd1032a5c6
parent 3 3eab4f9f42a9
child 6 8a7e51bd3ddb
permissions -rw-r--r--
+ new client option isPrintPromptOnInput
     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("append", new Method() {
    84         	public void invoke(final String methodName, final Object[] data) {
    85         		append((String) data[0]);
    86         	}
    87         });
    88         comm.register("prompt", new Method() {
    89             public void invoke(final String methodName, final Object[] data) {
    90                 prompt((String) (data.length > 0 ? data[0] : null));
    91             }
    92         });
    93         comm.register("ff", new Method() {
    94             public void invoke(final String methodName, final Object[] data) {
    95                 formFeed();
    96             }
    97         });
    98         comm.register("cr", new Method() {
    99             public void invoke(final String methodName, final Object[] data) {
   100                 carriageReturn();
   101             }
   102         });
   103         comm.register("lf", new Method() {
   104             public void invoke(final String methodName, final Object[] data) {
   105                 carriageReturn();
   106             }
   107         });
   108         comm.register("clearBuffer", new Method() {
   109             public void invoke(final String methodName, final Object[] data) {
   110                 clearBuffer();
   111             }
   112         });
   113         comm.register("reset", new Method() {
   114             public void invoke(final String methodName, final Object[] data) {
   115                 reset();
   116             }
   117         });
   118         comm.register("newLine", new Method() {
   119             public void invoke(final String methodName, final Object[] data) {
   120                 newLine();
   121             }
   122         });
   123         comm.register("scrollToEnd", new Method() {
   124             public void invoke(final String methodName, final Object[] data) {
   125                 scrollToEnd();
   126             }
   127         });
   128         comm.register("focusInput", new Method() {
   129         	public void invoke(final String methodName, final Object[] data) {
   130         		focusInput();
   131         	}
   132         });
   133         comm.register("bell", new Method() {
   134             public void invoke(final String methodName, final Object[] data) {
   135                 bell();
   136             }
   137         });
   138         comm.register("setMaxBufferSize", new Method() {
   139             public void invoke(final String methodName, final Object[] data) {
   140                 getConfig().setMaxBufferSize((Integer) data[0]);
   141                 setMaxBufferSize((Integer) data[0]);
   142             }
   143         });
   144         comm.register("clearHistory", new Method() {
   145             public void invoke(final String methodName, final Object[] data) {
   146                 clearCommandHistory();
   147             }
   148         });
   149         comm.register("setPrintPromptOnInput", new Method() {
   150             public void invoke(final String methodName, final Object[] data) {
   151                 getConfig().setPrintPromptOnInput((Boolean) data[0]);
   152             }
   153         });
   154     }
   155 
   156     /**
   157      * Called whenever an update is received from the server
   158      */
   159     public void updateFromUIDL(final UIDL serverData,
   160             final ApplicationConnection appConn) {
   161         comm.update(this,serverData, appConn);
   162 
   163     }
   164 
   165     private void sendSizeToServer() {
   166         if (comm.isClientInitialized()) {
   167             final Transcation tx = comm.startTx();
   168             tx.send("height", getHeight());
   169             tx.send("width", getWidth());
   170             tx.commit();
   171         }
   172 
   173     }
   174 
   175     public void terminalInput(final TextConsole term, final String input) {
   176         if (comm.isClientInitialized()) {
   177             comm.call("input", input);
   178         }
   179     }
   180 
   181     @Override
   182     protected void calculateColsFromWidth() {
   183         final int oldCols = getCols();
   184         super.calculateColsFromWidth();
   185 
   186         if (comm.isClientInitialized() && getCols() != oldCols) {
   187             final Transcation tx = comm.startTx();
   188             tx.send("cols", getCols());
   189             tx.commit();
   190         }
   191     }
   192 
   193     @Override
   194     protected void calculateRowsFromHeight() {
   195         final int oldRows = getRows();
   196         super.calculateRowsFromHeight();
   197 
   198         if (comm.isClientInitialized() && getRows() != oldRows) {
   199             final Transcation tx = comm.startTx();
   200             tx.send("rows", getRows());
   201             tx.commit();
   202 
   203         }
   204 
   205     }
   206 
   207     @Override
   208     protected void calculateHeightFromRows() {
   209         super.calculateHeightFromRows();
   210         notifyPaintableSizeChange();
   211         sendSizeToServer();
   212     }
   213 
   214     @Override
   215     protected void calculateWidthFromCols() {
   216         super.calculateWidthFromCols();
   217         notifyPaintableSizeChange();
   218         sendSizeToServer();
   219     }
   220 
   221     private void notifyPaintableSizeChange() {
   222         Util.notifyParentOfSizeChange(this, false);
   223     }
   224 
   225     @Override
   226     protected void suggest() {
   227         if (comm.isClientInitialized()) {
   228             comm.call("suggest", getInput());
   229         }
   230     }
   231 
   232     @Override
   233     public void setConfig(final TextConsoleConfig cfg) {
   234         // Wrap everything to a single tx
   235         final Transcation tx = comm.startTx();
   236         super.setConfig(cfg);
   237         tx.commit();
   238     }
   239 
   240     /**
   241      * Initialize the Console synchronously.
   242      *
   243      */
   244     public boolean initWidget(Object[] params) {
   245 
   246         // Console configuration
   247         final TextConsoleConfig cfg = getConfig();
   248         int i = 0;
   249         cfg.setCols((Integer) params[i++]);
   250         cfg.setRows((Integer) params[i++]);
   251         cfg.setMaxBufferSize((Integer) params[i++]);
   252         cfg.setWrap((Boolean) params[i++]);
   253         cfg.setPrintPromptOnInput((Boolean) params[i++]);
   254         cfg.setGreeting((String) params[i++]);
   255         cfg.setPs((String) params[i++]);
   256         comm.d("init: '" + cfg.getGreeting() + "';" + cfg.getCols() + "x"
   257                 + cfg.getRows() + "");
   258         setConfig(cfg);
   259 
   260         reset();
   261         return false; // Not an async initialization
   262     }
   263 
   264     public void handleCallFromServer(String method, Object[] params) {
   265         comm.d("Unknown method called: " + method);
   266     }
   267 
   268 }