1 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 42 public class ModelSynchronizeParticipantActionGroup extends SynchronizePageActionGroup { 43 44 48 public static final String MERGE_ACTION_GROUP = "merge"; 50 54 public static final String OTHER_ACTION_GROUP = "other"; 56 59 protected static final String MERGE_ALL_ACTION_ID = "org.eclipse.team.ui.mergeAll"; 61 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 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 } 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 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 [] 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 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 192 private void registerActionWithWorkbench(IAction action) { 193 ISynchronizePageSite site = getConfiguration().getSite(); 194 String 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 213 protected void configureMergeAction(String mergeActionId, Action action) { 214 if (mergeActionId == SynchronizationActionProvider.MERGE_ACTION_ID) { 215 Utils.initAction(action, "action.merge."); } else if (mergeActionId == SynchronizationActionProvider.OVERWRITE_ACTION_ID) { 217 if (isTwoWayMerge()) { 218 Utils.initAction(action, "action.replace."); } else { 220 Utils.initAction(action, "action.overwrite."); } 222 } else if (mergeActionId == SynchronizationActionProvider.MARK_AS_MERGE_ACTION_ID) { 223 Utils.initAction(action, "action.markAsMerged."); } else if (mergeActionId == MERGE_ALL_ACTION_ID) { 225 if (isTwoWayMerge()) { 226 Utils.initAction(action, "action.replaceAll."); } else { 228 Utils.initAction(action, "action.mergeAll."); } 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 254 protected void addToContextMenu(String 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 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 |