KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > contexts > DebugWindowContextService


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.contexts;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
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 /**
35  * Context service for a specific window.
36  *
37  * @since 3.2
38  */

39 public class DebugWindowContextService implements IDebugContextService, IPartListener2, IDebugContextListener {
40     
41     private Map JavaDoc fListenersByPartId = new HashMap JavaDoc();
42     private Map JavaDoc fProvidersByPartId = new HashMap JavaDoc();
43     private Map JavaDoc fPostListenersByPartId = new HashMap JavaDoc();
44     
45     private IWorkbenchWindow fWindow;
46     private List JavaDoc fProviders = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc 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     /* (non-Javadoc)
100      * @see org.eclipse.debug.ui.contexts.IDebugContextService#addDebugContextListener(org.eclipse.debug.ui.contexts.IDebugContextListener)
101      */

102     public void addDebugContextListener(IDebugContextListener listener) {
103         addDebugContextListener(listener, null);
104     }
105     
106     public void addPostDebugContextListener(IDebugContextListener listener, String JavaDoc 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 JavaDoc 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     /* (non-Javadoc)
131      * @see org.eclipse.debug.ui.contexts.IDebugContextService#removeDebugContextListener(org.eclipse.debug.ui.contexts.IDebugContextListener)
132      */

133     public void removeDebugContextListener(IDebugContextListener listener) {
134         removeDebugContextListener(listener, null);
135     }
136     
137     /**
138      * Notifies listeners of the context in the specified provider.
139      *
140      * @param provdier context provider
141      */

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 JavaDoc[] 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 JavaDoc {
169                         listener.debugContextChanged(event);
170                     }
171                     public void handleException(Throwable JavaDoc exception) {
172                         DebugUIPlugin.log(exception);
173                     }
174                 });
175             }
176         }
177     }
178     
179     protected ListenerList getListeners(IWorkbenchPart part) {
180         String JavaDoc 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 JavaDoc id = null;
189         if (part != null) {
190             id = part.getSite().getId();
191         }
192         return (ListenerList) fPostListenersByPartId.get(id);
193     }
194
195     /* (non-Javadoc)
196      * @see org.eclipse.debug.ui.contexts.IDebugContextService#addDebugContextListener(org.eclipse.debug.ui.contexts.IDebugContextListener, java.lang.String)
197      */

198     public synchronized void addDebugContextListener(IDebugContextListener listener, String JavaDoc 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     /* (non-Javadoc)
208      * @see org.eclipse.debug.ui.contexts.IDebugContextService#removeDebugContextListener(org.eclipse.debug.ui.contexts.IDebugContextListener, java.lang.String)
209      */

210     public void removeDebugContextListener(IDebugContextListener listener, String JavaDoc partId) {
211         ListenerList list = (ListenerList) fListenersByPartId.get(partId);
212         if (list != null) {
213             list.remove(listener);
214         }
215     }
216
217     /* (non-Javadoc)
218      * @see org.eclipse.debug.ui.contexts.IDebugContextService#getActiveContext(java.lang.String)
219      */

220     public ISelection getActiveContext(String JavaDoc partId) {
221         IDebugContextProvider provider = (IDebugContextProvider) fProvidersByPartId.get(partId);
222         if (provider != null) {
223             return provider.getActiveContext();
224         }
225         return null;
226     }
227
228     /* (non-Javadoc)
229      * @see org.eclipse.debug.ui.contexts.IDebugContextService#getActiveContext()
230      */

231     public ISelection getActiveContext() {
232         IDebugContextProvider activeProvider = getActiveProvider();
233         if (activeProvider != null) {
234             return activeProvider.getActiveContext();
235         }
236         return null;
237     }
238     
239     /**
240      * Returns the active provider or <code>null</code>
241      *
242      * @return active provider or <code>null</code>
243      */

244     protected IDebugContextProvider getActiveProvider() {
245         if (!fProviders.isEmpty()) {
246             return (IDebugContextProvider)fProviders.get(0);
247         }
248         return null;
249     }
250
251     /* (non-Javadoc)
252      * @see org.eclipse.ui.IPartListener2#partActivated(org.eclipse.ui.IWorkbenchPartReference)
253      */

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     /* (non-Javadoc)
268      * @see org.eclipse.ui.IPartListener2#partBroughtToTop(org.eclipse.ui.IWorkbenchPartReference)
269      */

270     public void partBroughtToTop(IWorkbenchPartReference partRef) {
271     }
272
273     /* (non-Javadoc)
274      * @see org.eclipse.ui.IPartListener2#partClosed(org.eclipse.ui.IWorkbenchPartReference)
275      */

276     public synchronized void partClosed(IWorkbenchPartReference partRef) {
277     }
278
279     /* (non-Javadoc)
280      * @see org.eclipse.ui.IPartListener2#partDeactivated(org.eclipse.ui.IWorkbenchPartReference)
281      */

282     public void partDeactivated(IWorkbenchPartReference partRef) {
283     }
284
285     /* (non-Javadoc)
286      * @see org.eclipse.ui.IPartListener2#partOpened(org.eclipse.ui.IWorkbenchPartReference)
287      */

288     public void partOpened(IWorkbenchPartReference partRef) {
289     }
290
291     /* (non-Javadoc)
292      * @see org.eclipse.ui.IPartListener2#partHidden(org.eclipse.ui.IWorkbenchPartReference)
293      */

294     public void partHidden(IWorkbenchPartReference partRef) {
295     }
296
297     /* (non-Javadoc)
298      * @see org.eclipse.ui.IPartListener2#partVisible(org.eclipse.ui.IWorkbenchPartReference)
299      */

300     public void partVisible(IWorkbenchPartReference partRef) {
301     }
302
303     /* (non-Javadoc)
304      * @see org.eclipse.ui.IPartListener2#partInputChanged(org.eclipse.ui.IWorkbenchPartReference)
305      */

306     public void partInputChanged(IWorkbenchPartReference partRef) {
307     }
308
309     /* (non-Javadoc)
310      * @see org.eclipse.debug.internal.ui.contexts.provisional.IDebugContextEventListener#contextEvent(org.eclipse.debug.internal.ui.contexts.provisional.DebugContextEvent)
311      */

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