/**
   * Create repository selection page, allowing user specifying URI or (optionally) choosing from
   * preconfigured remotes list.
   *
   * <p>Wizard page is created without image, just with text description.
   *
   * @param sourceSelection true if dialog is used for source selection; false otherwise
   *     (destination selection). This indicates appropriate text messages.
   * @param configuredRemotes list of configured remotes that user may select as an alternative to
   *     manual URI specification. Remotes appear in given order in GUI, with {@value
   *     Constants#DEFAULT_REMOTE_NAME} as the default choice. List may be null or empty - no
   *     remotes configurations appear in this case. Note that the provided list may be changed by
   *     this constructor.
   * @param presetUri the pre-set URI, may be null
   */
  public RepositorySelectionPage(
      final boolean sourceSelection, final List<RemoteConfig> configuredRemotes, String presetUri) {

    super(RepositorySelectionPage.class.getName());

    this.uri = new URIish();
    this.sourceSelection = sourceSelection;

    String preset = presetUri;
    if (presetUri == null) {
      Clipboard clippy = new Clipboard(Display.getCurrent());
      String text = (String) clippy.getContents(TextTransfer.getInstance());
      try {
        if (text != null) {
          text = text.trim();
          int index = text.indexOf(' ');
          if (index > 0) text = text.substring(0, index);
          URIish u = new URIish(text);
          if (canHandleProtocol(u))
            if (Protocol.GIT.handles(u)
                || Protocol.SSH.handles(u)
                || text.endsWith(Constants.DOT_GIT_EXT)) preset = text;
        }
      } catch (URISyntaxException e) {
        // ignore, preset is null
      }
      clippy.dispose();
    }
    this.presetUri = preset;

    this.configuredRemotes = getUsableConfigs(configuredRemotes);

    selection = RepositorySelection.INVALID_SELECTION;

    if (sourceSelection) {
      setTitle(UIText.RepositorySelectionPage_sourceSelectionTitle);
      setDescription(UIText.RepositorySelectionPage_sourceSelectionDescription);
    } else {
      setTitle(UIText.RepositorySelectionPage_destinationSelectionTitle);
      setDescription(UIText.RepositorySelectionPage_destinationSelectionDescription);
    }

    storeInSecureStore =
        getPreferenceStore().getBoolean(UIPreferences.CLONE_WIZARD_STORE_SECURESTORE);
  }
  /** Check the user input and set messages in case of invalid input. */
  protected void checkPage() {
    if (isURISelected()) {
      assert uri != null;
      if (uriText.getText().length() == 0) {
        selectionIncomplete(null);
        return;
      } else if (uriText.getText().endsWith(" ")) { // $NON-NLS-1$
        selectionIncomplete(UIText.RepositorySelectionPage_UriMustNotHaveTrailingSpacesMessage);
        return;
      }

      try {
        final URIish finalURI = new URIish(uriText.getText().trim());
        String proto = finalURI.getScheme();
        if (proto == null && scheme.getSelectionIndex() >= 0)
          proto = scheme.getItem(scheme.getSelectionIndex());

        if (uri.getPath() == null) {
          selectionIncomplete(
              NLS.bind(
                  UIText.RepositorySelectionPage_fieldRequired,
                  unamp(UIText.RepositorySelectionPage_promptPath),
                  proto));
          return;
        }

        if (Protocol.FILE.handles(finalURI)) {
          String badField = null;
          if (uri.getHost() != null) badField = UIText.RepositorySelectionPage_promptHost;
          else if (uri.getUser() != null) badField = UIText.RepositorySelectionPage_promptUser;
          else if (uri.getPass() != null) badField = UIText.RepositorySelectionPage_promptPassword;
          if (badField != null) {
            selectionIncomplete(
                NLS.bind(UIText.RepositorySelectionPage_fieldNotSupported, unamp(badField), proto));
            return;
          }

          final File d = FS.DETECTED.resolve(new File("."), uri.getPath()); // $NON-NLS-1$
          if (!d.exists()) {
            selectionIncomplete(
                NLS.bind(UIText.RepositorySelectionPage_fileNotFound, d.getAbsolutePath()));
            return;
          }

          selectionComplete(finalURI, null);
          return;
        }

        if (uri.getHost() == null) {
          selectionIncomplete(
              NLS.bind(
                  UIText.RepositorySelectionPage_fieldRequired,
                  unamp(UIText.RepositorySelectionPage_promptHost),
                  proto));
          return;
        }

        if (Protocol.GIT.handles(finalURI)) {
          String badField = null;
          if (uri.getUser() != null) badField = UIText.RepositorySelectionPage_promptUser;
          else if (uri.getPass() != null) badField = UIText.RepositorySelectionPage_promptPassword;
          if (badField != null) {
            selectionIncomplete(
                NLS.bind(UIText.RepositorySelectionPage_fieldNotSupported, unamp(badField), proto));
            return;
          }
        }

        if (Protocol.HTTP.handles(finalURI) || Protocol.HTTPS.handles(finalURI)) {
          UserPasswordCredentials credentials = getSecureStoreCredentials(finalURI);
          if (credentials != null) {
            String u = credentials.getUser();
            String p = credentials.getPassword();
            String uriUser = finalURI.getUser();
            if (uriUser == null) {
              if (setSafeUser(u) || setSafePassword(p)) setStoreInSecureStore(true);
            } else if (uriUser.length() != 0 && uriUser.equals(u)) {
              if (setSafePassword(p)) setStoreInSecureStore(true);
            }
          }
        }

        selectionComplete(finalURI, null);
        return;
      } catch (URISyntaxException e) {
        selectionIncomplete(e.getReason());
        return;
      } catch (Exception e) {
        Activator.logError(
            NLS.bind(UIText.RepositorySelectionPage_errorValidating, getClass().getName()), e);
        selectionIncomplete(UIText.RepositorySelectionPage_internalError);
        return;
      }
    } else {
      assert remoteButton.getSelection();
      selectionComplete(null, remoteConfig);
      return;
    }
  }