1 11 package org.eclipse.ui.internal.dialogs; 12 13 import java.lang.ref.SoftReference ; 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 20 import org.eclipse.core.expressions.EvaluationContext; 21 import org.eclipse.core.expressions.EvaluationResult; 22 import org.eclipse.core.expressions.Expression; 23 import org.eclipse.core.expressions.ExpressionConverter; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IAdaptable; 26 import org.eclipse.core.runtime.IConfigurationElement; 27 import org.eclipse.core.runtime.IStatus; 28 import org.eclipse.core.runtime.Status; 29 import org.eclipse.jface.resource.ImageDescriptor; 30 import org.eclipse.ui.IActionFilter; 31 import org.eclipse.ui.IWorkbenchPropertyPage; 32 import org.eclipse.ui.SelectionEnabler; 33 import org.eclipse.ui.internal.LegacyResourceSupport; 34 import org.eclipse.ui.internal.WorkbenchPlugin; 35 import org.eclipse.ui.internal.registry.CategorizedPageRegistryReader; 36 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 37 import org.eclipse.ui.internal.registry.PropertyPagesRegistryReader; 38 import org.eclipse.ui.internal.util.Util; 39 import org.eclipse.ui.model.IWorkbenchAdapter; 40 import org.eclipse.ui.plugin.AbstractUIPlugin; 41 42 47 48 public class RegistryPageContributor implements IPropertyPageContributor, 49 IAdaptable { 50 private static final String CHILD_ENABLED_WHEN = "enabledWhen"; 52 private String pageId; 53 54 58 private Collection subPages = new ArrayList (); 59 60 private boolean adaptable = false; 61 62 private IConfigurationElement pageElement; 63 64 private SoftReference filterProperties; 65 66 private Expression enablementExpression; 67 68 76 public RegistryPageContributor(String pageId, IConfigurationElement element) { 77 this.pageId = pageId; 78 this.pageElement = element; 79 adaptable = Boolean 80 .valueOf( 81 pageElement 82 .getAttribute(PropertyPagesRegistryReader.ATT_ADAPTABLE)) 83 .booleanValue(); 84 initializeEnablement(element); 85 } 86 87 93 public boolean contributePropertyPages(PropertyPageManager mng, 94 Object element) { 95 PropertyPageNode node = new PropertyPageNode(this, element); 96 97 if (getCategory() == null) { 98 mng.addToRoot(node); 99 return true; 100 } 101 if (!mng.addToDeep(getCategory(), node)) 102 mng.addToRoot(node); 103 104 return true; 105 } 106 107 116 public IWorkbenchPropertyPage createPage(Object element) 117 throws CoreException { 118 IWorkbenchPropertyPage ppage = null; 119 ppage = (IWorkbenchPropertyPage) WorkbenchPlugin.createExtension( 120 pageElement, IWorkbenchRegistryConstants.ATT_CLASS); 121 122 ppage.setTitle(getPageName()); 123 124 Object adapted = element; 125 if (adaptable) { 126 adapted = getAdaptedElement(element); 127 if (adapted == null) { 128 String message = "Error adapting selection to Property page " + pageId + " is being ignored"; throw new CoreException(new Status(IStatus.ERROR, 130 WorkbenchPlugin.PI_WORKBENCH, IStatus.ERROR, message, 131 null)); 132 } 133 } 134 135 if (adapted instanceof IAdaptable) 136 ppage.setElement((IAdaptable) adapted); 137 else 138 ppage.setElement(new AdaptableForwarder(adapted)); 139 140 return ppage; 141 } 142 143 150 private Object getAdaptedElement(Object element) { 151 Object adapted = LegacyResourceSupport.getAdapter(element, 152 getObjectClass()); 153 if (adapted != null) 154 return adapted; 155 156 return null; 157 } 158 159 164 public String getObjectClass() { 165 return pageElement 166 .getAttribute(PropertyPagesRegistryReader.ATT_OBJECTCLASS); 167 } 168 169 174 public ImageDescriptor getPageIcon() { 175 String iconName = pageElement 176 .getAttribute(IWorkbenchRegistryConstants.ATT_ICON); 177 if (iconName == null) 178 return null; 179 return AbstractUIPlugin.imageDescriptorFromPlugin(pageElement 180 .getNamespaceIdentifier(), iconName); 181 } 182 183 188 189 public String getPageId() { 190 return pageId; 191 } 192 193 198 public String getPageName() { 199 return pageElement.getAttribute(IWorkbenchRegistryConstants.ATT_NAME); 200 } 201 202 206 public boolean isApplicableTo(Object object) { 207 208 if (failsEnablement(object)) 209 return false; 210 211 String nameFilter = pageElement 213 .getAttribute(PropertyPagesRegistryReader.ATT_NAME_FILTER); 214 if (nameFilter != null) { 215 String objectName = object.toString(); 216 IWorkbenchAdapter adapter = (IWorkbenchAdapter) Util.getAdapter(object, 217 IWorkbenchAdapter.class); 218 if (adapter != null) { 219 String elementName = adapter.getLabel(object); 220 if (elementName != null) { 221 objectName = elementName; 222 } 223 } 224 if (!SelectionEnabler.verifyNameMatch(objectName, nameFilter)) 225 return false; 226 } 227 228 if (getFilterProperties() == null) 230 return true; 231 IActionFilter filter = null; 232 233 Object adaptedObject = LegacyResourceSupport.getAdaptedResource(object); 235 if (adaptedObject != null) { 236 object = adaptedObject; 237 } 238 239 filter = (IActionFilter)Util.getAdapter(object, IActionFilter.class); 240 241 if (filter != null) 242 return testCustom(object, filter); 243 244 return true; 245 } 246 247 253 private boolean failsEnablement(Object object) { 254 if (enablementExpression == null) 255 return false; 256 try { 257 return enablementExpression.evaluate( 258 new EvaluationContext(null, object)).equals( 259 EvaluationResult.FALSE); 260 } catch (CoreException e) { 261 WorkbenchPlugin.log(e); 262 return false; 263 } 264 } 265 266 269 protected void initializeEnablement(IConfigurationElement definingElement) { 270 IConfigurationElement[] elements = definingElement 271 .getChildren(CHILD_ENABLED_WHEN); 272 273 if (elements.length == 0) 274 return; 275 276 try { 277 IConfigurationElement[] enablement = elements[0].getChildren(); 278 if (enablement.length == 0) 279 return; 280 enablementExpression = ExpressionConverter.getDefault().perform( 281 enablement[0]); 282 } catch (CoreException e) { 283 WorkbenchPlugin.log(e); 284 } 285 286 } 287 288 292 private boolean testCustom(Object object, IActionFilter filter) { 293 Map filterProperties = getFilterProperties(); 294 295 if (filterProperties == null) 296 return false; 297 Iterator iter = filterProperties.keySet().iterator(); 298 while (iter.hasNext()) { 299 String key = (String ) iter.next(); 300 String value = (String ) filterProperties.get(key); 301 if (!filter.testAttribute(object, key, value)) 302 return false; 303 } 304 return true; 305 } 306 307 310 public boolean canAdapt() { 311 return adaptable; 312 } 313 314 320 public String getCategory() { 321 return pageElement 322 .getAttribute(CategorizedPageRegistryReader.ATT_CATEGORY); 323 } 324 325 330 public Collection getSubPages() { 331 return subPages; 332 } 333 334 339 public void addSubPage(RegistryPageContributor child) { 340 subPages.add(child); 341 } 342 343 private Map getFilterProperties() { 344 if (filterProperties == null || filterProperties.get() == null) { 345 Map map = new HashMap (); 346 filterProperties = new SoftReference (map); 347 IConfigurationElement[] children = pageElement.getChildren(); 348 for (int i = 0; i < children.length; i++) { 349 processChildElement(map, children[i]); 350 } 351 } 352 return (Map ) filterProperties.get(); 353 } 354 355 361 public Object getChild(String id) { 362 Iterator iterator = subPages.iterator(); 363 while (iterator.hasNext()) { 364 RegistryPageContributor next = (RegistryPageContributor) iterator 365 .next(); 366 if (next.getPageId().equals(id)) 367 return next; 368 } 369 return null; 370 } 371 372 377 private void processChildElement(Map map, IConfigurationElement element) { 378 String tag = element.getName(); 379 if (tag.equals(PropertyPagesRegistryReader.TAG_FILTER)) { 380 String key = element 381 .getAttribute(PropertyPagesRegistryReader.ATT_FILTER_NAME); 382 String value = element 383 .getAttribute(PropertyPagesRegistryReader.ATT_FILTER_VALUE); 384 if (key == null || value == null) 385 return; 386 map.put(key, value); 387 } 388 } 389 390 396 public Object getAdapter(Class adapter) { 397 if (adapter.equals(IConfigurationElement.class)) { 398 return getConfigurationElement(); 399 } 400 return null; 401 } 402 403 407 IConfigurationElement getConfigurationElement() { 408 return pageElement; 409 } 410 } 411 | Popular Tags |