public CreateNewPackagePresenterImpl(CreateNewPackageView view) {
    super(view);
    this.view = view;
    this.content_dir = null;
    directoryChooser = new DirectoryChooser();
    fileChooser = new FileChooser();

    fileChooser
        .getExtensionFilters()
        .add(new FileChooser.ExtensionFilter("Package Description (*.json)", "*.json"));
    fileChooser
        .getExtensionFilters()
        .add(new FileChooser.ExtensionFilter("All files (*.*)", "*.*"));

    InputStream defaultRulesStream =
        Thread.currentThread()
            .getContextClassLoader()
            .getResourceAsStream("rules/default-rules.xml");

    PackageDescriptionRulesBuilder builder = new JaxbPackageDescriptionRulesBuilder();

    RulesSpec packageDescriptionPrefs = builder.buildPackageDescriptionRules(defaultRulesStream);

    ruleProperties = new SystemPropertyPreferencesRulePropertiesManager();
    ruleProperties.init(packageDescriptionPrefs);

    creator = new GeneralPackageDescriptionCreator(packageDescriptionPrefs);

    view.setPresenter(this);
    view.promptForUndefinedProperties(ruleProperties);
    bind();
  }
  public Node display() {
    // Setup help content and then rebind the base class to this view.
    view.setupHelp();
    setView(view);
    super.bindBaseElements();

    return view.asNode();
  }
  @Override
  public void clear() {
    view.getSelectedBaseDirectoryTextField().setText("");
    view.getSelectedPackageDescriptionTextField().setText("");
    view.getErrorMessage().setText("");

    content_dir = null;
    root_artifact_dir = null;
  }
 protected void displayExceptionMessage(Throwable throwable) {
   String errorMessage = throwable.getMessage();
   if (throwable instanceof PackageDescriptionCreatorException
       && ((PackageDescriptionCreatorException) throwable).hasDetail()) {
     errorMessage =
         errorMessage + "\n" + ((PackageDescriptionCreatorException) throwable).getDetail();
   }
   view.getErrorMessage().setText(errorMessage);
   view.getErrorMessage().setVisible(true);
 }
  private void bind() {

    // Handles the continue button in the footer being pressed. Validates that all required fields
    // are present, sets the state on the controller,
    // and instructs the controller to move to the next page.
    view.getContinueButton()
        .setOnAction(
            arg0 -> {
              try {
                if (root_artifact_dir != null
                    && root_artifact_dir.exists()
                    && root_artifact_dir.canRead()) {

                  /* Insert properties, if any */
                  for (Map.Entry<String, String> property : view.getPropertyValues().entrySet()) {
                    ruleProperties.setProperty(property.getKey(), property.getValue());
                  }
                  // TODO: when we support multiple ontologies we will need to adjust the handling
                  // of the user's
                  // choice of ontology identifier instead of hardcoded value here

                  final PackageDescriptionServiceWorker packageDescriptionService =
                      new PackageDescriptionServiceWorker(
                          DcsPackageDescriptionSpec.SPECIFICATION_ID, root_artifact_dir);

                  view.showProgressIndicatorPopUp();

                  ((ProgressDialogPopup) view.getProgressIndicatorPopUp())
                      .setCancelEventHandler(event -> packageDescriptionService.cancel());

                  controller.setCrossPageProgressIndicatorPopUp(view.getProgressIndicatorPopUp());
                  controller.setContentRoot(content_dir);
                  controller.setRootArtifactDir(root_artifact_dir);

                  packageDescriptionService.setOnCancelled(
                      event -> {
                        packageDescriptionService.reset();
                        controller.getCrossPageProgressIndicatorPopUp().hide();
                        controller.showHome(false);
                      });

                  packageDescriptionService.setOnFailed(
                      workerStateEvent -> {
                        displayExceptionMessage(workerStateEvent.getSource().getException());
                        view.getErrorMessage().setVisible(true);
                        packageDescriptionService.reset();
                        controller.getCrossPageProgressIndicatorPopUp().hide();
                        controller.showHome(false);
                      });

                  packageDescriptionService.setOnSucceeded(
                      workerStateEvent -> {
                        PackageDescription packageDescription =
                            (PackageDescription) workerStateEvent.getSource().getValue();
                        controller.setPackageDescription(packageDescription);
                        controller.setPackageDescriptionFile(null);
                        packageDescriptionService.reset();
                        controller.goToNextPage();
                      });

                  packageDescriptionService.start();

                } else if (root_artifact_dir != null
                    && (!root_artifact_dir.exists() || !root_artifact_dir.canRead())) {
                  view.getErrorMessage().setText(errors.get(ErrorKey.INACCESSIBLE_CONTENT_DIR));
                  view.getErrorMessage().setVisible(true);
                } else if (controller.getPackageDescription() != null) {
                  controller.goToNextPage();
                } else {
                  view.getErrorMessage()
                      .setText(errors.get(ErrorKey.BASE_DIRECTORY_OR_DESCRIPTION_NOT_SELECTED));
                  view.getErrorMessage().setVisible(true);
                }
              } catch (Exception e) {
                view.getErrorMessage()
                    .setText(messages.formatErrorCreatingNewPackage(e.getMessage()));
                view.getErrorMessage().setVisible(true);
                log.error(e.getMessage());
              }
            });

    // Handles the user pressing the button to choose a base directory to create a package from.
    view.getChooseContentDirectoryButton()
        .setOnAction(
            event -> {
              if (directoryChooser.getInitialDirectory() != null
                  && !directoryChooser.getInitialDirectory().exists()) {
                directoryChooser.setInitialDirectory(null);
              }

              File dir = controller.showOpenDirectoryDialog(directoryChooser);

              if (dir != null) {
                root_artifact_dir = dir;
                content_dir = root_artifact_dir.getParentFile();
                view.getSelectedBaseDirectoryTextField().setText(root_artifact_dir.getPath());
                view.getSelectedPackageDescriptionTextField().setText("");
                // If the error message happens to be visible erase it.
                view.getErrorMessage().setVisible(false);
                directoryChooser.setInitialDirectory(dir);
              }
            });

    // Handles the user pressing a button to choose an existing package description.
    view.getChoosePackageDescriptionButton()
        .setOnAction(
            arg0 -> {
              File descriptionFile = controller.showOpenFileDialog(fileChooser);

              if (descriptionFile != null) {
                try {
                  FileInputStream fis = new FileInputStream(descriptionFile);
                  PackageDescription description = packageDescriptionBuilder.deserialize(fis);
                  // If the selected package description file is valid set it on the controller and
                  // remove the content directory if it was set.
                  if (description != null) {
                    // content_dir = null;
                    controller.setPackageDescription(description);
                    controller.setPackageDescriptionFile(descriptionFile);
                    controller.setRootArtifactDir(null);
                    controller.setContentRoot(null);
                    content_dir = null;
                    root_artifact_dir = null;

                    view.getErrorMessage().setVisible(false);
                    view.getSelectedPackageDescriptionTextField()
                        .setText(descriptionFile.getPath());
                    view.getSelectedBaseDirectoryTextField().setText("");
                    fileChooser.setInitialDirectory(descriptionFile.getParentFile());
                  }
                } catch (FileNotFoundException | PackageToolException e) {
                  view.getErrorMessage()
                      .setText(
                          messages.formatPackageDescriptionBuilderFailure(
                              descriptionFile.getName()));
                  view.getErrorMessage().setVisible(true);
                  log.error(e.getMessage());
                }
              }
            });
  }