Package com.casamind.adware.client.presenter

Source Code of com.casamind.adware.client.presenter.StatisticsPresenter$Display

package com.casamind.adware.client.presenter;

import com.casamind.adware.client.event.NavigationEvent;
import com.casamind.adware.client.helper.RPCCall;
import com.casamind.adware.client.resources.ErrorMessages;
import com.casamind.adware.client.resources.UIConstants;
import com.casamind.adware.client.service.DataServiceAsync;
import com.casamind.adware.client.view.StatisticsView;
import com.casamind.adware.shared.Pages;
import com.casamind.adware.shared.model.UserAccountDTO;
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.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.Query;
import com.google.gwt.visualization.client.Query.Callback;
import com.google.gwt.visualization.client.QueryResponse;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.visualizations.AnnotatedTimeLine;
import com.google.gwt.visualization.client.visualizations.AnnotatedTimeLine.Options;
import com.google.gwt.visualization.client.visualizations.AnnotatedTimeLine.WindowMode;
import com.google.gwt.visualization.client.visualizations.LineChart;
import com.google.gwt.visualization.client.visualizations.PieChart;

@SuppressWarnings("deprecation")
public class StatisticsPresenter implements Presenter {
  public interface Display {
    HasWidgets chartArea();

    HasWidgets upperLeftChartArea();

    HasWidgets upperRightChartArea();

    HasWidgets bottomChartArea();

    HasClickHandlers getCalendarLink();

    HasClickHandlers getConsoleLink();

    HasClickHandlers getOrderLink();

    int chartAreaWidth();

    Widget asWidget();
  }

  private UserAccountDTO user;
  private final int REFRESH_INTERVAL = 60000; // 1min
  private final DataServiceAsync rpcService;
  private final SimpleEventBus eventBus;
  private final Display display;
  private final ErrorMessages errorMessages = GWT.create(ErrorMessages.class);
  private final UIConstants uiConstants = GWT.create(UIConstants.class);

  public StatisticsPresenter(DataServiceAsync rpcService, SimpleEventBus eventBus, Display display) {
    this.rpcService = rpcService;
    this.eventBus = eventBus;
    this.display = display;
    bind();
  }

  public StatisticsPresenter(final DataServiceAsync rpcService, SimpleEventBus eventBus, Display display, UserAccountDTO user) {
    this(rpcService, eventBus, display);
    this.user = user;
    requestSpreadsheetURL(this.user.getId(), this.user.getUUID());
  }

