1 11 package org.eclipse.ui.part; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 17 import org.eclipse.core.expressions.Expression; 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.Platform; 20 import org.eclipse.jface.action.MenuManager; 21 import org.eclipse.jface.viewers.ISelectionProvider; 22 import org.eclipse.swt.widgets.Shell; 23 import org.eclipse.ui.IActionBars; 24 import org.eclipse.ui.IViewSite; 25 import org.eclipse.ui.IWorkbenchPage; 26 import org.eclipse.ui.IWorkbenchPart; 27 import org.eclipse.ui.IWorkbenchWindow; 28 import org.eclipse.ui.SubActionBars; 29 import org.eclipse.ui.commands.ICommandService; 30 import org.eclipse.ui.contexts.IContextService; 31 import org.eclipse.ui.handlers.IHandlerService; 32 import org.eclipse.ui.internal.PartSite; 33 import org.eclipse.ui.internal.PopupMenuExtender; 34 import org.eclipse.ui.internal.commands.SlaveCommandService; 35 import org.eclipse.ui.internal.contexts.NestableContextService; 36 import org.eclipse.ui.internal.expressions.ActivePartExpression; 37 import org.eclipse.ui.internal.handlers.NestableHandlerService; 38 import org.eclipse.ui.internal.services.INestable; 39 import org.eclipse.ui.internal.services.ServiceLocator; 40 import org.eclipse.ui.services.IServiceScopes; 41 42 47 public class PageSite implements IPageSite, INestable { 48 49 52 private ArrayList menuExtenders; 53 54 57 private IViewSite parentSite; 58 59 63 private ISelectionProvider selectionProvider; 64 65 69 private final ServiceLocator serviceLocator; 70 71 74 private SubActionBars subActionBars; 75 76 82 public PageSite(IViewSite parentViewSite) { 83 Assert.isNotNull(parentViewSite); 84 parentSite = parentViewSite; 85 subActionBars = new SubActionBars(parentViewSite.getActionBars(), this); 86 87 this.serviceLocator = new ServiceLocator(parentSite); 89 initializeDefaultServices(); 90 } 91 92 95 private void initializeDefaultServices() { 96 final IWorkbenchPart parentPart = parentSite.getPart(); 97 final Expression defaultExpression = new ActivePartExpression( 98 parentPart); 99 100 final IHandlerService parentService = (IHandlerService) parentSite 101 .getService(IHandlerService.class); 102 final IHandlerService slave = new NestableHandlerService(parentService, 103 defaultExpression); 104 serviceLocator.registerService(IHandlerService.class, slave); 105 106 final IContextService contextParent = (IContextService) serviceLocator 107 .getService(IContextService.class); 108 final IContextService contextSlave = new NestableContextService( 109 contextParent, defaultExpression); 110 serviceLocator.registerService(IContextService.class, contextSlave); 111 112 final ICommandService parentCommandService = (ICommandService) serviceLocator 113 .getService(ICommandService.class); 114 final ICommandService commandService = new SlaveCommandService( 115 parentCommandService, IServiceScopes.PAGESITE_SCOPE, 116 this); 117 serviceLocator.registerService(ICommandService.class, commandService); 118 119 } 120 121 124 protected void dispose() { 125 if (menuExtenders != null) { 126 HashSet managers = new HashSet (menuExtenders.size()); 127 for (int i = 0; i < menuExtenders.size(); i++) { 128 PopupMenuExtender ext = (PopupMenuExtender) menuExtenders.get(i); 129 managers.add(ext.getManager()); 130 ext.dispose(); 131 } 132 if (managers.size()>0) { 133 for (Iterator iterator = managers.iterator(); iterator 134 .hasNext();) { 135 MenuManager mgr = (MenuManager) iterator.next(); 136 mgr.dispose(); 137 } 138 } 139 menuExtenders = null; 140 } 141 subActionBars.dispose(); 142 serviceLocator.dispose(); 143 } 144 145 151 public IActionBars getActionBars() { 152 return subActionBars; 153 } 154 155 160 public Object getAdapter(Class adapter) { 161 return Platform.getAdapterManager().getAdapter(this, adapter); 162 } 163 164 167 public IWorkbenchPage getPage() { 168 return parentSite.getPage(); 169 } 170 171 174 public ISelectionProvider getSelectionProvider() { 175 return selectionProvider; 176 } 177 178 public final Object getService(final Class key) { 179 return serviceLocator.getService(key); 180 } 181 182 185 public Shell getShell() { 186 return parentSite.getShell(); 187 } 188 189 192 public IWorkbenchWindow getWorkbenchWindow() { 193 return parentSite.getWorkbenchWindow(); 194 } 195 196 public final boolean hasService(final Class key) { 197 return serviceLocator.hasService(key); 198 } 199 200 203 public void registerContextMenu(String menuID, MenuManager menuMgr, 204 ISelectionProvider selProvider) { 205 if (menuExtenders == null) { 206 menuExtenders = new ArrayList (1); 207 } 208 PartSite.registerContextMenu(menuID, menuMgr, selProvider, false, 209 parentSite.getPart(), menuExtenders); 210 } 211 212 215 public void setSelectionProvider(ISelectionProvider provider) { 216 selectionProvider = provider; 217 } 218 219 226 public void activate() { 227 serviceLocator.activate(); 228 } 229 230 237 public void deactivate() { 238 serviceLocator.deactivate(); 239 } 240 } 241 | Popular Tags |