KickJava   Java API By Example, From Geeks To Geeks.

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


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.runtime.CoreException;
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.ListenerList;
21 import org.eclipse.debug.core.DebugException;
22 import org.eclipse.debug.core.DebugPlugin;
23 import org.eclipse.debug.core.model.IBreakpoint;
24 import org.eclipse.debug.core.model.IExpression;
25 import org.eclipse.debug.core.model.IStackFrame;
26 import org.eclipse.debug.core.model.IThread;
27 import org.eclipse.debug.core.model.IValue;
28 import org.eclipse.debug.core.model.IVariable;
29 import org.eclipse.debug.internal.ui.views.variables.IndexedVariablePartition;
30 import org.eclipse.debug.ui.IDebugEditorPresentation;
31 import org.eclipse.debug.ui.IDebugModelPresentation;
32 import org.eclipse.debug.ui.IInstructionPointerPresentation;
33 import org.eclipse.debug.ui.IValueDetailListener;
34 import org.eclipse.jface.text.source.Annotation;
35 import org.eclipse.jface.text.source.SourceViewerConfiguration;
36 import org.eclipse.jface.viewers.IColorProvider;
37 import org.eclipse.jface.viewers.IFontProvider;
38 import org.eclipse.jface.viewers.ILabelProviderListener;
39 import org.eclipse.swt.graphics.Color;
40 import org.eclipse.swt.graphics.Font;
41 import org.eclipse.swt.graphics.Image;
42 import org.eclipse.ui.IEditorInput;
43 import org.eclipse.ui.IEditorPart;
44
45 /**
46  * A proxy to an IDebugModelPresentation extension. Instantiates the extension
47  * when it is needed.
48  */

