KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > sourcelookup > SourceLookupService


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.sourcelookup;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.core.runtime.PlatformObject;
15 import org.eclipse.debug.internal.ui.views.launch.DebugElementAdapterFactory;
16 import org.eclipse.debug.ui.DebugUITools;
17 import org.eclipse.debug.ui.contexts.DebugContextEvent;
18 import org.eclipse.debug.ui.contexts.IDebugContextListener;
19 import org.eclipse.debug.ui.sourcelookup.ISourceDisplay;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.ui.IWorkbenchPage;
23 import org.eclipse.ui.IWorkbenchPart;
24 import org.eclipse.ui.IWorkbenchWindow;
25
26 /**
27  * Performs source lookup in a window.
28  *
29  * @since 3.2
30  */

31 public class SourceLookupService implements IDebugContextListener, ISourceDisplay {
32     
33     private IWorkbenchWindow fWindow;
34     
35     public SourceLookupService(IWorkbenchWindow window) {
36         fWindow = window;
37         DebugUITools.getDebugContextManager().getContextService(window).addDebugContextListener(this);
38     }
39     
40     public void dispose() {
41         DebugUITools.getDebugContextManager().getContextService(fWindow).removeDebugContextListener(this);
42     }
43
44     public synchronized void debugContextChanged(DebugContextEvent event) {
45         if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0) {
46             displaySource(event.getContext(), event.getDebugContextProvider().getPart(), false);
47         }
48     }
49         
50     /**
51      * Displays source for the given selection and part, optionally forcing
52      * a source lookup.
53      *
54      * @param selection
55      * @param part
56      * @param force
57      */

58     protected synchronized void displaySource(ISelection selection, IWorkbenchPart part, boolean force) {
59         if (selection instanceof IStructuredSelection) {
60             IStructuredSelection structuredSelection = (IStructuredSelection)selection;
61             if (structuredSelection.size() == 1) {
62                 Object JavaDoc context = (structuredSelection).getFirstElement();
63                 IWorkbenchPage page = null;
64                 if (part == null) {
65                     page = fWindow.getActivePage();
66                 } else {
67                     page = part.getSite().getPage();
68                 }
69                 displaySource(context, page, force);
70             }
71         }
72     }
73
74     /* (non-Javadoc)
75      * @see org.eclipse.debug.internal.ui.contexts.ISourceDisplayAdapter#displaySource(java.lang.Object, org.eclipse.ui.IWorkbenchPage, boolean)
76      */

77     public void displaySource(Object JavaDoc context, IWorkbenchPage page, boolean forceSourceLookup) {
78         if (context instanceof IAdaptable) {
79             IAdaptable adaptable = (IAdaptable) context;
80             ISourceDisplay adapter = (ISourceDisplay) adaptable.getAdapter(ISourceDisplay.class);
81             if (adapter == null && !(context instanceof PlatformObject)) {
82                 // for objects that don't properly subclass PlatformObject to inherit default
83
// adapters, just delegate to the adapter factory
84
adapter = (ISourceDisplay) new DebugElementAdapterFactory().getAdapter(context, ISourceDisplay.class);
85             }
86             if (adapter != null) {
87                 adapter.displaySource(context, page, forceSourceLookup);
88             }
89         }
90     }
91 }
92
Popular Tags