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