49
50 public class LazyModelPresentation implements IDebugModelPresentation, IDebugEditorPresentation, IColorProvider, IFontProvider, IInstructionPointerPresentation {
51     
52     /**
53      * A temporary mapping of attribute ids to their values
54      * @see IDebugModelPresentation#setAttribute
55      */

56     protected HashMap JavaDoc fAttributes= new HashMap JavaDoc(3);
57
58     /**
59      * The config element that defines the extension
60      */

61     protected IConfigurationElement fConfig = null;
62     
63     /**
64      * The actual presentation instance - null until called upon
65      */

66     protected IDebugModelPresentation fPresentation = null;
67     
68     /**
69      * Temp holding for listeners - we do not add to presentation until
70      * it needs to be instantiated.
71      */

72     protected ListenerList fListeners= new ListenerList();
73     
74     protected boolean fImageRegistryInitialized = false;
75     
76         
77     /* (non-Javadoc)
78      * @see org.eclipse.debug.ui.IDebugEditorPresentation#removeAnntations(org.eclipse.ui.IEditorPart, org.eclipse.debug.core.model.IThread)
79      */

80     public void removeAnnotations(IEditorPart editorPart, IThread thread) {
81         IDebugModelPresentation presentation = getPresentation();
82         if (presentation instanceof IDebugEditorPresentation) {
83             ((IDebugEditorPresentation)presentation).removeAnnotations(editorPart, thread);
84         }
85     }
86
87     /* (non-Javadoc)
88      * @see org.eclipse.debug.ui.IDebugEditorPresentation#addAnnotations(org.eclipse.ui.IEditorPart, org.eclipse.debug.core.model.IStackFrame)
89      */

90     public boolean addAnnotations(IEditorPart editorPart, IStackFrame frame) {
91         IDebugModelPresentation presentation = getPresentation();
92         if (presentation instanceof IDebugEditorPresentation) {
93             return ((IDebugEditorPresentation)presentation).addAnnotations(editorPart, frame);
94         }
95         return false;
96     }
97
98     /**
99      * Constructs a lazy presentation from the config element.
100      */

101     public LazyModelPresentation(IConfigurationElement configElement) {
102         fConfig = configElement;
103     }
104
105     /**
106      * @see IDebugModelPresentation#getImage(Object)
107      */

108     public Image getImage(Object JavaDoc element) {
109         fImageRegistryInitialized = true;
110         Image image = getPresentation().getImage(element);
111         if (image == null) {
112             image = getDefaultImage(element);
113         }
114         if (image != null) {
115             int flags= computeAdornmentFlags(element);
116             if (flags > 0) {
117                 CompositeDebugImageDescriptor descriptor= new CompositeDebugImageDescriptor(image, flags);
118                 return DebugUIPlugin.getImageDescriptorRegistry().get(descriptor);
119             }
120         }
121         return image;
122     }
123     
124     public boolean isImageRegistryInitialized() {
125         return fImageRegistryInitialized;
126     }
127
128     /**
129      * Computes and return common adornment flags for the given element.
130      *
131      * @param element
132      * @return adornment flags defined in CompositeDebugImageDescriptor
133      */

134     private int computeAdornmentFlags(Object JavaDoc element) {
135         if (element instanceof IBreakpoint) {
136             if (!DebugPlugin.getDefault().getBreakpointManager().isEnabled()) {
137                 return CompositeDebugImageDescriptor.SKIP_BREAKPOINT;
138             }
139         }
140         return 0;
141     }
142
143     /**
144      * Returns a default text label for the debug element
145      */

146     protected String JavaDoc getDefaultText(Object JavaDoc element) {
147         return DebugUIPlugin.getDefaultLabelProvider().getText(element);
148     }
149
150     /**
151      * Returns a default image for the debug element
152      */

153     protected Image getDefaultImage(Object JavaDoc element) {
154         return DebugUIPlugin.getDefaultLabelProvider().getImage(element);
155     }
156     
157     /**
158      * @see IDebugModelPresentation#getText(Object)
159      */

160     public String JavaDoc getText(Object JavaDoc element) {
161         if (!(element instanceof IndexedVariablePartition)) {
162             // Attempt to delegate
163
String JavaDoc text = getPresentation().getText(element);
164             if (text != null) {
165                 return text;
166             }
167         }
168         // If no delegate returned a text label, use the default
169
if (showVariableTypeNames()) {
170             try {
171                 if (element instanceof IExpression) {
172                     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
173                     IValue value = ((IExpression)element).getValue();
174                     if (value != null) {
175                         buf.append(value.getReferenceTypeName());
176                         buf.append(' ');
177                     }
178                     buf.append(getDefaultText(element));
179                     return buf.toString();
180                 } else if (element instanceof IVariable) {
181                     return new StringBuffer JavaDoc(((IVariable)element).getValue().getReferenceTypeName()).append(' ').append(getDefaultText(element)).toString();
182                 }
183             } catch (DebugException de) {
184                 DebugUIPlugin.log(de);
185             }
186         }
187         return getDefaultText(element);
188     }
189     
190     /**
191      * Whether or not to show variable type names.
192      * This option is configured per model presentation.
193      * This allows this option to be set per view, for example.
194      */

195     protected boolean showVariableTypeNames() {
196         Boolean JavaDoc show= (Boolean JavaDoc) fAttributes.get(DISPLAY_VARIABLE_TYPE_NAMES);
197         show= show == null ? Boolean.FALSE : show;
198         return show.booleanValue();
199     }
200     
201     /**
202      * @see IDebugModelPresentation#computeDetail(IValue, IValueDetailListener)
203      */

204     public void computeDetail(IValue value, IValueDetailListener listener) {
205         getPresentation().computeDetail(value, listener);
206     }
207     
208     /**
209      * @see ISourcePresentation#getEditorInput(Object)
210      */

211     public IEditorInput getEditorInput(Object JavaDoc element) {
212         return getPresentation().getEditorInput(element);
213     }
214     
215     /**
216      * @see ISourcePresentation#getEditorId(IEditorInput, Object)
217      */

218     public String JavaDoc getEditorId(IEditorInput input, Object JavaDoc inputObject) {
219         return getPresentation().getEditorId(input, inputObject);
220     }
221
222     /**
223      * @see IBaseLabelProvider#addListener(ILabelProviderListener)
224      */

225     public void addListener(ILabelProviderListener listener) {
226         if (fPresentation != null) {
227             getPresentation().addListener(listener);
228         }
229         fListeners.add(listener);
230     }
231
232     /**
233      * @see IBaseLabelProvider#dispose()
234      */

235     public void dispose() {
236         if (fPresentation != null) {
237             getPresentation().dispose();
238         }
239         fListeners = null;
240     }
241
242     /**
243      * @see IBaseLabelProvider#isLabelProperty(Object, String)
244      */

245     public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
246         if (fPresentation != null) {
247             return getPresentation().isLabelProperty(element, property);
248         }
249         return false;
250     }
251
252     /**
253      * @see IBaseLabelProvider#removeListener(ILabelProviderListener)
254      */

