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