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