Results 1 to 2 of 2
  1. #1
    theseven is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default how to use google finance api java

    how to use google financeapi java add google stock watcher java ?

    this is my stockwatcher java cofe
    Code:


    public class StockWatcher implements EntryPoint {
    private static final int REFRESH_INTERVAL = 5000; // ms

    private VerticalPanel mainPanel = new VerticalPanel();
    private FlexTable stocksFlexTable = new FlexTable();
    private HorizontalPanel addPanel = new HorizontalPanel();
    private TextBox newSymbolTextBox = new TextBox();
    private Button addStockButton = new Button("Add");
    private Label lastUpdatedLabel = new Label();
    private ArrayList<String> stocks = new ArrayList<String>();


    /**
    * The message displayed to the user when the server cannot be reached or

    /**
    * This is the entry point method.
    */
    public void onModuleLoad() {
    stocksFlexTable.setText(0, 0, "Symbol");
    stocksFlexTable.setText(0, 1, "Price");
    stocksFlexTable.setText(0, 2, "Change");
    stocksFlexTable.setText(0, 3, "Remove");
    stocksFlexTable.setCellPadding(6);
    // Add styles to elements in the stock list table.
    stocksFlexTable.getRowFormatter().addStyleName(0, "watchListHeader");
    stocksFlexTable.addStyleName("watchList");
    stocksFlexTable.getCellFormatter().addStyleName(0, 1, "watchListNumericColumn");
    stocksFlexTable.getCellFormatter().addStyleName(0, 2, "watchListNumericColumn");
    stocksFlexTable.getCellFormatter().addStyleName(0, 3, "watchListRemoveColumn");


    addPanel.add(newSymbolTextBox);
    addPanel.add(addStockButton);
    addPanel.addStyleName("addPanel");

    // Assemble Main panel.
    mainPanel.add(stocksFlexTable);
    mainPanel.add(addPanel);
    mainPanel.add(lastUpdatedLabel);

    // Associate the Main panel with the HTML host page.
    RootPanel.get("stockList").add(mainPanel);

    // Move cursor focus to the input box.
    newSymbolTextBox.setFocus(true);
    // Setup timer to refresh list automatically.
    Timer refreshTimer = new Timer() {
    @Override
    public void run() {
    refreshWatchList();
    }
    };
    refreshTimer.scheduleRepeating(REFRESH_INTERVAL);

    addStockButton.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
    addStock();
    }
    });

    newSymbolTextBox.addKeyPressHandler(new KeyPressHandler() {
    public void onKeyPress(KeyPressEvent event) {
    if (event.getCharCode() == KeyCodes.KEY_ENTER) {
    addStock();
    }
    }
    });

    }
    private void addStock() {
    final String symbol = newSymbolTextBox.getText().toUpperCase().trim();
    newSymbolTextBox.setFocus(true);

    // Stock code must be between 1 and 10 chars that are numbers, letters, or dots.
    if (!symbol.matches("^[0-9A-Z\\.]{1,10}$")) {
    Window.alert("'" + symbol + "' is not a valid symbol.");
    newSymbolTextBox.selectAll();
    return;
    }

    newSymbolTextBox.setText("");
    // Don't add the stock if it's already in the table.
    if (stocks.contains(symbol))
    return;
    // Add the stock to the table.
    int row = stocksFlexTable.getRowCount();
    stocks.add(symbol);
    stocksFlexTable.setText(row, 0, symbol);
    stocksFlexTable.setWidget(row, 2, new Label());
    stocksFlexTable.getCellFormatter().addStyleName(ro w, 1, "watchListNumericColumn");
    stocksFlexTable.getCellFormatter().addStyleName(ro w, 2, "watchListNumericColumn");
    stocksFlexTable.getCellFormatter().addStyleName(ro w, 3, "watchListRemoveColumn");

    // Add a button to remove this stock from the table.
    Button removeStockButton = new Button("x");
    removeStockButton.addStyleDependentName("remove");
    removeStockButton.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
    int removedIndex = stocks.indexOf(symbol);
    stocks.remove(removedIndex);
    stocksFlexTable.removeRow(removedIndex + 1);
    }
    });
    stocksFlexTable.setWidget(row, 3, removeStockButton);
    // Get the stock price.
    refreshWatchList();
    }
    private void refreshWatchList() {

    final double MAX_PRICE = 100.0; // $100.00
    final double MAX_PRICE_CHANGE = 0.02; // +/- 2%

    StockPrice[] prices = new StockPrice[stocks.size()];
    for (int i = 0; i < stocks.size(); i++) {
    double price = 100;
    double change = 50;


    prices[i] = new StockPrice(stocks.get(i), price, change);
    }

    updateTable(prices);
    }


    private void updateTable(StockPrice[] prices) {
    for (int i = 0; i < prices.length; i++) {
    updateTable(prices[i]);

    }
    // Display timestamp showing last refresh.
    lastUpdatedLabel.setText("Last update : "
    + DateTimeFormat.getMediumDateTimeFormat().format(ne w Date()));

    }
    private void updateTable(StockPrice price) {
    // Make sure the stock is still in the stock table.
    if (!stocks.contains(price.getSymbol())) {
    return;
    }

    int row = stocks.indexOf(price.getSymbol()) + 1;

    // Format the data in the Price and Change fields.
    String priceText = NumberFormat.getFormat("#,##0.00").format(
    price.getPrice());
    NumberFormat changeFormat = NumberFormat.getFormat("+#,##0.00;-#,##0.00");
    String changeText = changeFormat.format(price.getChange());
    String changePercentText = changeFormat.format(price.getChangePercent());

    // Populate the Price and Change fields with new data.
    stocksFlexTable.setText(row, 1, priceText);
    Label changeWidget = (Label)stocksFlexTable.getWidget(row, 2);
    changeWidget.setText(changeText + " (" + changePercentText + "%)");
    // Change the color of text in the Change field based on its value.
    String changeStyleName = "noChange";
    if (price.getChangePercent() < -0.1f) {
    changeStyleName = "negativeChange";
    }
    else if (price.getChangePercent() > 0.1f) {
    changeStyleName = "positiveChange";
    }

    changeWidget.setStyleName(changeStyleName);
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Java application using google calendar
    By nikolas8 in forum Java Applets
    Replies: 3
    Last Post: 03-20-2009, 12:49 AM
  2. Plug google earth in your own portal
    By pradeepprathyu in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 09-24-2008, 05:10 AM
  3. google in web browser
    By jeff666 in forum AWT / Swing
    Replies: 1
    Last Post: 07-06-2008, 08:03 PM
  4. Google Maps API
    By mew in forum New To Java
    Replies: 0
    Last Post: 12-26-2007, 10:28 AM
  5. Google Singleton Detector 0.7.2
    By levent in forum Java Software
    Replies: 0
    Last Post: 07-26-2007, 08:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •