Package pl.onet.dreamcalc.client

Source Code of pl.onet.dreamcalc.client.DreamCalc

package pl.onet.dreamcalc.client;

import net.ffxml.gwt.json.client.JsonRpc;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class DreamCalc implements EntryPoint {

  private static final String JSON_URL = GWT.getModuleBaseURL() + "calc";
  private static final String JSON_EVAL_METHOD = "calc.eval";
  private static final String[] themes = { "default", "contrast", "big"};
 
  private DockPanel mainPanel = new DockPanel();
  private Label display;
  private VerticalPanel digitsPanel;
  private VerticalPanel operatorsPanel;
  private HorizontalPanel clearPanel;
  private ThemeManager themeManager;
 
  private String operator;
  private String arg;
  private boolean displayReset = true;
 
  private JsonRpc jsonRpc;

  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {

    //Create a new JsonRpc instance
    jsonRpc = new JsonRpc();
   
    mainPanel = new DockPanel();
    mainPanel.addStyleName("calc");

    // display
    display = new Label("0");
    display.addStyleName("display");
    mainPanel.add(display, DockPanel.NORTH);

    // digits
    digitsPanel = createDigitsPanel();
    mainPanel.add(digitsPanel, DockPanel.CENTER);

    // operators
    operatorsPanel = createOperatorsPanel();
    mainPanel.add(operatorsPanel, DockPanel.EAST);

    // C and CE
    clearPanel = createClearPanel();
    mainPanel.add(clearPanel, DockPanel.SOUTH);
   
    RootPanel.get("calc").add(mainPanel);
   
    // themeManager
    themeManager = new ThemeManager(this, themes);
   
    RootPanel.get("themes1").add(themeManager.getMainPanel());
  }

  public void setStyle(String styleName) {
    RootPanel.get().setStyleName(styleName);
  }
 
  public Panel getMainPanel() {
    return mainPanel;
  }
 
  private VerticalPanel createDigitsPanel() {
    String[] symbols = { "7", "8", "9", "4", "5", "6", "1", "2", "3", "0",
        "." };
    VerticalPanel digits = new VerticalPanel();
    HorizontalPanel row = new HorizontalPanel();
    Button digit;

    for (int i = 0; i < symbols.length; i++) {

      digit = new Button(symbols[i]);
      digit.addStyleDependentName("basic");
      digit.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
          Button d = (Button) event.getSource();
          digitClicked(d.getText());
        }
      });

      if (i % 3 == 0) {
        row = new HorizontalPanel();
        digits.add(row);
      }
      row.add(digit);

    }
    return digits;
  }

  private VerticalPanel createOperatorsPanel() {
    VerticalPanel operators = new VerticalPanel();
    String[] op = { "+", "-", "*", "/", "=" };

    Button operator;

    for (int i = 0; i < op.length; i++) {
      operator = new Button(op[i]);
      operator.addStyleDependentName("basic");
      if (op[i].equals("="))
        operator.addStyleDependentName("result");

      operator.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
          Button d = (Button) event.getSource();
          operatorClicked(d.getText());
        }
      });
      operators.add(operator);
    }
    return operators;
  }

  private HorizontalPanel createClearPanel() {
    HorizontalPanel clear = new HorizontalPanel();
    String[] symbols = { "C", "CE" };

    Button button;

    for (int i = 0; i < symbols.length; i++) {
      button = new Button(symbols[i]);
      button.addStyleDependentName("basic");
     
      button.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
          Button d = (Button) event.getSource();
          clearClicked(d.getText());
        }
      });
      clear.add(button);
    }
    return clear;
  }
 
  private void digitClicked(String num) {
    String arg = display.getText();

    if (displayReset) {
      display.setText(num);
      displayReset = false;
    } else if (!num.equals(".") || !arg.contains("."))
      display.setText(arg + num);
  }

  private void operatorClicked(String op) {
    if (operator == null) {
      arg = display.getText();
    }

    if (!op.equals("=")) {
      operator = op;
    } else if (operator != null) {
      String arg2 = display.getText();
      getResult(Double.parseDouble(arg), Double.parseDouble(arg2),
          operator);
      operator = null;
    }
    displayReset = true;
  }

  private void clearClicked(String sym) {
    display.setText("0");
    displayReset = true;
    if(sym.equals("C")) {
      operator = null;
    }
  }
 
  private void getResult(Double arg1, Double arg2, String operator) {
    //Create a callback handler
    AsyncCallback<Double> callback = new AsyncCallback<Double>() {
      public void onFailure(Throwable caught) {
        caught.printStackTrace();
        displayError("Couldn't retrieve JSON: " + caught.getMessage());
      }

      public void onSuccess(Double result) {
        displayResult(result);
      }
    };

    jsonRpc.request(
        JSON_URL,
        JSON_EVAL_METHOD,
        new Object[] { arg1, arg2, operator },
        callback);
   
    /*
     * Request przez GET
     *
    String url = JSON_URL;

    url += arg1.toString() + "+" + arg2.toString() + "+"
        + (operator.equals("+") ? "p" : operator);

    url = URL.encode(url);

    // Send request to server and catch any errors.
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

    System.out.println("WYSYLAM REQ");
    try {
      // Request request =
      builder.sendRequest(null, new RequestCallback() {
        public void onError(Request request, Throwable exception) {
          displayError("Couldn't retrieve JSON");
        }

        public void onResponseReceived(Request request,
            Response response) {
          if (200 == response.getStatusCode()) {
            System.out.println("ODP: " + response.getText());
            displayResult(response.getText());
          } else {
            displayError("Couldn't retrieve JSON ("
                + response.getStatusText() + ")");
          }
        }
      });
    } catch (RequestException e) {
      displayError("Couldn't retrieve JSON");
    }
    */
  }

  private void displayResult(Double result) {
    display.setText(result.toString());
  }

  private void displayError(String error) {
    display.setText(error);
  }

}
TOP

Related Classes of pl.onet.dreamcalc.client.DreamCalc

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.