KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > memory > RenderingBindings


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.memory;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.expressions.EvaluationContext;
17 import org.eclipse.core.expressions.EvaluationResult;
18 import org.eclipse.core.expressions.Expression;
19 import org.eclipse.core.expressions.ExpressionConverter;
20 import org.eclipse.core.expressions.ExpressionTagNames;
21 import org.eclipse.core.expressions.IEvaluationContext;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IConfigurationElement;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.Status;
26 import org.eclipse.debug.core.model.IMemoryBlock;
27 import org.eclipse.debug.internal.ui.DebugUIPlugin;
28 import org.eclipse.debug.ui.DebugUITools;
29 import org.eclipse.debug.ui.IDebugUIConstants;
30 import org.eclipse.debug.ui.memory.AbstractMemoryRenderingBindingsProvider;
31 import org.eclipse.debug.ui.memory.IMemoryRenderingBindingsListener;
32 import org.eclipse.debug.ui.memory.IMemoryRenderingBindingsProvider;
33 import org.eclipse.debug.ui.memory.IMemoryRenderingManager;
34 import org.eclipse.debug.ui.memory.IMemoryRenderingType;
35
36 /**
37  * Represents a renderingBindings element of a memoryRenderings
38  * contribution.
39  *
40  * @since 3.1
41  */

