KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > DelegatingModelPresentation


1 /*******************************************************************************
2  * Copyright (c) 2000, 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;
12
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.resources.IMarker;
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.IExtension;
21 import org.eclipse.core.runtime.IExtensionPoint;
22 import org.eclipse.core.runtime.Platform;
23 import org.eclipse.debug.core.DebugException;
24 import org.eclipse.debug.core.DebugPlugin;
25 import org.eclipse.debug.core.model.IBreakpoint;
26 import org.eclipse.debug.core.model.IDebugElement;
27 import org.eclipse.debug.core.model.IStackFrame;
28 import org.eclipse.debug.core.model.IThread;
29 import org.eclipse.debug.core.model.IValue;
30 import org.eclipse.debug.ui.DebugUITools;
31 import org.eclipse.debug.ui.IDebugEditorPresentation;
32 import org.eclipse.debug.ui.IDebugModelPresentation;
33 import org.eclipse.debug.ui.IDebugUIConstants;
34 import org.eclipse.debug.ui.IInstructionPointerPresentation;
35 import org.eclipse.debug.ui.IValueDetailListener;
36 import org.eclipse.jface.text.source.Annotation;
37 import org.eclipse.jface.viewers.IColorProvider;
38 import org.eclipse.jface.viewers.IFontProvider;
39 import org.eclipse.jface.viewers.ILabelProvider;
40 import org.eclipse.jface.viewers.ILabelProviderListener;
41 import org.eclipse.swt.graphics.Color;
42 import org.eclipse.swt.graphics.Font;
43 import org.eclipse.swt.graphics.Image;
44 import org.eclipse.ui.IEditorInput;
45 import org.eclipse.ui.IEditorPart;
46
47 /**
48  * A model presentation that delegates to the appropriate extension. This
49  * presentation contains a table of specialized presentations that are defined
50  * as <code>org.eclipse.debug.ui.debugModelPresentations</code> extensions. When
51  * asked to render an object from a debug model, this presentation delegates
52  * to the extension registered for that debug model.
53  */

54 public class DelegatingModelPresentation implements IDebugModelPresentation, IDebugEditorPresentation, IColorProvider, IFontProvider, IInstructionPointerPresentation {
55     
56     /**
57      * A mapping of attribute ids to their values
58      * @see IDebugModelPresentation#setAttribute
59      */

60     private HashMap JavaDoc fAttributes= new HashMap JavaDoc(3);
61     /**
62      * A table of label providers keyed by debug model identifiers.
63      */

64     private HashMap JavaDoc fLabelProviders= new HashMap JavaDoc(5);
65     
66     /**
67      * Whether the image registry has been initialized.
68      */

69     private boolean fInitialized = false;
70
71     /* (non-Javadoc)
72      * @see org.eclipse.debug.ui.IDebugEditorPresentation#removeAnnotations(org.eclipse.ui.IEditorPart, org.eclipse.debug.core.model.IThread)
73      */

74     public void removeAnnotations(IEditorPart editorPart, IThread thread) {
75         IDebugModelPresentation presentation = getConfiguredPresentation(thread);
76         if (presentation instanceof IDebugEditorPresentation) {
77             ((IDebugEditorPresentation)presentation).removeAnnotations(editorPart, thread);
78         }
79     }
80
81     /* (non-Javadoc)
82      * @see org.eclipse.debug.ui.IDebugEditorPresentation#addAnnotations(org.eclipse.ui.IEditorPart, org.eclipse.debug.core.model.IStackFrame)
83      */

84     public boolean addAnnotations(IEditorPart editorPart, IStackFrame frame) {
85         IDebugModelPresentation presentation = getConfiguredPresentation(frame);
86         if (presentation instanceof IDebugEditorPresentation) {
87             return((IDebugEditorPresentation)presentation).addAnnotations(editorPart, frame);
88         }
89         return false;
90     }
91
92     /**
93      * Constructs a new DelegatingLabelProvider that delegates to extensions
94      * of kind <code>org.eclipse.debug.ui.debugLabelProvider</code>
95      */

96     public DelegatingModelPresentation() {
97         IExtensionPoint point= Platform.getExtensionRegistry().getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.ID_DEBUG_MODEL_PRESENTATION);
98         if (point != null) {
99             IExtension[] extensions= point.getExtensions();
100             for (int i= 0; i < extensions.length; i++) {
101                 IExtension extension= extensions[i];
102                 IConfigurationElement[] configElements= extension.getConfigurationElements();
103                 for (int j= 0; j < configElements.length; j++) {
104                     IConfigurationElement elt= configElements[j];
105                     String JavaDoc id= elt.getAttribute("id"); //$NON-NLS-1$
106
if (id != null) {
107                         IDebugModelPresentation lp= new LazyModelPresentation(elt);
108                         getLabelProviders().put(id, lp);
109                     }
110                 }
111             }
112         }
113     }
114
115     /**
116      * Delegate to all extensions.
117      *
118      * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
119      */