255     public void removeListener(ILabelProviderListener listener) {
256         if (fPresentation != null) {
257             getPresentation().removeListener(listener);
258         }
259         ListenerList listeners = fListeners;
260         if (listeners != null) {
261             listeners.remove(listener);
262         }
263     }
264     
265     /**
266      * Returns the real presentation, instantiating if required.
267      */

268     protected IDebugModelPresentation getPresentation() {
269         if (fPresentation == null) {
270             synchronized (this) {
271                 if (fPresentation != null) {
272                     // In the case that the synchronization is enforced, the "blocked" thread
273
// should return the presentation configured by the "owning" thread.
274
return fPresentation;
275                 }
276                 try {
277                     IDebugModelPresentation tempPresentation= (IDebugModelPresentation) DebugUIPlugin.createExtension(fConfig, "class"); //$NON-NLS-1$
278
// configure it
279
if (fListeners != null) {
280                         Object JavaDoc[] list = fListeners.getListeners();
281                         for (int i= 0; i < list.length; i++) {
282                             tempPresentation.addListener((ILabelProviderListener)list[i]);
283                         }
284                     }
285                     Iterator JavaDoc keys= fAttributes.keySet().iterator();
286                     while (keys.hasNext()) {
287                         String JavaDoc key= (String JavaDoc)keys.next();
288                         tempPresentation.setAttribute(key, fAttributes.get(key));
289                     }
290                     // Only assign to the instance variable after it's been configured. Otherwise,
291
// the synchronization is defeated (a thread could return the presentation before
292
// it's been configured).
293
fPresentation= tempPresentation;
294                 } catch (CoreException e) {
295                     DebugUIPlugin.log(e);
296                 }
297             }
298         }
299         return fPresentation;
300     }
301
302     /**
303      * @see IDebugModelPresentation#setAttribute(String, Object)
304      */

305     public void setAttribute(String JavaDoc id, Object JavaDoc value) {
306         if (value == null) {
307             return;
308         }
309         if (fPresentation != null) {
310             getPresentation().setAttribute(id, value);
311         }
312
313         fAttributes.put(id, value);
314     }
315     
316     /**
317      * Returns the identifier of the debug model this
318      * presentation is registered for.
319      */

320     public String JavaDoc getDebugModelIdentifier() {
321         return fConfig.getAttribute("id"); //$NON-NLS-1$
322
}
323     
324     /**
325      * Returns a new source viewer configuration for the details
326      * area of the variables view, or <code>null</code> if
327      * unspecified.
328      *
329      * @return source viewer configuration or <code>null</code>
330      * @exception CoreException if unable to create the specified
331      * source viewer configuration
332      */

333     public SourceViewerConfiguration newDetailsViewerConfiguration() throws CoreException {
334         String JavaDoc attr = fConfig.getAttribute("detailsViewerConfiguration"); //$NON-NLS-1$
335
if (attr != null) {
336             return (SourceViewerConfiguration)fConfig.createExecutableExtension("detailsViewerConfiguration"); //$NON-NLS-1$
337
}
338         return null;
339     }
340     
341     /**
342      * Returns a copy of the attributes in this model presentation.
343      *
344      * @return a copy of the attributes in this model presentation
345      * @since 3.0
346      */

347     public Map JavaDoc getAttributeMap() {
348         return (Map JavaDoc) fAttributes.clone();
349     }
350     
351     public Map JavaDoc getAttributes() {
352         return fAttributes;
353     }
354
355     /* (non-Javadoc)
356      * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
357      */

