KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > SynchronizeManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ui.synchronize;
12
13 import java.io.*;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
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 /**
33  * Manages the registered synchronize participants. It handles notification of
34  * participant lifecycles, creation of <code>static</code> participants, management
35  * of dynamic participants, and the re-creation of persisted participants.
36  * <p>
37  * A participant is defined in a plugin manifest and can have several properties:
38  * - static: means that they always exist and don't have to be added to the manager
39  * - dynamic: will be added to the manager at some later time
40  *
41  * Part (title, id, icon, composite) - described in plugin.xml (IPartInstance)
42  * Can have multiple parts of the same type at runtime -> (IPart)
43  * - must acquire a part (IPartInstance.createPart())
44  * - must released to part when done (IPartInstance.releasePart())
45  * Some parts can added dynamically to the registry and events are fired to listeners. Listeners can create the newly added part via
46  * the #createPart() method.
47  * Parts can be persisted/restored with some state
48  *
49  *
50  *
51  * Lifecycle:
52  * startup -> registry read and stored in a participant instance
53  * createParticipant(id) ->
54  * releaseParticipant(IParticipantDescriptor) ->
55  * getParticipantRegistry -> return IParticipantDescriptors that describe the participants
56  * shutdown -> persist all settings
57  *
58  * @see ISynchronizeView
59  * @see ISynchronizeParticipant
60  * @since 3.0
61  */

62 public class SynchronizeManager implements ISynchronizeManager {
63     /**
64      * Synchronize participants listeners
65      */

66     private ListenerList fListeners = null;
67     
68     /**
69      * Contains the participant descriptions
70      */

71     private SynchronizeParticipantRegistry participantRegistry = new SynchronizeParticipantRegistry();
72     
73     /**
74      * Contains the synchronize wizard descriptions
75      */

76     private SynchronizeWizardRegistry wizardRegistry = new SynchronizeWizardRegistry();
77     
78     /**
79      * Contains a table of the state saved between sessions for a participant. The set is keyed
80      * as such {String key -> ISynchronizeParticipantReference}.
81      */

82     private Map JavaDoc participantReferences = Collections.synchronizedMap(new HashMap JavaDoc(10));
83
84     // change notification constants
85
private final static int ADDED = 1;
86     private final static int REMOVED = 2;
87
88     // save context constants
89
private final static String JavaDoc CTX_PARTICIPANTS = "syncparticipants"; //$NON-NLS-1$
90
private final static String JavaDoc CTX_PARTICIPANT = "participant"; //$NON-NLS-1$
91
private final static String JavaDoc CTX_ID = "id"; //$NON-NLS-1$
92
private final static String JavaDoc CTX_SECONDARY_ID = "secondary_id"; //$NON-NLS-1$
93
private final static String JavaDoc CTX_PARTICIPANT_DISPLAY_NAME = "displayName"; //$NON-NLS-1$
94
private final static String JavaDoc CTX_PARTICIPANT_DATA = "data"; //$NON-NLS-1$
95
private final static String JavaDoc FILENAME = "syncParticipants.xml"; //$NON-NLS-1$
96

97     /**
98      * Notifies a participant listeners of additions or removals of participant references.
99      */

100     class SynchronizeViewPageNotifier implements ISafeRunnable {
101
102         private ISynchronizeParticipantListener fListener;
103         private int fType;
104         private ISynchronizeParticipant[] fChanged;
105
106         public void handleException(Throwable JavaDoc exception) {
107             TeamUIPlugin.log(IStatus.ERROR, TeamUIMessages.SynchronizeManager_7, exception);
108         }
109
110         public void run() throws Exception JavaDoc {
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         /**
122          * Notifies the given listener of the adds/removes
123          * @param participants the participants that changed
124          * @param update the type of change
125          */

126         public void notify(ISynchronizeParticipant[] participants, int update) {
127             if (fListeners == null) {
128                 return;
129             }
130             fChanged = participants;
131             fType = update;
132             Object JavaDoc[] 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     /**
143      * Represents a paticipant instance and allows lazy initialization of the instance
144      * only when the participant is required.
145      */

146     private class ParticipantInstance implements ISynchronizeParticipantReference {
147         private Map JavaDoc participants;
148         private IMemento savedState;
149         private SynchronizeParticipantDescriptor descriptor;
150         private String JavaDoc secondaryId;
151         private String JavaDoc displayName;
152         private boolean dead;
153         
154         public ParticipantInstance(SynchronizeParticipantDescriptor descriptor, String JavaDoc secondaryId, String JavaDoc displayName, IMemento savedState) {
155             this.participants = new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc other) {
174             if(other == this) return true;
175             if (! (other instanceof ISynchronizeParticipantReference)) return false;
176             ISynchronizeParticipantReference otherRef = (ISynchronizeParticipantReference) other;
177             String JavaDoc otherSecondaryId = otherRef.getSecondaryId();
178             return otherRef.getId().equals(getId()) && Utils.equalObject(getSecondaryId(), otherSecondaryId);
179         }
180         
181         /* (non-Javadoc)
182          * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipantReference#getId()
183          */

184         public String JavaDoc getId() {
185             return descriptor.getId();
186         }
187
188         /* (non-Javadoc)
189          * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipantReference#getSecondaryId()
190          */

191         public String JavaDoc getSecondaryId() {
192             return secondaryId;
193         }
194         
195         
196         /* (non-Javadoc)
197          * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipantReference#getDisplayName()
198          */

199         public String JavaDoc getDisplayName() {
200             String JavaDoc 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 JavaDoc key = Utils.getKey(descriptor.getId(), getSecondaryId());
210             return (ISynchronizeParticipant) participants.get(key) != null;
211         }
212
213         /* (non-Javadoc)
214          * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipantReference#createParticipant()
215          */

216         public ISynchronizeParticipant getParticipant() throws TeamException {
217             if (dead) return null;
218             String JavaDoc 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 JavaDoc key = Utils.getKey(descriptor.getId(), getSecondaryId());
236             participants.put(key, participant);
237         }
238         
239         /* (non-Javadoc)
240          * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipantReference#getDescriptor()
241          */

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 JavaDoc[] { descriptor.getName() }), e);
255                 } catch (CoreException e) {
256                     throw TeamException.asTeamException(e);
257                 } catch(Exception JavaDoc e) {
258                     throw new TeamException(NLS.bind(TeamUIMessages.SynchronizeManager_11, new String JavaDoc[] { descriptor.getName() }), e);
259                 }
260             }
261
262         /**
263          * Dispose of the reference
264          */

265         public void dispose() {
266             try {
267                 ISynchronizeParticipant participant = getParticipant();
268                 if (participant != null)
269                     participant.dispose();
270             } catch (TeamException e) {
271                 // Ignore since we are disposing anyway;
272
} finally {
273                 dead = true;
274             }
275         }
276     }
277
278     public SynchronizeManager() {
279         init();
280     }
281
282     /*
283      * (non-Javadoc)
284      *
285      * @see org.eclipse.team.ui.sync.ISynchronizeManager#addSynchronizeParticipantListener(org.eclipse.team.ui.sync.ISynchronizeParticipantListener)
286      */

