1 11 package org.eclipse.team.ui.synchronize; 12 13 import java.util.*; 14 15 import org.eclipse.core.runtime.ListenerList; 16 import org.eclipse.core.runtime.SafeRunner; 17 import org.eclipse.jface.action.*; 18 import org.eclipse.jface.util.SafeRunnable; 19 import org.eclipse.jface.viewers.*; 20 import org.eclipse.team.internal.ui.synchronize.SynchronizePageConfiguration; 21 import org.eclipse.ui.IActionBars; 22 import org.eclipse.ui.IKeyBindingService; 23 import org.eclipse.ui.actions.ActionGroup; 24 25 59 public abstract class SynchronizePageActionGroup extends ActionGroup { 60 61 private ISynchronizePageConfiguration configuration; 62 63 private Map menuContributions = new HashMap(); 64 65 private VisibleRootsSelectionProvider visibleRootSelectionProvider; 66 67 72 private class VisibleRootsSelectionProvider extends SynchronizePageActionGroup implements ISelectionProvider { 73 74 private ListenerList selectionChangedListeners = new ListenerList(ListenerList.IDENTITY); 75 private ISelection selection; 76 77 protected VisibleRootsSelectionProvider(ISynchronizeModelElement element) { 78 modelChanged(element); 79 } 80 81 84 public void modelChanged(ISynchronizeModelElement root) { 85 if (root == null) { 86 setSelection(StructuredSelection.EMPTY); 87 } else { 88 setSelection(new StructuredSelection(root)); 89 } 90 } 91 92 95 public void addSelectionChangedListener(ISelectionChangedListener listener) { 96 selectionChangedListeners.add(listener); 97 } 98 99 102 public void removeSelectionChangedListener(ISelectionChangedListener listener) { 103 selectionChangedListeners.remove(listener); 104 } 105 106 109 public ISelection getSelection() { 110 return selection; 111 } 112 113 116 public void setSelection(ISelection selection) { 117 this.selection = selection; 118 selectionChanged(new SelectionChangedEvent(this, getSelection())); 119 } 120 121 private void selectionChanged(final SelectionChangedEvent event) { 122 Object [] listeners = selectionChangedListeners.getListeners(); 124 for (int i = 0; i < listeners.length; ++i) { 125 final ISelectionChangedListener l = (ISelectionChangedListener)listeners[i]; 126 SafeRunner.run(new SafeRunnable() { 127 public void run() { 128 l.selectionChanged(event); 129 } 130 }); 131 } 132 } 133 } 134 135 146 public void initialize(ISynchronizePageConfiguration configuration) { 147 this.configuration = configuration; 148 if (visibleRootSelectionProvider != null) { 149 configuration.addActionContribution(visibleRootSelectionProvider); 150 } 151 } 152 153 161 public void modelChanged(ISynchronizeModelElement root) { 162 } 164 165 169 public void dispose() { 170 super.dispose(); 171 if (configuration != null) { 172 configuration.removeActionContribution(this); 173 } 174 } 175 176 186 protected IContributionItem findGroup(IContributionManager menu, String groupId) { 187 if(menu == null) return null; 188 IContributionItem item = menu.find(((SynchronizePageConfiguration)configuration).getGroupId(groupId)); 189 if (item == null) { 190 item = menu.find(groupId); 192 } 193 return item; 194 } 195 196 207 protected boolean appendToGroup(IContributionManager manager, String groupId, IAction action) { 208 if (internalAppendToGroup(manager, groupId, action)) { 209 registerActionWithWorkbench(action); 210 return true; 211 } 212 return false; 213 } 214 215 private boolean internalAppendToGroup(IContributionManager manager, String groupId, IAction action) { 216 if (manager == null || action == null) return false; 217 IContributionItem group = findGroup(manager, groupId); 218 if (group != null) { 219 manager.appendToGroup(group.getId(), action); 220 return true; 221 } 222 return false; 223 } 224 225 237 protected boolean appendToGroup(IContributionManager manager, String groupId, IContributionItem item) { 238 if (manager == null || item == null) return false; 239 IContributionItem group = findGroup(manager, groupId); 240 if (group != null) { 241 manager.appendToGroup(group.getId(), item); 242 return true; 243 } 244 return false; 245 } 246 247 259 protected void appendToGroup(String menuId, String groupId, IAction action) { 260 registerActionWithWorkbench(action); 261 internalAppendToGroup(menuId, groupId, action); 262 } 263 264 270 private void registerActionWithWorkbench(IAction action) { 271 ISynchronizePageSite site = configuration.getSite(); 272 String id = action.getId(); 273 if (id != null) { 274 site.getActionBars().setGlobalActionHandler(id, action); 275 IKeyBindingService keyBindingService = site.getKeyBindingService(); 276 if(keyBindingService != null) 277 keyBindingService.registerAction(action); 278 } 279 } 280 281 292 protected void appendToGroup(String menuId, String groupId, IContributionItem item) { 293 internalAppendToGroup(menuId, groupId, item); 294 } 295 296 304 protected ISelectionProvider getVisibleRootsSelectionProvider() { 305 if (visibleRootSelectionProvider == null) { 306 ISynchronizeModelElement root = null; 307 if (configuration != null) { 308 root = (ISynchronizeModelElement)configuration.getProperty(SynchronizePageConfiguration.P_MODEL); 309 } 310 visibleRootSelectionProvider = new VisibleRootsSelectionProvider(root); 311 if (configuration != null) { 312 configuration.addActionContribution(visibleRootSelectionProvider); 313 } 314 } 315 return visibleRootSelectionProvider; 316 } 317 318 321 public void fillContextMenu(IMenuManager menu) { 322 super.fillContextMenu(menu); 323 fillMenu(menu, ISynchronizePageConfiguration.P_CONTEXT_MENU); 324 } 325 326 329 public void fillActionBars(IActionBars actionBars) { 330 super.fillActionBars(actionBars); 331 if (actionBars != null) { 332 fillMenu(actionBars.getMenuManager(), ISynchronizePageConfiguration.P_VIEW_MENU); 333 fillMenu(actionBars.getToolBarManager(), ISynchronizePageConfiguration.P_TOOLBAR_MENU); 334 } 335 } 336 337 private void fillMenu(IContributionManager menu, String menuId) { 338 Map groups = (Map)menuContributions.get(menuId); 339 if (menu != null && groups != null) { 340 for (Iterator iter = groups.keySet().iterator(); iter.hasNext(); ) { 341 String groupId = (String ) iter.next(); 342 List actions = (List)groups.get(groupId); 343 if (actions != null) { 344 for (Iterator iter2 = actions.iterator(); iter2.hasNext();) { 345 Object element = iter2.next(); 346 if (element instanceof IAction) { 347 internalAppendToGroup(menu, groupId, (IAction)element); 351 } else if (element instanceof IContributionItem) { 352 appendToGroup(menu, groupId, (IContributionItem)element); 353 } 354 } 355 } 356 } 357 } 358 } 359 360 private void internalAppendToGroup(String menuId, String groupId, Object action) { 361 Map groups = (Map)menuContributions.get(menuId); 362 if (groups == null) { 363 groups = new HashMap(); 364 menuContributions.put(menuId, groups); 365 } 366 List actions = (List)groups.get(groupId); 367 if (actions == null) { 368 actions = new ArrayList(); 369 groups.put(groupId, actions); 370 } 371 actions.add(action); 372 } 373 374 382 public ISynchronizePageConfiguration getConfiguration() { 383 return configuration; 384 } 385 } 386 | Popular Tags |