120     public void addListener(ILabelProviderListener listener) {
121         Iterator JavaDoc i= getLabelProviders().values().iterator();
122         while (i.hasNext()) {
123             ((ILabelProvider) i.next()).addListener(listener);
124         }
125     }
126
127     /**
128      * Delegate to all extensions.
129      *
130      * @see IBaseLabelProvider#dispose()
131      */

132     public void dispose() {
133         Iterator JavaDoc i= getLabelProviders().values().iterator();
134         while (i.hasNext()) {
135             ((ILabelProvider) i.next()).dispose();
136         }
137     }
138
139     /**
140      * @see IDebugModelPresentation#getImage(Object)
141      */

142     public Image getImage(Object JavaDoc item) {
143         initImageRegistries();
144         // Attempt to delegate
145
IDebugModelPresentation lp= getConfiguredPresentation(item);
146         if (lp != null) {
147             Image image= lp.getImage(item);
148             if (image != null) {
149                 return image;
150             }
151         }
152         // If no delegate returned an image, use the default
153
return getDefaultImage(item);
154     }
155     
156     /**
157      * @see IDebugModelPresentation#getText(Object)
158      */

159     public String JavaDoc getText(Object JavaDoc item) {
160         IDebugModelPresentation lp= getConfiguredPresentation(item);
161         if (lp != null) {
162             return lp.getText(item);
163         }
164         return getDefaultText(item);
165     }
166     
167     /* (non-Javadoc)
168      * @see org.eclipse.debug.ui.ISourcePresentation#getEditorInput(java.lang.Object)
169      */

170     public IEditorInput getEditorInput(Object JavaDoc item) {
171         IDebugModelPresentation lp= getConfiguredPresentation(item);
172         if (lp != null) {
173             return lp.getEditorInput(item);
174         }
175         return null;
176     }
177     
178     /* (non-Javadoc)
179      * @see org.eclipse.debug.ui.ISourcePresentation#getEditorId(org.eclipse.ui.IEditorInput, java.lang.Object)
180      */

181     public String JavaDoc getEditorId(IEditorInput input, Object JavaDoc objectInput) {
182         IDebugModelPresentation lp= getConfiguredPresentation(objectInput);
183         if (lp != null) {
184             return lp.getEditorId(input, objectInput);
185         }
186         return null;
187     }
188
189
190     /**
191      * Returns a default text label for the debug element
192      */

193     protected String JavaDoc getDefaultText(Object JavaDoc element) {
194         return DebugUIPlugin.getDefaultLabelProvider().getText(element);
195     }
196
197     /**
198      * Returns a default image for the debug element
199      */

200     protected Image getDefaultImage(Object JavaDoc element) {
201         return DebugUIPlugin.getDefaultLabelProvider().getImage(element);
202     }
203     
204     /* (non-Javadoc)
205      * @see org.eclipse.debug.ui.IDebugModelPresentation#computeDetail(org.eclipse.debug.core.model.IValue, org.eclipse.debug.ui.IValueDetailListener)
206      */

207     public void computeDetail(IValue value, IValueDetailListener listener) {
208         IDebugModelPresentation lp= getConfiguredPresentation(value);
209         if (lp != null) {
210             lp.computeDetail(value, listener);
211         } else {
212             listener.detailComputed(value, getText(value));
213         }
214     }
215
216     /**
217      * Delegate to all extensions.
218      *
219      * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
220      */

221     public void removeListener(ILabelProviderListener listener) {
222         Iterator JavaDoc i= getLabelProviders().values().iterator();
223         while (i.hasNext()) {
224             ((ILabelProvider) i.next()).removeListener(listener);
225         }
226     }
227
228     /**
229      * Delegate to the appropriate label provider.
230      *
231      * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)
232      */

233     public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
234         if (element instanceof IDebugElement) {
235             IDebugModelPresentation lp= getConfiguredPresentation(element);
236             if (lp != null) {
237                 return lp.isLabelProperty(element, property);
238             }
239         }
240
241         return true;
242     }
243
244     /**
245      * Returns a configured model presentation for the given object,
246      * or <code>null</code> if one is not registered.
247      */

