1 11 package org.eclipse.ui.internal.decorators; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IConfigurationElement; 15 import org.eclipse.jface.viewers.IBaseLabelProvider; 16 import org.eclipse.jface.viewers.ILabelProviderListener; 17 import org.eclipse.ui.internal.ActionExpression; 18 import org.eclipse.ui.internal.WorkbenchPlugin; 19 import org.eclipse.ui.internal.registry.RegistryReader; 20 21 26 27 public abstract class DecoratorDefinition { 28 29 private static final String ATT_LABEL = "label"; 31 private static final String ATT_OBJECT_CLASS = "objectClass"; 33 static final String CHILD_ENABLEMENT = "enablement"; 35 private static final String ATT_ADAPTABLE = "adaptable"; 37 private static final String ATT_ENABLED = "state"; 39 private ActionExpression enablement; 40 41 protected boolean enabled; 42 43 private boolean defaultEnabled; 44 45 private String id; 46 47 protected IConfigurationElement definingElement; 48 49 protected boolean labelProviderCreationFailed = false; 51 52 private boolean hasReadEnablement; 53 54 static final String ATT_CLASS = "class"; 56 60 61 DecoratorDefinition(String identifier, IConfigurationElement element) { 62 63 this.id = identifier; 64 this.definingElement = element; 65 66 this.enabled = this.defaultEnabled = Boolean.valueOf(element.getAttribute(ATT_ENABLED)).booleanValue(); 67 } 68 69 73 public String getName() { 74 return definingElement.getAttribute(ATT_LABEL); 75 } 76 77 81 public String getDescription() { 82 return RegistryReader.getDescription(definingElement); 83 } 84 85 89 public boolean isEnabled() { 90 return enabled; 91 } 92 93 98 public void setEnabled(boolean newState) { 99 100 if (this.enabled != newState) { 102 this.enabled = newState; 103 try { 104 refreshDecorator(); 105 } catch (CoreException exception) { 106 handleCoreException(exception); 107 } 108 109 } 110 } 111 112 116 protected abstract void refreshDecorator() throws CoreException; 117 118 123 protected void disposeCachedDecorator(IBaseLabelProvider disposedDecorator) { 124 disposedDecorator.removeListener(WorkbenchPlugin.getDefault() 125 .getDecoratorManager()); 126 disposedDecorator.dispose(); 127 128 } 129 130 137 public boolean isAdaptable() { 138 return Boolean.valueOf(definingElement.getAttribute(ATT_ADAPTABLE)).booleanValue(); 139 } 140 141 145 public String getId() { 146 return id; 147 } 148 149 156 public boolean getDefaultValue() { 157 return defaultEnabled; 158 } 159 160 164 protected ActionExpression getEnablement() { 165 if (!hasReadEnablement) { 166 hasReadEnablement = true; 167 initializeEnablement(); 168 } 169 return enablement; 170 } 171 172 175 protected void initializeEnablement() { 176 IConfigurationElement[] elements = definingElement.getChildren(CHILD_ENABLEMENT); 177 if (elements.length == 0) { 178 String className = definingElement.getAttribute(ATT_OBJECT_CLASS); 179 if (className != null) { 180 enablement = new ActionExpression(ATT_OBJECT_CLASS, 181 className); 182 } 183 } else { 184 enablement = new ActionExpression(elements[0]); 185 } 186 } 187 188 194 void addListener(ILabelProviderListener listener) { 195 try { 196 IBaseLabelProvider currentDecorator = internalGetLabelProvider(); 198 if (currentDecorator != null) { 199 currentDecorator.addListener(listener); 200 } 201 } catch (CoreException exception) { 202 handleCoreException(exception); 203 } 204 } 205 206 213 boolean isLabelProperty(Object element, String property) { 214 try { IBaseLabelProvider currentDecorator = internalGetLabelProvider(); 216 if (currentDecorator != null) { 217 return currentDecorator.isLabelProperty(element, property); 218 } 219 } catch (CoreException exception) { 220 handleCoreException(exception); 221 return false; 222 } 223 return false; 224 } 225 226 234 protected abstract IBaseLabelProvider internalGetLabelProvider() 235 throws CoreException; 236 237 241 242 protected void handleCoreException(CoreException exception) { 243 244 WorkbenchPlugin.log(exception); 246 crashDisable(); 247 } 248 249 252 public void crashDisable() { 253 this.enabled = false; 254 } 255 256 260 public abstract boolean isFull(); 261 262 268 public IConfigurationElement getConfigurationElement() { 269 return definingElement; 270 } 271 272 277 public boolean isEnabledFor(Object element) { 278 if(isEnabled()){ 279 ActionExpression expression = getEnablement(); 280 if(expression != null) { 281 return expression.isEnabledFor(element); 282 } 283 return true; } 285 return false; 286 287 } 288 } 289 | Popular Tags |