42 class RenderingBindings extends AbstractMemoryRenderingBindingsProvider implements IMemoryRenderingBindingsProvider {
43     
44     // element
45
protected IConfigurationElement fConfigurationElement;
46     
47     // cached rendering ids
48
private IMemoryRenderingType[] fAllTypes;
49     private IMemoryRenderingType[] fRenderingTypes;
50     private IMemoryRenderingType[] fDefaultTypes;
51     
52     // rendering type provider, or null (optional)
53
private IMemoryRenderingBindingsProvider fProvider;
54     
55     // optional exprssion
56
private Expression fExpression;
57     
58     // element attribute
59
public static final String JavaDoc ATTR_RENDERING_IDS = "renderingIds"; //$NON-NLS-1$
60
public static final String JavaDoc ATTR_DEFAULT_IDS = "defaultIds"; //$NON-NLS-1$
61
public static final String JavaDoc ATTR_PRIMARY = "primaryId"; //$NON-NLS-1$
62
public static final String JavaDoc ATTR_PROVIDER = "class"; //$NON-NLS-1$
63

64     // empty bindings
65
private static final IMemoryRenderingType[] EMPTY = new IMemoryRenderingType[0];
66     
67     /**
68      * Constructs a bindings element from the given contribution.
69      *
70      * @param element contribution
71      */

72     RenderingBindings(IConfigurationElement element) {
73         fConfigurationElement = element;
74     }
75     
76     /**
77      * Returns the non-default bindings specified by this contribution.
78      *
79      * @return the non-default bindings specified by this contribution
80      */

81     private IMemoryRenderingType[] getBindings() {
82         if (fRenderingTypes == null) {
83             String JavaDoc ids = fConfigurationElement.getAttribute(ATTR_RENDERING_IDS);
84             List JavaDoc list = new ArrayList JavaDoc();
85             IMemoryRenderingManager manager = getManager();
86             if (ids != null) {
87                 String JavaDoc[] strings = ids.split(","); //$NON-NLS-1$
88
for (int i = 0; i < strings.length; i++) {
89                     String JavaDoc id = strings[i].trim();
90                     IMemoryRenderingType type = manager.getRenderingType(id);
91                     if (type != null) {
92                         list.add(type);
93                     }
94                 }
95             }
96             // remove any default bindings, in case of duplicate specification
97
IMemoryRenderingType[] defaultBindings = getDefaultBindings();
98             for (int i = 0; i < defaultBindings.length; i++) {
99                 list.remove(defaultBindings[i]);
100             }
101             fRenderingTypes = (IMemoryRenderingType[]) list.toArray(new IMemoryRenderingType[list.size()]);
102         }
103         return fRenderingTypes;
104     }
105     
106     /**
107      * Returns the default bindings specified by this contribution.
108      *
109      * @return the default bindings specified by this contribution
110      */

111     private IMemoryRenderingType[] getDefaultBindings() {
112         if (fDefaultTypes == null) {
113             String JavaDoc ids = fConfigurationElement.getAttribute(ATTR_DEFAULT_IDS);
114             List JavaDoc list = new ArrayList JavaDoc();
115             IMemoryRenderingManager manager = getManager();
116             if (ids != null) {
117                 String JavaDoc[] strings = ids.split(","); //$NON-NLS-1$
118
for (int i = 0; i < strings.length; i++) {
119                     String JavaDoc id = strings[i].trim();
120                     IMemoryRenderingType type = manager.getRenderingType(id);
121                     if (type != null) {
122                         list.add(type);
123                     }
124                 }
125             }
126             // the primary is also considered a default rendering
127
String JavaDoc primaryId = getPrimaryId();
128             if (primaryId != null) {
129                 IMemoryRenderingType type = manager.getRenderingType(primaryId);
130                 if (type != null) {
131                     list.add(type);
132                 }
133             }
134             fDefaultTypes = (IMemoryRenderingType[]) list.toArray(new IMemoryRenderingType[list.size()]);
135         }
136         return fDefaultTypes;
137     }
138     
139     /**
140      * Returns the primary id, or <code>null</code> if none.
141      *
142      * @return the primary id, or <code>null</code> if none
143      */

144     private String JavaDoc getPrimaryId() {
145         return fConfigurationElement.getAttribute(ATTR_PRIMARY);
146     }
147     
148     /**
149      * Returns the provider for this binding or <code>null</code> of none.
150      *
151      * @return the provider for this binding or <code>null</code> of none
152      */

153     protected IMemoryRenderingBindingsProvider getProvider(IMemoryBlock memoryBlock) {
154         if (isBound(memoryBlock))
155         {
156             if (fProvider == null) {
157                 String JavaDoc name = fConfigurationElement.getAttribute(ATTR_PROVIDER);
158                 if (name != null) {
159                     try {
160                         fProvider = (IMemoryRenderingBindingsProvider) fConfigurationElement.createExecutableExtension(ATTR_PROVIDER);
161                     } catch (CoreException e) {
162                         DebugUIPlugin.log(e);
163                     }
164                 }
165                 
166                 if (fProvider != null)
167                 {
168                     fProvider.addListener(new IMemoryRenderingBindingsListener() {
169                         public void memoryRenderingBindingsChanged() {
170                             fireBindingsChanged();
171                         }});
172                 }
173             }
174         }
175         return fProvider;
176     }
177     
178     /**
179      * Returns whether this binding is applies to the given memory block.
180      *
181      * @param block memory block
182      * @return whether this binding is applies to the given memory block
183      */

184     private boolean isBound(IMemoryBlock block) {
185         Expression expression = getExpression();
186         if (expression != null) {
187             IEvaluationContext context = new EvaluationContext(null, block);
188             try {
189                 return expression.evaluate(context) == EvaluationResult.TRUE;
190             } catch (CoreException e) {
191                 DebugUIPlugin.log(e);
192                 return false;
193             }
194         }
195         return true;
196     }
197     
198     /**
199      * Validates this contribution.
200      *
201      * @exception CoreException if invalid
202      */

203     void validate() throws CoreException {
204         if (fConfigurationElement.getAttribute(ATTR_PROVIDER) != null) {
205             if (fConfigurationElement.getAttribute(ATTR_RENDERING_IDS) != null ||
206                     fConfigurationElement.getAttribute(ATTR_DEFAULT_IDS) != null ||
207                     fConfigurationElement.getAttribute(ATTR_PRIMARY) != null) {
208                 Status status = new Status(IStatus.ERROR, DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.INTERNAL_ERROR,
209                         "<renderingBindings> element cannot specify other attributes when " + ATTR_PROVIDER + " is present", null); //$NON-NLS-1$ //$NON-NLS-2$
210
throw new CoreException(status);
211             }
212         }
213     }
214     
215     /**
216      * Returns this binding's enablement expression, or <code>null</code> if none.
217      *
218      * @return enablement expression, or <code>null</code> if none
219      */

220     private Expression getExpression() {
221         if (fExpression == null) {
222             IConfigurationElement[] elements = fConfigurationElement.getChildren(ExpressionTagNames.ENABLEMENT);
223             IConfigurationElement enablement = elements.length > 0 ? elements[0] : null;
224             if (enablement != null) {
225                 try {
226                     fExpression = ExpressionConverter.getDefault().perform(enablement);
227                 } catch (CoreException e) {
228                     DebugUIPlugin.log(e);
229                 }
230             }
231         }
232         return fExpression;
233     }
234
235     /* (non-Javadoc)
236      * @see org.eclipse.debug.ui.memory.IMemoryRenderingBindingsProvider#getRenderingTypes(org.eclipse.debug.core.model.IMemoryBlock)
237      */

238     public IMemoryRenderingType[] getRenderingTypes(IMemoryBlock block) {
239         if (isBound(block)) {
240             IMemoryRenderingBindingsProvider provider = getProvider(block);
241             if (provider == null) {
242                 if (fAllTypes == null) {
243                     IMemoryRenderingType[] defaultBindings = getDefaultBindings();
244                     IMemoryRenderingType[] bindings = getBindings();
245                     fAllTypes = new IMemoryRenderingType[defaultBindings.length + bindings.length];
246                     for (int i = 0; i < defaultBindings.length; i++) {
247                         fAllTypes[i] = defaultBindings[i];
248                     }
249                     for (int i = 0, j = defaultBindings.length; i < bindings.length; i++, j++) {
250                         fAllTypes[j] = bindings[i];
251                     }
252                 }
253                 return fAllTypes;
254             }
255             return provider.getRenderingTypes(block);
256         }
257         return EMPTY;
258     }
259
260     /* (non-Javadoc)
261      * @see org.eclipse.debug.ui.memory.IMemoryRenderingBindingsProvider#getDefaultRenderingTypes(org.eclipse.debug.core.model.IMemoryBlock)
262      */

263     public IMemoryRenderingType[] getDefaultRenderingTypes(IMemoryBlock block) {
264         if (isBound(block)) {
265             IMemoryRenderingBindingsProvider provider = getProvider(block);
266             if (provider == null) {
267                 return getDefaultBindings();
268             }
269             return provider.getDefaultRenderingTypes(block);
270         }
271         return EMPTY;
272     }
273
274     /* (non-Javadoc)
275      * @see org.eclipse.debug.ui.memory.IMemoryRenderingBindingsProvider#getPrimaryRenderingType(org.eclipse.debug.core.model.IMemoryBlock)
276      */

277     public IMemoryRenderingType getPrimaryRenderingType(IMemoryBlock block) {
278         if (isBound(block)) {
279             IMemoryRenderingBindingsProvider provider = getProvider(block);
280             if (provider == null) {
281                 String JavaDoc primaryId = getPrimaryId();
282                 if (primaryId != null) {
283                     return getManager().getRenderingType(primaryId);
284                 }
285             } else {
286                 return provider.getPrimaryRenderingType(block);
287             }
288         }
289         return null;
290     }
291     
292     private IMemoryRenderingManager getManager() {
293         return DebugUITools.getMemoryRenderingManager();
294     }
295     
296 }
297
Popular Tags