/**
   * 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;
  }
 private IContributionItem[] getContributionItems() {
   ArrayList list = new ArrayList();
   final ChooseWorkspaceData data =
       new ChooseWorkspaceData(Platform.getInstanceLocation().getURL());
   data.readPersistedData();
   String current = data.getInitialDefault();
   String[] workspaces = data.getRecentWorkspaces();
   for (int i = 0; i < workspaces.length; i++) {
     if (workspaces[i] != null && !workspaces[i].equals(current)) {
       list.add(new ActionContributionItem(new WorkspaceMRUAction(workspaces[i], data)));
     }
   }
   if (list.size() > 0) {
     list.add(new Separator());
   }
   return (IContributionItem[]) list.toArray(new IContributionItem[list.size()]);
 }
Beispiel #3
0
  /**
   * Open a workspace selection dialog on the argument shell, populating the argument data with the
   * user's selection. Perform first level validation on the selection by comparing the version
   * information. This method does not examine the runtime state (e.g., is the workspace already
   * locked?).
   *
   * @param shell
   * @param launchData
   * @param force setting to true makes the dialog open regardless of the showDialog value
   * @return An URL storing the selected workspace or null if the user has canceled the launch
   *     operation.
   */
  private URL promptForWorkspace(Shell shell, ChooseWorkspaceData launchData, boolean force) {
    URL url = null;
    do {
      // okay to use the shell now - this is the splash shell
      new ChooseWorkspaceDialog(shell, launchData, false, true).prompt(force);
      String instancePath = launchData.getSelection();
      if (instancePath == null) {
        return null;
      }

      // the dialog is not forced on the first iteration, but is on every
      // subsequent one -- if there was an error then the user needs to be
      // allowed to fix it
      force = true;

      // 70576: don't accept empty input
      if (instancePath.length() <= 0) {
        MessageDialog.openError(
            shell,
            IDEWorkbenchMessages.IDEApplication_workspaceEmptyTitle,
            IDEWorkbenchMessages.IDEApplication_workspaceEmptyMessage);
        continue;
      }

      // create the workspace if it does not already exist
      File workspace = new File(instancePath);
      if (!workspace.exists()) {
        workspace.mkdir();
      }

      try {
        // Don't use File.toURL() since it adds a leading slash that Platform does not
        // handle properly. See bug 54081 for more details.
        String path = workspace.getAbsolutePath().replace(File.separatorChar, '/');
        url = new URL("file", null, path); // $NON-NLS-1$
      } catch (MalformedURLException e) {
        MessageDialog.openError(
            shell,
            IDEWorkbenchMessages.IDEApplication_workspaceInvalidTitle,
            IDEWorkbenchMessages.IDEApplication_workspaceInvalidMessage);
        continue;
      }
    } while (!checkValidWorkspace(shell, url));

    return url;
  }
Beispiel #4
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);
 }