KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.ui.synchronize;
12
13 import org.eclipse.jface.action.*;
14 import org.eclipse.jface.viewers.IStructuredSelection;
15 import org.eclipse.team.core.mapping.IMergeContext;
16 import org.eclipse.team.core.mapping.ISynchronizationContext;
17 import org.eclipse.team.internal.ui.Utils;
18 import org.eclipse.team.internal.ui.mapping.*;
19 import org.eclipse.team.internal.ui.synchronize.SynchronizePageConfiguration;
20 import org.eclipse.team.internal.ui.synchronize.actions.OpenInCompareAction;
21 import org.eclipse.team.internal.ui.synchronize.actions.SyncViewerShowPreferencesAction;
22 import org.eclipse.team.ui.mapping.SynchronizationActionProvider;
23 import org.eclipse.ui.*;
24
25 /**
26  * Action group that contributes the merge actions to the model
27  * synchronize participant. The groups adds the following:
28  * <ul>
29  * <li>A toolbar action for attempting an auto-merge
30  * <li>Context menu merge actions that delegate to the
31  * model's merge action handlers.
32  * <li>TODO a merge all and overwrite all menu item?
33  * </ul>
34  * <p>
35  * Subclasses can configure the label and icons used for the merge actions
36  * by overriding {@link #configureMergeAction(String, Action)} and can
37  * configure where in the context menu the actions appear by overriding
38  * {@link #addToContextMenu(String, Action, IMenuManager)}.
39  *
40  * @since 3.2
41  **/