  private void bind() {
    display.getOrderLink().addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        eventBus.fireEvent(new NavigationEvent(Pages.Order));
      }
    });
    display.getConsoleLink().addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        eventBus.fireEvent(new NavigationEvent(Pages.Console));
      }
    });
    display.getCalendarLink().addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        eventBus.fireEvent(new NavigationEvent(Pages.Calendar));
      }
    });

  }

  protected void requestSpreadsheetURL(final Long userId, final String fileName) {
    new RPCCall<String>() {
      @Override
      protected void callService(AsyncCallback<String> cb) {
        rpcService.getSpreadsheetURL(userId, fileName, cb);
      }

      @Override
      public void onSuccess(String result) {
        if (result != null) {
          initVizualisation(result);
        }
      }

      @Override
      public void onFailure(Throwable caught) {
        Window.alert(errorMessages.rpcError(caught.getMessage()));
      }
    }.retry(3);
  }

  protected void initVizualisation(String datasourceURL) {
    buildPieChart(datasourceURL + "&gid=0");
    buildLineChart(datasourceURL + "&gid=1");
    buildAnnotated(datasourceURL + "&gid=2");

  }

  private void buildAnnotated(final String datasourceURL) {
    // Create a callback to be called when the visualization API
    // has been loaded.
    Runnable onLoadCallback = new Runnable() {
      public void run() {
        // Create a query to go to the above URL.
        Query query = Query.create(datasourceURL);
        query.setRefreshInterval(REFRESH_INTERVAL);
        // Send the query.
        query.send(new Callback() {
          public void onResponse(QueryResponse response) {
            if (response.isError()) {
              display.chartArea().add(new Label(response.getMessage()));
            } else {
              // Get the data from the QueryResponse.
              DataTable data = response.getDataTable();

              // Create the Options object.
              Options options = Options.create();
              options.setDisplayAnnotations(true);
              options.setWindowMode(WindowMode.TRANSPARENT);

              String width = StatisticsPresenter.this.display.chartAreaWidth() + "px";
              // Create a PieChart and add it to a panel.
              StatisticsPresenter.this.display.bottomChartArea().add(new AnnotatedTimeLine(data, options, width, "350px"));

            }
          }
        });
      }
    };

    // Load the visualization api, passing the onLoadCallback to be called
    // when loading is done.
    VisualizationUtils.loadVisualizationApi(onLoadCallback, AnnotatedTimeLine.PACKAGE);
  }

  private void buildLineChart(final String datasourceURL) {
    // Create a callback to be called when the visualization API
    // has been loaded.
    Runnable onLoadCallback = new Runnable() {
      public void run() {
        // Create a query to go to the above URL.
        Query query = Query.create(datasourceURL);
        query.setRefreshInterval(REFRESH_INTERVAL);
        // Send the query.
        query.send(new Callback() {
          public void onResponse(QueryResponse response) {
            if (response.isError()) {
              display.chartArea().add(new Label(response.getMessage()));
            } else {
              // Get the data from the QueryResponse.
              DataTable data = response.getDataTable();

              // Create the Options object.

              int width = StatisticsPresenter.this.display.chartAreaWidth() / 2;
              com.google.gwt.visualization.client.visualizations.LineChart.Options options = com.google.gwt.visualization.client.visualizations.LineChart.Options.create();
              options.setTitle("Line");
              options.setWidth(width);
              options.setHeight(240);
              // Create a PieChart and add it to a panel.
              StatisticsPresenter.this.display.upperRightChartArea().add(new LineChart(data, options));

            }
          }
        });
      }
    };

    // Load the visualization api, passing the onLoadCallback to be called
    // when loading is done.
    VisualizationUtils.loadVisualizationApi(onLoadCallback, LineChart.PACKAGE);

  }

  private void buildPieChart(final String datasourceURL) {
    // Create a callback to be called when the visualization API
    // has been loaded.
    Runnable onLoadCallback = new Runnable() {
      public void run() {
        // Create a query to go to the above URL.
        Query query = Query.create(datasourceURL);
        query.setRefreshInterval(REFRESH_INTERVAL);
        // Send the query.
        query.send(new Callback() {
          public void onResponse(QueryResponse response) {
            if (response.isError()) {
              display.chartArea().add(new Label(response.getMessage()));
            } else {
              // Get the data from the QueryResponse.
              DataTable data = response.getDataTable();

              // Create the Options object.
              // PieChart.PieOptions options =
              // PieChart.createPieOptions();
              // options.set3D(true);
              // options.setTitle("PieChart");
              // options.setLegend(LegendPosition.LEFT);
              int width = StatisticsPresenter.this.display.chartAreaWidth() / 2;
              com.google.gwt.visualization.client.visualizations.PieChart.Options options = com.google.gwt.visualization.client.visualizations.PieChart.Options.create();
              options.set3D(true);
              options.setTitle("PieChart");
              options.setWidth(width);
              options.setHeight(240);
              // Create a PieChart and add it to a panel.
              StatisticsPresenter.this.display.upperLeftChartArea().add(new com.google.gwt.visualization.client.visualizations.PieChart(data, options));

            }
          }
        });
      }
    };

    // Load the visualization api, passing the onLoadCallback to be called
    // when loading is done.
    VisualizationUtils.loadVisualizationApi(onLoadCallback, PieChart.PACKAGE);

  }

  // private SelectHandler createSelectHandler(final PieChart chart) {
  // return new SelectHandler() {
  // @Override
  // public void onSelect(SelectEvent event) {
  // String message = "";
  //
  // // May be multiple selections.
  // JsArray<Selection> selections = chart.getSelections();
  //
  // for (int i = 0; i < selections.length(); i++) {
  // // add a new line for each selection
  // message += i == 0 ? "" : "\n";
  //
  // Selection selection = selections.get(i);
  //
  // if (selection.isCell()) {
  // // isCell() returns true if a cell has been selected.
  //
  // // getRow() returns the row number of the selected cell.
  // int row = selection.getRow();
  // // getColumn() returns the column number of the selected
  // // cell.
  // int column = selection.getColumn();
  // message += "cell " + row + ":" + column + " selected";
  // } else if (selection.isRow()) {
  // // isRow() returns true if an entire row has been
  // // selected.
  //
  // // getRow() returns the row number of the selected row.
  // int row = selection.getRow();
  // message += "row " + row + " selected";
  // } else {
  // // unreachable
  // message +=
  // "Pie chart selections should be either row selections or cell selections.";
  // message += "  Other visualizations support column selections as well.";
  // }
  // }
  //
  // //Window.alert(message);
  // }
  // };
  // }

  @Override
  public void go(DeckPanel container) {
    for (int i = 0; i < container.getWidgetCount(); i++) {
      if (!Pages.isHomePage(container.getWidget(i).getClass()))
        container.remove(i);
    }
    for (int i = 0; i < container.getWidgetCount(); i++) {
      if (container.getWidget(i).getClass().equals(StatisticsView.class)) {
        container.showWidget(i);
        return;
      }

    }
    container.add(display.asWidget());
    container.showWidget(container.getWidgetIndex(display.asWidget()));

  }
}
TOP

Related Classes of com.casamind.adware.client.presenter.StatisticsPresenter$Display

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.