Esempio n. 1
0
  /** Opens the given editor on the selected file revision. */
  protected void openEditor(IEditorDescriptor editorDescriptor, boolean openUsingDescriptor) {
    IFileRevision fileRevision = getFileRevision();
    if (fileRevision == null) {
      return;
    }
    try {
      IProgressMonitor monitor = new NullProgressMonitor();
      IStorage storage = fileRevision.getStorage(monitor);
      boolean isFile = storage instanceof IFile;

      if (openUsingDescriptor) {
        // discouraged access to open system editors
        ((WorkbenchPage) (page.getSite().getPage()))
            .openEditorFromDescriptor(
                isFile
                    ? new FileEditorInput((IFile) storage)
                    : (IEditorInput)
                        FileRevisionEditorInput.createEditorInputFor(fileRevision, monitor),
                editorDescriptor,
                true,
                null);
      } else {
        String editorId =
            editorDescriptor == null
                ? IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID
                : editorDescriptor.getId();
        page.getSite()
            .getPage()
            .openEditor(
                isFile
                    ? new FileEditorInput((IFile) storage)
                    : (IEditorInput)
                        FileRevisionEditorInput.createEditorInputFor(fileRevision, monitor),
                editorId,
                true,
                MATCH_BOTH);
      }
    } catch (PartInitException e) {
      StatusAdapter statusAdapter = new StatusAdapter(e.getStatus());
      statusAdapter.setProperty(
          IStatusAdapterConstants.TITLE_PROPERTY, TeamUIMessages.LocalHistoryPage_OpenEditorError);
      StatusManager.getManager().handle(statusAdapter, StatusManager.SHOW);
    } catch (CoreException e) {
      StatusAdapter statusAdapter = new StatusAdapter(e.getStatus());
      statusAdapter.setProperty(
          IStatusAdapterConstants.TITLE_PROPERTY, TeamUIMessages.LocalHistoryPage_OpenEditorError);
      StatusManager.getManager().handle(statusAdapter, StatusManager.LOG);
    }
  }
  protected void setStatusAdapter(StatusAdapter adapter) {
    list.removeAll();
    populateList(list, adapter.getStatus(), 0);
    if (workbenchStatusDialog.getStatusAdapters().size() == 1) {
      Long timestamp = (Long) adapter.getProperty(IStatusAdapterConstants.TIMESTAMP_PROPERTY);

      if (timestamp != null) {
        String date =
            DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG)
                .format(new Date(timestamp.longValue()));
        list.add(
            NLS.bind(ProgressMessages.JobInfo_Error, (new Object[] {"", date}))); // $NON-NLS-1$
      }
    }
  }
  protected IStatus createAndRefreshProject(
      boolean isBlocking, boolean revealProject, IProgressMonitor monitor) {
    IStatus resultStatus = Status.CANCEL_STATUS;
    try {
      if (isBlocking) {
        getContainer()
            .run(
                true,
                true,
                new IRunnableWithProgress() {

                  public void run(IProgressMonitor monitor)
                      throws InvocationTargetException, InterruptedException {
                    doCreateProject(monitor);
                  }
                });
      } else {
        doCreateProject(monitor);
      }
      resultStatus = Status.OK_STATUS;
    } catch (InterruptedException e) {
      // StatusManager.getManager().handle(new Status(IStatus.ERROR, ProjectsPlugin.PLUGIN_ID,
      // e.getMessage(), e),
      // StatusManager.BLOCK);
    } catch (InvocationTargetException e) {
      Throwable t = e.getTargetException();
      if (t instanceof ExecutionException && t.getCause() instanceof CoreException) {
        CoreException cause = (CoreException) t.getCause();
        resultStatus = cause.getStatus();
        StatusAdapter status;
        if (cause.getStatus().getCode() == IResourceStatus.CASE_VARIANT_EXISTS) {
          status =
              new StatusAdapter(
                  new Status(
                      IStatus.WARNING,
                      ProjectsPlugin.PLUGIN_ID,
                      NLS.bind(
                          Messages.NewProjectWizard_Warning_DirectoryExists,
                          mainPage.getProjectHandle().getName()),
                      cause));
        } else {
          status =
              new StatusAdapter(
                  new Status(
                      cause.getStatus().getSeverity(),
                      ProjectsPlugin.PLUGIN_ID,
                      Messages.NewProjectWizard_CreationProblem,
                      cause));
        }
        status.setProperty(
            IStatusAdapterConstants.TITLE_PROPERTY, Messages.NewProjectWizard_CreationProblem);
        StatusManager.getManager().handle(status, StatusManager.BLOCK);
      } else {
        resultStatus =
            new Status(
                IStatus.WARNING,
                ProjectsPlugin.PLUGIN_ID,
                0,
                NLS.bind(Messages.NewProjectWizard_InternalError, t.getMessage()),
                t);
        StatusAdapter status = new StatusAdapter(resultStatus);
        status.setProperty(
            IStatusAdapterConstants.TITLE_PROPERTY, Messages.NewProjectWizard_CreationProblem);
        StatusManager.getManager().handle(status, StatusManager.LOG | StatusManager.BLOCK);
      }
    }

    if (!resultStatus.isOK()) {
      return resultStatus;
    }

    if (revealProject) {
      // TODO Run all of this in a job?
      updatePerspective();
      selectAndReveal(newProject);
    }
    openIndexFile(revealProject);
    sendProjectCreateEvent();

    return resultStatus;
  }
  /**
   * Creates a new project resource.
   *
   * @param name the project name
   * @param newProjectHandle the project handle
   * @param projectType the type of project
   * @param location the location
   * @param runnableContext a context for executing the creation operation
   * @param shell the shell (for UI context)
   * @return the created project resource, or <code>null</code> if the project was not created
   */
  public static IProject createNewProject(
      String name,
      final IProject newProjectHandle,
      final ProjectType projectType,
      URI location,
      final IRunnableContext runnableContext,
      final Shell shell) {

    final IProjectDescription description = createProjectDescription(newProjectHandle, location);

    // create the new project operation
    IRunnableWithProgress op =
        new IRunnableWithProgress() {
          @Override
          public void run(IProgressMonitor monitor) throws InvocationTargetException {
            CreateProjectOperation op =
                new CreateProjectOperation(description, ResourceMessages.NewProject_windowTitle);
            try {
              IStatus status = op.execute(monitor, WorkspaceUndoUtil.getUIInfoAdapter(shell));

              if (status.isOK() && projectType != ProjectType.NONE) {
                createProjectContent(newProjectHandle, projectType);
              }
            } catch (ExecutionException e) {
              throw new InvocationTargetException(e);
            } catch (CoreException e) {
              throw new InvocationTargetException(e);
            }
          }
        };

    try {
      runnableContext.run(true, true, op);
    } catch (InterruptedException e) {
      return null;
    } catch (InvocationTargetException e) {
      Throwable t = e.getTargetException();
      if (t instanceof ExecutionException && t.getCause() instanceof CoreException) {
        CoreException cause = (CoreException) t.getCause();
        StatusAdapter status;
        if (cause.getStatus().getCode() == IResourceStatus.CASE_VARIANT_EXISTS) {
          status =
              new StatusAdapter(
                  StatusUtil.newStatus(
                      IStatus.WARNING,
                      NLS.bind(
                          ResourceMessages.NewProject_caseVariantExistsError,
                          newProjectHandle.getName()),
                      cause));
        } else {
          status =
              new StatusAdapter(
                  StatusUtil.newStatus(
                      cause.getStatus().getSeverity(),
                      ResourceMessages.NewProject_errorMessage,
                      cause));
        }
        status.setProperty(
            IStatusAdapterConstants.TITLE_PROPERTY, ResourceMessages.NewProject_errorMessage);
        StatusManager.getManager().handle(status, StatusManager.BLOCK);
      } else {
        StatusAdapter status =
            new StatusAdapter(
                new Status(
                    IStatus.WARNING,
                    IDEWorkbenchPlugin.IDE_WORKBENCH,
                    0,
                    NLS.bind(ResourceMessages.NewProject_internalError, t.getMessage()),
                    t));
        status.setProperty(
            IStatusAdapterConstants.TITLE_PROPERTY, ResourceMessages.NewProject_errorMessage);
        StatusManager.getManager().handle(status, StatusManager.LOG | StatusManager.BLOCK);
      }
      return null;
    }
    try {
      IProjectUtilities.configurePackagesFilter(newProjectHandle);
    } catch (CoreException e) {
      DartCore.logError("Could not set package filter on folder " + newProjectHandle.getName(), e);
    }

    return newProjectHandle;
  }
  /**
   * Creates a new project resource with the selected name.
   *
   * <p>In normal usage, this method is invoked after the user has pressed Finish on the wizard; the
   * enablement of the Finish button implies that all controls on the pages currently contain valid
   * values.
   *
   * <p>Note that this wizard caches the new project once it has been successfully created;
   * subsequent invocations of this method will answer the same project resource without attempting
   * to create it again.
   *
   * @return the created project resource, or <code>null</code> if the project was not created
   */
  private IProject createNewProject() {
    if (newProject != null) {
      return newProject;
    }

    // get a project handle
    final String projectName = page.getProjectName();
    IProject projectHandle = page.getProjectHandle(projectName);
    if (projectHandle.exists()) {
      String newProjectName = ProjectUtils.generateUniqueNameFrom(projectName);
      projectHandle = page.getProjectHandle(newProjectName);
    }
    final AbstractSample sampleContent = page.getCurrentSample();
    final IProject newProjectHandle = projectHandle;
    // get a project descriptor
    URI location = page.getLocationURI();

    final IProjectDescription description = createProjectDescription(newProjectHandle, location);

    // create the new project operation
    IRunnableWithProgress op =
        new IRunnableWithProgress() {
          @Override
          public void run(IProgressMonitor monitor) throws InvocationTargetException {
            CreateProjectOperation op =
                new CreateProjectOperation(description, ResourceMessages.NewProject_windowTitle);
            try {
              IStatus status = op.execute(monitor, WorkspaceUndoUtil.getUIInfoAdapter(getShell()));

              if (status.isOK()) {
                createdFile =
                    createProjectContent(newProjectHandle, null, projectName, sampleContent);
              }
            } catch (ExecutionException e) {
              throw new InvocationTargetException(e);
            } catch (CoreException e) {
              throw new InvocationTargetException(e);
            }
          }
        };

    try {
      getContainer().run(true, true, op);
    } catch (InterruptedException e) {
      return null;
    } catch (InvocationTargetException e) {
      Throwable t = e.getTargetException();
      if (t instanceof ExecutionException && t.getCause() instanceof CoreException) {
        CoreException cause = (CoreException) t.getCause();
        StatusAdapter status;
        if (cause.getStatus().getCode() == IResourceStatus.CASE_VARIANT_EXISTS) {
          status =
              new StatusAdapter(
                  StatusUtil.newStatus(
                      IStatus.WARNING,
                      NLS.bind(
                          ResourceMessages.NewProject_caseVariantExistsError,
                          newProjectHandle.getName()),
                      cause));
        } else {
          status =
              new StatusAdapter(
                  StatusUtil.newStatus(
                      cause.getStatus().getSeverity(),
                      ResourceMessages.NewProject_errorMessage,
                      cause));
        }
        status.setProperty(
            IStatusAdapterConstants.TITLE_PROPERTY, ResourceMessages.NewProject_errorMessage);
        StatusManager.getManager().handle(status, StatusManager.BLOCK);
      } else {
        StatusAdapter status =
            new StatusAdapter(
                new Status(
                    IStatus.WARNING,
                    IDEWorkbenchPlugin.IDE_WORKBENCH,
                    0,
                    NLS.bind(ResourceMessages.NewProject_internalError, t.getMessage()),
                    t));
        status.setProperty(
            IStatusAdapterConstants.TITLE_PROPERTY, ResourceMessages.NewProject_errorMessage);
        StatusManager.getManager().handle(status, StatusManager.LOG | StatusManager.BLOCK);
      }
      return null;
    }

    newProject = newProjectHandle;

    return newProject;
  }
  private void doBasicCreateProject(IProject project, final IProjectDescription description)
      throws CoreException {
    // create the new project operation
    IRunnableWithProgress op =
        new IRunnableWithProgress() {
          public void run(IProgressMonitor monitor) throws InvocationTargetException {
            CreateProjectOperation op =
                new CreateProjectOperation(
                    description, Messages.NewSampleProjectWizard_CreateOp_Title);
            try {
              // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=219901
              // directly execute the operation so that the undo state is
              // not preserved. Making this undoable resulted in too many
              // accidental file deletions.
              op.execute(monitor, WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
            } catch (ExecutionException e) {
              throw new InvocationTargetException(e);
            }
          }
        };

    // run the new project creation operation
    try {
      getContainer().run(true, true, op);
    } catch (InterruptedException e) {
      throw new CoreException(
          new Status(IStatus.ERROR, SamplesUIPlugin.PLUGIN_ID, e.getMessage(), e));
    } catch (InvocationTargetException e) {
      Throwable t = e.getTargetException();
      if (t instanceof ExecutionException && t.getCause() instanceof CoreException) {
        CoreException cause = (CoreException) t.getCause();
        StatusAdapter status;
        if (cause.getStatus().getCode() == IResourceStatus.CASE_VARIANT_EXISTS) {
          status =
              new StatusAdapter(
                  new Status(
                      IStatus.WARNING,
                      SamplesUIPlugin.PLUGIN_ID,
                      MessageFormat.format(
                          Messages.NewSampleProjectWizard_Warning_DirectoryExists,
                          project.getName()),
                      cause));
        } else {
          status =
              new StatusAdapter(
                  new Status(
                      cause.getStatus().getSeverity(),
                      SamplesUIPlugin.PLUGIN_ID,
                      Messages.NewSampleProjectWizard_CreationProblems,
                      cause));
        }
        status.setProperty(
            IStatusAdapterConstants.TITLE_PROPERTY,
            Messages.NewSampleProjectWizard_CreationProblems);
        StatusManager.getManager().handle(status, StatusManager.BLOCK);
      } else {
        StatusAdapter status =
            new StatusAdapter(
                new Status(
                    IStatus.WARNING,
                    SamplesUIPlugin.PLUGIN_ID,
                    0,
                    MessageFormat.format(
                        Messages.NewSampleProjectWizard_InternalError, t.getMessage()),
                    t));
        status.setProperty(
            IStatusAdapterConstants.TITLE_PROPERTY,
            Messages.NewSampleProjectWizard_CreationProblems);
        StatusManager.getManager().handle(status, StatusManager.LOG | StatusManager.BLOCK);
      }
    }
  }
Esempio n. 7
0
  /* (non-Javadoc)
   * @see org.eclipse.jface.wizard.IWizardNode#getWizard()
   */
  public IWizard getWizard() {
    if (wizard != null) {
      return wizard; // we've already created it
    }

    final IWorkbenchWizard[] workbenchWizard = new IWorkbenchWizard[1];
    final IStatus statuses[] = new IStatus[1];
    // Start busy indicator.
    BusyIndicator.showWhile(
        parentWizardPage.getShell().getDisplay(),
        new Runnable() {
          public void run() {
            SafeRunner.run(
                new SafeRunnable() {
                  /** Add the exception details to status is one happens. */
                  public void handleException(Throwable e) {
                    IPluginContribution contribution =
                        (IPluginContribution)
                            Util.getAdapter(wizardElement, IPluginContribution.class);
                    statuses[0] =
                        new Status(
                            IStatus.ERROR,
                            contribution != null
                                ? contribution.getPluginId()
                                : WorkbenchPlugin.PI_WORKBENCH,
                            IStatus.OK,
                            WorkbenchMessages.get().WorkbenchWizard_errorMessage,
                            e);
                  }

                  public void run() {
                    try {
                      workbenchWizard[0] = createWizard();
                      // create instance of target wizard
                    } catch (CoreException e) {
                      IPluginContribution contribution =
                          (IPluginContribution)
                              Util.getAdapter(wizardElement, IPluginContribution.class);
                      statuses[0] =
                          new Status(
                              IStatus.ERROR,
                              contribution != null
                                  ? contribution.getPluginId()
                                  : WorkbenchPlugin.PI_WORKBENCH,
                              IStatus.OK,
                              WorkbenchMessages.get().WorkbenchWizard_errorMessage,
                              e);
                    }
                  }
                });
          }
        });

    if (statuses[0] != null) {
      parentWizardPage.setErrorMessage(WorkbenchMessages.get().WorkbenchWizard_errorMessage);
      StatusAdapter statusAdapter = new StatusAdapter(statuses[0]);
      statusAdapter.addAdapter(Shell.class, parentWizardPage.getShell());
      statusAdapter.setProperty(
          StatusAdapter.TITLE_PROPERTY, WorkbenchMessages.get().WorkbenchWizard_errorTitle);
      StatusManager.getManager().handle(statusAdapter, StatusManager.SHOW);
      return null;
    }

    IStructuredSelection currentSelection = getCurrentResourceSelection();

    // Get the adapted version of the selection that works for the
    // wizard node
    currentSelection = wizardElement.adaptedSelection(currentSelection);

    workbenchWizard[0].init(getWorkbench(), currentSelection);

    wizard = workbenchWizard[0];
    return wizard;
  }