42 public class ModelSynchronizeParticipantActionGroup extends SynchronizePageActionGroup {
43
44     /**
45      * The id of the merge action group that determines where the merge
46      * actions (e.g. merge and overwrite) appear in the context menu or toolbar.
47      */

48     public static final String JavaDoc MERGE_ACTION_GROUP = "merge"; //$NON-NLS-1$
49

50     /**
51      * The id of the action group that determines where the other
52      * actions (e.g. mark-as-mered) appear in the context menu.
53      */

54     public static final String JavaDoc OTHER_ACTION_GROUP = "other"; //$NON-NLS-1$
55

56     /**
57      * The id used to identify the Merge All action.
58      */

59     protected static final String JavaDoc MERGE_ALL_ACTION_ID = "org.eclipse.team.ui.mergeAll"; //$NON-NLS-1$
60

61     /**
62      * Create a merge action group.
63      */

64     public ModelSynchronizeParticipantActionGroup() {
65     }
66
67     private MergeIncomingChangesAction updateToolbarAction;
68     private ModelSelectionDropDownAction modelPicker;
69     private SyncViewerShowPreferencesAction showPreferences;
70     private OpenInCompareAction openInCompareAction;
71     private MergeAction merge;
72     private MergeAction overwrite;
73     private MergeAction markAsMerged;
74     
75     /* (non-Javadoc)
76      * @see org.eclipse.team.ui.synchronize.SynchronizePageActionGroup#initialize(org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration)
77      */

78     public void initialize(ISynchronizePageConfiguration configuration) {
79         super.initialize(configuration);
80         
81         ModelSynchronizeParticipant participant = ((ModelSynchronizeParticipant)configuration.getParticipant());
82         if (participant.isMergingEnabled()) {
83             updateToolbarAction = new MergeIncomingChangesAction(configuration);
84             configureMergeAction(MERGE_ALL_ACTION_ID, updateToolbarAction);
85             appendToGroup(
86                     ISynchronizePageConfiguration.P_TOOLBAR_MENU,
87                     MERGE_ACTION_GROUP,
88                     updateToolbarAction);
89             // TODO: Should add a merge all to the context menu as well?
90
}
91         modelPicker = new ModelSelectionDropDownAction(configuration);
92         appendToGroup(
93                 ISynchronizePageConfiguration.P_TOOLBAR_MENU,
94                 ISynchronizePageConfiguration.NAVIGATE_GROUP,
95                 modelPicker);
96         ISynchronizePageSite site = configuration.getSite();
97         IWorkbenchSite ws = site.getWorkbenchSite();
98         if (ws instanceof IViewSite) {
99             showPreferences = new SyncViewerShowPreferencesAction(configuration);
100             openInCompareAction = new OpenInCompareAction(configuration);
101             configuration.setProperty(SynchronizePageConfiguration.P_OPEN_ACTION, new Action() {
102                 public void run() {
103                     openInCompareAction.run();
104                 }
105             });
106         }
107     }
108     
109     public void fillActionBars(IActionBars actionBars) {
110         super.fillActionBars(actionBars);
111         if (actionBars != null && showPreferences != null) {
112             IMenuManager menu = actionBars.getMenuManager();
113             appendToGroup(menu, ISynchronizePageConfiguration.PREFERENCES_GROUP, showPreferences);
114         }
115     }
116     
117     /* (non-Javadoc)
118      * @see org.eclipse.team.ui.synchronize.SynchronizePageActionGroup#fillContextMenu(org.eclipse.jface.action.IMenuManager)
119      */

120     public void fillContextMenu(IMenuManager menu) {
121         super.fillContextMenu(menu);
122         if (menu instanceof CommonMenuManager) {
123             CommonMenuManager cmm = (CommonMenuManager) menu;
124             addMergeActions(cmm);
125         }
126         Object JavaDoc[] elements = ((IStructuredSelection)getContext().getSelection()).toArray();
127         if (elements.length > 0 && openInCompareAction != null) {
128             IContributionItem fileGroup = findGroup(menu, ISynchronizePageConfiguration.FILE_GROUP);
129             if (fileGroup != null) {
130                 ModelSynchronizeParticipant msp = ((ModelSynchronizeParticipant)getConfiguration().getParticipant());
131                 boolean allElementsHaveCompareInput = true;
132                 for (int i = 0; i < elements.length; i++) {
133                     if (!msp.hasCompareInputFor(elements[i])) {
134                         allElementsHaveCompareInput = false;
135                         break;
136                     }
137                 }
138                 if (allElementsHaveCompareInput) {
139                     menu.appendToGroup(fileGroup.getId(), openInCompareAction);
140                 }
141             }
142         }
143     }
144     
145     /*
146      * Method to add the merge actions to the context menu. This method
147      * is called by the internal synchronization framework and should not
148      * to be invoked by other clients. Subclasses can configure the
149      * merge actions by overriding {@link #configureMergeAction(String, Action)}
150      * and can control where in the context menu the action appears by
151      * overriding {@link #addToContextMenu(String, Action, IMenuManager)}.
152      * @param cmm the menu manager
153      */

154     private void addMergeActions(CommonMenuManager cmm) {
155         ModelSynchronizeParticipant participant = ((ModelSynchronizeParticipant)getConfiguration().getParticipant());
156         if (participant.isMergingEnabled()) {
157             if (!isTwoWayMerge()) {
158                 if (merge == null) {
159                     merge = new MergeAction(SynchronizationActionProvider.MERGE_ACTION_ID, cmm, getConfiguration());
160                     configureMergeAction(SynchronizationActionProvider.MERGE_ACTION_ID, merge);
161                     registerActionWithWorkbench(merge);
162                     
163                 }
164                 merge.update();
165                 addToContextMenu(SynchronizationActionProvider.MERGE_ACTION_ID, merge, cmm);
166             }
167             if (overwrite == null) {
168                 overwrite = new MergeAction(SynchronizationActionProvider.OVERWRITE_ACTION_ID, cmm, getConfiguration());
169                 configureMergeAction(SynchronizationActionProvider.OVERWRITE_ACTION_ID, overwrite);
170                 registerActionWithWorkbench(overwrite);
171             }
172             overwrite.update();
173             addToContextMenu(SynchronizationActionProvider.OVERWRITE_ACTION_ID, overwrite, cmm);
174             if (!isTwoWayMerge()) {
175                 if (markAsMerged == null) {
176                     markAsMerged = new MergeAction(SynchronizationActionProvider.MARK_AS_MERGE_ACTION_ID, cmm, getConfiguration());
177                     configureMergeAction(SynchronizationActionProvider.MARK_AS_MERGE_ACTION_ID, markAsMerged);
178                 }
179                 markAsMerged.update();
180                 addToContextMenu(SynchronizationActionProvider.MARK_AS_MERGE_ACTION_ID, markAsMerged, cmm);
181                 registerActionWithWorkbench(markAsMerged);
182             }
183         }
184     }
185     
186     /**
187      * Register this action with the workbench so that it can participate in keybindings and
188      * retargetable actions.
189      *
190      * @param action the action to register
191      */

192     private void registerActionWithWorkbench(IAction action) {
193         ISynchronizePageSite site = getConfiguration().getSite();
194         String JavaDoc id = action.getId();
195         if (id != null) {
196             site.getActionBars().setGlobalActionHandler(id, action);
197             IKeyBindingService keyBindingService = site.getKeyBindingService();
198             if(keyBindingService != null)
199                 keyBindingService.registerAction(action);
200         }
201     }
202     
203     /**
204      * Configure the merge action to have appropriate label, image, etc.
205      * Subclasses may override but should invoke the overridden
206      * method for unrecognized ids in order to support future additions.
207      * @param mergeActionId the id of the merge action (one of
208      * {@link SynchronizationActionProvider#MERGE_ACTION_ID},
209      * {@link SynchronizationActionProvider#OVERWRITE_ACTION_ID} or
210      * {@link SynchronizationActionProvider#MARK_AS_MERGE_ACTION_ID})
211      * @param action the action for the given id
212      */

213     protected void configureMergeAction(String JavaDoc mergeActionId, Action action) {
214         if (mergeActionId == SynchronizationActionProvider.MERGE_ACTION_ID) {
215             Utils.initAction(action, "action.merge."); //$NON-NLS-1$
216
} else if (mergeActionId == SynchronizationActionProvider.OVERWRITE_ACTION_ID) {
217             if (isTwoWayMerge()) {
218                 Utils.initAction(action, "action.replace."); //$NON-NLS-1$
219
} else {
220                 Utils.initAction(action, "action.overwrite."); //$NON-NLS-1$
221
}
222         } else if (mergeActionId == SynchronizationActionProvider.MARK_AS_MERGE_ACTION_ID) {
223             Utils.initAction(action, "action.markAsMerged."); //$NON-NLS-1$
224
} else if (mergeActionId == MERGE_ALL_ACTION_ID) {
225             if (isTwoWayMerge()) {
226                 Utils.initAction(action, "action.replaceAll."); //$NON-NLS-1$
227
} else {
228                 Utils.initAction(action, "action.mergeAll."); //$NON-NLS-1$
229
}
230         }
231     }
232     
233     private boolean isTwoWayMerge() {
234         ModelSynchronizeParticipant participant = ((ModelSynchronizeParticipant)getConfiguration().getParticipant());
235         ISynchronizationContext context = participant.getContext();
236         if (context instanceof IMergeContext) {
237             IMergeContext mc = (IMergeContext) context;
238             return (mc.getMergeType() == ISynchronizationContext.TWO_WAY);
239         }
240         return false;
241     }
242     
243     /**
244      * Add the merge action to the context menu manager.
245      * Subclasses may override but should invoke the overridden
246      * method for unrecognized ids in order to support future additions.
247      * @param mergeActionId the id of the merge action (one of
248      * {@link SynchronizationActionProvider#MERGE_ACTION_ID},
249      * {@link SynchronizationActionProvider#OVERWRITE_ACTION_ID} or
250      * {@link SynchronizationActionProvider#MARK_AS_MERGE_ACTION_ID})
251      * @param action the action for the given id
252      * @param manager the context menu manager
253      */

254     protected void addToContextMenu(String JavaDoc mergeActionId, Action action, IMenuManager manager) {
255         IContributionItem group = null;;
256         if (mergeActionId == SynchronizationActionProvider.MERGE_ACTION_ID) {
257             group = manager.find(MERGE_ACTION_GROUP);
258         } else if (mergeActionId == SynchronizationActionProvider.OVERWRITE_ACTION_ID) {
259             group = manager.find(MERGE_ACTION_GROUP);
260         } else if (mergeActionId == SynchronizationActionProvider.MARK_AS_MERGE_ACTION_ID) {
261             group = manager.find(OTHER_ACTION_GROUP);
262         }
263         if (group != null) {
264             manager.appendToGroup(group.getId(), action);
265         } else {
266             manager.add(action);
267         }
268     }
269     
270     /* (non-Javadoc)
271      * @see org.eclipse.team.ui.synchronize.SynchronizePageActionGroup#dispose()
272      */

273     public void dispose() {
274         if (modelPicker != null)
275             modelPicker.dispose();
276         if (merge != null)
277             merge.dispose();
278         if (overwrite != null)
279             overwrite.dispose();
280         if (markAsMerged != null)
281             markAsMerged.dispose();
282         if (updateToolbarAction != null)
283             updateToolbarAction.dispose();
284         super.dispose();
285     }
286 }
287
Popular Tags