287     public void addSynchronizeParticipantListener(ISynchronizeParticipantListener listener) {
288         if (fListeners == null) {
289             fListeners = new ListenerList(ListenerList.IDENTITY);
290         }
291         fListeners.add(listener);
292     }
293
294     /*
295      * (non-Javadoc)
296      *
297      * @see org.eclipse.team.ui.sync.ISynchronizeManager#removeSynchronizeParticipantListener(org.eclipse.team.ui.sync.ISynchronizeParticipantListener)
298      */

299     public void removeSynchronizeParticipantListener(ISynchronizeParticipantListener listener) {
300         if (fListeners != null) {
301             fListeners.remove(listener);
302         }
303     }
304     
305     /**
306      * Creates a new participant reference with of the provided type. If the secondayId is specified it
307      * is used as the qualifier for multiple instances of the same type.
308      * <p>
309      * The returned participant reference is a light weight handle describing the participant. The plug-in
310      * defining the participant is not loaded. To instantiate a participant a client must call
311      * {@link ISynchronizeParticipantReference#createParticipant()} and must call
312      * {@link ISynchronizeParticipantReference#releaseParticipant()} when finished with the participant.
313      * </p>
314      * @param type the type of the participant
315      * @param secondaryId a unique id for multiple instance support
316      * @return a reference to a participant
317      */

