private List<String> validate() {
   List<String> errors = new ArrayList<String>();
   if (selectionListbox.getItemCount() == 0 && !ViewUtil.isTextPopulated(dbInstructionArea)) {
     errors.add(TEXT_CONSTANTS.noDBSurveyMandatory());
   }
   if (!ViewUtil.isTextPopulated(notificationEmailBox)) {
     errors.add(TEXT_CONSTANTS.emailMandatory());
   }
   return errors;
 }
  private void loadResponses() {
    statusLabel.setText(TEXT_CONSTANTS.pleaseWait());
    statusLabel.setVisible(true);

    surveyInstanceSvc.listResponsesByQuestion(
        questionId,
        getCursor(currentPage - 1),
        new AsyncCallback<ResponseDto<ArrayList<QuestionAnswerStoreDto>>>() {
          @Override
          public void onFailure(Throwable caught) {
            MessageDialog errDia =
                new MessageDialog(
                    TEXT_CONSTANTS.error(),
                    TEXT_CONSTANTS.errorTracePrefix() + " " + caught.getLocalizedMessage());
            errDia.showCentered();
          }

          @Override
          public void onSuccess(ResponseDto<ArrayList<QuestionAnswerStoreDto>> result) {
            currentAnswers = result.getPayload();
            setCursor(result.getCursorString());
            populateData(result.getPayload());
          }
        });
  }
  @Override
  public void onClick(final ClickEvent event) {
    if (event.getSource() == nextButton) {
      currentPage++;
      loadResponses();
    } else if (event.getSource() == previousButton) {
      currentPage--;
      loadResponses();
    } else if (event.getSource() == clearButton) {
      populateData(currentAnswers);
    } else if (event.getSource() == saveButton) {
      if (changedAnswers != null && changedAnswers.size() > 0) {
        statusLabel.setText(TEXT_CONSTANTS.pleaseWait());
        statusLabel.setVisible(true);
        surveyInstanceSvc.updateQuestions(
            new ArrayList<QuestionAnswerStoreDto>(changedAnswers.values()),
            true,
            new AsyncCallback<List<QuestionAnswerStoreDto>>() {

              @Override
              public void onFailure(Throwable caught) {
                MessageDialog errDia =
                    new MessageDialog(
                        TEXT_CONSTANTS.error(),
                        TEXT_CONSTANTS.errorTracePrefix() + " " + caught.getLocalizedMessage());
                errDia.showCentered();
                statusLabel.setVisible(false);
              }

              @Override
              public void onSuccess(List<QuestionAnswerStoreDto> result) {
                statusLabel.setVisible(false);
                if (result != null) {
                  // update the value in the
                  // questionsList so we can
                  // keep the data consistent
                  // if the user presses clear
                  for (QuestionAnswerStoreDto dto : result) {
                    for (int i = 0; i < currentAnswers.size(); i++) {
                      if (currentAnswers.get(i).getKeyId().equals(dto.getKeyId())) {
                        currentAnswers.get(i).setValue(dto.getValue());
                      }
                    }
                  }
                }
                populateData(currentAnswers);
              }
            });
      }
    } else if (event.getSource() == doneButton) {
      dismissDialog();
    }
  }
 public BootstrapGeneratorWidget() {
   contentPanel = new VerticalPanel();
   contentPanel.setWidth("800px");
   surveyService = GWT.create(SurveyService.class);
   addSurveyButton = new Button(TEXT_CONSTANTS.addSelected());
   addSurveyButton.addClickHandler(this);
   generateFileButton = new Button(TEXT_CONSTANTS.generate());
   generateFileButton.addClickHandler(this);
   removeButton = new Button(TEXT_CONSTANTS.removeSelected());
   removeButton.addClickHandler(this);
   CaptionPanel selectorPanel = new CaptionPanel(TEXT_CONSTANTS.selectSurveyForInclusion());
   HorizontalPanel temp = new HorizontalPanel();
   selectionWidget = new SurveySelectionWidget(Orientation.HORIZONTAL, TerminalType.SURVEY);
   temp.add(selectionWidget);
   temp.add(addSurveyButton);
   selectorPanel.add(temp);
   contentPanel.add(selectorPanel);
   CaptionPanel zipPanel = new CaptionPanel(TEXT_CONSTANTS.fileContents());
   selectionListbox = new ListBox(true);
   selectionListbox.setVisibleItemCount(DEFAULT_ITEM_COUNT);
   VerticalPanel zipPanelContent = new VerticalPanel();
   HorizontalPanel selectedSurveyPanel = new HorizontalPanel();
   ViewUtil.installFieldRow(
       selectedSurveyPanel, TEXT_CONSTANTS.selectedSurveys(), selectionListbox, LABEL_STYLE);
   selectedSurveyPanel.add(removeButton);
   zipPanelContent.add(selectedSurveyPanel);
   temp = new HorizontalPanel();
   includeDbScriptBox = new CheckBox();
   includeDbScriptBox.addClickHandler(this);
   ViewUtil.installFieldRow(temp, TEXT_CONSTANTS.includeDB(), includeDbScriptBox, LABEL_STYLE);
   zipPanelContent.add(temp);
   dbInstructionArea = new TextArea();
   dbInstructionArea.setVisible(false);
   zipPanelContent.add(dbInstructionArea);
   temp = new HorizontalPanel();
   notificationEmailBox = new TextBox();
   ViewUtil.installFieldRow(
       temp, TEXT_CONSTANTS.notificationEmail(), notificationEmailBox, LABEL_STYLE);
   zipPanelContent.add(temp);
   zipPanelContent.add(generateFileButton);
   zipPanel.add(zipPanelContent);
   contentPanel.add(zipPanel);
   initWidget(contentPanel);
 }
 public QuestionResponseDialog(Long questionId) {
   super();
   this.questionId = questionId;
   // Set the dialog box's caption.
   setText(TEXT_CONSTANTS.questionResponses());
   setAnimationEnabled(true);
   setGlassEnabled(true);
   currentAnswers = new ArrayList<QuestionAnswerStoreDto>();
   changedAnswers = new HashMap<Long, QuestionAnswerStoreDto>();
   currentTextboxes = new ArrayList<TextBox>();
   cursorArray = new ArrayList<String>();
   statusLabel = new Label();
   surveyInstanceSvc = GWT.create(SurveyInstanceService.class);
   nextButton = new Button(TEXT_CONSTANTS.next());
   nextButton.setVisible(true);
   currentPage = 0;
   nextButton.addClickHandler(this);
   previousButton = new Button(TEXT_CONSTANTS.previous());
   previousButton.setVisible(false);
   previousButton.addClickHandler(this);
   paginationPanel = new HorizontalPanel();
   paginationPanel.add(previousButton);
   paginationPanel.add(nextButton);
   dataGrid = new Grid();
   contentPanel = new VerticalPanel();
   contentPanel.add(statusLabel);
   contentPanel.add(dataGrid);
   doneButton = new Button(TEXT_CONSTANTS.done());
   saveButton = new Button(TEXT_CONSTANTS.save());
   saveButton.addClickHandler(this);
   saveButton.setEnabled(false);
   clearButton = new Button(TEXT_CONSTANTS.discardChanges());
   clearButton.addClickHandler(this);
   clearButton.setEnabled(false);
   contentPanel.add(doneButton);
   doneButton.addClickHandler(this);
   setWidget(new ScrollPanel(contentPanel));
   loadResponses();
   setPopupPosition(Window.getClientWidth() / 5, Window.getClientHeight() / 5);
 }
  @Override
  public void onClick(ClickEvent event) {
    if (event.getSource() == includeDbScriptBox) {
      if (includeDbScriptBox.getValue()) {
        dbInstructionArea.setValue("");
        dbInstructionArea.setVisible(true);
      } else {
        dbInstructionArea.setValue("");
        dbInstructionArea.setVisible(false);
      }
    } else if (event.getSource() == addSurveyButton) {
      String group = selectionWidget.getSelectedSurveyGroupName();
      List<String> name = selectionWidget.getSelectedSurveyNames();
      List<Long> ids = selectionWidget.getSelectedSurveyIds();
      for (int i = 0; i < name.size(); i++) {
        boolean alreadyThere = false;
        for (int j = 0; j < selectionListbox.getItemCount(); j++) {
          if (selectionListbox.getValue(j).equals(ids.get(i).toString())) {
            alreadyThere = true;
            break;
          }
        }
        if (!alreadyThere) {
          selectionListbox.addItem(group + ": " + name.get(i), ids.get(i).toString());
        }
      }
    } else if (event.getSource() == removeButton) {
      List<Integer> victimList = new ArrayList<Integer>();
      for (int i = 0; i < selectionListbox.getItemCount(); i++) {
        if (selectionListbox.isItemSelected(i)) {
          victimList.add(i);
        }
      }
      if (victimList.size() > 0) {
        // remove the items in descending order so we don't have to
        // worry about adjusting indexes as we remove
        Collections.sort(victimList);
        for (int i = victimList.size() - 1; i >= 0; i--) {
          selectionListbox.removeItem(victimList.get(i));
        }
      }
    } else if (event.getSource() == generateFileButton) {

      List<Long> idList = new ArrayList<Long>();
      for (int i = 0; i < selectionListbox.getItemCount(); i++) {
        idList.add(new Long(selectionListbox.getValue(i)));
      }

      List<String> errors = validate();
      if (errors.size() == 0) {
        surveyService.generateBootstrapFile(
            idList,
            dbInstructionArea.getText(),
            notificationEmailBox.getText(),
            new AsyncCallback<Void>() {

              @Override
              public void onFailure(Throwable caught) {
                MessageDialog errDia =
                    new MessageDialog(
                        TEXT_CONSTANTS.error(),
                        TEXT_CONSTANTS.errorTracePrefix() + " " + caught.getLocalizedMessage());
                errDia.showCentered();
              }

              @Override
              public void onSuccess(Void result) {
                MessageDialog dia =
                    new MessageDialog(
                        TEXT_CONSTANTS.requestSubmitted(), TEXT_CONSTANTS.emailWillBeSent());
                dia.showCentered();
                resetUI();
              }
            });
      } else {
        StringBuilder builder = new StringBuilder(TEXT_CONSTANTS.pleaseCorrect() + "<br><ul>");
        for (String e : errors) {
          builder.append("<li>").append(e).append("</li>");
        }
        builder.append("</ul>");
        MessageDialog dia = new MessageDialog(TEXT_CONSTANTS.inputError(), builder.toString());
        dia.showCentered();
      }
    }
  }
 private void loadHeaderRow() {
   addHeaderItem(0, TEXT_CONSTANTS.id());
   addHeaderItem(1, TEXT_CONSTANTS.value());
   addHeaderItem(2, TEXT_CONSTANTS.collectionDate());
   setGridRowStyle(dataGrid, 0, false);
 }
  private void populateData(final List<QuestionAnswerStoreDto> data) {
    dataGrid.clear(true);
    updateSaveButtonStatus(false);
    if (data != null && data.size() > 0) {
      statusLabel.setVisible(false);
      dataGrid.resize(data.size() + 2, 3);
      loadHeaderRow();
      for (int i = 0; i < data.size(); i++) {
        final int idx = i;
        dataGrid.setWidget(i + 1, 0, new Label(data.get(i).getSurveyInstanceId().toString()));
        TextBox temp = new TextBox();
        temp.setText(data.get(i).getValue());
        currentTextboxes.add(temp);
        temp.addChangeHandler(
            new ChangeHandler() {

              @Override
              public void onChange(ChangeEvent event) {
                String oldVal = data.get(idx).getValue();
                String newVal = ((TextBox) event.getSource()).getValue();
                if (!newVal.trim().equals(oldVal)) {
                  dataGrid.getCellFormatter().setStyleName(idx + 1, 1, EDITED_ROW_CSS);
                  // create a new copy of the answer so we don't
                  // overwrite the old value
                  QuestionAnswerStoreDto newAnswer = new QuestionAnswerStoreDto();
                  newAnswer.setKeyId(data.get(idx).getKeyId());
                  newAnswer.setArbitratyNumber(data.get(idx).getArbitratyNumber());
                  newAnswer.setCollectionDate(data.get(idx).getCollectionDate());
                  newAnswer.setType(data.get(idx).getType());
                  newAnswer.setQuestionID(data.get(idx).getQuestionID());
                  newAnswer.setValue(newVal.trim());
                  newAnswer.setSurveyId(data.get(idx).getSurveyId());
                  newAnswer.setSurveyInstanceId(data.get(idx).getSurveyInstanceId());
                  newAnswer.setOldValue(data.get(idx).getValue());
                  changedAnswers.put(newAnswer.getKeyId(), newAnswer);

                } else {
                  dataGrid.getCellFormatter().setStyleName(idx + 1, 1, "");
                  changedAnswers.remove(data.get(idx).getKeyId());
                }
                if (changedAnswers.size() > 0) {
                  updateSaveButtonStatus(true);
                } else {
                  updateSaveButtonStatus(false);
                }
              }
            });
        dataGrid.setWidget(i + 1, 1, temp);
        dataGrid.getCellFormatter().setStyleName(idx + 1, 1, "");
        dataGrid.setWidget(i + 1, 2, new Label(DATE_FMT.format(data.get(i).getCollectionDate())));
      }

      if (data.size() >= 20) {
        nextButton.setVisible(true);
      } else {
        nextButton.setVisible(false);
      }
      if (currentPage > 0) {
        previousButton.setVisible(true);
      } else {
        previousButton.setVisible(false);
      }
      dataGrid.setWidget(data.size() + 1, 0, paginationPanel);
      dataGrid.setWidget(data.size() + 1, 1, saveButton);
      dataGrid.setWidget(data.size() + 1, 2, clearButton);

    } else {
      dataGrid.resize(1, 1);
      statusLabel.setText(TEXT_CONSTANTS.noResponsesForQuestion());
      statusLabel.setVisible(true);
    }
  }