1 11 package org.eclipse.debug.internal.ui.memory; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 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 42 class RenderingBindings extends AbstractMemoryRenderingBindingsProvider implements IMemoryRenderingBindingsProvider { 43 44 protected IConfigurationElement fConfigurationElement; 46 47 private IMemoryRenderingType[] fAllTypes; 49 private IMemoryRenderingType[] fRenderingTypes; 50 private IMemoryRenderingType[] fDefaultTypes; 51 52 private IMemoryRenderingBindingsProvider fProvider; 54 55 private Expression fExpression; 57 58 public static final String ATTR_RENDERING_IDS = "renderingIds"; public static final String ATTR_DEFAULT_IDS = "defaultIds"; public static final String ATTR_PRIMARY = "primaryId"; public static final String ATTR_PROVIDER = "class"; 64 private static final IMemoryRenderingType[] EMPTY = new IMemoryRenderingType[0]; 66 67 72 RenderingBindings(IConfigurationElement element) { 73 fConfigurationElement = element; 74 } 75 76 81 private IMemoryRenderingType[] getBindings() { 82 if (fRenderingTypes == null) { 83 String ids = fConfigurationElement.getAttribute(ATTR_RENDERING_IDS); 84 List list = new ArrayList (); 85 IMemoryRenderingManager manager = getManager(); 86 if (ids != null) { 87 String [] strings = ids.split(","); for (int i = 0; i < strings.length; i++) { 89 String id = strings[i].trim(); 90 IMemoryRenderingType type = manager.getRenderingType(id); 91 if (type != null) { 92 list.add(type); 93 } 94 } 95 } 96 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 111 private IMemoryRenderingType[] getDefaultBindings() { 112 if (fDefaultTypes == null) { 113 String ids = fConfigurationElement.getAttribute(ATTR_DEFAULT_IDS); 114 List list = new ArrayList (); 115 IMemoryRenderingManager manager = getManager(); 116 if (ids != null) { 117 String [] strings = ids.split(","); for (int i = 0; i < strings.length; i++) { 119 String id = strings[i].trim(); 120 IMemoryRenderingType type = manager.getRenderingType(id); 121 if (type != null) { 122 list.add(type); 123 } 124 } 125 } 126 String 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 144 private String getPrimaryId() { 145 return fConfigurationElement.getAttribute(ATTR_PRIMARY); 146 } 147 148 153 protected IMemoryRenderingBindingsProvider getProvider(IMemoryBlock memoryBlock) { 154 if (isBound(memoryBlock)) 155 { 156 if (fProvider == null) { 157 String 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 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 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); throw new CoreException(status); 211 } 212 } 213 } 214 215 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 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 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 277 public IMemoryRenderingType getPrimaryRenderingType(IMemoryBlock block) { 278 if (isBound(block)) { 279 IMemoryRenderingBindingsProvider provider = getProvider(block); 280 if (provider == null) { 281 String 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 |