Ejemplo n.º 1
0
  public CurrencyViewImpl() {

    Element eBasePrefix = DOM.getElementById("baseCurrencyPrefix");
    if (eBasePrefix == null) {
      throw new IllegalStateException("Base Currency Prefix div not found in hosted page");
    }
    baseCurrencyPrefix = eBasePrefix.getInnerText();

    Element eDocPrefix = DOM.getElementById("docCurrencyPrefix");
    if (eDocPrefix == null) {
      throw new IllegalStateException("Doc Currency Prefix div not found in hosted page");
    }
    docCurrencyPrefix = eDocPrefix.getInnerText();

    currencyGrid = new ListGrid();
    currencyGrid.setShowAllColumns(true);
    currencyGrid.setCanEdit(true);
    currencyGrid.setAutoFetchData(true);
    currencyGrid.setWidth100();
    currencyGrid.setHeight100();
    currencyGrid.setShowAllColumns(true);
    currencyGrid.setSelectionType(SelectionStyle.SINGLE);
    currencyGrid.setDataSource(CurrencyDataSource.getInstance());
    currencyGrid.setSortDirection(SortDirection.DESCENDING);
    currencyGrid.setSortField("dt");
    currencyGrid.setShowHeaderContextMenu(false);
    currencyGrid.setCanAutoFitFields(false);

    addRowButton = new IButton(constants.add());
    addRowButton.addClickHandler(
        new ClickHandler() {

          @Override
          public void onClick(ClickEvent event) {

            // Boolean isWindowInput = (Boolean)isWindowInputItem.getValue();
            // currencyGrid.startEditingNew();
            // currencyGrid.endEditing();
            Record firstRecord = currencyGrid.getRecordList().first();
            Record lastRecord = currencyGrid.getRecordList().last();

            Date firstDate = firstRecord.getAttributeAsDate("dt");
            Date lastDate = lastRecord.getAttributeAsDate("dt");
            Date nowDate = new Date();
            DateTimeFormat dateFormatter = DateTimeFormat.getFormat("dd.MM.yyyy");
            String firstDateStr = dateFormatter.format(firstDate);
            String lastDateStr = dateFormatter.format(lastDate);
            String nowDateStr = dateFormatter.format(nowDate);

            if (nowDateStr.equals(firstDateStr) || nowDateStr.equals(lastDateStr)) {
              SC.say(constants.warningCurrencyAlreadySet());
            } else {
              CurrencyRecord currencyRecord = new CurrencyRecord();
              currencyRecord.setDt(new Date());
              currencyRecord.setAttribute(
                  CurrencyRecord.IDENTITY_CURRENCY + baseCurrencyPrefix, 1.0);
              currencyRecord.setAttribute(
                  CurrencyRecord.IDENTITY_CURRENCY + docCurrencyPrefix, 1.0);
              currencyGrid.startEditingNew(currencyRecord);
            }
          }
        });

    editRowButton = new IButton(constants.editCurrency());
    editRowButton.setAutoFit(true);
    editRowButton.addClickHandler(
        new ClickHandler() {

          @Override
          public void onClick(ClickEvent event) {
            presenter.goTo(new CurrencyTypePlace(""));
          }
        });

    currencyGrid.addCellDoubleClickHandler(
        new CellDoubleClickHandler() {

          @Override
          public void onCellDoubleClick(CellDoubleClickEvent event) {
            if (currencyGrid.getFieldNum("remove") == event.getColNum()) {
              final ListGridRecord record = currencyGrid.getRecord(event.getRowNum());
              if (record != null) {
                SC.ask(
                    constants.askRemoveRecord(),
                    new BooleanCallback() {

                      @Override
                      public void execute(Boolean value) {
                        if (value) {
                          currencyGrid.removeData(record);
                        }
                      }
                    });
              }
            } else {
              Boolean isWindowInput = (Boolean) isWindowInputItem.getValue();
              if (isWindowInput) {
                String fieldName = currencyGrid.getFieldName(event.getColNum());
                Double exchange = null;
                try {
                  exchange = Double.parseDouble(event.getRecord().getAttribute(fieldName));
                } catch (Exception ex) {
                }
                if (fieldName != null
                    && exchange != null
                    && fieldName.startsWith(CurrencyRecord.IDENTITY_CURRENCY)) {
                  String name = fieldName.substring(1);
                  Currency currency = new Currency(name, exchange);
                  showCurrencyForm(event.getRowNum(), event.getColNum(), currency);
                }
              }
            }
          }
        });
    currencyGrid.addCellClickHandler(
        new CellClickHandler() {

          @Override
          public void onCellClick(CellClickEvent event) {

            if (currencyGrid.getFieldNum("remove") == event.getColNum()) {
              final ListGridRecord record = currencyGrid.getRecord(event.getRowNum());
              if (record != null) {
                SC.ask(
                    constants.askRemoveRecord(),
                    new BooleanCallback() {

                      @Override
                      public void execute(Boolean value) {
                        if (value) {
                          currencyGrid.removeData(record);
                        }
                      }
                    });
              }
            } else {
              Boolean isWindowInput = (Boolean) isWindowInputItem.getValue();
              if (isWindowInput) event.cancel();
            }
          }
        });

    DynamicForm form = new DynamicForm();
    isWindowInputItem = new CheckboxItem("output", constants.windowsInput());
    isWindowInputItem.setDefaultValue(false);
    isWindowInputItem.setAlign(Alignment.RIGHT);
    form.setItems(isWindowInputItem);
    form.setWidth("*");

    HLayout controlLayout = new HLayout();
    controlLayout.setMembers(addRowButton, editRowButton, form);

    addMember(currencyGrid);
    addMember(controlLayout);
  }