248     protected IDebugModelPresentation getConfiguredPresentation(Object JavaDoc element) {
249         String JavaDoc id= null;
250         if (element instanceof IDebugElement) {
251             IDebugElement de= (IDebugElement) element;
252             id= de.getModelIdentifier();
253         } else if (element instanceof IMarker) {
254             IMarker m= (IMarker) element;
255             IBreakpoint bp = DebugPlugin.getDefault().getBreakpointManager().getBreakpoint(m);
256             if (bp != null) {
257                 id= bp.getModelIdentifier();
258             }
259         } else if (element instanceof IBreakpoint) {
260             id = ((IBreakpoint)element).getModelIdentifier();
261         }
262         if (id != null) {
263             return getPresentation(id);
264         }
265
266         return null;
267     }
268     
269     /**
270      * Returns the presentation registered for the given id, or <code>null</code>
271      * of nothing is registered for the id.
272      */

273     public IDebugModelPresentation getPresentation(String JavaDoc id) {
274         return (IDebugModelPresentation) getLabelProviders().get(id);
275     }
276     
277     /* (non-Javadoc)
278      * @see org.eclipse.debug.ui.IDebugModelPresentation#setAttribute(java.lang.String, java.lang.Object)
279      */

280     public void setAttribute(String JavaDoc id, Object JavaDoc value) {
281         if (value == null) {
282             return;
283         }
284         getAttributes().put(id, value);
285         Iterator JavaDoc presentations = fLabelProviders.values().iterator();
286         while (presentations.hasNext()) {
287             ((IDebugModelPresentation)presentations.next()).setAttribute(id, value);
288         }
289     }
290
291     /**
292      * Whether or not to show variable type names.
293      * This option is configured per model presentation.
294      * This allows this option to be set per view, for example.
295      */

296     protected boolean showVariableTypeNames() {
297         Boolean JavaDoc show= (Boolean JavaDoc) fAttributes.get(DISPLAY_VARIABLE_TYPE_NAMES);
298         show= show == null ? Boolean.FALSE : show;
299         return show.booleanValue();
300     }
301         
302     public HashMap JavaDoc getAttributes() {
303         return fAttributes;
304     }
305     
306     /**
307      * Returns a copy of the attribute map for this presentation.
308      *
309      * @return a copy of the attribute map for this presentation
310      * @since 3.0
311      */

312     public Map JavaDoc getAttributeMap() {
313         return (Map JavaDoc) getAttributes().clone();
314     }
315
316     protected HashMap JavaDoc getLabelProviders() {
317         return fLabelProviders;
318     }
319
320     /* (non-Javadoc)
321      * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
322      */

323     public Color getForeground(Object JavaDoc element) {
324         IDebugModelPresentation presentation = getConfiguredPresentation(element);
325         if (presentation instanceof IColorProvider) {
326             IColorProvider colorProvider = (IColorProvider) presentation;
327             return colorProvider.getForeground(element);
328         }
329         return null;
330     }
331
332     /* (non-Javadoc)
333      * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
334      */

335     public Color getBackground(Object JavaDoc element) {
336         IDebugModelPresentation presentation = getConfiguredPresentation(element);
337         if (presentation instanceof IColorProvider) {
338             IColorProvider colorProvider = (IColorProvider) presentation;
339             return colorProvider.getBackground(element);
340         }
341         return null;
342     }
343
344     /* (non-Javadoc)
345      * @see org.eclipse.jface.viewers.IFontProvider#getFont(java.lang.Object)
346      */

347     public Font getFont(Object JavaDoc element) {
348         IDebugModelPresentation presentation = getConfiguredPresentation(element);
349         if (presentation instanceof IFontProvider) {
350             IFontProvider fontProvider = (IFontProvider) presentation;
351             return fontProvider.getFont(element);
352         }
353         return null;
354     }
355
356     /* (non-Javadoc)
357      * @see org.eclipse.debug.ui.IInstructionPointerPresentation#getInstructionPointerAnnotation(org.eclipse.ui.IEditorPart, org.eclipse.debug.core.model.IStackFrame)
358      */