318     private ParticipantInstance createParticipantReference(String JavaDoc type, String JavaDoc secondaryId, String JavaDoc displayName) throws PartInitException {
319         SynchronizeParticipantDescriptor desc = participantRegistry.find(type);
320         // ensure that the view id is valid
321
if (desc == null)
322             throw new PartInitException(NLS.bind(TeamUIMessages.SynchronizeManager_19, new String JavaDoc[] { type }));
323         // ensure that multiple instances are allowed if a secondary id is given
324
if (secondaryId != null) {
325 // if (!desc.isMultipleInstances()) {
326
// throw new PartInitException(Policy.bind("SynchronizeManager.20", type)); //$NON-NLS-1$
327
// }
328
}
329         String JavaDoc 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     /*
338      * (non-Javadoc)
339      *
340      * @see org.eclipse.team.ui.sync.ISynchronizeManager#addSynchronizeParticipants(org.eclipse.team.ui.sync.ISynchronizeParticipant[])
341      */

342     public synchronized void addSynchronizeParticipants(ISynchronizeParticipant[] participants) {
343         // renamed to createSynchronizeParticipant(id)
344
List JavaDoc added = new ArrayList JavaDoc(participants.length);
345         for (int i = 0; i < participants.length; i++) {
346             ISynchronizeParticipant participant = participants[i];
347             String JavaDoc 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 JavaDoc id) {
368         ISynchronizeParticipantReference[] refs = get(id);
369         if (refs.length > 0) {
370             // Find an un-pinned participant and replace it
371
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     /*
399      * (non-Javadoc)
400      *
401      * @see org.eclipse.team.ui.sync.ISynchronizeManager#removeSynchronizeParticipants(org.eclipse.team.ui.sync.ISynchronizeParticipant[])
402      */

403     public synchronized void removeSynchronizeParticipants(ISynchronizeParticipant[] participants) {
404         List JavaDoc removed = new ArrayList JavaDoc(participants.length);
405         for (int i = 0; i < participants.length; i++) {
406             ISynchronizeParticipant participant = participants[i];
407             String JavaDoc 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     /* (non-Javadoc)
423      * @see org.eclipse.team.ui.synchronize.ISynchronizeManager#get(java.lang.String)
424      */

425     public ISynchronizeParticipantReference get(String JavaDoc id, String JavaDoc secondaryId) {
426         String JavaDoc key = Utils.getKey(id, secondaryId);
427         return (ISynchronizeParticipantReference) participantReferences.get(key);
428     }
429     
430     /* (non-Javadoc)
431      * @see org.eclipse.team.ui.synchronize.ISynchronizeManager#get(java.lang.String)
432      */

433     public ISynchronizeParticipantReference[] get(String JavaDoc id) {
434         ISynchronizeParticipantReference[] refs = getSynchronizeParticipants();
435         ArrayList JavaDoc refsForId = new ArrayList JavaDoc();
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     /*
446      * (non-Javadoc)
447      *
448      * @see org.eclipse.team.ui.sync.ISynchronizeManager#getSynchronizeParticipants()
449      */

450     public synchronized ISynchronizeParticipantReference[] getSynchronizeParticipants() {
451         return (ISynchronizeParticipantReference[]) participantReferences.values().toArray(new ISynchronizeParticipantReference[participantReferences.values().size()]);
452     }
453
454     /* (non-Javadoc)
455      * @see org.eclipse.team.ui.synchronize.ISynchronizeManager#showSynchronizeViewInActivePage()
456      */

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 JavaDoc 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, Long.toString(System.currentTimeMillis()), IWorkbenchPage.VIEW_ACTIVATE);
478
IViewPart part = activePage.showView(ISynchronizeView.VIEW_ID);
479             try {
480                 return (ISynchronizeView) part;
481             } catch (ClassCastException JavaDoc e) {
482                 // Strange that we cannot cast the part (see bug 53671)
483
TeamUIPlugin.log(IStatus.ERROR, NLS.bind(TeamUIMessages.SynchronizeManager_18, new String JavaDoc[] { 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     /**
493      * Decides what action to take when switching perspectives and showing the synchronize view. Basically there are a
494      * set of user preferences that control how perspective switching.
495      */

496     private boolean promptForPerspectiveSwitch() {
497         // Decide if a prompt is even required
498
IPreferenceStore store = TeamUIPlugin.getPlugin().getPreferenceStore();
499         String JavaDoc 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         // Otherwise determine if a prompt is required
507
IPerspectiveRegistry registry= PlatformUI.getWorkbench().getPerspectiveRegistry();
508         String JavaDoc 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                 // currently in default perspective
515
return false;
516             }
517         }
518         
519         if(perspectiveDescriptor != null) {
520             
521             String JavaDoc message;;
522             String JavaDoc desc = perspectiveDescriptor.getDescription();
523             if (desc == null) {
524                 message = NLS.bind(TeamUIMessages.SynchronizeManager_30, new String JavaDoc[] { perspectiveDescriptor.getLabel() });
525             } else {
526                 message = NLS.bind(TeamUIMessages.SynchronizeManager_32, new String JavaDoc[] { perspectiveDescriptor.getLabel(), desc });
527             }
528             MessageDialogWithToggle m = MessageDialogWithToggle.openYesNoQuestion(Utils.getShell(null),
529                         TeamUIMessages.SynchronizeManager_27,
530                         message,
531                         TeamUIMessages.SynchronizeManager_31,
532                         false /* toggle state */,
533                         store,
534                         IPreferenceIds.SYNCHRONIZING_COMPLETE_PERSPECTIVE);
535         
536             int result = m.getReturnCode();
537             switch (result) {
538                 // yes, ok
539
case IDialogConstants.YES_ID:
540                 case IDialogConstants.OK_ID :
541                     return true;
542                 // no
543
case IDialogConstants.NO_ID :
544                     return false;
545             }
546         }
547         return false;
548     }
549
550     /**
551      * Creates the participant registry and restore any saved participants.
552      * Will also instantiate any static participants.
553      */

554     public void init() {
555         try {
556             // Initialize the participant registry - reads all participant extension descriptions.
557
participantRegistry.readRegistry(Platform.getExtensionRegistry(), TeamUIPlugin.ID, SynchronizeParticipantRegistry.PT_SYNCPARTICIPANTS);
558             // Initialize the wizard registry
559
wizardRegistry.readRegistry(Platform.getExtensionRegistry(), TeamUIPlugin.ID, SynchronizeWizardRegistry.PT_SYNCHRONIZE_WIZARDS);
560             
561             // Instantiate and register any dynamic participants saved from a
562
// previous session.
563
restoreSavedParticipants();
564         } catch (CoreException e) {
565             TeamUIPlugin.log(new Status(IStatus.ERROR, TeamUIPlugin.ID, 1, TeamUIMessages.SynchronizeManager_8, e));
566         }
567     }
568
569     /**
570      * Allow participant instances to clean-up.
571      */

572     public void dispose() {
573         // save state and settings for existing participants.
574
saveState();
575         for (Iterator JavaDoc 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     /**
589      * Restores participants that have been saved between sessions.
590      */

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 JavaDoc id = memento2.getString(CTX_ID);
604             String JavaDoc secondayId = memento2.getString(CTX_SECONDARY_ID);
605             if (secondayId != null) {
606                 String JavaDoc displayName = memento2.getString(CTX_PARTICIPANT_DISPLAY_NAME);
607                 SynchronizeParticipantDescriptor desc = participantRegistry.find(id);
608                 if (desc != null) {
609                     String JavaDoc 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 JavaDoc[] { id }), null));
613                 }
614             }
615         }
616     }
617
618     /**
619      * Saves a file containing the list of participant ids that are registered
620      * with this manager. Each initialized participant is also given the chance to save
621      * it's state.
622      */

623     private void saveState() {
624         XMLMemento xmlMemento = XMLMemento.createWriteRoot(CTX_PARTICIPANTS);
625         for (Iterator JavaDoc it = participantReferences.values().iterator(); it.hasNext(); ) {
626             ParticipantInstance ref = (ParticipantInstance) it.next();
627             // Participants can opt out of being saved between sessions
628
if(! ref.getDescriptor().isPersistent()) continue;
629             // Create the state placeholder for a participant
630
IMemento participantNode = xmlMemento.createChild(CTX_PARTICIPANT);
631             participantNode.putString(CTX_ID, ref.getId());
632             String JavaDoc 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(); //
655
}
656     
657     /**
658      * Fires notification.
659      * @param participants participants added/removed
660      * @param type ADD or REMOVE
661      */

662     private void fireUpdate(ISynchronizeParticipant[] participants, int type) {
663         new SynchronizeViewPageNotifier().notify(participants, type);
664     }
665
666     /* (non-Javadoc)
667      * @see org.eclipse.team.ui.synchronize.ISynchronizeManager#getDescriptor()
668      */

669     public ISynchronizeParticipantDescriptor getParticipantDescriptor(String JavaDoc id) {
670         return participantRegistry.find(id);
671     }
672     
673     public SynchronizeWizardDescription[] getWizardDescriptors() {
674         return wizardRegistry.getSynchronizeWizards();
675     }
676 }
677
Popular Tags