KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > synchronize > AbstractSynchronizeParticipant


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.ui.synchronize;
12
13 import org.eclipse.compare.CompareConfiguration;
14 import org.eclipse.core.runtime.*;
15 import org.eclipse.jface.preference.PreferencePage;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.jface.util.IPropertyChangeListener;
18 import org.eclipse.jface.viewers.IBasicPropertyConstants;
19 import org.eclipse.team.core.TeamException;
20 import org.eclipse.team.core.synchronize.SyncInfo;
21 import org.eclipse.team.internal.ui.*;
22 import org.eclipse.team.internal.ui.preferences.SyncViewerPreferencePage;
23 import org.eclipse.team.internal.ui.registry.SynchronizeParticipantDescriptor;
24 import org.eclipse.team.internal.ui.synchronize.SyncInfoModelElement;
25 import org.eclipse.team.internal.ui.synchronize.SynchronizePageConfiguration;
26 import org.eclipse.team.ui.TeamImages;
27 import org.eclipse.ui.IMemento;
28 import org.eclipse.ui.PartInitException;
29
30 /**
31  * This class is the abstract base class for all synchronize view participants. Clients must subclass
32  * this class instead of directly implementing {@link ISynchronizeParticipant}.
33  * <p>
34  * This class provides lifecycle support and hooks for configuration of synchronize view pages.
35  * </p>
36  * @see ISynchronizeParticipant
37  * @since 3.0
38  */

