KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > mappings > WorkspaceModelParticipant


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.mappings;
12
13 import org.eclipse.core.resources.mapping.ResourceMapping;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.jface.action.*;
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.osgi.util.NLS;
18 import org.eclipse.team.core.diff.IThreeWayDiff;
19 import org.eclipse.team.core.mapping.*;
20 import org.eclipse.team.core.mapping.provider.MergeContext;
21 import org.eclipse.team.core.mapping.provider.SynchronizationContext;
22 import org.eclipse.team.internal.ccvs.ui.*;
23 import org.eclipse.team.internal.ccvs.ui.actions.*;
24 import org.eclipse.team.internal.ccvs.ui.mappings.WorkspaceSubscriberContext.ChangeSetSubscriberScopeManager;
25 import org.eclipse.team.internal.ccvs.ui.subscriber.CVSActionDelegateWrapper;
26 import org.eclipse.team.internal.ui.TeamUIPlugin;
27 import org.eclipse.team.internal.ui.Utils;
28 import org.eclipse.team.internal.ui.mapping.MergeAllActionHandler;
29 import org.eclipse.team.internal.ui.synchronize.*;
30 import org.eclipse.team.ui.TeamUI;
31 import org.eclipse.team.ui.mapping.ITeamContentProviderManager;
32 import org.eclipse.team.ui.mapping.SynchronizationActionProvider;
33 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
34 import org.eclipse.team.ui.synchronize.ModelSynchronizeParticipantActionGroup;
35 import org.eclipse.ui.IMemento;
36 import org.eclipse.ui.PartInitException;
37
38 public class WorkspaceModelParticipant extends
39     CVSModelSynchronizeParticipant implements IChangeSetProvider {
40
41     public static final String JavaDoc VIEWER_ID = "org.eclipse.team.cvs.ui.workspaceSynchronization"; //$NON-NLS-1$
42

43     public static final String JavaDoc CONTEXT_MENU_UPDATE_GROUP_1 = "update"; //$NON-NLS-1$
44
public static final String JavaDoc CONTEXT_MENU_COMMIT_GROUP_1 = "commit"; //$NON-NLS-1$
45
public static final String JavaDoc CONTEXT_MENU_CONTRIBUTION_GROUP_2 = "overrideActions"; //$NON-NLS-1$
46
public static final String JavaDoc CONTEXT_MENU_CONTRIBUTION_GROUP_3 = "otherActions1"; //$NON-NLS-1$
47
public static final String JavaDoc CONTEXT_MENU_CONTRIBUTION_GROUP_4 = "otherActions2"; //$NON-NLS-1$
48

49     public static final String JavaDoc ID = "org.eclipse.team.cvs.ui.workspace-participant"; //$NON-NLS-1$
50

51     private static final String JavaDoc CTX_CONSULT_CHANGE_SETS = "consultChangeSets"; //$NON-NLS-1$
52

53     /**
54      * CVS workspace action contribution
55      */

56     public class WorkspaceMergeActionGroup extends ModelSynchronizeParticipantActionGroup {
57         private WorkspaceCommitAction commitToolbar;
58         
59         public void initialize(ISynchronizePageConfiguration configuration) {
60             configuration.setProperty(MERGE_ALL_ACTION_ID, new MergeAllActionHandler(configuration) {
61                 protected String JavaDoc getJobName() {
62                     String JavaDoc name = getConfiguration().getParticipant().getName();
63                     return NLS.bind(CVSUIMessages.WorkspaceModelParticipant_0, Utils.shortenText(SynchronizeView.MAX_NAME_LENGTH, name));
64                 }
65                 
66                 protected boolean promptToUpdate() {
67                     final IResourceDiffTree tree = getMergeContext().getDiffTree();
68                     if (tree.isEmpty()) {
69                         return false;
70                     }
71                     final long count = tree.countFor(IThreeWayDiff.INCOMING, IThreeWayDiff.DIRECTION_MASK) + tree.countFor(IThreeWayDiff.CONFLICTING, IThreeWayDiff.DIRECTION_MASK);
72                     if (count == 0)
73                         return false;
74                     final boolean[] result = new boolean[] {true};
75                     TeamUIPlugin.getStandardDisplay().syncExec(new Runnable JavaDoc() {
76                         public void run() {
77                             String JavaDoc sizeString = Long.toString(count);
78                             String JavaDoc message = tree.size() > 1 ? NLS.bind(CVSUIMessages.UpdateAction_promptForUpdateSeveral, new String JavaDoc[] { sizeString }) : NLS.bind(CVSUIMessages.UpdateAction_promptForUpdateOne, new String JavaDoc[] { sizeString }); //
79
result[0] = MessageDialog.openQuestion(getConfiguration().getSite().getShell(), NLS.bind(CVSUIMessages.UpdateAction_promptForUpdateTitle, new String JavaDoc[] { sizeString }), message);
80                  
81                         }
82                     });
83                     return result[0];
84                 }
85                 private IMergeContext getMergeContext() {
86                     return ((IMergeContext)getConfiguration().getProperty(ITeamContentProviderManager.P_SYNCHRONIZATION_CONTEXT));
87                 }
88                 protected boolean needsToSaveDirtyEditors() {
89                     int option = CVSUIPlugin.getPlugin().getPreferenceStore().getInt(ICVSUIConstants.PREF_SAVE_DIRTY_EDITORS);
90                     return option != ICVSUIConstants.OPTION_NEVER;
91                 }
92                 protected boolean confirmSaveOfDirtyEditor() {
93                     int option = CVSUIPlugin.getPlugin().getPreferenceStore().getInt(ICVSUIConstants.PREF_SAVE_DIRTY_EDITORS);
94                     return option == ICVSUIConstants.OPTION_PROMPT;
95                 }
96             });
97             super.initialize(configuration);
98             
99             int modes = configuration.getSupportedModes();
100             if ((modes & (ISynchronizePageConfiguration.OUTGOING_MODE | ISynchronizePageConfiguration.BOTH_MODE)) != 0) {
101                 appendToGroup(
102                         ISynchronizePageConfiguration.P_CONTEXT_MENU,
103                         CONTEXT_MENU_COMMIT_GROUP_1,
104                         new CommitAction(configuration));
105                 
106                 commitToolbar = new WorkspaceCommitAction(configuration);
107                 appendToGroup(
108                         ISynchronizePageConfiguration.P_TOOLBAR_MENU,
109                         MERGE_ACTION_GROUP,
110                         commitToolbar);
111                 // TODO: let's leave off override and commit for now
112
// appendToGroup(
113
// ISynchronizePageConfiguration.P_CONTEXT_MENU,
114
// CONTEXT_MENU_CONTRIBUTION_GROUP_2,
115
// new OverrideAndCommitAction(configuration));
116

117                 appendToGroup(
118                         ISynchronizePageConfiguration.P_CONTEXT_MENU,
119                         CONTEXT_MENU_CONTRIBUTION_GROUP_3,
120                         new CVSActionDelegateWrapper(new IgnoreAction(), configuration));
121             }
122             
123             if (!configuration.getSite().isModal()) {
124                 appendToGroup(
125                         ISynchronizePageConfiguration.P_CONTEXT_MENU,
126                         CONTEXT_MENU_CONTRIBUTION_GROUP_3,
127                         new CreatePatchAction(configuration));
128                 appendToGroup(
129                         ISynchronizePageConfiguration.P_CONTEXT_MENU,
130                         CONTEXT_MENU_CONTRIBUTION_GROUP_3,
131                         new CVSActionDelegateWrapper(new BranchAction(), configuration));
132                 appendToGroup(
133                         ISynchronizePageConfiguration.P_CONTEXT_MENU,
134                         CONTEXT_MENU_CONTRIBUTION_GROUP_3,
135                         new CVSActionDelegateWrapper(new ShowAnnotationAction(), configuration));
136                 appendToGroup(
137                         ISynchronizePageConfiguration.P_CONTEXT_MENU,
138                         CONTEXT_MENU_CONTRIBUTION_GROUP_3,
139                         new CVSActionDelegateWrapper(new ShowResourceInHistoryAction(), configuration));
140                 appendToGroup(
141                         ISynchronizePageConfiguration.P_CONTEXT_MENU,
142                         CONTEXT_MENU_CONTRIBUTION_GROUP_3,
143                         new CVSActionDelegateWrapper(new SetKeywordSubstitutionAction(), configuration));
144             }
145         }
146         
147         /* (non-Javadoc)
148          * @see org.eclipse.team.ui.operations.MergeActionGroup#configureMergeAction(java.lang.String, org.eclipse.jface.action.Action)
149          */

150         protected void configureMergeAction(String JavaDoc mergeActionId, Action action) {
151             if (mergeActionId == SynchronizationActionProvider.MERGE_ACTION_ID) {
152                 Utils.initAction(action, "WorkspaceUpdateAction.", Policy.getActionBundle()); //$NON-NLS-1$
153
action.setId(ICVSUIConstants.CMD_UPDATE);
154                 action.setActionDefinitionId(ICVSUIConstants.CMD_UPDATE);
155             } else if (mergeActionId == SynchronizationActionProvider.OVERWRITE_ACTION_ID) {
156                 Utils.initAction(action, "OverrideAndUpdateAction.", Policy.getActionBundle()); //$NON-NLS-1$
157
} else if (mergeActionId == SynchronizationActionProvider.MARK_AS_MERGE_ACTION_ID) {
158                 Utils.initAction(action, "ConfirmMergedAction.", Policy.getActionBundle()); //$NON-NLS-1$
159
} else if (mergeActionId == MERGE_ALL_ACTION_ID) {
160                 Utils.initAction(action, "WorkspaceToolbarUpdateAction.", Policy.getActionBundle()); //$NON-NLS-1$
161
action.setId(ICVSUIConstants.CMD_UPDATE_ALL);
162                 action.setActionDefinitionId(ICVSUIConstants.CMD_UPDATE_ALL);
163             } else {
164                 super.configureMergeAction(mergeActionId, action);
165             }
166         }
167         
168         protected void addToContextMenu(String JavaDoc mergeActionId, Action action, IMenuManager manager) {
169             IContributionItem group = null;;
170             if (mergeActionId == SynchronizationActionProvider.MERGE_ACTION_ID) {
171                 group = manager.find(CONTEXT_MENU_UPDATE_GROUP_1);
172             } else if (mergeActionId == SynchronizationActionProvider.OVERWRITE_ACTION_ID) {
173                 group = manager.find(CONTEXT_MENU_CONTRIBUTION_GROUP_2);
174             } else if (mergeActionId == SynchronizationActionProvider.MARK_AS_MERGE_ACTION_ID) {
175                 group = manager.find(CONTEXT_MENU_CONTRIBUTION_GROUP_2);
176             } else {
177                 super.addToContextMenu(mergeActionId, action, manager);
178                 return;
179             }
180             if (group != null) {
181                 manager.appendToGroup(group.getId(), action);
182             } else {
183                 manager.add(action);
184             }
185         }
186     }
187
188     private WorkspaceChangeSetCapability capability;
189
190     private boolean isConsultChangeSets;
191     
192     public WorkspaceModelParticipant() {
193     }
194     
195     public WorkspaceModelParticipant(SynchronizationContext context) {
196         super(context);
197         try {
198             setInitializationData(TeamUI.getSynchronizeManager().getParticipantDescriptor("org.eclipse.team.cvs.ui.workspace-participant")); //$NON-NLS-1$
199
} catch (CoreException e) {
200             TeamUIPlugin.log(e);
201         }
202         setSecondaryId(Long.toString(System.currentTimeMillis()));
203         isConsultChangeSets = isConsultChangeSets(context.getScopeManager());
204     }
205     
206     /* (non-Javadoc)
207      * @see org.eclipse.team.ui.operations.ModelSynchronizeParticipant#initializeConfiguration(org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration)
208      */

209     protected void initializeConfiguration(ISynchronizePageConfiguration configuration) {
210         configuration.setProperty(ISynchronizePageConfiguration.P_VIEWER_ID, VIEWER_ID);
211         super.initializeConfiguration(configuration);
212     }
213     
214     /* (non-Javadoc)
215      * @see org.eclipse.team.ui.operations.ResourceMappingSynchronizeParticipant#createMergeActionGroup()
216      */

217     protected ModelSynchronizeParticipantActionGroup createMergeActionGroup() {
218         return new WorkspaceMergeActionGroup();
219     }
220     
221     /* (non-Javadoc)
222      * @see org.eclipse.team.ui.operations.ModelSynchronizeParticipant#restoreContext(org.eclipse.team.core.mapping.IResourceMappingScope, org.eclipse.core.runtime.IProgressMonitor)
223      */

224     protected MergeContext restoreContext(ISynchronizationScopeManager manager) {
225         return WorkspaceSubscriberContext.createContext(manager, ISynchronizationContext.THREE_WAY);
226     }
227     
228     /* (non-Javadoc)
229      * @see org.eclipse.team.ui.operations.ModelSynchronizeParticipant#createScopeManager(org.eclipse.core.resources.mapping.ResourceMapping[])
230      */

231     protected ISynchronizationScopeManager createScopeManager(ResourceMapping[] mappings) {
232         return WorkspaceSubscriberContext.createWorkspaceScopeManager(mappings, true, isConsultChangeSets);
233     }
234     
235     public ChangeSetCapability getChangeSetCapability() {
236         if (capability == null) {
237             capability = new WorkspaceChangeSetCapability();
238         }
239         return capability;
240     }
241     
242     public void saveState(IMemento memento) {
243         super.saveState(memento);
244         memento.putString(CTX_CONSULT_CHANGE_SETS, Boolean.toString(isConsultChangeSets));
245     }
246     
247     public void init(String JavaDoc secondaryId, IMemento memento) throws PartInitException {
248         try {
249             String JavaDoc consult = memento.getString(CTX_CONSULT_CHANGE_SETS);
250             if (consult != null)
251                 isConsultChangeSets = Boolean.valueOf(consult).booleanValue();
252         } finally {
253             super.init(secondaryId, memento);
254         }
255     }
256
257     private boolean isConsultChangeSets(ISynchronizationScopeManager manager) {
258         if (manager instanceof ChangeSetSubscriberScopeManager) {
259             ChangeSetSubscriberScopeManager man = (ChangeSetSubscriberScopeManager) manager;
260             return man.isConsultSets();
261         }
262         return false;
263     }
264     
265 }
266
Popular Tags