1 11 package org.eclipse.team.ui.synchronize; 12 13 import java.util.Arrays ; 14 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.core.runtime.jobs.Job; 19 import org.eclipse.jface.util.IPropertyChangeListener; 20 import org.eclipse.jface.util.PropertyChangeEvent; 21 import org.eclipse.jface.viewers.IBasicPropertyConstants; 22 import org.eclipse.osgi.util.NLS; 23 import org.eclipse.swt.widgets.Shell; 24 import org.eclipse.team.core.TeamException; 25 import org.eclipse.team.core.subscribers.Subscriber; 26 import org.eclipse.team.core.synchronize.SyncInfoFilter; 27 import org.eclipse.team.core.synchronize.SyncInfoTree; 28 import org.eclipse.team.internal.core.subscribers.SubscriberSyncInfoCollector; 29 import org.eclipse.team.internal.ui.*; 30 import org.eclipse.team.internal.ui.synchronize.*; 31 import org.eclipse.team.ui.TeamUI; 32 import org.eclipse.ui.*; 33 import org.eclipse.ui.part.IPageBookViewPage; 34 35 53 public abstract class SubscriberParticipant extends AbstractSynchronizeParticipant implements IPropertyChangeListener { 54 55 58 private SubscriberSyncInfoCollector collector; 59 60 63 private SubscriberRefreshSchedule refreshSchedule; 64 65 68 private ISynchronizeScope scope; 69 70 73 private static final String CTX_SUBSCRIBER_PARTICIPANT_SETTINGS = TeamUIPlugin.ID + ".TEAMSUBSRCIBERSETTINGS"; 75 78 private static final String CTX_SUBSCRIBER_SCHEDULE_SETTINGS = TeamUIPlugin.ID + ".TEAMSUBSRCIBER_REFRESHSCHEDULE"; 80 83 public SubscriberParticipant() { 84 refreshSchedule = new SubscriberRefreshSchedule(createRefreshable()); 85 } 86 87 private IRefreshable createRefreshable() { 88 return new IRefreshable() { 89 public RefreshParticipantJob createJob(String interval) { 90 return new RefreshSubscriberParticipantJob(SubscriberParticipant.this, 91 TeamUIMessages.RefreshSchedule_14, 92 NLS.bind(TeamUIMessages.RefreshSchedule_15, new String [] { SubscriberParticipant.this.getName(), interval }), getResources(), 93 new RefreshUserNotificationPolicy(SubscriberParticipant.this)); 94 } 95 public ISynchronizeParticipant getParticipant() { 96 return SubscriberParticipant.this; 97 } 98 public void setRefreshSchedule(SubscriberRefreshSchedule schedule) { 99 SubscriberParticipant.this.setRefreshSchedule(schedule); 100 } 101 public SubscriberRefreshSchedule getRefreshSchedule() { 102 return SubscriberParticipant.this.getRefreshSchedule(); 103 } 104 105 }; 106 } 107 108 114 public SubscriberParticipant(ISynchronizeScope scope) { 115 this(); 116 this.scope = scope; 117 scope.addPropertyChangeListener(this); 118 } 119 120 123 public final IPageBookViewPage createPage(ISynchronizePageConfiguration configuration) { 124 validateConfiguration(configuration); 125 return new SubscriberParticipantPage(configuration, getSubscriberSyncInfoCollector()); 126 } 127 128 135 public IResource[] getResources() { 136 return collector.getRoots(); 137 } 138 139 146 private void setResources(IResource[] roots) { 147 collector.setRoots(roots); 148 } 149 150 160 public final void refreshInDialog(Shell shell, IResource[] resources, String jobName, String taskName, ISynchronizePageConfiguration configuration, IWorkbenchSite site) { 161 IRefreshSubscriberListener listener = new RefreshUserNotificationPolicyInModalDialog(shell, taskName, configuration, this); 162 internalRefresh(resources, jobName, taskName, site, listener); 163 } 164 165 181 public final void refresh(IResource[] resources, String shortTaskName, String longTaskName, IWorkbenchSite site) { 182 IRefreshSubscriberListener listener = new RefreshUserNotificationPolicy(this); 183 internalRefresh(resources, shortTaskName, longTaskName, site, listener); 184 } 185 186 193 public final IStatus refreshNow(IResource[] resources, String taskName, IProgressMonitor monitor) { 194 Job.getJobManager().cancel(this); 195 RefreshParticipantJob job = new RefreshSubscriberParticipantJob(this, taskName, taskName, resources, null); 196 return job.run(monitor); 197 } 198 199 202 public void dispose() { 203 Job.getJobManager().cancel(this); 204 refreshSchedule.dispose(); 205 TeamUI.removePropertyChangeListener(this); 206 collector.dispose(); 207 scope.dispose(); 208 } 209 210 213 public String getName() { 214 String name = super.getName(); 215 return NLS.bind(TeamUIMessages.SubscriberParticipant_namePattern, new String [] { name, scope.getName() }); 216 } 217 218 225 protected final String getShortName() { 226 return super.getName(); 227 } 228 229 236 public SyncInfoTree getSyncInfoSet() { 237 return getSubscriberSyncInfoCollector().getSyncInfoSet(); 238 } 239 240 247 public Subscriber getSubscriber() { 248 if (collector == null) return null; 249 return collector.getSubscriber(); 250 } 251 252 259 public static SubscriberParticipant getMatchingParticipant(String ID, IResource[] resources) { 260 ISynchronizeParticipantReference[] refs = TeamUI.getSynchronizeManager().getSynchronizeParticipants(); 261 for (int i = 0; i < refs.length; i++) { 262 ISynchronizeParticipantReference reference = refs[i]; 263 if(reference.getId().equals(ID)) { 264 SubscriberParticipant p; 265 try { 266 p = (SubscriberParticipant)reference.getParticipant(); 267 } catch (TeamException e) { 268 continue; 269 } 270 IResource[] roots = p.getResources(); 271 Arrays.sort(resources, Utils.resourceComparator); 272 Arrays.sort(roots, Utils.resourceComparator); 273 if (Arrays.equals(resources, roots)) { 274 return p; 275 } 276 } 277 } 278 return null; 279 } 280 281 284 public void propertyChange(PropertyChangeEvent event) { 285 if (event.getProperty().equals(TeamUI.GLOBAL_IGNORES_CHANGED)) { 286 collector.reset(); 287 } 288 if (event.getProperty().equals(ISynchronizeScope.ROOTS)) { 289 setResources(scope.getRoots()); 290 } 291 if (event.getProperty().equals(ISynchronizeScope.NAME)) { 292 firePropertyChange(this, IBasicPropertyConstants.P_TEXT, null, getName()); 295 } 296 } 297 298 301 public void init(String secondaryId, IMemento memento) throws PartInitException { 302 super.init(secondaryId, memento); 303 if(memento != null) { 304 IMemento settings = memento.getChild(CTX_SUBSCRIBER_PARTICIPANT_SETTINGS); 305 if(settings != null) { 306 SubscriberRefreshSchedule schedule = SubscriberRefreshSchedule.init(settings.getChild(CTX_SUBSCRIBER_SCHEDULE_SETTINGS), createRefreshable()); 307 setRefreshSchedule(schedule); 308 this.scope = AbstractSynchronizeScope.createScope(settings); 309 scope.addPropertyChangeListener(this); 310 } 311 } 312 } 313 314 317 public void saveState(IMemento memento) { 318 super.saveState(memento); 319 IMemento settings = memento.createChild(CTX_SUBSCRIBER_PARTICIPANT_SETTINGS); 320 refreshSchedule.saveState(settings.createChild(CTX_SUBSCRIBER_SCHEDULE_SETTINGS)); 321 AbstractSynchronizeScope.saveScope(scope, settings); 322 } 323 324 327 public void reset() { 328 getSubscriberSyncInfoCollector().reset(); 329 } 330 331 337 public SubscriberSyncInfoCollector getSubscriberSyncInfoCollector() { 338 return collector; 339 } 340 341 344 public void setRefreshSchedule(SubscriberRefreshSchedule schedule) { 345 if (refreshSchedule != schedule) { 346 if (refreshSchedule != null) { 347 refreshSchedule.dispose(); 348 } 349 this.refreshSchedule = schedule; 350 } 351 firePropertyChange(this, AbstractSynchronizeParticipant.P_SCHEDULED, schedule, schedule); 353 } 354 355 358 public SubscriberRefreshSchedule getRefreshSchedule() { 359 return refreshSchedule; 360 } 361 362 365 protected void initializeConfiguration(ISynchronizePageConfiguration configuration) { 366 configuration.setProperty(SynchronizePageConfiguration.P_PARTICIPANT_SYNC_INFO_SET, collector.getSyncInfoSet()); 367 } 368 369 372 public void run(IWorkbenchPart part) { 373 refresh(getResources(), null, null, part != null ? part.getSite() : null); 374 } 375 376 383 protected String getShortTaskName() { 384 return TeamUIMessages.Participant_synchronizing; 385 } 386 387 395 protected String getLongTaskName() { 396 return TeamUIMessages.Participant_synchronizing; 397 } 398 399 407 protected String getLongTaskName(IResource[] resources) { 408 int resourceCount = 0; 409 if (getResources().length == resources.length) { 410 ISynchronizeScope scope = getScope(); 413 if (scope instanceof ResourceScope) { 414 resourceCount = scope.getRoots().length; 415 } 416 } else { 417 resourceCount = resources.length; 418 } 419 if (resourceCount == 1) { 420 return NLS.bind(TeamUIMessages.Participant_synchronizingMoreDetails, new String [] { getShortName(), resources[0].getFullPath().toString() }); 421 } else if (resourceCount > 1) { 422 return NLS.bind(TeamUIMessages.Participant_synchronizingResources, new String [] { getShortName(), Integer.toString(resourceCount) }); 423 } 424 return NLS.bind(TeamUIMessages.Participant_synchronizingDetails, new String [] { getName() }); 426 } 427 428 438 protected void validateConfiguration(ISynchronizePageConfiguration configuration) { 439 } 441 442 449 protected void setSubscriber(Subscriber subscriber) { 450 if (scope == null) { 451 scope = new WorkspaceScope(); 452 } 453 collector = new SubscriberSyncInfoCollector(subscriber, scope.getRoots()); 454 455 TeamUI.addPropertyChangeListener(this); 457 458 collector.start(); 460 461 SubscriberRefreshSchedule schedule = getRefreshSchedule(); 463 if(schedule.isEnabled()) { 464 getRefreshSchedule().startJob(); 465 } 466 } 467 468 477 protected void setSyncInfoFilter(SyncInfoFilter filter) { 478 collector.setFilter(filter); 479 } 480 481 489 private void internalRefresh(IResource[] resources, String jobName, String taskName, IWorkbenchSite site, IRefreshSubscriberListener listener) { 490 if (jobName == null) 491 jobName = getShortTaskName(); 492 if (taskName == null) 493 taskName = getLongTaskName(resources); 494 Job.getJobManager().cancel(this); 495 RefreshParticipantJob job = new RefreshSubscriberParticipantJob(this, jobName, taskName, resources, listener); 496 job.setUser(true); 497 Utils.schedule(job, site); 498 499 TeamUIPlugin.getPlugin().getPreferenceStore().setValue(IPreferenceIds.SYNCHRONIZING_DEFAULT_PARTICIPANT, getId()); 501 TeamUIPlugin.getPlugin().getPreferenceStore().setValue(IPreferenceIds.SYNCHRONIZING_DEFAULT_PARTICIPANT_SEC_ID, getSecondaryId()); 502 } 503 504 509 public ISynchronizeScope getScope() { 510 return scope; 511 } 512 513 516 public Object getAdapter(Class adapter) { 517 if (adapter == IRefreshable.class && refreshSchedule != null) { 518 return refreshSchedule.getRefreshable(); 519 520 } 521 return super.getAdapter(adapter); 522 } 523 } 524 | Popular Tags |