public StockPresenter(final StockView view) { this.view = view; stocks = new ListVariable<StockSymbol>(); // set up event listeners for adding a new stock StreamMerge.<Object>mergeStreams( view.addButtonClick(), view.newSymbolTextEntry().keyDown().filter(new IsEnterKeyCode())) .loop( new Effect<Object>() { @Override public void e(Object o) { addStock(); } }); // set up timer to refresh list automatically final Timer refreshTimer = new Timer() { public void run() { refreshWatchList(); } }; refreshTimer.scheduleRepeating(REFRESH_INTERVAL); // move cursor focus to the text box view.newSymbolTextEntry().focus().put(true); }
private void addStock() { final String symbol = view.newSymbolTextEntry().value().get().toUpperCase().trim(); view.newSymbolTextEntry().focus().put(true); // symbol must be between 1 and 10 chars that are numbers, letters, or dots if (!symbol.matches("^[0-9a-zA-Z\\.]{1,10}$")) { Window.alert("'" + symbol + "' is not a valid symbol."); view.newSymbolTextEntry().selectAll(); return; } view.newSymbolTextEntry().value().put(""); final StockSymbol stockSymbol = new StockSymbol(symbol); // don't add the stock if it's already in the watch list if (stocks.get().exists(stockSymbol.equalsFunction())) { return; } // add the stock to the list stocks.snoc(stockSymbol); int row = stocksFlexTable.getRowCount(); stocksFlexTable.setText(row, 0, symbol); Button removeStock = new Button("x"); removeStock.addClickListener( new ClickListener() { public void onClick(Widget sender) { int removedIndex = stocks.elementIndex(StockSymbol.EQUAL, stockSymbol).some(); stocks.delete(stockSymbol, StockSymbol.EQUAL); stocksFlexTable.removeRow(removedIndex + 1); } }); stocksFlexTable.setWidget(row, 3, removeStock); // get stock price refreshWatchList(); }