1 11 package org.eclipse.debug.internal.ui.contexts; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import org.eclipse.core.runtime.ISafeRunnable; 19 import org.eclipse.core.runtime.ListenerList; 20 import org.eclipse.core.runtime.SafeRunner; 21 import org.eclipse.debug.internal.ui.DebugUIPlugin; 22 import org.eclipse.debug.ui.contexts.DebugContextEvent; 23 import org.eclipse.debug.ui.contexts.IDebugContextListener; 24 import org.eclipse.debug.ui.contexts.IDebugContextProvider; 25 import org.eclipse.debug.ui.contexts.IDebugContextService; 26 import org.eclipse.jface.viewers.ISelection; 27 import org.eclipse.jface.viewers.StructuredSelection; 28 import org.eclipse.ui.IPartListener2; 29 import org.eclipse.ui.IWorkbenchPage; 30 import org.eclipse.ui.IWorkbenchPart; 31 import org.eclipse.ui.IWorkbenchPartReference; 32 import org.eclipse.ui.IWorkbenchWindow; 33 34 39 public class DebugWindowContextService implements IDebugContextService, IPartListener2, IDebugContextListener { 40 41 private Map fListenersByPartId = new HashMap (); 42 private Map fProvidersByPartId = new HashMap (); 43 private Map fPostListenersByPartId = new HashMap (); 44 45 private IWorkbenchWindow fWindow; 46 private List fProviders = new ArrayList (); 47 48 public DebugWindowContextService(IWorkbenchWindow window) { 49 fWindow = window; 50 fWindow.getPartService().addPartListener(this); 51 } 52 53 public void dispose() { 54 fWindow.getPartService().removePartListener(this); 55 fWindow = null; 56 } 57 58 public synchronized void addDebugContextProvider(IDebugContextProvider provider) { 59 IWorkbenchPart part = provider.getPart(); 60 String id = null; 61 if (part != null) { 62 id = part.getSite().getId(); 63 } 64 fProvidersByPartId.put(id, provider); 65 fProviders.add(provider); 66 IWorkbenchPart active = null; 67 IWorkbenchPage activePage = fWindow.getActivePage(); 68 if (activePage != null) { 69 active = activePage.getActivePart(); 70 } 71 if (fProviders.size() == 1 && (part == null || part.equals(active))) { 72 notify(provider); 73 } 74 provider.addDebugContextListener(this); 75 } 76 77 public synchronized void removeDebugContextProvider(IDebugContextProvider provider) { 78 int index = fProviders.indexOf(provider); 79 if (index >= 0) { 80 IWorkbenchPart part = provider.getPart(); 81 String id = null; 82 if (part != null) { 83 id = part.getSite().getId(); 84 } 85 fProvidersByPartId.remove(id); 86 fProviders.remove(index); 87 if (index == 0) { 88 IDebugContextProvider activeProvider = getActiveProvider(); 89 if (activeProvider != null) { 90 notify(activeProvider); 91 } else { 92 notify(new DebugContextEvent(provider, new StructuredSelection(), DebugContextEvent.ACTIVATED)); 93 } 94 } 95 } 96 provider.removeDebugContextListener(this); 97 } 98 99 102 public void addDebugContextListener(IDebugContextListener listener) { 103 addDebugContextListener(listener, null); 104 } 105 106 public void addPostDebugContextListener(IDebugContextListener listener, String partId) { 107 ListenerList list = (ListenerList) fPostListenersByPartId.get(partId); 108 if (list == null) { 109 list = new ListenerList(); 110 fPostListenersByPartId.put(partId, list); 111 } 112 list.add(listener); 113 } 114 115 public void addPostDebugContextListener(IDebugContextListener listener) { 116 addPostDebugContextListener(listener, null); 117 } 118 119 public void removePostDebugContextListener(IDebugContextListener listener, String partId) { 120 ListenerList list = (ListenerList) fPostListenersByPartId.get(partId); 121 if (list != null) { 122 list.remove(listener); 123 } 124 } 125 126 public void removePostDebugContextListener(IDebugContextListener listener) { 127 removePostDebugContextListener(listener, null); 128 } 129 130 133 public void removeDebugContextListener(IDebugContextListener listener) { 134 removeDebugContextListener(listener, null); 135 } 136 137 142 protected void notify(IDebugContextProvider provdier) { 143 ISelection activeContext = provdier.getActiveContext(); 144 if (activeContext == null) { 145 activeContext = new StructuredSelection(); 146 } 147 notify(new DebugContextEvent(provdier, activeContext, DebugContextEvent.ACTIVATED)); 148 } 149 150 protected void notify(DebugContextEvent event) { 151 notify(event, getListeners(null)); 152 IWorkbenchPart part = event.getDebugContextProvider().getPart(); 153 if (part != null) { 154 notify(event, getListeners(part)); 155 } 156 notify(event, getPostListeners(null)); 157 if (part != null) { 158 notify(event, getPostListeners(part)); 159 } 160 } 161 162 protected void notify(final DebugContextEvent event, ListenerList list) { 163 if (list != null) { 164 Object [] listeners = list.getListeners(); 165 for (int i = 0; i < listeners.length; i++) { 166 final IDebugContextListener listener = (IDebugContextListener) listeners[i]; 167 SafeRunner.run(new ISafeRunnable() { 168 public void run() throws Exception { 169 listener.debugContextChanged(event); 170 } 171 public void handleException(Throwable exception) { 172 DebugUIPlugin.log(exception); 173 } 174 }); 175 } 176 } 177 } 178 179 protected ListenerList getListeners(IWorkbenchPart part) { 180 String id = null; 181 if (part != null) { 182 id = part.getSite().getId(); 183 } 184 return (ListenerList) fListenersByPartId.get(id); 185 } 186 187 protected ListenerList getPostListeners(IWorkbenchPart part) { 188 String id = null; 189 if (part != null) { 190 id = part.getSite().getId(); 191 } 192 return (ListenerList) fPostListenersByPartId.get(id); 193 } 194 195 198 public synchronized void addDebugContextListener(IDebugContextListener listener, String partId) { 199 ListenerList list = (ListenerList) fListenersByPartId.get(partId); 200 if (list == null) { 201 list = new ListenerList(); 202 fListenersByPartId.put(partId, list); 203 } 204 list.add(listener); 205 } 206 207 210 public void removeDebugContextListener(IDebugContextListener listener, String partId) { 211 ListenerList list = (ListenerList) fListenersByPartId.get(partId); 212 if (list != null) { 213 list.remove(listener); 214 } 215 } 216 217 220 public ISelection getActiveContext(String partId) { 221 IDebugContextProvider provider = (IDebugContextProvider) fProvidersByPartId.get(partId); 222 if (provider != null) { 223 return provider.getActiveContext(); 224 } 225 return null; 226 } 227 228 231 public ISelection getActiveContext() { 232 IDebugContextProvider activeProvider = getActiveProvider(); 233 if (activeProvider != null) { 234 return activeProvider.getActiveContext(); 235 } 236 return null; 237 } 238 239 244 protected IDebugContextProvider getActiveProvider() { 245 if (!fProviders.isEmpty()) { 246 return (IDebugContextProvider)fProviders.get(0); 247 } 248 return null; 249 } 250 251 254 public void partActivated(IWorkbenchPartReference partRef) { 255 IDebugContextProvider provider = (IDebugContextProvider) fProvidersByPartId.get(partRef.getId()); 256 if (provider != null) { 257 int index = fProviders.indexOf(provider); 258 if (index > 0) { 259 fProviders.remove(index); 260 fProviders.add(0, provider); 261 notify(provider); 262 } 263 } 264 265 } 266 267 270 public void partBroughtToTop(IWorkbenchPartReference partRef) { 271 } 272 273 276 public synchronized void partClosed(IWorkbenchPartReference partRef) { 277 } 278 279 282 public void partDeactivated(IWorkbenchPartReference partRef) { 283 } 284 285 288 public void partOpened(IWorkbenchPartReference partRef) { 289 } 290 291 294 public void partHidden(IWorkbenchPartReference partRef) { 295 } 296 297 300 public void partVisible(IWorkbenchPartReference partRef) { 301 } 302 303 306 public void partInputChanged(IWorkbenchPartReference partRef) { 307 } 308 309 312 public void debugContextChanged(DebugContextEvent event) { 313 if (!fProviders.isEmpty()) { 314 IDebugContextProvider provider = (IDebugContextProvider) fProviders.get(0); 315 if (provider == event.getDebugContextProvider()) { 316 notify(event); 317 } 318 } 319 } 320 321 } 322 | Popular Tags |