Java tutorial
package com.google.gwt.sample.client; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.FlexTable; import com.google.gwt.user.client.ui.HorizontalPanel; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.dom.client.KeyPressHandler; import com.google.gwt.event.dom.client.KeyPressEvent; import com.google.gwt.user.client.Window; import java.util.ArrayList; import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.Random; import com.google.gwt.i18n.client.NumberFormat; import com.google.gwt.i18n.client.DateTimeFormat; import java.util.Date; public class stockwatcher implements EntryPoint { private static final int INTERVAL = 3000; private VerticalPanel mainPanel = new VerticalPanel(); private HorizontalPanel addPanel = new HorizontalPanel(); private FlexTable stocksTable = new FlexTable(); private TextBox textBox = new TextBox(); private Button addButton = new Button("Add"); private Label lastUpdatedLabel = new Label(); private ArrayList<String> stocks = new ArrayList<String>(); private Timer timer; public void onModuleLoad() { // Create table for stock data stocksTable.setText(0, 0, "Symbol"); stocksTable.setText(0, 1, "Price"); stocksTable.setText(0, 2, "Change"); stocksTable.setText(0, 3, "Remove"); // Add styles to elements in the stock list table. stocksTable.setCellPadding(6); stocksTable.getRowFormatter().addStyleName(0, "watchListHeader"); stocksTable.addStyleName("watchList"); stocksTable.getCellFormatter().addStyleName(0, 1, "watchListNumericColumn"); stocksTable.getCellFormatter().addStyleName(0, 2, "watchListNumericColumn"); stocksTable.getCellFormatter().addStyleName(0, 3, "watchListRemoveColumn"); // Assemble Add Stock panel addPanel.add(textBox); addPanel.add(addButton); addPanel.addStyleName("addPanel"); // Assemble Main panel mainPanel.add(stocksTable); mainPanel.add(addPanel); mainPanel.add(lastUpdatedLabel); // Associate the Main panel with the HTML host page. RootPanel.get("div_GWTRoot").add(mainPanel); // Move cursor focus to the input box. addButton.setFocus(true); // Setup timer to refresh list automatically timer = new Timer() { @Override public void run() { refreshWatchList(); } }; timer.scheduleRepeating(INTERVAL); // Listen for mouse events on the Add button. addButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { addStock(); } }); // Listen for keyboard events in the input box. textBox.addKeyPressHandler(new KeyPressHandler() { public void onKeyPress(KeyPressEvent event) { if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) { addStock(); } } }); } //Add new stock to the Stocks Watcher private void addStock() { // Add new stock final String stock = textBox.getText().toUpperCase().trim(); //The name of new stock int row; //The row of new stock Button deleteButton; textBox.setFocus(true); if (stock.matches("^[0-9A-Z\\.]{1,10}$") == false) { Window.alert("Warning: '" + stock + "' is not a valid stock name."); textBox.setText(""); return; } textBox.setText(""); // Don't add the stock if it's already in the table. if (stocks.contains(stock) == true) { Window.alert("Warning: Stocks Watcher is already have '" + stock + "' stock."); textBox.setText(""); return; } // Add the stock to the table. row = stocksTable.getRowCount(); stocks.add(stock); stocksTable.setText(row, 0, stock); stocksTable.setWidget(row, 2, new Label()); stocksTable.getCellFormatter().addStyleName(row, 1, "watchListNumericColumn"); stocksTable.getCellFormatter().addStyleName(row, 2, "watchListNumericColumn"); stocksTable.getCellFormatter().addStyleName(row, 3, "watchListRemoveColumn"); // Add a button to remove this stock from the table. deleteButton = new Button("x"); deleteButton.addStyleDependentName("remove"); deleteButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { int deletedIndex = stocks.indexOf(stock); stocks.remove(deletedIndex); stocksTable.removeRow(deletedIndex + 1); } }); stocksTable.setWidget(row, 3, deleteButton); // Get the stock price. refreshWatchList(); } private void refreshWatchList() { // Update the price of stocks final double MAX_PRICE = 100.0; final double MAX_PRICE_CHANGE = 0.02; // Generate random stock prices StockPrice[] prices = new StockPrice[stocks.size()]; for (int i = 0; i < stocks.size(); i++) { double price = Random.nextDouble() * MAX_PRICE; double diff = price * MAX_PRICE_CHANGE * (Random.nextDouble() * 2.0 - 1.0); prices[i] = new StockPrice(stocks.get(i), price, diff); } updateTable(prices); } private void updateTable(StockPrice[] prices) { // TODO Just update table information for (int i = 0; i < prices.length; ++i) { updateTable(prices[i]); } lastUpdatedLabel.setText("Last update: " + DateTimeFormat.getMediumDateTimeFormat().format(new Date())); } private void updateTable(StockPrice price) { int row; String priceText, diffText, diffPercentText; NumberFormat diffFormat; Label changeWidget; String changeStyleName = "noChange"; if (stocks.contains(price.getName()) == false) { return; } //Format text row = stocks.indexOf(price.getName()) + 1; priceText = NumberFormat.getFormat("#,##0.00").format(price.getPrice()); diffFormat = NumberFormat.getFormat("+#,##0.00;-#,##0.00"); diffText = diffFormat.format(price.getDiff()); diffPercentText = diffFormat.format(price.getDiffPercent()); //Populate price stocksTable.setText(row, 1, priceText); changeWidget = (Label) stocksTable.getWidget(row, 2); changeWidget.setText(diffText + " (" + diffPercentText + "%)"); // Change the color of text in the Change field based on its value. if (price.getDiffPercent() < -0.1f) { changeStyleName = "negativeChange"; } else if (price.getDiffPercent() > 0.1f) { changeStyleName = "positiveChange"; } changeWidget.setStyleName(changeStyleName); } }