From f1ff7ea556f3a67e5cd7bf549d435fb5468308af Mon Sep 17 00:00:00 2001 From: Jean Michel-Lemieux Date: Mon, 3 May 2004 15:53:15 +0000 Subject: Allow synchronous refresh. --- .../ui/synchronize/RefreshSubscriberJob.java | 126 +++++++++++++++++---- .../synchronize/RefreshUserNotificationPolicy.java | 14 +-- .../ui/synchronize/SubscriberRefreshSchedule.java | 8 +- .../team/ui/synchronize/SubscriberParticipant.java | 120 ++++---------------- 4 files changed, 141 insertions(+), 127 deletions(-) diff --git a/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/RefreshSubscriberJob.java b/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/RefreshSubscriberJob.java index f5529fbd9..20cdd7bf9 100644 --- a/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/RefreshSubscriberJob.java +++ b/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/RefreshSubscriberJob.java @@ -16,6 +16,9 @@ import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.WorkspaceJob; import org.eclipse.core.runtime.*; import org.eclipse.core.runtime.jobs.*; +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.util.IPropertyChangeListener; +import org.eclipse.jface.util.PropertyChangeEvent; import org.eclipse.team.core.TeamException; import org.eclipse.team.core.subscribers.Subscriber; import org.eclipse.team.core.synchronize.*; @@ -25,7 +28,11 @@ import org.eclipse.team.internal.core.subscribers.SubscriberSyncInfoCollector; import org.eclipse.team.internal.ui.Policy; import org.eclipse.team.internal.ui.TeamUIPlugin; import org.eclipse.team.ui.synchronize.ISynchronizeManager; +import org.eclipse.team.ui.synchronize.SubscriberParticipant; +import org.eclipse.ui.actions.ActionFactory; +import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction; import org.eclipse.ui.internal.progress.ProgressManager; +import org.eclipse.ui.progress.UIJob; /** * Job to refresh a {@link Subscriber} in the background. The job can be configured @@ -59,7 +66,8 @@ public final class RefreshSubscriberJob extends WorkspaceJob { * The subscribers and resources to refresh. */ private IResource[] resources; - private Subscriber subscriber; + + private SubscriberParticipant participant; /** * Refresh started/completed listener for every refresh @@ -67,8 +75,6 @@ public final class RefreshSubscriberJob extends WorkspaceJob { private static List listeners = new ArrayList(1); private static final int STARTED = 1; private static final int DONE = 2; - - private SubscriberSyncInfoCollector collector; /** * Notification for safely notifying listeners of refresh lifecycle. @@ -98,12 +104,13 @@ public final class RefreshSubscriberJob extends WorkspaceJob { * @param resources * @param subscriber */ - public RefreshSubscriberJob(String name, IResource[] resources, Subscriber subscriber) { + public RefreshSubscriberJob(SubscriberParticipant participant, String name, IResource[] resources, IRefreshSubscriberListener listener) { super(name); Assert.isNotNull(resources); - Assert.isNotNull(subscriber); + Assert.isNotNull(participant); + Assert.isNotNull(resources); this.resources = resources; - this.subscriber = subscriber; + this.participant = participant; setPriority(Job.DECORATE); setRefreshInterval(3600 /* 1 hour */); @@ -119,10 +126,8 @@ public final class RefreshSubscriberJob extends WorkspaceJob { } } }); - } - - public void setSubscriberCollector(SubscriberSyncInfoCollector collector) { - this.collector = collector; + + initialize(listener); } /** @@ -165,7 +170,7 @@ public final class RefreshSubscriberJob extends WorkspaceJob { if(subscriber == null || roots == null) { return Status.OK_STATUS; } - + SubscriberSyncInfoCollector collector = getCollector(); RefreshEvent event = new RefreshEvent(reschedule ? IRefreshEvent.SCHEDULED_REFRESH : IRefreshEvent.USER_REFRESH, roots, collector.getSubscriber()); RefreshChangeListener changeListener = new RefreshChangeListener(collector); try { @@ -194,10 +199,8 @@ public final class RefreshSubscriberJob extends WorkspaceJob { monitor.done(); } - Boolean modelProperty = (Boolean)getProperty(ProgressManager.PROPERTY_IN_DIALOG); - boolean isModal = modelProperty == null ? true : false; - setProperty(new QualifiedName("org.eclipse.ui.workbench.progress", "keep"), Boolean.valueOf(! isModal)); - setProperty(new QualifiedName("org.eclipse.ui.workbench.progress", "keepone"), Boolean.valueOf(! isModal)); + setProperty(new QualifiedName("org.eclipse.ui.workbench.progress", "keep"), Boolean.valueOf(! isJobModal())); + setProperty(new QualifiedName("org.eclipse.ui.workbench.progress", "keepone"), Boolean.valueOf(! isJobModal())); // Post-Notify event.setChanges(changeListener.getChanges()); @@ -214,6 +217,7 @@ public final class RefreshSubscriberJob extends WorkspaceJob { int code = IStatus.OK; SyncInfo[] changes = event.getChanges(); IResource[] resources = event.getResources(); + SubscriberSyncInfoCollector collector = getCollector(); if (collector != null) { SyncInfoSet set = collector.getSyncInfoSet(); int numChanges = refreshedResourcesContainChanges(event); @@ -225,7 +229,7 @@ public final class RefreshSubscriberJob extends WorkspaceJob { if (changes.length > 0) { // New changes found String numNewChanges = Integer.toString(event.getChanges().length); - text.append(Policy.bind("RefreshCompleteDialog.5a", new Object[]{numNewChanges, subscriber.getName(), outgoing, incoming, conflicting})); //$NON-NLS-1$ + text.append(Policy.bind("RefreshCompleteDialog.5a", new Object[]{numNewChanges, participant.getName(), outgoing, incoming, conflicting})); //$NON-NLS-1$ } else { // Refreshed resources contain changes text.append(Policy.bind("RefreshCompleteDialog.5", new Object[]{new Integer(numChanges), outgoing, incoming, conflicting})); //$NON-NLS-1$ @@ -242,6 +246,7 @@ public final class RefreshSubscriberJob extends WorkspaceJob { private int refreshedResourcesContainChanges(IRefreshEvent event) { int numChanges = 0; + SubscriberSyncInfoCollector collector = getCollector(); if (collector != null) { SyncInfoTree set = collector.getSyncInfoSet(); IResource[] resources = event.getResources(); @@ -256,16 +261,94 @@ public final class RefreshSubscriberJob extends WorkspaceJob { return numChanges; } + private void initialize(final IRefreshSubscriberListener listener) { + final IWorkbenchAction[] gotoAction = new IWorkbenchAction[] {null}; + IProgressMonitor group = Platform.getJobManager().createProgressGroup(); + group.beginTask(participant.getName(), 100); + setProgressGroup(group, 80); + getCollector().setProgressGroup(group, 20); + setProperty(new QualifiedName("org.eclipse.ui.workbench.progress", "icon"), participant.getImageDescriptor()); + setProperty(new QualifiedName("org.eclipse.ui.workbench.progress", "goto"), new WorkbenchAction() { + public void run() { + if(gotoAction[0] != null) { + gotoAction[0].run(); + } + } + public boolean isEnabled() { + if(gotoAction[0] != null) { + return gotoAction[0].isEnabled(); + } + return false; + } + + public void dispose() { + super.dispose(); + if(gotoAction[0] != null) { + gotoAction[0].dispose(); + } + } + }); + // Listener delagate + IRefreshSubscriberListener autoListener = new IRefreshSubscriberListener() { + public void refreshStarted(IRefreshEvent event) { + if(listener != null) { + listener.refreshStarted(event); + } + } + public ActionFactory.IWorkbenchAction refreshDone(IRefreshEvent event) { + if(listener != null) { + boolean isModal = isJobModal(); + ActionFactory.IWorkbenchAction runnable = listener.refreshDone(event); + if(runnable != null) { + // If the job is being run modally then simply prompt the user immediatly + if(isModal) { + if(runnable != null) { + final IAction[] r = new IAction[] {runnable}; + Job update = new UIJob("") { + public IStatus runInUIThread(IProgressMonitor monitor) { + r[0].run(); + return Status.OK_STATUS; + } + }; + update.setSystem(true); + update.schedule(); + } + // If the job is being run in the background, don't interrupt the user and simply update the goto action + // to perform the results. + } else { + gotoAction[0] = runnable; + gotoAction[0].setEnabled(runnable.isEnabled()); + runnable.addPropertyChangeListener(new IPropertyChangeListener() { + public void propertyChange(PropertyChangeEvent event) { + if(event.getProperty().equals(IAction.ENABLED)) { + Boolean bool = (Boolean) event.getNewValue(); + gotoAction[0].setEnabled(bool.booleanValue()); + } + } + }); + } + } + RefreshSubscriberJob.removeRefreshListener(this); + } + return null; + } + }; + + if (listener != null) { + RefreshSubscriberJob.addRefreshListener(autoListener); + } + } + protected IResource[] getResources() { return resources; } protected Subscriber getSubscriber() { - return subscriber; + return participant.getSubscriber(); } protected SubscriberSyncInfoCollector getCollector() { - return collector; + return participant.getSubscriberSyncInfoCollector(); } public long getScheduleDelay() { @@ -275,7 +358,6 @@ public final class RefreshSubscriberJob extends WorkspaceJob { protected void start() { if(getState() == Job.NONE) { if(shouldReschedule()) { - setUser(collector != null); schedule(getScheduleDelay()); } } @@ -357,4 +439,10 @@ public final class RefreshSubscriberJob extends WorkspaceJob { notification.run(listener); } } + + private boolean isJobModal() { + Boolean isModal = (Boolean)getProperty(ProgressManager.PROPERTY_IN_DIALOG); + if(isModal == null) return false; + return isModal.booleanValue(); + } } \ No newline at end of file diff --git a/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/RefreshUserNotificationPolicy.java b/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/RefreshUserNotificationPolicy.java index b15bb23db..f1107cf34 100644 --- a/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/RefreshUserNotificationPolicy.java +++ b/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/RefreshUserNotificationPolicy.java @@ -4,17 +4,15 @@ import java.util.*; import org.eclipse.compare.CompareUI; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.Status; -import org.eclipse.swt.widgets.Shell; +import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.team.core.synchronize.SyncInfo; -import org.eclipse.team.internal.ui.TeamUIPlugin; +import org.eclipse.team.internal.ui.*; import org.eclipse.team.ui.TeamUI; import org.eclipse.team.ui.synchronize.*; import org.eclipse.ui.actions.ActionFactory; /** * This class manages the notification and setup that occurs after a refresh is completed. - * - * */ public class RefreshUserNotificationPolicy implements IRefreshSubscriberListener { @@ -90,9 +88,11 @@ public class RefreshUserNotificationPolicy implements IRefreshSubscriberListener private void notifyIfNeededModal(final IRefreshEvent event) { TeamUIPlugin.getStandardDisplay().asyncExec(new Runnable() { public void run() { - RefreshCompleteDialog d = new RefreshCompleteDialog(new Shell(TeamUIPlugin.getStandardDisplay()), event, participant); - d.setBlockOnOpen(false); - d.open(); + String title = (event.getRefreshType() == IRefreshEvent.SCHEDULED_REFRESH ? + Policy.bind("RefreshCompleteDialog.4a", participant.getName()) : //$NON-NLS-1$ + Policy.bind("RefreshCompleteDialog.4", participant.getName()) //$NON-NLS-1$ + ); + MessageDialog.openInformation(Utils.getShell(null), title, event.getStatus().getMessage()); } }); } diff --git a/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/SubscriberRefreshSchedule.java b/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/SubscriberRefreshSchedule.java index cc9b76dc1..dc41165d1 100644 --- a/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/SubscriberRefreshSchedule.java +++ b/bundles/org.eclipse.team.ui/src/org/eclipse/team/internal/ui/synchronize/SubscriberRefreshSchedule.java @@ -3,7 +3,6 @@ package org.eclipse.team.internal.ui.synchronize; import java.text.DateFormat; import java.util.Date; import org.eclipse.core.runtime.jobs.Job; -import org.eclipse.team.core.subscribers.Subscriber; import org.eclipse.team.core.synchronize.SyncInfo; import org.eclipse.team.core.synchronize.SyncInfoSet; import org.eclipse.team.internal.ui.Policy; @@ -112,16 +111,15 @@ public class SubscriberRefreshSchedule { return; } if(job == null) { - Subscriber s = participant.getSubscriber(); - job = new RefreshSubscriberJob(Policy.bind("RefreshSchedule.14", participant.getName(), getRefreshIntervalAsString()),s.roots(), s); //$NON-NLS-1$ - job.setSubscriberCollector(participant.getSubscriberSyncInfoCollector()); + SubscriberParticipant participant = getParticipant(); + job = new RefreshSubscriberJob(participant, Policy.bind("RefreshSchedule.14", participant.getName(), getRefreshIntervalAsString()), participant.getResources(), new RefreshUserNotificationPolicy(getParticipant())); //$NON-NLS-1$ job.setUser(false); } else if(job.getState() != Job.NONE){ stopJob(); } job.setRestartOnCancel(true); job.setReschedule(true); - job.schedule(getRefreshInterval()); + job.schedule(getRefreshInterval()); } protected void stopJob() { diff --git a/bundles/org.eclipse.team.ui/src/org/eclipse/team/ui/synchronize/SubscriberParticipant.java b/bundles/org.eclipse.team.ui/src/org/eclipse/team/ui/synchronize/SubscriberParticipant.java index d1c35035c..768ff96a1 100644 --- a/bundles/org.eclipse.team.ui/src/org/eclipse/team/ui/synchronize/SubscriberParticipant.java +++ b/bundles/org.eclipse.team.ui/src/org/eclipse/team/ui/synchronize/SubscriberParticipant.java @@ -11,9 +11,8 @@ package org.eclipse.team.ui.synchronize; import org.eclipse.core.resources.IResource; -import org.eclipse.core.runtime.*; -import org.eclipse.core.runtime.jobs.Job; -import org.eclipse.jface.action.IAction; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; import org.eclipse.jface.util.IPropertyChangeListener; import org.eclipse.jface.util.PropertyChangeEvent; import org.eclipse.jface.wizard.IWizard; @@ -27,11 +26,7 @@ import org.eclipse.team.internal.ui.Utils; import org.eclipse.team.internal.ui.synchronize.*; import org.eclipse.team.ui.TeamUI; import org.eclipse.ui.*; -import org.eclipse.ui.actions.ActionFactory; -import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction; -import org.eclipse.ui.internal.progress.ProgressManager; import org.eclipse.ui.part.IPageBookViewPage; -import org.eclipse.ui.progress.UIJob; /** * A synchronize participant that displays synchronization information for local resources that are @@ -112,17 +107,25 @@ public abstract class SubscriberParticipant extends AbstractSynchronizeParticipa */ public final void refreshInDialog(Shell shell, IResource[] resources, String taskName, ISynchronizePageConfiguration configuration, IWorkbenchSite site) { IRefreshSubscriberListener listener = new RefreshUserNotificationPolicyInModalDialog(shell, configuration, this); - internalRefresh(resources, listener, taskName, site); + internalRefresh(resources, taskName, site, listener); } - + /** - * Will refresh a participant in the background. + * Refresh a participant in the background the result of the refresh are shown in the progress view. * * @param resources the resources to be refreshed. */ public final void refresh(IResource[] resources, String taskName, IWorkbenchSite site) { IRefreshSubscriberListener listener = new RefreshUserNotificationPolicy(this); - internalRefresh(resources, listener, taskName, site); + internalRefresh(resources, taskName, site, listener); + } + + /** + * Refresh a participant. The returned status describes the result of the refresh. + */ + public final IStatus refreshNow(IResource[] resources, String taskName, IProgressMonitor monitor) { + RefreshSubscriberJob job = new RefreshSubscriberJob(this, taskName, resources, null); + return job.runInWorkspace(monitor); } /* (non-Javadoc) @@ -278,92 +281,17 @@ public abstract class SubscriberParticipant extends AbstractSynchronizeParticipa collector.setFilter(filter); } - private void internalRefresh(IResource[] resources, final IRefreshSubscriberListener listener, String taskName, IWorkbenchSite site) { - final IWorkbenchAction[] gotoAction = new IWorkbenchAction[] {null}; - final RefreshSubscriberJob job = new RefreshSubscriberJob(taskName, resources, collector.getSubscriber()); - - IProgressMonitor group = Platform.getJobManager().createProgressGroup(); - group.beginTask(taskName + " " + getName(), 100); - job.setProgressGroup(group, 80); - collector.setProgressGroup(group, 20); + /** + * Create and schedule a subscriber refresh job. + * + * @param resources resources to be synchronized + * @param taskName the task name to be shown to the user + * @param site the site in which to run the refresh + * @param listener the listener to handle the refresh workflow + */ + private void internalRefresh(IResource[] resources, String taskName, IWorkbenchSite site, IRefreshSubscriberListener listener) { + RefreshSubscriberJob job = new RefreshSubscriberJob(this, taskName, resources, listener); job.setUser(true); - job.setSubscriberCollector(collector); - job.setProperty(new QualifiedName("org.eclipse.ui.workbench.progress", "icon"), getImageDescriptor()); - job.setProperty(new QualifiedName("org.eclipse.ui.workbench.progress", "goto"), new WorkbenchAction() { - public void run() { - if(gotoAction[0] != null) { - gotoAction[0].run(); - } - } - public boolean isEnabled() { - if(gotoAction[0] != null) { - return gotoAction[0].isEnabled(); - } - return false; - } - - public void dispose() { - super.dispose(); - if(gotoAction[0] != null) { - gotoAction[0].dispose(); - } - } - }); - // Listener delagate - IRefreshSubscriberListener autoListener = new IRefreshSubscriberListener() { - public void refreshStarted(IRefreshEvent event) { - if(listener != null) { - listener.refreshStarted(event); - } - } - public ActionFactory.IWorkbenchAction refreshDone(IRefreshEvent event) { - if(listener != null) { - // Update the progress properties. Only keep the synchronize if the operation is non-modal. - Boolean modelProperty = (Boolean)job.getProperty(ProgressManager.PROPERTY_IN_DIALOG); - boolean isModal = true; - if(modelProperty != null) { - isModal = modelProperty.booleanValue(); - } - - ActionFactory.IWorkbenchAction runnable = listener.refreshDone(event); - if(runnable != null) { - // If the job is being run modally then simply prompt the user immediatly - if(isModal) { - if(runnable != null) { - final IAction[] r = new IAction[] {runnable}; - Job update = new UIJob("") { - public IStatus runInUIThread(IProgressMonitor monitor) { - r[0].run(); - return Status.OK_STATUS; - } - }; - update.setSystem(true); - update.schedule(); - } - // If the job is being run in the background, don't interrupt the user and simply update the goto action - // to perform the results. - } else { - gotoAction[0] = runnable; - gotoAction[0].setEnabled(runnable.isEnabled()); - runnable.addPropertyChangeListener(new IPropertyChangeListener() { - public void propertyChange(PropertyChangeEvent event) { - if(event.getProperty().equals(IAction.ENABLED)) { - Boolean bool = (Boolean) event.getNewValue(); - gotoAction[0].setEnabled(bool.booleanValue()); - } - } - }); - } - } - RefreshSubscriberJob.removeRefreshListener(this); - } - return null; - } - }; - - if (listener != null) { - RefreshSubscriberJob.addRefreshListener(autoListener); - } Utils.schedule(job, site); } } \ No newline at end of file -- cgit v1.2.3