KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > subscriber > CompareParticipant


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.ccvs.ui.subscriber;
12
13 import java.util.Arrays JavaDoc;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.Preferences.IPropertyChangeListener;
19 import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.team.core.TeamException;
22 import org.eclipse.team.core.subscribers.Subscriber;
23 import org.eclipse.team.core.synchronize.*;
24 import org.eclipse.team.internal.ccvs.core.CVSCompareSubscriber;
25 import org.eclipse.team.internal.ccvs.core.CVSTag;
26 import org.eclipse.team.internal.ccvs.ui.*;
27 import org.eclipse.team.internal.ccvs.ui.actions.ShowAnnotationAction;
28 import org.eclipse.team.internal.ccvs.ui.actions.ShowResourceInHistoryAction;
29 import org.eclipse.team.internal.core.subscribers.ActiveChangeSetManager;
30 import org.eclipse.team.internal.ui.Utils;
31 import org.eclipse.team.ui.TeamUI;
32 import org.eclipse.team.ui.synchronize.*;
33
34 public class CompareParticipant extends CVSParticipant implements IPropertyChangeListener {
35     
36     public static final String JavaDoc CONTEXT_MENU_CONTRIBUTION_GROUP = "context_group_1"; //$NON-NLS-1$
37
public static final String JavaDoc NON_MODAL_CONTEXT_MENU_CONTRIBUTION_GROUP = "context_group_2"; //$NON-NLS-1$
38

39     /**
40      * Actions for the compare particpant's toolbar
41      */

42     public class CompareParticipantActionContribution extends SynchronizePageActionGroup {
43         public void initialize(ISynchronizePageConfiguration configuration) {
44             super.initialize(configuration);
45             
46             appendToGroup(
47                     ISynchronizePageConfiguration.P_CONTEXT_MENU,
48                     CONTEXT_MENU_CONTRIBUTION_GROUP,
49                     new CompareRevertAction(configuration));
50             
51             if (!configuration.getSite().isModal()) {
52                 ShowAnnotationAction showAnnotationAction = new ShowAnnotationAction();
53                 appendToGroup(
54                         ISynchronizePageConfiguration.P_CONTEXT_MENU,
55                         NON_MODAL_CONTEXT_MENU_CONTRIBUTION_GROUP,
56                         new CVSActionDelegateWrapper(showAnnotationAction, configuration));
57                 appendToGroup(
58                         ISynchronizePageConfiguration.P_CONTEXT_MENU,
59                         NON_MODAL_CONTEXT_MENU_CONTRIBUTION_GROUP,
60                         new CVSActionDelegateWrapper(new ShowResourceInHistoryAction(), configuration));
61             }
62         }
63     }
64     
65     private SyncInfoFilter contentComparison = new SyncInfoFilter() {
66         private SyncInfoFilter contentCompare = new SyncInfoFilter.ContentComparisonSyncInfoFilter();
67         public boolean select(SyncInfo info, IProgressMonitor monitor) {
68             // Want to select infos whose contents do not match
69
return !contentCompare.select(info, monitor);
70         }
71     };
72     
73     public CompareParticipant(CVSCompareSubscriber subscriber) {
74         setSubscriber(subscriber);
75     }
76         
77     /* (non-Javadoc)
78      * @see org.eclipse.team.ui.synchronize.subscriber.SubscriberParticipant#setSubscriber(org.eclipse.team.core.subscribers.Subscriber)
79      */

80     protected void setSubscriber(Subscriber subscriber) {
81         super.setSubscriber(subscriber);
82         if (CVSUIPlugin.getPlugin().getPluginPreferences().getBoolean(ICVSUIConstants.PREF_CONSIDER_CONTENTS)) {
83             setSyncInfoFilter(contentComparison);
84         }
85         try {
86             ISynchronizeParticipantDescriptor descriptor = TeamUI.getSynchronizeManager().getParticipantDescriptor(CVSCompareSubscriber.ID);
87             setInitializationData(descriptor);
88             CVSCompareSubscriber s = getCVSCompareSubscriber();
89             setSecondaryId(s.getId().getLocalName());
90         } catch (CoreException e) {
91             CVSUIPlugin.log(e);
92         }
93         CVSUIPlugin.getPlugin().getPluginPreferences().addPropertyChangeListener(this);
94     }
95     
96     /* (non-Javadoc)
97      * @see org.eclipse.team.ui.synchronize.AbstractSynchronizeParticipant#getName()
98      */

99     public String JavaDoc getName() {
100         return NLS.bind(CVSUIMessages.CompareParticipant_0, new String JavaDoc[] { getSubscriber().getName(), Utils.convertSelection(getSubscriber().roots()) });
101     }
102     
103     /*
104      * Returns a merge participant that exist and is configured with the given set of resources, start, and end tags.
105      */

106     public static CompareParticipant getMatchingParticipant(IResource[] resources, CVSTag tag) {
107         ISynchronizeParticipantReference[] refs = TeamUI.getSynchronizeManager().getSynchronizeParticipants();
108         for (int i = 0; i < refs.length; i++) {
109             ISynchronizeParticipantReference reference = refs[i];
110             if (reference.getId().equals(CVSCompareSubscriber.ID)) {
111                 try {
112                     CompareParticipant p = (CompareParticipant) reference.getParticipant();
113                     if (p.matches(resources, tag)) {
114                         return p;
115                     }
116                 } catch (TeamException e) {
117                     continue;
118                 }
119             }
120         }
121         return null;
122     }
123     
124     /**
125      * Return whether this compare subscriber matches persisly the
126      * provided list of resources and the single tag.
127      * @param resources the resources
128      * @param tag the tag
129      * @return whether this compare subscriber matches persisly the
130      * provided list of resources and the single tag
131      */

132     protected boolean matches(IResource[] resources, CVSTag tag) {
133         CVSTag existingTag = getCVSCompareSubscriber().getTag();
134         // The tag can be null if the compare participant has a different tag for each root
135
if (existingTag != null) {
136             IResource[] roots = getResources();
137             Arrays.sort(resources, Utils.resourceComparator);
138             Arrays.sort(roots, Utils.resourceComparator);
139             if (Arrays.equals(resources, roots) && existingTag.equals(tag)) {
140                 return true;
141             }
142         }
143         return false;
144     }
145
146     /**
147      * Return the subscriber as an instance of CVSCompareSubscriber.
148      * @return the subscriber as an instance of CVSCompareSubscriber
149      */

150     public CVSCompareSubscriber getCVSCompareSubscriber() {
151         return (CVSCompareSubscriber)getSubscriber();
152     }
153
154     /* (non-Javadoc)
155      * @see org.eclipse.team.ui.synchronize.subscribers.SubscriberParticipant#initializeConfiguration(org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration)
156      */

157     protected void initializeConfiguration(ISynchronizePageConfiguration configuration) {
158         super.initializeConfiguration(configuration);
159         configuration.addMenuGroup(
160                 ISynchronizePageConfiguration.P_CONTEXT_MENU,
161                 CONTEXT_MENU_CONTRIBUTION_GROUP);
162         configuration.addMenuGroup(
163                 ISynchronizePageConfiguration.P_CONTEXT_MENU,
164                 NON_MODAL_CONTEXT_MENU_CONTRIBUTION_GROUP);
165         configuration.addActionContribution(new CompareParticipantActionContribution());
166     }
167     
168     /* (non-Javadoc)
169      * @see org.eclipse.team.ui.synchronize.SubscriberParticipant#dispose()
170      */

171     public void dispose() {
172         super.dispose();
173         CVSUIPlugin.getPlugin().getPluginPreferences().removePropertyChangeListener(this);
174         getCVSCompareSubscriber().dispose();
175     }
176
177     /* (non-Javadoc)
178      * @see org.eclipse.core.runtime.Preferences.IPropertyChangeListener#propertyChange(org.eclipse.core.runtime.Preferences.PropertyChangeEvent)
179      */

180     public void propertyChange(PropertyChangeEvent event) {
181         if (event.getProperty().equals(ICVSUIConstants.PREF_CONSIDER_CONTENTS)) {
182             if (CVSUIPlugin.getPlugin().getPluginPreferences().getBoolean(ICVSUIConstants.PREF_CONSIDER_CONTENTS)) {
183                 setSyncInfoFilter(contentComparison);
184             } else {
185                 setSyncInfoFilter(new FastSyncInfoFilter());
186             }
187         }
188     }
189     
190     /* (non-Javadoc)
191      * @see org.eclipse.team.ui.synchronize.SubscriberParticipant#getLongTaskName()
192      */

193     protected String JavaDoc getLongTaskName() {
194         return getName();
195     }
196     
197     /* (non-Javadoc)
198      * @see org.eclipse.team.ui.synchronize.SubscriberParticipant#getShortTaskName()
199      */

200     protected String JavaDoc getShortTaskName() {
201         return CVSUIMessages.Participant_comparing;
202     }
203     
204     /* (non-Javadoc)
205      * @see org.eclipse.team.internal.ccvs.ui.subscriber.CVSParticipant#createChangeSetCapability()
206      */

207     protected CVSChangeSetCapability createChangeSetCapability() {
208         return new CVSChangeSetCapability() {
209             public ActiveChangeSetManager getActiveChangeSetManager() {
210                 return CVSUIPlugin.getPlugin().getChangeSetManager();
211             }
212             /* (non-Javadoc)
213              * @see org.eclipse.team.ui.synchronize.ChangeSetCapability#enableActiveChangeSetsFor(org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration)
214              */

215             public boolean enableActiveChangeSetsFor(ISynchronizePageConfiguration configuration) {
216                 return super.enableActiveChangeSetsFor(configuration) ||
217                     configuration.getComparisonType() == ISynchronizePageConfiguration.TWO_WAY;
218             }
219         };
220     }
221 }
222
Popular Tags