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);
    }
  }
 public static Set getApplicationNameSet() {
   TreeSet result = new TreeSet();
   IExtension[] extensions =
       MDECore.getDefault()
           .getExtensionsRegistry()
           .findExtensions("org.eclipse.core.runtime.applications", true); // $NON-NLS-1$
   for (int i = 0; i < extensions.length; i++) {
     String id = extensions[i].getUniqueIdentifier();
     IConfigurationElement[] elements = extensions[i].getConfigurationElements();
     if (elements.length != 1) continue;
     String visiblity = elements[0].getAttribute("visible"); // $NON-NLS-1$
     boolean visible = visiblity == null ? true : Boolean.valueOf(visiblity).booleanValue();
     if (id != null && visible) {
       result.add(id);
     }
   }
   result.add("org.eclipse.ui.ide.workbench"); // $NON-NLS-1$
   return result;
 }
  /**
   * 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();
    }
  }
예제 #4
0
 private void processEntryChanges(
     IResourceDelta projectDelta, Map<IProject, Boolean> projectsToSave) {
   // check each resource with user-set encoding to see if it has
   // been moved/deleted or if derived state has been changed
   IProject currentProject = (IProject) projectDelta.getResource();
   Preferences projectRegularPrefs = getPreferences(currentProject, false, false, true);
   Preferences projectDerivedPrefs = getPreferences(currentProject, false, true, true);
   Map<Boolean, String[]> affectedResourcesMap = new HashMap<>();
   try {
     // no regular preferences for this project
     if (projectRegularPrefs == null) affectedResourcesMap.put(Boolean.FALSE, new String[0]);
     else affectedResourcesMap.put(Boolean.FALSE, projectRegularPrefs.keys());
     // no derived preferences for this project
     if (projectDerivedPrefs == null) affectedResourcesMap.put(Boolean.TRUE, new String[0]);
     else affectedResourcesMap.put(Boolean.TRUE, projectDerivedPrefs.keys());
   } catch (BackingStoreException e) {
     // problems with the project scope... we will miss the changes (but will log)
     String message = Messages.resources_readingEncoding;
     Policy.log(
         new ResourceStatus(
             IResourceStatus.FAILED_GETTING_CHARSET, currentProject.getFullPath(), message, e));
     return;
   }
   for (Iterator<Boolean> it = affectedResourcesMap.keySet().iterator(); it.hasNext(); ) {
     Boolean isDerived = it.next();
     String[] affectedResources = affectedResourcesMap.get(isDerived);
     Preferences projectPrefs =
         isDerived.booleanValue() ? projectDerivedPrefs : projectRegularPrefs;
     for (int i = 0; i < affectedResources.length; i++) {
       IResourceDelta memberDelta = projectDelta.findMember(new Path(affectedResources[i]));
       // no changes for the given resource
       if (memberDelta == null) continue;
       if (memberDelta.getKind() == IResourceDelta.REMOVED) {
         boolean shouldDisableCharsetDeltaJobForCurrentProject = false;
         // remove the setting for the original location - save its value though
         String currentValue = projectPrefs.get(affectedResources[i], null);
         projectPrefs.remove(affectedResources[i]);
         if ((memberDelta.getFlags() & IResourceDelta.MOVED_TO) != 0) {
           IPath movedToPath = memberDelta.getMovedToPath();
           IResource resource = workspace.getRoot().findMember(movedToPath);
           if (resource != null) {
             Preferences encodingSettings =
                 getPreferences(
                     resource.getProject(), true, resource.isDerived(IResource.CHECK_ANCESTORS));
             if (currentValue == null || currentValue.trim().length() == 0)
               encodingSettings.remove(getKeyFor(movedToPath));
             else encodingSettings.put(getKeyFor(movedToPath), currentValue);
             IProject targetProject = workspace.getRoot().getProject(movedToPath.segment(0));
             if (targetProject.equals(currentProject))
               // if the file was moved inside the same project disable charset listener
               shouldDisableCharsetDeltaJobForCurrentProject = true;
             else projectsToSave.put(targetProject, Boolean.FALSE);
           }
         }
         projectsToSave.put(
             currentProject, Boolean.valueOf(shouldDisableCharsetDeltaJobForCurrentProject));
       }
     }
     if (moveSettingsIfDerivedChanged(
         projectDelta, currentProject, projectPrefs, affectedResources)) {
       // if settings were moved between preferences files disable charset listener so we don't
       // react to changes made by ourselves
       projectsToSave.put(currentProject, Boolean.TRUE);
     }
   }
 }