private void initialize(final IRefreshSubscriberListener listener) {
    final GotoActionWrapper actionWrapper = new GotoActionWrapper();

    IProgressMonitor group = Job.getJobManager().createProgressGroup();
    group.beginTask(taskName, 100);
    setProgressGroup(group, 80);
    handleProgressGroupSet(group, 20);
    setProperty(IProgressConstants.ICON_PROPERTY, participant.getImageDescriptor());
    setProperty(IProgressConstants.ACTION_PROPERTY, actionWrapper);
    setProperty(IProgressConstants.KEEPONE_PROPERTY, Boolean.valueOf(!isJobModal()));
    // Listener delegate
    IRefreshSubscriberListener autoListener =
        new IRefreshSubscriberListener() {
          @Override
          public void refreshStarted(IRefreshEvent event) {
            if (listener != null) {
              listener.refreshStarted(event);
            }
          }

          @Override
          public ActionFactory.IWorkbenchAction refreshDone(IRefreshEvent event) {
            if (listener != null) {
              boolean isModal = isJobModal();
              event.setIsLink(!isModal);
              final ActionFactory.IWorkbenchAction runnable = listener.refreshDone(event);
              if (runnable != null) {
                // If the job is being run modally then simply prompt the user immediately
                if (isModal) {
                  if (runnable != null) {
                    Job update = new UIJob("") { // $NON-NLS-1$
                          @Override
                          public IStatus runInUIThread(IProgressMonitor monitor) {
                            runnable.run();
                            return Status.OK_STATUS;
                          }
                        };
                    update.setSystem(true);
                    update.schedule();
                  }
                } else {
                  // If the job is being run in the background, don't interrupt the user and simply
                  // update the goto action
                  // to perform the results.
                  actionWrapper.setGotoAction(runnable);
                }
              }
              RefreshParticipantJob.removeRefreshListener(this);
            }
            return null;
          }
        };

    if (listener != null) {
      RefreshParticipantJob.addRefreshListener(autoListener);
    }
  }
 private boolean isJobModal() {
   Boolean isModal = (Boolean) getProperty(IProgressConstants.PROPERTY_IN_DIALOG);
   if (isModal == null) return false;
   return isModal.booleanValue();
 }
  /**
   * This is run by the job scheduler. A list of subscribers will be refreshed, errors will not stop
   * the job and it will continue to refresh the other subscribers.
   */
  @Override
  public IStatus run(IProgressMonitor monitor) {
    // Perform a pre-check for auto-build or manual build jobs
    // when auto-refreshing
    if (shouldReschedule()
        && (isJobInFamilyRunning(ResourcesPlugin.FAMILY_AUTO_BUILD)
            || isJobInFamilyRunning(ResourcesPlugin.FAMILY_MANUAL_BUILD))) {
      return POSTPONED;
    }
    // Only allow one refresh job at a time
    // NOTE: It would be cleaner if this was done by a scheduling
    // rule but at the time of writing, it is not possible due to
    // the scheduling rule containment rules.
    // Acquiring lock to ensure only one refresh job is running at a particular time
    boolean acquired = false;
    try {
      while (!acquired) {
        try {
          acquired = lock.acquire(1000);
        } catch (InterruptedException e1) {
          acquired = false;
        }
        Policy.checkCanceled(monitor);
      }

      IChangeDescription changeDescription = createChangeDescription();
      RefreshEvent event =
          new RefreshEvent(
              reschedule ? IRefreshEvent.SCHEDULED_REFRESH : IRefreshEvent.USER_REFRESH,
              participant,
              changeDescription);
      IStatus status = null;
      NonblockingProgressMonitor wrappedMonitor = null;
      try {
        event.setStartTime(System.currentTimeMillis());
        if (monitor.isCanceled()) {
          return Status.CANCEL_STATUS;
        }
        // Pre-Notify
        notifyListeners(STARTED, event);
        // Perform the refresh
        monitor.setTaskName(getName());
        wrappedMonitor = new NonblockingProgressMonitor(monitor, this);
        doRefresh(changeDescription, wrappedMonitor);
        // Prepare the results
        setProperty(IProgressConstants.KEEPONE_PROPERTY, Boolean.valueOf(!isJobModal()));
      } catch (OperationCanceledException e2) {
        if (monitor.isCanceled()) {
          // The refresh was canceled by the user
          status = Status.CANCEL_STATUS;
        } else {
          // The refresh was canceled due to a blockage or a canceled authentication
          if (wrappedMonitor != null && wrappedMonitor.wasBlocking()) {
            status = POSTPONED;
          } else {
            status = Status.CANCEL_STATUS;
          }
        }
      } catch (CoreException e) {
        // Determine the status to be returned and the GOTO action
        status = e.getStatus();
        if (!isUser()) {
          // Use the GOTO action to show the error and return OK
          Object prop = getProperty(IProgressConstants.ACTION_PROPERTY);
          if (prop instanceof GotoActionWrapper) {
            GotoActionWrapper wrapper = (GotoActionWrapper) prop;
            wrapper.setStatus(e.getStatus());
            status =
                new Status(IStatus.OK, TeamUIPlugin.ID, IStatus.OK, e.getStatus().getMessage(), e);
          }
        }
        if (!isUser() && status.getSeverity() == IStatus.ERROR) {
          // Never prompt for errors on non-user jobs
          setProperty(IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY, Boolean.TRUE);
        }
      } finally {
        event.setStopTime(System.currentTimeMillis());
      }

      // Post-Notify
      if (status == null) {
        status = calculateStatus(event);
      }
      event.setStatus(status);
      notifyListeners(DONE, event);
      if (event.getChangeDescription().getChangeCount() > 0) {
        if (participant instanceof AbstractSynchronizeParticipant) {
          AbstractSynchronizeParticipant asp = (AbstractSynchronizeParticipant) participant;
          asp.firePropertyChange(
              participant, ISynchronizeParticipant.P_CONTENT, null, event.getChangeDescription());
        }
      }
      return event.getStatus();
    } finally {
      if (acquired) lock.release();
      monitor.done();
    }
  }
Esempio n. 4
0
  public boolean isModal() {
    Boolean m = (Boolean) getProperty(IProgressConstants.PROPERTY_IN_DIALOG);

    if (m == null) return false;
    return m.booleanValue();
  }