/**
   * Use the ChooseWorkspaceDialog to get the new workspace from the user.
   *
   * @return a string naming the new workspace and null if cancel was selected
   */
  private String promptForWorkspace() {
    // get the current workspace as the default
    ChooseWorkspaceData data = new ChooseWorkspaceData(Platform.getInstanceLocation().getURL());
    ChooseWorkspaceDialog dialog =
        new ChooseWorkspaceWithSettingsDialog(window.getShell(), data, true, false);
    dialog.prompt(true);

    // return null if the user changed their mind
    String selection = data.getSelection();
    if (selection == null) {
      return null;
    }

    // otherwise store the new selection and return the selection
    data.writePersistedData();
    return selection;
  }
Esempio n. 2
0
  /**
   * Return <code>null</code> if a valid workspace path has been set and an exit code otherwise.
   * Prompt for and set the path if possible and required.
   *
   * @param applicationArguments the command line arguments
   * @return <code>null</code> if a valid instance location has been set and an exit code otherwise
   */
  private Object checkInstanceLocation(Shell shell, Map applicationArguments) {
    // -data @none was specified but an ide requires workspace
    Location instanceLoc = Platform.getInstanceLocation();
    if (instanceLoc == null) {
      MessageDialog.openError(
          shell,
          IDEWorkbenchMessages.IDEApplication_workspaceMandatoryTitle,
          IDEWorkbenchMessages.IDEApplication_workspaceMandatoryMessage);
      return EXIT_OK;
    }

    // -data "/valid/path", workspace already set
    if (instanceLoc.isSet()) {
      // make sure the meta data version is compatible (or the user has
      // chosen to overwrite it).
      if (!checkValidWorkspace(shell, instanceLoc.getURL())) {
        return EXIT_OK;
      }

      // at this point its valid, so try to lock it and update the
      // metadata version information if successful
      try {
        if (instanceLoc.lock()) {
          writeWorkspaceVersion();
          return null;
        }

        // we failed to create the directory.
        // Two possibilities:
        // 1. directory is already in use
        // 2. directory could not be created
        File workspaceDirectory = new File(instanceLoc.getURL().getFile());
        if (workspaceDirectory.exists()) {
          if (isDevLaunchMode(applicationArguments)) {
            return EXIT_WORKSPACE_LOCKED;
          }
          MessageDialog.openError(
              shell,
              IDEWorkbenchMessages.IDEApplication_workspaceCannotLockTitle,
              NLS.bind(
                  IDEWorkbenchMessages.IDEApplication_workspaceCannotLockMessage,
                  workspaceDirectory.getAbsolutePath()));
        } else {
          MessageDialog.openError(
              shell,
              IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetTitle,
              IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetMessage);
        }
      } catch (IOException e) {
        IDEWorkbenchPlugin.log(
            "Could not obtain lock for workspace location", //$NON-NLS-1$
            e);
        MessageDialog.openError(shell, IDEWorkbenchMessages.InternalError, e.getMessage());
      }
      return EXIT_OK;
    }

    // -data @noDefault or -data not specified, prompt and set
    ChooseWorkspaceData launchData = new ChooseWorkspaceData(instanceLoc.getDefault());

    boolean force = false;
    while (true) {
      URL workspaceUrl = promptForWorkspace(shell, launchData, force);
      if (workspaceUrl == null) {
        return EXIT_OK;
      }

      // if there is an error with the first selection, then force the
      // dialog to open to give the user a chance to correct
      force = true;

      try {
        // the operation will fail if the url is not a valid
        // instance data area, so other checking is unneeded
        if (instanceLoc.set(workspaceUrl, true)) {
          launchData.writePersistedData();
          writeWorkspaceVersion();
          return null;
        }
      } catch (IllegalStateException e) {
        MessageDialog.openError(
            shell,
            IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetTitle,
            IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetMessage);
        return EXIT_OK;
      } catch (IOException e) {
        MessageDialog.openError(
            shell,
            IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetTitle,
            IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetMessage);
      }

      // by this point it has been determined that the workspace is
      // already in use -- force the user to choose again
      MessageDialog.openError(
          shell,
          IDEWorkbenchMessages.IDEApplication_workspaceInUseTitle,
          NLS.bind(
              IDEWorkbenchMessages.IDEApplication_workspaceInUseMessage, workspaceUrl.getFile()));
    }
  }
 /*
  * (non-Javadoc)
  *
  * @see org.eclipse.jface.action.Action#run()
  */
 public void run() {
   data.workspaceSelected(location);
   data.writePersistedData();
   restart(location);
 }