359     public Annotation getInstructionPointerAnnotation(IEditorPart editorPart, IStackFrame frame) {
360         IDebugModelPresentation presentation = getConfiguredPresentation(frame);
361         Annotation annotation = null;
362         String JavaDoc id = null;
363         Image image = null;
364         String JavaDoc text = null;
365         if (presentation instanceof IInstructionPointerPresentation) {
366             // first check if an annotaion object is provided
367
IInstructionPointerPresentation pointerPresentation = (IInstructionPointerPresentation) presentation;
368             annotation = pointerPresentation.getInstructionPointerAnnotation(editorPart, frame);
369             if (annotation == null) {
370                 // next check for a marker annotation specification extension
371
id = pointerPresentation.getInstructionPointerAnnotationType(editorPart, frame);
372                 if (id == null) {
373                     // check for an image
374
image = pointerPresentation.getInstructionPointerImage(editorPart, frame);
375                 }
376                 text = pointerPresentation.getInstructionPointerText(editorPart, frame);
377             }
378         }
379         if (annotation == null) {
380             boolean defaultAnnotation = id == null;
381             if (id == null || text == null || (defaultAnnotation && image == null)) {
382                 IThread thread = frame.getThread();
383                 IStackFrame tos = null;
384                 boolean top = false;
385                 try {
386                     tos = thread.getTopStackFrame();
387                     top = frame.equals(tos);
388                 } catch (DebugException de) {
389                 }
390                 if (id == null) {
391                     if (top) {
392                         id = IDebugUIConstants.ANNOTATION_TYPE_INSTRUCTION_POINTER_CURRENT;
393                     } else {
394                         id = IDebugUIConstants.ANNOTATION_TYPE_INSTRUCTION_POINTER_SECONDARY;
395                     }
396                 }
397                 if (text == null) {
398                     if (top) {
399                         text = DebugUIMessages.InstructionPointerAnnotation_0;
400                     } else {
401                         text = DebugUIMessages.InstructionPointerAnnotation_1;
402                     }
403                 }
404                 if (defaultAnnotation && image == null) {
405                     if (top) {
406                         image = DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_INSTRUCTION_POINTER_TOP);
407                     } else {
408                         image = DebugUITools.getImage(IDebugUIConstants.IMG_OBJS_INSTRUCTION_POINTER);
409                     }
410                 }
411             }
412             if (defaultAnnotation) {
413                 annotation = new InstructionPointerAnnotation(frame, id, text, image);
414             } else {
415                 annotation = new DynamicInstructionPointerAnnotation(frame, id, text);
416             }
417         }
418         return annotation;
419     }
420
421     /* (non-Javadoc)
422      * @see org.eclipse.debug.ui.IInstructionPointerPresentation#getMarkerAnnotationSpecificationId(org.eclipse.ui.IEditorPart, org.eclipse.debug.core.model.IStackFrame)
423      */

424     public String JavaDoc getInstructionPointerAnnotationType(IEditorPart editorPart, IStackFrame frame) {
425         IDebugModelPresentation presentation = getConfiguredPresentation(frame);
426         if (presentation instanceof IInstructionPointerPresentation) {
427             return ((IInstructionPointerPresentation)presentation).getInstructionPointerAnnotationType(editorPart, frame);
428         }
429         return null;
430     }
431
432     /* (non-Javadoc)
433      * @see org.eclipse.debug.ui.IInstructionPointerPresentation#getInstructionPointerImage(org.eclipse.ui.IEditorPart, org.eclipse.debug.core.model.IStackFrame)
434      */

435     public Image getInstructionPointerImage(IEditorPart editorPart, IStackFrame frame) {
436         IDebugModelPresentation presentation = getConfiguredPresentation(frame);
437         if (presentation instanceof IInstructionPointerPresentation) {
438             return ((IInstructionPointerPresentation)presentation).getInstructionPointerImage(editorPart, frame);
439         }
440         return null;
441     }
442
443     /* (non-Javadoc)
444      * @see org.eclipse.debug.ui.IInstructionPointerPresentation#getInstructionPointerText(org.eclipse.ui.IEditorPart, org.eclipse.debug.core.model.IStackFrame)
445      */

446     public String JavaDoc getInstructionPointerText(IEditorPart editorPart, IStackFrame frame) {
447         IDebugModelPresentation presentation = getConfiguredPresentation(frame);
448         if (presentation instanceof IInstructionPointerPresentation) {
449             return ((IInstructionPointerPresentation)presentation).getInstructionPointerText(editorPart, frame);
450         }
451         return null;
452     }
453
454     public boolean isInitialized(Object JavaDoc element) {
455         LazyModelPresentation configuredPresentation = (LazyModelPresentation) getConfiguredPresentation(element);
456         if (configuredPresentation != null) {
457             return configuredPresentation.isImageRegistryInitialized();
458         }
459         return false;
460     }
461     
462     /**
463      * Initialize image registries that this model presentation references to
464      */

465     private synchronized void initImageRegistries() {
466         // if not initialized and this is called on the UI thread
467
if (!fInitialized && Thread.currentThread().equals(DebugUIPlugin.getStandardDisplay().getThread())) {
468             // force image registries to be created on the UI thread
469
DebugUIPlugin.getDefault().getImageRegistry();
470             fInitialized = true;
471         }
472     }
473 }
474
Popular Tags