KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > launch > LaunchTreeModel


1 /*******************************************************************************
2  * Copyright (c) 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.views.launch;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.NullProgressMonitor;
20 import org.eclipse.debug.core.model.IStackFrame;
21 import org.eclipse.debug.internal.ui.DebugUIPlugin;
22 import org.eclipse.debug.internal.ui.viewers.AsynchronousTreeModel;
23 import org.eclipse.debug.internal.ui.viewers.AsynchronousTreeViewer;
24 import org.eclipse.debug.internal.ui.viewers.provisional.AsynchronousContentAdapter;
25 import org.eclipse.debug.internal.ui.viewers.provisional.IAsynchronousContentAdapter;
26 import org.eclipse.debug.internal.ui.viewers.provisional.IPresentationContext;
27 import org.eclipse.debug.ui.IDebugUIConstants;
28 import org.eclipse.ui.model.IWorkbenchAdapter;
29 import org.eclipse.ui.progress.IDeferredWorkbenchAdapter;
30 import org.eclipse.ui.progress.IElementCollector;
31 import org.osgi.framework.Bundle;
32
33 /**
34  * Must provide access to legacy workbench and deferred workbench content
35  * adapters.
36  *
37  */

38 public class LaunchTreeModel extends AsynchronousTreeModel {
39
40     public LaunchTreeModel(AsynchronousTreeViewer viewer) {
41         super(viewer);
42     }
43
44     protected IAsynchronousContentAdapter getContentAdapter(Object JavaDoc element) {
45         IAsynchronousContentAdapter contentAdapter = super.getContentAdapter(element);
46         if (contentAdapter != null) {
47             return contentAdapter;
48         }
49         
50         AsynchronousContentAdapter legacyAdapter = getLegacyAdapter(element);
51         if (legacyAdapter != null) {
52             return legacyAdapter;
53         }
54         
55         return null;
56     }
57
58     /**
59      * Returns a wrapper to the legacy workbench adapter if supported by the given object.
60      *
61      * @param element
62      * @return
63      */

64     private AsynchronousContentAdapter getLegacyAdapter(Object JavaDoc element) {
65         if (element instanceof IDeferredWorkbenchAdapter) {
66             return new WrappedDeferredWorkbenchTreeAdapter((IDeferredWorkbenchAdapter) element, element);
67         }
68
69         if (!(element instanceof IAdaptable)) {
70             return null;
71         }
72
73         IAdaptable adaptable = (IAdaptable) element;
74         IDeferredWorkbenchAdapter deferred = (IDeferredWorkbenchAdapter) adaptable.getAdapter(IDeferredWorkbenchAdapter.class);
75         if (deferred != null) {
76             DebugUIPlugin plugin = DebugUIPlugin.getDefault();
77             Bundle bundle = plugin.getBundle(deferred.getClass());
78             Bundle debugBundle = plugin.getBundle();
79             if (!debugBundle.equals(bundle)) {
80                 // if client contributed, use it
81
return new WrappedDeferredWorkbenchTreeAdapter(deferred, element);
82             }
83         }
84         // if the client provided an IWorkbenchAdapter, use it
85
IWorkbenchAdapter nonDeferred = (IWorkbenchAdapter) adaptable.getAdapter(IWorkbenchAdapter.class);
86         if (nonDeferred != null) {
87             DebugUIPlugin plugin = DebugUIPlugin.getDefault();
88             Bundle bundle = plugin.getBundle(nonDeferred.getClass());
89             Bundle debugBundle = plugin.getBundle();
90             bundle = plugin.getBundle(nonDeferred.getClass());
91             if (!debugBundle.equals(bundle)) {
92                 return new WrappedWorkbenchTreeAdapter(nonDeferred);
93             }
94         }
95         return null;
96     }
97
98     private class ElementCollector implements IElementCollector {
99         List JavaDoc children = new ArrayList JavaDoc();
100
101         public void add(Object JavaDoc element, IProgressMonitor monitor) {
102             children.add(element);
103         }
104
105         public void add(Object JavaDoc[] elements, IProgressMonitor monitor) {
106             for (int i = 0; i < elements.length; i++) {
107                 children.add(elements[i]);
108             }
109         }
110
111         public void done() {
112         }
113
114         public Object JavaDoc[] getChildren() {
115             return children.toArray();
116         }
117
118     }
119
120     private class WrappedDeferredWorkbenchTreeAdapter extends AsynchronousContentAdapter {
121         private IDeferredWorkbenchAdapter fAdapter;
122
123         private Object JavaDoc fElement;
124
125         public WrappedDeferredWorkbenchTreeAdapter(IDeferredWorkbenchAdapter adapter, Object JavaDoc element) {
126             fAdapter = adapter;
127             fElement = element;
128         }
129
130         protected Object JavaDoc[] getChildren(Object JavaDoc parent, IPresentationContext context) throws CoreException {
131             ElementCollector elementCollector = new ElementCollector();
132             fAdapter.fetchDeferredChildren(fElement, elementCollector, new NullProgressMonitor());
133             return elementCollector.getChildren();
134         }
135
136         protected boolean hasChildren(Object JavaDoc element, IPresentationContext context) throws CoreException {
137             if (element instanceof IStackFrame) {
138                 return false;
139             }
140             return fAdapter.isContainer();
141         }
142         
143         protected boolean supportsPartId(String JavaDoc id) {
144             return IDebugUIConstants.ID_DEBUG_VIEW.equals(id);
145         }
146     }
147
148     private class WrappedWorkbenchTreeAdapter extends AsynchronousContentAdapter {
149         private IWorkbenchAdapter fAdapter;
150
151         public WrappedWorkbenchTreeAdapter(IWorkbenchAdapter adapter) {
152             fAdapter = adapter;
153         }
154
155         protected Object JavaDoc[] getChildren(Object JavaDoc parent, IPresentationContext context) throws CoreException {
156             return fAdapter.getChildren(parent);
157         }
158
159         protected boolean hasChildren(Object JavaDoc element, IPresentationContext context) throws CoreException {
160             if (element instanceof IStackFrame) {
161                 return false;
162             }
163             return fAdapter.getChildren(element).length > 0;
164         }
165
166         protected boolean supportsPartId(String JavaDoc id) {
167             return IDebugUIConstants.ID_DEBUG_VIEW.equals(id);
168         }
169
170     }
171     
172 }
173
Popular Tags