39 public abstract class AbstractSynchronizeParticipant extends PlatformObject implements ISynchronizeParticipant {
40     
41     /**
42      * Property key used in the property change event fired when the pinned
43      * state of a participant changes.
44      */

45     public static final String JavaDoc P_PINNED = "org.eclipse.team.pinned"; //$NON-NLS-1$
46

47     /**
48      * Property key used in the property change event fired when the
49      * participants refresh schedule changes.
50      * @since 3.2
51      */

52     public static final String JavaDoc P_SCHEDULED = "org.eclipse.team.schedule"; //$NON-NLS-1$
53

54     // key for persisting the pinned state of a participant
55
private final static String JavaDoc CTX_PINNED = "root"; //$NON-NLS-1$
56

57     // property listeners
58
private PropertyChangeHandler fChangeHandler;
59
60     private String JavaDoc fName;
61     private String JavaDoc fId;
62     private String JavaDoc fSecondaryId;
63     private boolean pinned;
64     private ImageDescriptor fImageDescriptor;
65     protected IConfigurationElement configElement;
66
67     /**
68      * Default constructor is a no-op. Subclasses that are persistable must support a no-arg constructor
69      * and
70      */

71     public AbstractSynchronizeParticipant() {
72     }
73
74     /* (non-Javadoc)
75      * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipant#getName()
76      */

77     public String JavaDoc getName() {
78         return fName;
79     }
80
81     /* (non-Javadoc)
82      * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipant#getImageDescriptor()
83      */

84     public ImageDescriptor getImageDescriptor() {
85         return fImageDescriptor;
86     }
87
88
89     /* (non-Javadoc)
90      * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipant#getId()
91      */

92     public String JavaDoc getId() {
93         return fId;
94     }
95     
96     /* (non-Javadoc)
97      * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipant#getSecondaryId()
98      */

99     public String JavaDoc getSecondaryId() {
100         return fSecondaryId;
101     }
102     
103     
104     /* (non-Javadoc)
105      * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipant#setPinned(boolean)
106      */

107     public final void setPinned(boolean pinned) {
108         this.pinned = pinned;
109         pinned(pinned);
110         firePropertyChange(this, P_PINNED, Boolean.valueOf(!pinned), Boolean.valueOf(pinned));
111     }
112
113     /* (non-Javadoc)
114      * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipant#isPinned()
115      */

116     public final boolean isPinned() {
117         return pinned;
118     }
119     
120     /**
121      * Called when the pinned state is changed. Allows subclasses to react to pin state changes.
122      *
123      * @param pinned whether the participant is pinned.
124      */

125     protected void pinned(boolean pinned) {
126         // Subclasses can re-act to changes in the pinned state
127
}
128     
129     /* (non-Javadoc)
130      * @see java.lang.Object#equals(java.lang.Object)
131      */

132     public boolean equals(Object JavaDoc obj) {
133         if(obj == this) return true;
134         if( ! (obj instanceof ISynchronizeParticipant)) return false;
135         ISynchronizeParticipant other = (ISynchronizeParticipant)obj;
136         return getId().equals(other.getId()) && Utils.equalObject(getSecondaryId(), other.getSecondaryId());
137     }
138     
139     /* (non-Javadoc)
140      * @see java.lang.Object#hashCode()
141      */

142     public int hashCode() {
143         return Utils.getKey(getId(), getSecondaryId()).hashCode();
144     }
145     
146     /**
147      * Return whether this participant can be refreshed. Participants that can
148      * be refreshed may have a Synchronize menu item contributed to their context menu
149      * and can also be refreshed from the Synchronize drop-down toolbar item.
150      * When refreshed from the toolbar item, the {@link ISynchronizeParticipant#run(org.eclipse.ui.IWorkbenchPart)}
151      * method is called.
152      * @return whether this participant can be refreshed
153      */

154     public boolean doesSupportSynchronize() {
155         return true;
156     }
157     
158     /* (non-Javadoc)
159      * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipant#addPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
160      */

161     public synchronized void addPropertyChangeListener(IPropertyChangeListener listener) {
162         if (fChangeHandler == null) {
163             fChangeHandler = new PropertyChangeHandler();
164         }
165         fChangeHandler.addPropertyChangeListener(listener);
166     }
167
168     /* (non-Javadoc)
169      * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipant#removePropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
170      */

171     public void removePropertyChangeListener(IPropertyChangeListener listener) {
172         if (fChangeHandler != null) {
173             fChangeHandler.removePropertyChangeListener(listener);
174         }
175     }
176
177     /**
178      * Notify all listeners that the given property has changed.
179      *
180      * @param source the object on which a property has changed
181      * @param property identifier of the property that has changed
182      * @param oldValue the old value of the property, or <code>null</code>
183      * @param newValue the new value of the property, or <code>null</code>
184      */

185     public void firePropertyChange(Object JavaDoc source, String JavaDoc property, Object JavaDoc oldValue, Object JavaDoc newValue) {
186         if (fChangeHandler == null) {
187             return;
188         }
189         fChangeHandler.firePropertyChange(source, property, oldValue, newValue);
190     }
191
192     /* (non-Javadoc)
193      * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement, java.lang.String, java.lang.Object)
194      */

195     public void setInitializationData(IConfigurationElement config, String JavaDoc propertyName, Object JavaDoc data) throws CoreException {
196         // Save config element.
197
configElement = config;
198
199         // Id
200
fId = config.getAttribute("id"); //$NON-NLS-1$
201

202         // Title.
203
fName = config.getAttribute("name"); //$NON-NLS-1$
204
if (config == null) {
205             fName = "Unknown"; //$NON-NLS-1$
206
}
207
208         // Icon.
209
String JavaDoc strIcon = config.getAttribute("icon"); //$NON-NLS-1$
210
if (strIcon != null) {
211             fImageDescriptor = TeamImages.getImageDescriptorFromExtension(configElement.getDeclaringExtension(), strIcon);
212         }
213     }
214     
215     protected void setInitializationData(ISynchronizeParticipantDescriptor descriptor) throws CoreException {
216         if(descriptor instanceof SynchronizeParticipantDescriptor) {
217             setInitializationData(((SynchronizeParticipantDescriptor)descriptor).getConfigurationElement(), null, null);
218         } else {
219             throw new TeamException(TeamUIMessages.AbstractSynchronizeParticipant_4);
220         }
221     }
222
223     /**
224      * Sets the name of this participant to the specified value and notifies
225      * property listeners of the change.
226      *
227      * @param name the new name
228      */

229     protected void setName(String JavaDoc name) {
230         String JavaDoc old = fName;
231         fName = name;
232         firePropertyChange(this, IBasicPropertyConstants.P_TEXT, old, name);
233     }
234     
235     /**
236      * Sets the image descriptor for this participant to the specified value and
237      * notifies property listeners of the change.
238      *
239      * @param imageDescriptor the new image descriptor
240      */

241     protected void setImageDescriptor(ImageDescriptor imageDescriptor) {
242         ImageDescriptor old = fImageDescriptor;
243         fImageDescriptor = imageDescriptor;
244         firePropertyChange(this, IBasicPropertyConstants.P_IMAGE, old, imageDescriptor);
245     }
246     
247     /**
248      * Sets the secondary id for this participant.
249      *
250      * @param secondaryId the secondary id for this participant.
251      */

252     protected void setSecondaryId(String JavaDoc secondaryId) {
253         this.fSecondaryId = secondaryId;
254     }
255     
256     /**
257      * Classes that are persisted must override this method and perform
258      * the following initialization.
259      * <pre>
260      * super.init(secondaryId, memento);
261      * try {
262      * ISynchronizeParticipantDescriptor descriptor = TeamUI.getSynchronizeManager().getParticipantDescriptor(PARTICIPANT_ID);
263      * setInitializationData(descriptor);
264      * } catch (CoreException e) {
265      * TeamUIPlugin.log(e);
266      * }
267      * </pre>
268      * where <code>PARTICIPANT_ID</code> is the id of the particant as defined in the plugin manifest.
269      * </p>
270      * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipant#init(String, org.eclipse.ui.IMemento)
271      */

272     public void init(String JavaDoc secondaryId, IMemento memento) throws PartInitException {
273         setSecondaryId(secondaryId);
274         pinned = Boolean.valueOf(memento.getString(CTX_PINNED)).booleanValue();
275     }
276
277     /* (non-Javadoc)
278      * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipant#saveState(org.eclipse.ui.IMemento)
279      */

280     public void saveState(IMemento memento) {
281         memento.putString(CTX_PINNED, Boolean.toString(pinned));
282     }
283
284     /* (non-Javadoc)
285      * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipant#createPageConfiguration()
286      */

287     public final ISynchronizePageConfiguration createPageConfiguration() {
288         SynchronizePageConfiguration configuration = new SynchronizePageConfiguration(this);
289         if (isViewerContributionsSupported()) {
290             configuration.setProperty(ISynchronizePageConfiguration.P_OBJECT_CONTRIBUTION_ID, getId());
291         }
292         initializeConfiguration(configuration);
293         return configuration;
294     }
295
296     /**
297      * This method is invoked after a page configuration is created but before it is returned by the
298      * <code>createPageConfiguration</code> method. Subclasses can implement this method to
299      * tailor the configuration in ways appropriate to the participant.
300      *
301      * @param configuration the newly create page configuration
302      */

303     protected abstract void initializeConfiguration(ISynchronizePageConfiguration configuration);
304     
305     /**
306      * Default implementation will update the labels in the given configuration using
307      * information from the provided element if it adapts to <code>SyncInfo</code>.
308      * It will also cache the contents for the remote and base if the element is
309      * sync info based.
310      * @param element the sync model element whose contents are about to be displayed to the user
311      * in a compare editor or compare dialog
312      * @param config the compare configuration that will be used to configure the compare editor or dialog
313      * @param monitor a progress monitor that can be used if contacting a server to prepare the element and configuration
314      * @throws TeamException if an error occurred that shoudl rpevent the display of the compare editor containing
315      * the element
316      *
317      * @since 3.1
318      * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipant#prepareCompareInput(org.eclipse.team.ui.synchronize.ISynchronizeModelElement, org.eclipse.compare.CompareConfiguration, org.eclipse.core.runtime.IProgressMonitor)
319      */

320     public void prepareCompareInput(ISynchronizeModelElement element, CompareConfiguration config, IProgressMonitor monitor) throws TeamException {
321         SyncInfo sync = getSyncInfo(element);
322         if (sync != null)
323             Utils.updateLabels(sync, config);
324         if (element instanceof SyncInfoModelElement) {
325             SyncInfoModelElement node = (SyncInfoModelElement)element;
326             (node).cacheContents(monitor);
327         }
328     }
329     
330     /*
331      * Get the sync info node from the element using the adaptable mechanism.
332      * A <code>null</code> is returned if the element doesn't have a sync info
333      * @param element the sync model element
334      * @return the sync info for the element or <code>null</code>
335      */

336     private SyncInfo getSyncInfo(ISynchronizeModelElement element) {
337         if (element instanceof IAdaptable) {
338             return (SyncInfo)((IAdaptable)element).getAdapter(SyncInfo.class);
339         }
340         return null;
341     }
342     
343     /* (non-Javadoc)
344      * @see org.eclipse.team.ui.synchronize.ISynchronizeParticipant#getPreferencePages()
345      */

346     public PreferencePage[] getPreferencePages() {
347         return new PreferencePage[] { new SyncViewerPreferencePage() };
348     }
349     
350     /**
351      * Return whether this participant supports the contribution of actions to
352      * the context menu by contributing a <code>viewerContribution</code>
353      * to the <code>org.eclipse.ui.popupMenus</code> extension point. By default,
354      * <code>false</code> is returned. If a subclasses overrides to return <code>true</code>,
355      * the <code>id</code> of the participant is used as the <code>targetId</code>. Here is
356      * an extension that could be added to the plugin manifest to contribute an action to
357      * the context menu for a participant
358      *
359      * <pre>
360      * &lt;extension point="org.eclipse.ui.popupMenus"&gt;
361      * &lt;viewerContribution
362      * id="org.eclipse.team.cvs.ui.viewContributionId"
363      * targetID="org.eclipse.team.cvs.ui.cvsworkspace-participant"&gt;
364      * &lt;action
365      * label="Add"
366      * menubarPath="additions"
367      * tooltip="Add a file to CVS version control"
368      * class="org.eclipse.team.internal.ccvs.ui.actions.AddAction"
369      * helpContextId="org.eclipse.team.cvs.ui.workspace_subscriber_add"
370      * id="org.eclipse.team.ccvs.ui.CVSWorkspaceSubscriber.add"&gt;
371      * &lt;/action&gt;
372      * &lt;/viewerContribution&gt;
373      * &lt;/extension&gt;
374      * </pre>
375      *
376      *
377      * @return whether this participant supports the contribution of actions to
378      * the context menu using the <code>org.eclipse.ui.popupMenus</code> extension point
379      * @since 3.1
380      */

381     protected boolean isViewerContributionsSupported() {
382         return false;
383     }
384 }
385
Popular Tags