358     public Color getForeground(Object JavaDoc element) {
359         IDebugModelPresentation presentation = getPresentation();
360         if (presentation instanceof IColorProvider) {
361             IColorProvider colorProvider = (IColorProvider) presentation;
362             return colorProvider.getForeground(element);
363         }
364         return null;
365     }
366
367     /* (non-Javadoc)
368      * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
369      */

370     public Color getBackground(Object JavaDoc element) {
371         IDebugModelPresentation presentation = getPresentation();
372         if (presentation instanceof IColorProvider) {
373             IColorProvider colorProvider = (IColorProvider) presentation;
374             return colorProvider.getBackground(element);
375         }
376         return null;
377     }
378     
379     /* (non-Javadoc)
380      * @see org.eclipse.jface.viewers.IFontProvider#getFont(java.lang.Object)
381      */

382     public Font getFont(Object JavaDoc element) {
383         IDebugModelPresentation presentation = getPresentation();
384         if (presentation instanceof IFontProvider) {
385             IFontProvider fontProvider = (IFontProvider) presentation;
386             return fontProvider.getFont(element);
387         }
388         return null;
389     }
390
391     /* (non-Javadoc)
392      * @see org.eclipse.debug.ui.IInstructionPointerPresentation#getInstructionPointerAnnotation(org.eclipse.ui.IEditorPart, org.eclipse.debug.core.model.IStackFrame)
393      */

394     public Annotation getInstructionPointerAnnotation(IEditorPart editorPart, IStackFrame frame) {
395         IDebugModelPresentation presentation = getPresentation();
396         if (presentation instanceof IInstructionPointerPresentation) {
397             IInstructionPointerPresentation pointerPresentation = (IInstructionPointerPresentation) presentation;
398             return pointerPresentation.getInstructionPointerAnnotation(editorPart, frame);
399         }
400         return null;
401     }
402
403     /* (non-Javadoc)
404      * @see org.eclipse.debug.ui.IInstructionPointerPresentation#getMarkerAnnotationSpecificationId(org.eclipse.ui.IEditorPart, org.eclipse.debug.core.model.IStackFrame)
405      */

406     public String JavaDoc getInstructionPointerAnnotationType(IEditorPart editorPart, IStackFrame frame) {
407         IDebugModelPresentation presentation = getPresentation();
408         if (presentation instanceof IInstructionPointerPresentation) {
409             IInstructionPointerPresentation pointerPresentation = (IInstructionPointerPresentation) presentation;
410             return pointerPresentation.getInstructionPointerAnnotationType(editorPart, frame);
411         }
412         return null;
413     }
414
415     /* (non-Javadoc)
416      * @see org.eclipse.debug.ui.IInstructionPointerPresentation#getInstructionPointerImage(org.eclipse.ui.IEditorPart, org.eclipse.debug.core.model.IStackFrame)
417      */

418     public Image getInstructionPointerImage(IEditorPart editorPart, IStackFrame frame) {
419         IDebugModelPresentation presentation = getPresentation();
420         if (presentation instanceof IInstructionPointerPresentation) {
421             IInstructionPointerPresentation pointerPresentation = (IInstructionPointerPresentation) presentation;
422             return pointerPresentation.getInstructionPointerImage(editorPart, frame);
423         }
424         return null;
425     }
426
427     /* (non-Javadoc)
428      * @see org.eclipse.debug.ui.IInstructionPointerPresentation#getInstructionPointerText(org.eclipse.ui.IEditorPart, org.eclipse.debug.core.model.IStackFrame)
429      */

430     public String JavaDoc getInstructionPointerText(IEditorPart editorPart, IStackFrame frame) {
431         IDebugModelPresentation presentation = getPresentation();
432         if (presentation instanceof IInstructionPointerPresentation) {
433             IInstructionPointerPresentation pointerPresentation = (IInstructionPointerPresentation) presentation;
434             return pointerPresentation.getInstructionPointerText(editorPart, frame);
435         }
436         return null;
437     }
438 }
439
Popular Tags