/**
   * Converting ResultObject into text for preview according to checkboxes.
   *
   * @return String for preview
   */
  private String resultToText() {
    String resultString = "";

    // Adding column names:
    if (typeTB.getValue()) {
      resultString += "MoleculeType\t";
    }

    if (identifierTB.getValue()) {
      resultString += "Identifier\t";
    }

    if (nameTB.getValue()) {
      resultString += "MoleculeName\t";
    }

    // Adding line break in case there are column names
    if (resultString.length() > 0) {
      resultString += "\n";
    }

    resultString +=
        buildGroupString(chemTB, result.getChemicals(), PropertyType.CHEMICAL_COMPOUNDS.getTitle());
    resultString +=
        buildGroupString(protTB, result.getProteins(), PropertyType.PROTEINS.getTitle());
    resultString +=
        buildGroupString(sequTB, result.getSequences(), PropertyType.SEQUENCES.getTitle());
    resultString += buildGroupString(otheTB, result.getOthers(), PropertyType.OTHERS.getTitle());

    return resultString;
  }
  public void initialise(final Result result) {
    this.clear(); // if not cleared then updated panels are added under old ones

    TextPanel information =
        new TextPanel(
            " Here you can download the information the Molecules Tab provides.\n"
                + "The format will be TSV which can easily be handled with most word processors.");
    information.setStyleName("elv-InformationPanel-Download");
    this.addNorth(information, 25);

    // Creating ToggleButton for each possible category of molecules.
    VerticalPanel requiredType = new VerticalPanel();
    requiredType.add(new TextPanel("Please select the type of Molecules you are interested in:"));

    // Allow immediate changes to Preview by adding ClickHandler to every CheckBox.
    ClickHandler updateText =
        new ClickHandler() {
          @Override
          public void onClick(ClickEvent clickEvent) {
            textArea.setText(resultToText());
            textArea.setStyleName("elv-PreviewPanel-Download");
          }
        };

    // Checking for each category of molecules if they are present before adding Handler.
    if (result.getChemicals().size() > 0) {
      requiredType.add(chemTB);
      chemTB.addClickHandler(updateText);
    }

    if (result.getProteins().size() > 0) {
      requiredType.add(protTB);
      protTB.addClickHandler(updateText);
    }

    if (result.getSequences().size() > 0) {
      requiredType.add(sequTB);
      sequTB.addClickHandler(updateText);
    }

    if (result.getOthers().size() > 0) {
      requiredType.add(otheTB);
      otheTB.addClickHandler(updateText);
    }

    requiredType.setStyleName("elv-SelectionPanels-Download");

    // Creating ToggleButton for each available attribute of molecules and adding Handler.
    VerticalPanel requiredFields = new VerticalPanel();
    requiredFields.add(new TextPanel("Please select the fields you are interested in:"));

    typeTB.addClickHandler(updateText);
    requiredFields.add(typeTB);

    identifierTB.addClickHandler(updateText);
    requiredFields.add(identifierTB);

    nameTB.addClickHandler(updateText);
    requiredFields.add(nameTB);

    requiredFields.setStyleName("elv-SelectionPanels-Download");

    // Creating button for download.
    // Browsers that fully support Blob/Download:
    // Chrome, Chrome for Android, Firefox 20+, IE 10+, Opera 15+, Safari 6.1+
    VerticalPanel buttonField = new VerticalPanel();
    buttonField.add(startDownloadBtn);
    startDownloadBtn.setStyleName("elv-Download-Button");
    buttonField.setStyleName("elv-ButtonPanel-Download");
    startDownloadBtn.setTitle(
        "Depending on your browser you can either download your file by clicking on this button"
            + " or your will be redirected to a new tab in your browser where you can right click and"
            + " save the data.");
    startDownloadBtn.addClickHandler(
        new ClickHandler() {
          @Override
          public void onClick(ClickEvent event) {
            if ((chemTB.getValue() || protTB.getValue() || sequTB.getValue() || otheTB.getValue())
                && (typeTB.getValue() || nameTB.getValue() || identifierTB.getValue())) {
              alertDownload(textArea.getText());
            } else {
              DialogBoxFactory.alert(
                  "Molecules Download",
                  "You are trying to download an empty file.\n"
                      + "Please select at least one type of molecules AND one field for the download.");
            }
            presenter.moleculeDownloadStarted();
          }
        });
    buttonField.add(startGenomeSpaceDownloadBtn);
    startGenomeSpaceDownloadBtn.setStyleName("elv-Download-Button");
    startGenomeSpaceDownloadBtn.setTitle(
        "Clicking this button should open a window to import the data to GenomeSpace. "
            + "Depending on your browser configuration, you may have to enable popups for the Reactome website");
    startGenomeSpaceDownloadBtn.addClickHandler(
        new ClickHandler() {
          @Override
          public void onClick(ClickEvent event) {
            if ((chemTB.getValue() || protTB.getValue() || sequTB.getValue() || otheTB.getValue())
                && (typeTB.getValue() || nameTB.getValue() || identifierTB.getValue())) {
              Integer randomInt = random.nextInt(1000 - 1) + 1;
              try {
                uploadListToGenomeSpace(textArea.getText(), randomInt.toString());
              } catch (JavaScriptException exception) {
                Window.alert(exception.getMessage());
              }
            } else {
              Window.alert(
                  "You are trying to download an empty file.\n"
                      + "Please select at least one type of molecules AND one field for the download.");
            }
            presenter.moleculeDownloadStarted();
          }
        });

    // Bringing together the two panels.
    FlowPanel controlArea = new FlowPanel();
    controlArea.insert(requiredType.asWidget(), 0);
    controlArea.insert(requiredFields.asWidget(), 1);
    ScrollPanel scrollPanel = new ScrollPanel(controlArea);

    this.addWest(scrollPanel, 200);
    this.addEast(buttonField, 100);

    // Preview
    this.textArea = new TextArea();
    this.textArea.setVisible(true);
    this.textArea.setTitle("Preview of what your download file will look like.");
    this.textArea.setText(resultToText());
    this.add(textArea);
    this.textArea.setStyleName("elv-PreviewPanel-Download");

    this.addStyleName("elv-Details-OverviewPanel");
  }