1 11 package org.eclipse.team.internal.ui.synchronize; 12 13 import java.io.*; 14 import java.util.ArrayList ; 15 import java.util.Collections ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import org.eclipse.core.runtime.*; 22 import org.eclipse.jface.dialogs.IDialogConstants; 23 import org.eclipse.jface.dialogs.MessageDialogWithToggle; 24 import org.eclipse.jface.preference.IPreferenceStore; 25 import org.eclipse.osgi.util.NLS; 26 import org.eclipse.team.core.TeamException; 27 import org.eclipse.team.internal.ui.*; 28 import org.eclipse.team.internal.ui.registry.*; 29 import org.eclipse.team.ui.synchronize.*; 30 import org.eclipse.ui.*; 31 32 62 public class SynchronizeManager implements ISynchronizeManager { 63 66 private ListenerList fListeners = null; 67 68 71 private SynchronizeParticipantRegistry participantRegistry = new SynchronizeParticipantRegistry(); 72 73 76 private SynchronizeWizardRegistry wizardRegistry = new SynchronizeWizardRegistry(); 77 78 82 private Map participantReferences = Collections.synchronizedMap(new HashMap (10)); 83 84 private final static int ADDED = 1; 86 private final static int REMOVED = 2; 87 88 private final static String CTX_PARTICIPANTS = "syncparticipants"; private final static String CTX_PARTICIPANT = "participant"; private final static String CTX_ID = "id"; private final static String CTX_SECONDARY_ID = "secondary_id"; private final static String CTX_PARTICIPANT_DISPLAY_NAME = "displayName"; private final static String CTX_PARTICIPANT_DATA = "data"; private final static String FILENAME = "syncParticipants.xml"; 97 100 class SynchronizeViewPageNotifier implements ISafeRunnable { 101 102 private ISynchronizeParticipantListener fListener; 103 private int fType; 104 private ISynchronizeParticipant[] fChanged; 105 106 public void handleException(Throwable exception) { 107 TeamUIPlugin.log(IStatus.ERROR, TeamUIMessages.SynchronizeManager_7, exception); 108 } 109 110 public void run() throws Exception { 111 switch (fType) { 112 case ADDED : 113 fListener.participantsAdded(fChanged); 114 break; 115 case REMOVED : 116 fListener.participantsRemoved(fChanged); 117 break; 118 } 119 } 120 121 126 public void notify(ISynchronizeParticipant[] participants, int update) { 127 if (fListeners == null) { 128 return; 129 } 130 fChanged = participants; 131 fType = update; 132 Object [] copiedListeners = fListeners.getListeners(); 133 for (int i = 0; i < copiedListeners.length; i++) { 134 fListener = (ISynchronizeParticipantListener) copiedListeners[i]; 135 Platform.run(this); 136 } 137 fChanged = null; 138 fListener = null; 139 } 140 } 141 142 146 private class ParticipantInstance implements ISynchronizeParticipantReference { 147 private Map participants; 148 private IMemento savedState; 149 private SynchronizeParticipantDescriptor descriptor; 150 private String secondaryId; 151 private String displayName; 152 private boolean dead; 153 154 public ParticipantInstance(SynchronizeParticipantDescriptor descriptor, String secondaryId, String displayName, IMemento savedState) { 155 this.participants = new HashMap (); 156 this.secondaryId = secondaryId; 157 this.savedState = savedState; 158 this.descriptor = descriptor; 159 this.displayName = displayName; 160 } 161 162 public void save(IMemento memento) { 163 if (dead) return; 164 String key = Utils.getKey(descriptor.getId(), getSecondaryId()); 165 ISynchronizeParticipant ref = (ISynchronizeParticipant) participants.get(key); 166 if(ref != null) { 167 ref.saveState(memento); 168 } else if(savedState != null) { 169 memento.putMemento(savedState); 170 } 171 } 172 173 public boolean equals(Object other) { 174 if(other == this) return true; 175 if (! (other instanceof ISynchronizeParticipantReference)) return false; 176 ISynchronizeParticipantReference otherRef = (ISynchronizeParticipantReference) other; 177 String otherSecondaryId = otherRef.getSecondaryId(); 178 return otherRef.getId().equals(getId()) && Utils.equalObject(getSecondaryId(), otherSecondaryId); 179 } 180 181 184 public String getId() { 185 return descriptor.getId(); 186 } 187 188 191 public String getSecondaryId() { 192 return secondaryId; 193 } 194 195 196 199 public String getDisplayName() { 200 String key = Utils.getKey(descriptor.getId(), getSecondaryId()); 201 ISynchronizeParticipant participant = (ISynchronizeParticipant) participants.get(key); 202 if(participant != null) { 203 return participant.getName(); 204 } 205 return displayName != null ? displayName : descriptor.getName(); 206 } 207 208 public boolean isInstantiated() { 209 String key = Utils.getKey(descriptor.getId(), getSecondaryId()); 210 return (ISynchronizeParticipant) participants.get(key) != null; 211 } 212 213 216 public ISynchronizeParticipant getParticipant() throws TeamException { 217 if (dead) return null; 218 String key = Utils.getKey(descriptor.getId(), getSecondaryId()); 219 try { 220 ISynchronizeParticipant participant = (ISynchronizeParticipant) participants.get(key); 221 if (participant == null) { 222 participant = instantiate(); 223 if(participant != null) 224 participants.put(key, participant); 225 } 226 return participant; 227 } catch (TeamException e) { 228 TeamUIPlugin.log(e); 229 participantReferences.remove(key); 230 throw new TeamException(TeamUIMessages.SynchronizeManager_8, e); 231 } 232 } 233 234 public void setParticipant(ISynchronizeParticipant participant) { 235 String key = Utils.getKey(descriptor.getId(), getSecondaryId()); 236 participants.put(key, participant); 237 } 238 239 242 public ISynchronizeParticipantDescriptor getDescriptor() { 243 return descriptor; 244 } 245 246 private ISynchronizeParticipant instantiate() throws TeamException { 247 try { 248 ISynchronizeParticipant participant = (ISynchronizeParticipant) TeamUIPlugin.createExtension(descriptor.getConfigurationElement(), SynchronizeParticipantDescriptor.ATT_CLASS); 249 participant.setInitializationData(descriptor.getConfigurationElement(), null, null); 250 participant.init(getSecondaryId(), savedState); 251 savedState = null; 252 return participant; 253 } catch (PartInitException e) { 254 throw new TeamException(NLS.bind(TeamUIMessages.SynchronizeManager_11, new String [] { descriptor.getName() }), e); 255 } catch (CoreException e) { 256 throw TeamException.asTeamException(e); 257 } catch(Exception e) { 258 throw new TeamException(NLS.bind(TeamUIMessages.SynchronizeManager_11, new String [] { descriptor.getName() }), e); 259 } 260 } 261 262 265 public void dispose() { 266 try { 267 ISynchronizeParticipant participant = getParticipant(); 268 if (participant != null) 269 participant.dispose(); 270 } catch (TeamException e) { 271 } finally { 273 dead = true; 274 } 275 } 276 } 277 278 public SynchronizeManager() { 279 init(); 280 } 281 282 287 public void addSynchronizeParticipantListener(ISynchronizeParticipantListener listener) { 288 if (fListeners == null) { 289 fListeners = new ListenerList(ListenerList.IDENTITY); 290 } 291 fListeners.add(listener); 292 } 293 294 299 public void removeSynchronizeParticipantListener(ISynchronizeParticipantListener listener) { 300 if (fListeners != null) { 301 fListeners.remove(listener); 302 } 303 } 304 305 318 private ParticipantInstance createParticipantReference(String type, String secondaryId, String displayName) throws PartInitException { 319 SynchronizeParticipantDescriptor desc = participantRegistry.find(type); 320 if (desc == null) 322 throw new PartInitException(NLS.bind(TeamUIMessages.SynchronizeManager_19, new String [] { type })); 323 if (secondaryId != null) { 325 } 329 String key = Utils.getKey(type, secondaryId); 330 ParticipantInstance ref = (ParticipantInstance) participantReferences.get(key); 331 if (ref == null) { 332 ref = new ParticipantInstance(desc, secondaryId, displayName, null); 333 } 334 return ref; 335 } 336 337 342 public synchronized void addSynchronizeParticipants(ISynchronizeParticipant[] participants) { 343 List added = new ArrayList (participants.length); 345 for (int i = 0; i < participants.length; i++) { 346 ISynchronizeParticipant participant = participants[i]; 347 String key = Utils.getKey(participant.getId(), participant.getSecondaryId()); 348 if(! participantReferences.containsKey(key)) { 349 try { 350 ParticipantInstance ref = createParticipantReference(participant.getId(), participant.getSecondaryId(), participant.getName()); 351 ref.setParticipant(participant); 352 removeMatchingParticipant(participant.getId()); 353 participantReferences.put(key, ref); 354 added.add(participant); 355 } catch (PartInitException e) { 356 TeamUIPlugin.log(e); 357 continue; 358 } 359 } 360 } 361 if (!added.isEmpty()) { 362 saveState(); 363 fireUpdate((ISynchronizeParticipant[]) added.toArray(new ISynchronizeParticipant[added.size()]), ADDED); 364 } 365 } 366 367 private void removeMatchingParticipant(String id) { 368 ISynchronizeParticipantReference[] refs = get(id); 369 if (refs.length > 0) { 370 for (int i = 0; i < refs.length; i++) { 372 ISynchronizeParticipantReference reference = refs[i]; 373 ISynchronizeParticipant p; 374 try { 375 p = reference.getParticipant(); 376 if (!p.isPinned() && !isDirty(p)) { 377 removeSynchronizeParticipants(new ISynchronizeParticipant[]{p}); 378 break; 379 } 380 } catch (TeamException e) { 381 continue; 382 } 383 } 384 } 385 } 386 387 private boolean isDirty(ISynchronizeParticipant p) { 388 if (p instanceof ModelSynchronizeParticipant) { 389 ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) p; 390 Saveable s = msp.getActiveSaveable(); 391 if (s != null && s.isDirty()) { 392 return true; 393 } 394 } 395 return false; 396 } 397 398 403 public synchronized void removeSynchronizeParticipants(ISynchronizeParticipant[] participants) { 404 List removed = new ArrayList (participants.length); 405 for (int i = 0; i < participants.length; i++) { 406 ISynchronizeParticipant participant = participants[i]; 407 String key = Utils.getKey(participant.getId(), participant.getSecondaryId()); 408 if(participantReferences.containsKey(key)) { 409 ParticipantInstance ref = (ParticipantInstance)participantReferences.remove(key); 410 if(ref.isInstantiated()) { 411 ref.dispose(); 412 } 413 removed.add(participant); 414 } 415 } 416 if (!removed.isEmpty()) { 417 saveState(); 418 fireUpdate((ISynchronizeParticipant[]) removed.toArray(new ISynchronizeParticipant[removed.size()]), REMOVED); 419 } 420 } 421 422 425 public ISynchronizeParticipantReference get(String id, String secondaryId) { 426 String key = Utils.getKey(id, secondaryId); 427 return (ISynchronizeParticipantReference) participantReferences.get(key); 428 } 429 430 433 public ISynchronizeParticipantReference[] get(String id) { 434 ISynchronizeParticipantReference[] refs = getSynchronizeParticipants(); 435 ArrayList refsForId = new ArrayList (); 436 for (int i = 0; i < refs.length; i++) { 437 ISynchronizeParticipantReference reference = refs[i]; 438 if(reference.getId().equals(id)) { 439 refsForId.add(reference); 440 } 441 } 442 return (ISynchronizeParticipantReference[]) refsForId.toArray(new ISynchronizeParticipantReference[refsForId.size()]); 443 } 444 445 450 public synchronized ISynchronizeParticipantReference[] getSynchronizeParticipants() { 451 return (ISynchronizeParticipantReference[]) participantReferences.values().toArray(new ISynchronizeParticipantReference[participantReferences.values().size()]); 452 } 453 454 457 public ISynchronizeView showSynchronizeViewInActivePage() { 458 IWorkbench workbench = TeamUIPlugin.getPlugin().getWorkbench(); 459 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow(); 460 461 boolean switchPerspectives = promptForPerspectiveSwitch(); 462 IWorkbenchPage activePage = null; 463 if(switchPerspectives) { 464 try { 465 String pId = TeamUIPlugin.getPlugin().getPreferenceStore().getString(IPreferenceIds.SYNCVIEW_DEFAULT_PERSPECTIVE); 466 activePage = workbench.showPerspective(pId, window); 467 } catch (WorkbenchException e) { 468 Utils.handleError(window.getShell(), e, TeamUIMessages.SynchronizeView_14, e.getMessage()); 469 } 470 } 471 try { 472 if (activePage == null) { 473 activePage = TeamUIPlugin.getActivePage(); 474 if (activePage == null) 475 return null; 476 } 477 IViewPart part = activePage.showView(ISynchronizeView.VIEW_ID); 479 try { 480 return (ISynchronizeView) part; 481 } catch (ClassCastException e) { 482 TeamUIPlugin.log(IStatus.ERROR, NLS.bind(TeamUIMessages.SynchronizeManager_18, new String [] { part.getClass().getName() }), e); 484 return null; 485 } 486 } catch (PartInitException pe) { 487 Utils.handleError(window.getShell(), pe, TeamUIMessages.SynchronizeView_16, pe.getMessage()); 488 return null; 489 } 490 } 491 492 496 private boolean promptForPerspectiveSwitch() { 497 IPreferenceStore store = TeamUIPlugin.getPlugin().getPreferenceStore(); 499 String option = store.getString(IPreferenceIds.SYNCHRONIZING_COMPLETE_PERSPECTIVE); 500 if(option.equals(MessageDialogWithToggle.ALWAYS)) { 501 return true; 502 } else if(option.equals(MessageDialogWithToggle.NEVER)) { 503 return false; 504 } 505 506 IPerspectiveRegistry registry= PlatformUI.getWorkbench().getPerspectiveRegistry(); 508 String defaultSyncPerspectiveId = store.getString(IPreferenceIds.SYNCVIEW_DEFAULT_PERSPECTIVE); 509 IPerspectiveDescriptor perspectiveDescriptor = registry.findPerspectiveWithId(defaultSyncPerspectiveId); 510 IWorkbenchPage page = TeamUIPlugin.getActivePage(); 511 if(page != null) { 512 IPerspectiveDescriptor p = page.getPerspective(); 513 if(p != null && p.getId().equals(defaultSyncPerspectiveId)) { 514 return false; 516 } 517 } 518 519 if(perspectiveDescriptor != null) { 520 521 String message;; 522 String desc = perspectiveDescriptor.getDescription(); 523 if (desc == null) { 524 message = NLS.bind(TeamUIMessages.SynchronizeManager_30, new String [] { perspectiveDescriptor.getLabel() }); 525 } else { 526 message = NLS.bind(TeamUIMessages.SynchronizeManager_32, new String [] { perspectiveDescriptor.getLabel(), desc }); 527 } 528 MessageDialogWithToggle m = MessageDialogWithToggle.openYesNoQuestion(Utils.getShell(null), 529 TeamUIMessages.SynchronizeManager_27, 530 message, 531 TeamUIMessages.SynchronizeManager_31, 532 false , 533 store, 534 IPreferenceIds.SYNCHRONIZING_COMPLETE_PERSPECTIVE); 535 536 int result = m.getReturnCode(); 537 switch (result) { 538 case IDialogConstants.YES_ID: 540 case IDialogConstants.OK_ID : 541 return true; 542 case IDialogConstants.NO_ID : 544 return false; 545 } 546 } 547 return false; 548 } 549 550 554 public void init() { 555 try { 556 participantRegistry.readRegistry(Platform.getExtensionRegistry(), TeamUIPlugin.ID, SynchronizeParticipantRegistry.PT_SYNCPARTICIPANTS); 558 wizardRegistry.readRegistry(Platform.getExtensionRegistry(), TeamUIPlugin.ID, SynchronizeWizardRegistry.PT_SYNCHRONIZE_WIZARDS); 560 561 restoreSavedParticipants(); 564 } catch (CoreException e) { 565 TeamUIPlugin.log(new Status(IStatus.ERROR, TeamUIPlugin.ID, 1, TeamUIMessages.SynchronizeManager_8, e)); 566 } 567 } 568 569 572 public void dispose() { 573 saveState(); 575 for (Iterator it = participantReferences.values().iterator(); it.hasNext();) { 576 ParticipantInstance ref = (ParticipantInstance) it.next(); 577 if((ref).isInstantiated()) { 578 try { 579 ref.getParticipant().dispose(); 580 } catch (TeamException e) { 581 continue; 582 } 583 } 584 } 585 participantReferences = null; 586 } 587 588 591 private void restoreSavedParticipants() throws CoreException { 592 File file = getStateFile(); 593 Reader reader; 594 try { 595 reader = new BufferedReader(new FileReader(file)); 596 } catch (FileNotFoundException e) { 597 return; 598 } 599 IMemento memento = XMLMemento.createReadRoot(reader); 600 IMemento[] participantNodes = memento.getChildren(CTX_PARTICIPANT); 601 for (int i = 0; i < participantNodes.length; i++) { 602 IMemento memento2 = participantNodes[i]; 603 String id = memento2.getString(CTX_ID); 604 String secondayId = memento2.getString(CTX_SECONDARY_ID); 605 if (secondayId != null) { 606 String displayName = memento2.getString(CTX_PARTICIPANT_DISPLAY_NAME); 607 SynchronizeParticipantDescriptor desc = participantRegistry.find(id); 608 if (desc != null) { 609 String key = Utils.getKey(id, secondayId); 610 participantReferences.put(key, new ParticipantInstance(desc, secondayId, displayName, memento2.getChild(CTX_PARTICIPANT_DATA))); 611 } else { 612 TeamUIPlugin.log(new Status(IStatus.ERROR, TeamUIPlugin.ID, 1, NLS.bind(TeamUIMessages.SynchronizeManager_9, new String [] { id }), null)); 613 } 614 } 615 } 616 } 617 618 623 private void saveState() { 624 XMLMemento xmlMemento = XMLMemento.createWriteRoot(CTX_PARTICIPANTS); 625 for (Iterator it = participantReferences.values().iterator(); it.hasNext(); ) { 626 ParticipantInstance ref = (ParticipantInstance) it.next(); 627 if(! ref.getDescriptor().isPersistent()) continue; 629 IMemento participantNode = xmlMemento.createChild(CTX_PARTICIPANT); 631 participantNode.putString(CTX_ID, ref.getId()); 632 String secondaryId = ref.getSecondaryId(); 633 if(secondaryId != null) { 634 participantNode.putString(CTX_SECONDARY_ID,secondaryId); 635 } 636 participantNode.putString(CTX_PARTICIPANT_DISPLAY_NAME, ref.getDisplayName()); 637 IMemento participantData = participantNode.createChild(CTX_PARTICIPANT_DATA); 638 ref.save(participantData); 639 } 640 try { 641 Writer writer = new BufferedWriter(new FileWriter(getStateFile())); 642 try { 643 xmlMemento.save(writer); 644 } finally { 645 writer.close(); 646 } 647 } catch (IOException e) { 648 TeamUIPlugin.log(new Status(IStatus.ERROR, TeamUIPlugin.ID, 1, TeamUIMessages.SynchronizeManager_10, e)); 649 } 650 } 651 652 private File getStateFile() { 653 IPath pluginStateLocation = TeamUIPlugin.getPlugin().getStateLocation(); 654 return pluginStateLocation.append(FILENAME).toFile(); } 656 657 662 private void fireUpdate(ISynchronizeParticipant[] participants, int type) { 663 new SynchronizeViewPageNotifier().notify(participants, type); 664 } 665 666 669 public ISynchronizeParticipantDescriptor getParticipantDescriptor(String id) { 670 return participantRegistry.find(id); 671 } 672 673 public SynchronizeWizardDescription[] getWizardDescriptors() { 674 return wizardRegistry.getSynchronizeWizards(); 675 } 676 } 677 | Popular Tags |