src/main/java/org/vaadin/console/client/ui/VTextConsole.java
author indvdum
Tue, 17 May 2011 03:27:01 +0400
changeset 3 3eab4f9f42a9
parent 1 9b9d2f4b4ce0
child 4 27bd1032a5c6
permissions -rw-r--r--
+ client method append(String)
     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     }
   150 
   151     /**
   152      * Called whenever an update is received from the server
   153      */
   154     public void updateFromUIDL(final UIDL serverData,
   155             final ApplicationConnection appConn) {
   156         comm.update(this,serverData, appConn);
   157 
   158     }
   159 
   160     private void sendSizeToServer() {
   161         if (comm.isClientInitialized()) {
   162             final Transcation tx = comm.startTx();
   163             tx.send("height", getHeight());
   164             tx.send("width", getWidth());
   165             tx.commit();
   166         }
   167 
   168     }
   169 
   170     public void terminalInput(final TextConsole term, final String input) {
   171         if (comm.isClientInitialized()) {
   172             comm.call("input", input);
   173         }
   174     }
   175 
   176     @Override
   177     protected void calculateColsFromWidth() {
   178         final int oldCols = getCols();
   179         super.calculateColsFromWidth();
   180 
   181         if (comm.isClientInitialized() && getCols() != oldCols) {
   182             final Transcation tx = comm.startTx();
   183             tx.send("cols", getCols());
   184             tx.commit();
   185         }
   186     }
   187 
   188     @Override
   189     protected void calculateRowsFromHeight() {
   190         final int oldRows = getRows();
   191         super.calculateRowsFromHeight();
   192 
   193         if (comm.isClientInitialized() && getRows() != oldRows) {
   194             final Transcation tx = comm.startTx();
   195             tx.send("rows", getRows());
   196             tx.commit();
   197 
   198         }
   199 
   200     }
   201 
   202     @Override
   203     protected void calculateHeightFromRows() {
   204         super.calculateHeightFromRows();
   205         notifyPaintableSizeChange();
   206         sendSizeToServer();
   207     }
   208 
   209     @Override
   210     protected void calculateWidthFromCols() {
   211         super.calculateWidthFromCols();
   212         notifyPaintableSizeChange();
   213         sendSizeToServer();
   214     }
   215 
   216     private void notifyPaintableSizeChange() {
   217         Util.notifyParentOfSizeChange(this, false);
   218     }
   219 
   220     @Override
   221     protected void suggest() {
   222         if (comm.isClientInitialized()) {
   223             comm.call("suggest", getInput());
   224         }
   225     }
   226 
   227     @Override
   228     public void setConfig(final TextConsoleConfig cfg) {
   229         // Wrap everything to a single tx
   230         final Transcation tx = comm.startTx();
   231         super.setConfig(cfg);
   232         tx.commit();
   233     }
   234 
   235     /**
   236      * Initialize the Console synchronously.
   237      *
   238      */
   239     public boolean initWidget(Object[] params) {
   240 
   241         // Console configuration
   242         final TextConsoleConfig cfg = getConfig();
   243         int i = 0;
   244         cfg.setCols((Integer) params[i++]);
   245         cfg.setRows((Integer) params[i++]);
   246         cfg.setMaxBufferSize((Integer) params[i++]);
   247         cfg.setWrap((Boolean) params[i++]);
   248         cfg.setGreeting((String) params[i++]);
   249         cfg.setPs((String) params[i++]);
   250         comm.d("init: '" + cfg.getGreeting() + "';" + cfg.getCols() + "x"
   251                 + cfg.getRows() + "");
   252         setConfig(cfg);
   253 
   254         reset();
   255         return false; // Not an async initialization
   256     }
   257 
   258     public void handleCallFromServer(String method, Object[] params) {
   259         comm.d("Unknown method called: " + method);
   260     }
   261 
   262 }