1 11 package org.eclipse.ui.navigator; 12 13 import java.util.HashMap ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 import java.util.Set ; 18 19 import org.eclipse.core.runtime.Assert; 20 import org.eclipse.core.runtime.ISafeRunnable; 21 import org.eclipse.core.runtime.SafeRunner; 22 import org.eclipse.jface.action.GroupMarker; 23 import org.eclipse.jface.action.IContributionItem; 24 import org.eclipse.jface.action.IMenuManager; 25 import org.eclipse.jface.action.MenuManager; 26 import org.eclipse.jface.action.Separator; 27 import org.eclipse.jface.viewers.ISelectionProvider; 28 import org.eclipse.jface.viewers.StructuredSelection; 29 import org.eclipse.jface.viewers.StructuredViewer; 30 import org.eclipse.ui.IActionBars; 31 import org.eclipse.ui.IMemento; 32 import org.eclipse.ui.actions.ActionContext; 33 import org.eclipse.ui.actions.ActionGroup; 34 import org.eclipse.ui.internal.navigator.NavigatorContentService; 35 import org.eclipse.ui.internal.navigator.NavigatorPlugin; 36 import org.eclipse.ui.internal.navigator.actions.CommonActionDescriptorManager; 37 import org.eclipse.ui.internal.navigator.actions.CommonActionProviderDescriptor; 38 import org.eclipse.ui.internal.navigator.extensions.CommonActionExtensionSite; 39 import org.eclipse.ui.internal.navigator.extensions.SkeletonActionProvider; 40 41 70 public final class NavigatorActionService extends ActionGroup implements IMementoAware { 71 72 private static final IContributionItem[] DEFAULT_GROUPS = new IContributionItem[]{new Separator(ICommonMenuConstants.GROUP_NEW), new GroupMarker(ICommonMenuConstants.GROUP_GOTO), new GroupMarker(ICommonMenuConstants.GROUP_OPEN), new GroupMarker(ICommonMenuConstants.GROUP_OPEN_WITH), new Separator(ICommonMenuConstants.GROUP_EDIT), new GroupMarker(ICommonMenuConstants.GROUP_SHOW), new GroupMarker(ICommonMenuConstants.GROUP_REORGANIZE), new GroupMarker(ICommonMenuConstants.GROUP_PORT), new Separator(ICommonMenuConstants.GROUP_GENERATE), new Separator(ICommonMenuConstants.GROUP_SEARCH), new Separator(ICommonMenuConstants.GROUP_BUILD), new Separator(ICommonMenuConstants.GROUP_ADDITIONS), new Separator(ICommonMenuConstants.GROUP_PROPERTIES)}; 73 74 private final ICommonViewerSite commonViewerSite; 75 76 private final StructuredViewer structuredViewer; 77 78 private final NavigatorContentService contentService; 79 80 private final INavigatorViewerDescriptor viewerDescriptor; 81 82 private final Set actionProviderDescriptors = new HashSet (); 83 84 87 private final Map actionProviderInstances = new HashMap (); 88 89 private IMemento memento; 90 91 private IContributionItem[] menuGroups; 92 93 private boolean disposed = false; 94 95 106 public NavigatorActionService(ICommonViewerSite aCommonViewerSite, StructuredViewer aStructuredViewer, INavigatorContentService aContentService) { 107 super(); 108 Assert.isNotNull(aCommonViewerSite); 109 Assert.isNotNull(aStructuredViewer); 110 Assert.isNotNull(aContentService); 111 112 commonViewerSite = aCommonViewerSite; 113 contentService = (NavigatorContentService) aContentService; 114 structuredViewer = aStructuredViewer; 115 viewerDescriptor = contentService.getViewerDescriptor(); 116 117 } 118 119 135 public void prepareMenuForPlatformContributions(MenuManager menu, ISelectionProvider aSelectionProvider, boolean force) { 136 Assert.isTrue(!disposed); 137 138 if (commonViewerSite instanceof ICommonViewerWorkbenchSite) { 139 142 if (force || viewerDescriptor.allowsPlatformContributionsToContextMenu()) { 143 ((ICommonViewerWorkbenchSite) commonViewerSite).registerContextMenu(contentService.getViewerDescriptor().getPopupMenuId(), menu, aSelectionProvider); 144 } 145 } 146 } 147 148 161 public void fillContextMenu(IMenuManager aMenu) { 162 Assert.isTrue(!disposed); 163 164 if (menuGroups == null) { 165 createMenuGroups(); 166 } 167 168 for (int i = 0; i < menuGroups.length; i++) { 169 aMenu.add(menuGroups[i]); 170 } 171 172 addCommonActionProviderMenu(aMenu); 173 174 } 175 176 private void createMenuGroups() { 177 MenuInsertionPoint[] customPoints = viewerDescriptor.getCustomInsertionPoints(); 178 179 if (customPoints == null) { 180 menuGroups = DEFAULT_GROUPS; 181 } else { 182 menuGroups = new IContributionItem[customPoints.length]; 183 for (int i = 0; i < customPoints.length; i++) { 184 if (customPoints[i].isSeparator()) { 185 menuGroups[i] = new Separator(customPoints[i].getName()); 186 } else { 187 menuGroups[i] = new GroupMarker(customPoints[i].getName()); 188 } 189 } 190 } 191 } 192 193 196 private void addCommonActionProviderMenu(IMenuManager aMenu) { 197 198 CommonActionProviderDescriptor[] providerDescriptors = CommonActionDescriptorManager.getInstance().findRelevantActionDescriptors(contentService, getContext()); 199 if (providerDescriptors.length > 0) { 200 CommonActionProvider provider = null; 201 for (int i = 0; i < providerDescriptors.length; i++) { 202 try { 203 provider = getActionProviderInstance(providerDescriptors[i]); 204 provider.setContext(getContext()); 205 provider.fillContextMenu(aMenu); 206 } catch (Throwable t) { 207 NavigatorPlugin.logError(0, t.getMessage(), t); 208 } 209 } 210 } 211 } 212 213 222 public void fillActionBars(IActionBars theActionBars) { 223 Assert.isTrue(!disposed); 224 225 theActionBars.clearGlobalActionHandlers(); 226 ActionContext context = getContext(); 227 if (context == null) { 228 context = new ActionContext(StructuredSelection.EMPTY); 229 } 230 231 CommonActionProviderDescriptor[] providerDescriptors = CommonActionDescriptorManager.getInstance().findRelevantActionDescriptors(contentService, context); 232 if (providerDescriptors.length > 0) { 233 CommonActionProvider provider = null; 234 for (int i = 0; i < providerDescriptors.length; i++) { 235 try { 236 provider = getActionProviderInstance(providerDescriptors[i]); 237 if(provider != null) { 238 provider.setContext(context); 239 provider.fillActionBars(theActionBars); 240 provider.updateActionBars(); 241 } 242 243 } catch (RuntimeException e) { 244 NavigatorPlugin.logError(0, e.getMessage(), e); 245 } 246 } 247 } 248 theActionBars.updateActionBars(); 249 theActionBars.getMenuManager().update(); 250 } 251 252 257 public void dispose() { 258 synchronized (actionProviderInstances) { 259 for (Iterator iter = actionProviderInstances.values().iterator(); iter.hasNext();) { 260 CommonActionProvider element = (CommonActionProvider) iter.next(); 261 element.dispose(); 262 } 263 actionProviderInstances.clear(); 264 } 265 actionProviderDescriptors.clear(); 266 disposed = false; 267 } 268 269 275 public void restoreState(IMemento aMemento) { 276 Assert.isTrue(!disposed); 277 memento = aMemento; 278 279 synchronized (actionProviderInstances) { 280 for (Iterator actionProviderIterator = actionProviderInstances.values().iterator(); actionProviderIterator.hasNext();) { 281 final CommonActionProvider provider = (CommonActionProvider) actionProviderIterator.next(); 282 ISafeRunnable runnable = new ISafeRunnable() { 283 public void run() throws Exception { 284 provider.restoreState(memento); 285 } 286 287 public void handleException(Throwable exception) { 288 NavigatorPlugin.logError(0, "Could not restore state for action provider " + provider.getClass(), exception); 290 } 291 }; 292 SafeRunner.run(runnable); 293 294 } 295 } 296 } 297 298 304 public void saveState(IMemento aMemento) { 305 Assert.isTrue(!disposed); 306 307 memento = aMemento; 308 CommonActionProvider provider = null; 309 synchronized (actionProviderInstances) { 310 for (Iterator actionProviderIterator = actionProviderInstances.values().iterator(); actionProviderIterator.hasNext();) { 311 provider = (CommonActionProvider) actionProviderIterator.next(); 312 provider.saveState(memento); 313 } 314 } 315 } 316 317 private CommonActionProvider getActionProviderInstance( 318 CommonActionProviderDescriptor aProviderDescriptor) { 319 CommonActionProvider provider = null; 320 try { 321 provider = (CommonActionProvider) actionProviderInstances 322 .get(aProviderDescriptor); 323 if (provider != null) { 324 return provider; 325 } 326 synchronized (actionProviderInstances) { 327 provider = (CommonActionProvider) actionProviderInstances 328 .get(aProviderDescriptor); 329 if (provider == null) { 330 provider = aProviderDescriptor.createActionProvider(); 331 if (provider != null) { 332 initialize(aProviderDescriptor.getId(), provider); 333 actionProviderInstances.put(aProviderDescriptor, provider); 334 } else { 335 actionProviderInstances.put(aProviderDescriptor, 336 (provider = SkeletonActionProvider.INSTANCE)); 337 } 338 } 339 } 340 } catch(Throwable t) { 341 NavigatorPlugin.logError(0, t.getMessage(), t); 342 } 343 return provider; 344 } 345 346 private void initialize(String id, CommonActionProvider anActionProvider) { 347 if (anActionProvider != null && anActionProvider != SkeletonActionProvider.INSTANCE) { 348 ICommonActionExtensionSite configuration = new CommonActionExtensionSite(id, commonViewerSite, contentService, structuredViewer); 349 anActionProvider.init(configuration); 350 anActionProvider.restoreState(memento); 351 anActionProvider.setContext(new ActionContext(StructuredSelection.EMPTY)); 352 if (commonViewerSite instanceof ICommonViewerWorkbenchSite) { 353 anActionProvider.fillActionBars(((ICommonViewerWorkbenchSite) commonViewerSite).getActionBars()); 354 } 355 } 356 } 357 } 358 | Popular Tags |