public void run(final RootPanel rp, final String nick) { if (Cookies.getCookie(nick) == null) Cookies.setCookie(nick, "" + 0); cl.setPageSize(500); final Button sendMessage = new Button( "sendMessage", new ClickHandler() { public void onClick(ClickEvent event) { if (!message.getText().equals("")) { new Post().postJson(SERVERURL, nick.toString(), message.getText()); message.setText(""); } } }); rp.get("mainDiv2").setVisible(true); message.getElement().setAttribute("placeholder", "Introduce your message"); message.getElement().setAttribute("id", "message"); cl.getElement().setAttribute("id", "chatBox"); sendMessage.getElement().setAttribute("id", "sendMessage"); sendMessage.setText("Send"); vp.getElement().setAttribute("id", "verticalPanel"); hp.getElement().setAttribute("id", "horizontalPanel"); panel.getElement().setAttribute("id", "scroller"); hp.add(message); hp.add(sendMessage); panel.add(cl); vp.add(panel); vp.add(hp); rp.get("mainDiv2").add(vp); Timer t = new Timer() { @Override public void run() { getMessages(); if (chatList != null && Integer.parseInt(Cookies.getCookie(nick)) < chatList.size()) { cl.setRowCount(chatList.size() + 1, true); cl.setRowData( Integer.parseInt(Cookies.getCookie(nick)), chatList.subList(Integer.parseInt(Cookies.getCookie(nick)), chatList.size())); panel.setVerticalScrollPosition(panel.getMaximumVerticalScrollPosition() - 1); Cookies.setCookie(nick, "" + chatList.size()); } } }; t.scheduleRepeating(1000); }
public void onModuleLoad() { final HorizontalPanel hPanel = new HorizontalPanel(); final Label messagesReceived = new Label("Messaged Received: "); final Label messagesReceivedVal = new Label(); class Counter { int count = 0; public void increment() { messagesReceivedVal.setText(String.valueOf(++count)); } } final Counter counter = new Counter(); for (int i = 0; i < 7; i++) { final VerticalPanel panel = new VerticalPanel(); final Button startStopButton = new Button("Start" + i); final TextBox resultBox = new TextBox(); resultBox.setEnabled(false); final String receiverName = "RandomNumberReceiver" + i; final Style resultStyle = resultBox.getElement().getStyle(); /** Create a callback receiver to receive the data from the server. */ final MessageCallback receiver = new MessageCallback() { public void callback(Message message) { counter.increment(); Double value = message.get(Double.class, "Data"); resultBox.setText(String.valueOf(value)); if (value > 0.5d) { resultStyle.setProperty("backgroundColor", "green"); } else { resultStyle.setProperty("backgroundColor", "red"); } } }; /** Subscribe to the receiver using the recevierName. */ ErraiBus.get().subscribe(receiverName, receiver); final int num = i; startStopButton.addClickHandler( new ClickHandler() { public void onClick(ClickEvent clickEvent) { /** Send a message to Start/Stop the task on the server. */ MessageBuilder.createMessage() .toSubject("AsyncService") .command(startStopButton.getText()) .with(MessageParts.ReplyTo, receiverName) .noErrorHandling() .sendNowWith(ErraiBus.get()); /** * Flip-flop the value of the button every time it's pushed between 'Start' and 'Stop' */ startStopButton.setText( ("Start" + num).equals(startStopButton.getText()) ? "Stop" + num : "Start" + num); } }); panel.add(startStopButton); panel.add(resultBox); hPanel.add(panel); } final VerticalPanel outerPanel = new VerticalPanel(); outerPanel.add(hPanel); final HorizontalPanel messageCounter = new HorizontalPanel(); messageCounter.add(messagesReceived); messageCounter.add(messagesReceivedVal); outerPanel.add(messageCounter); RootPanel.get().add(outerPanel); }