1 11 package org.eclipse.ui.internal.views.properties.tabbed.view; 12 13 import com.ibm.icu.text.MessageFormat; 14 import java.util.ArrayList ; 15 import java.util.Collections ; 16 import java.util.Comparator ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IConfigurationElement; 22 import org.eclipse.core.runtime.IExtensionPoint; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.Platform; 25 import org.eclipse.core.runtime.Status; 26 import org.eclipse.jface.viewers.ILabelProvider; 27 import org.eclipse.jface.viewers.ISelection; 28 import org.eclipse.ui.IWorkbenchPart; 29 import org.eclipse.ui.internal.views.properties.tabbed.TabbedPropertyViewPlugin; 30 import org.eclipse.ui.internal.views.properties.tabbed.TabbedPropertyViewStatusCodes; 31 import org.eclipse.ui.internal.views.properties.tabbed.l10n.TabbedPropertyMessages; 32 import org.eclipse.ui.views.properties.tabbed.IActionProvider; 33 import org.eclipse.ui.views.properties.tabbed.ISectionDescriptor; 34 import org.eclipse.ui.views.properties.tabbed.ISectionDescriptorProvider; 35 import org.eclipse.ui.views.properties.tabbed.ITypeMapper; 36 37 43 public class TabbedPropertyRegistry { 44 45 private final static String NO_TAB_ERROR = TabbedPropertyMessages.TabbedPropertyRegistry_Non_existing_tab; 46 47 private final static String CONTRIBUTOR_ERROR = TabbedPropertyMessages.TabbedPropertyRegistry_contributor_error; 48 49 private static final String EXTPT_CONTRIBUTOR = "propertyContributor"; 52 private static final String EXTPT_TABS = "propertyTabs"; 54 private static final String EXTPT_SECTIONS = "propertySections"; 56 private static final String ELEMENT_TAB = "propertyTab"; 58 private static final String ELEMENT_SECTION = "propertySection"; 60 private static final String ELEMENT_PROPERTY_CATEGORY = "propertyCategory"; 62 private static final String ATT_CATEGORY = "category"; 64 private static final String ATT_CONTRIBUTOR_ID = "contributorId"; 66 private static final String ATT_TYPE_MAPPER = "typeMapper"; 68 private static final String ATT_LABEL_PROVIDER = "labelProvider"; 70 private static final String ATT_ACTION_PROVIDER = "actionProvider"; 72 private static final String TOP = "top"; 74 protected String contributorId; 75 76 protected IConfigurationElement contributorConfigurationElement; 77 78 protected List propertyCategories; 79 80 protected ILabelProvider labelProvider; 81 82 protected IActionProvider actionProvider; 83 84 protected ITypeMapper typeMapper; 85 86 protected ISectionDescriptorProvider sectionDescriptorProvider; 87 88 protected TabDescriptor[] tabDescriptors; 89 90 protected static final TabDescriptor[] EMPTY_DESCRIPTOR_ARRAY = new TabDescriptor[0]; 91 92 95 protected TabbedPropertyRegistry(String id) { 96 this.contributorId = id; 97 this.propertyCategories = new ArrayList (); 98 IConfigurationElement[] extensions = getConfigurationElements(EXTPT_CONTRIBUTOR); 99 for (int i = 0; i < extensions.length; i++) { 100 IConfigurationElement configurationElement = extensions[i]; 101 String contributor = configurationElement 102 .getAttribute(ATT_CONTRIBUTOR_ID); 103 if (contributor == null || !id.equals(contributor)) { 104 continue; 105 } 106 this.contributorConfigurationElement = configurationElement; 107 try { 108 if (configurationElement.getAttribute(ATT_LABEL_PROVIDER) != null) { 109 labelProvider = (ILabelProvider) configurationElement 110 .createExecutableExtension(ATT_LABEL_PROVIDER); 111 } 112 if (configurationElement.getAttribute(ATT_ACTION_PROVIDER) != null) { 113 actionProvider = (IActionProvider) configurationElement 114 .createExecutableExtension(ATT_ACTION_PROVIDER); 115 } 116 if (configurationElement.getAttribute(ATT_TYPE_MAPPER) != null) { 117 typeMapper = (ITypeMapper) configurationElement 118 .createExecutableExtension(ATT_TYPE_MAPPER); 119 } 120 } catch (CoreException exception) { 121 handleConfigurationError(id, exception); 122 } 123 addPropertyCategories(configurationElement); 124 } 125 if (propertyCategories == null || contributorId == null 126 || contributorConfigurationElement == null) { 127 handleConfigurationError(id, null); 128 this.contributorId = null; 129 } 130 } 131 132 139 private void addPropertyCategories( 140 IConfigurationElement configurationElement) { 141 IConfigurationElement[] elements = configurationElement 142 .getChildren(ELEMENT_PROPERTY_CATEGORY); 143 for (int i = 0; i < elements.length; i++) { 144 IConfigurationElement element = elements[i]; 145 propertyCategories.add(element.getAttribute(ATT_CATEGORY)); 146 } 147 } 148 149 158 private void handleConfigurationError(String id, CoreException exception) { 159 String message = MessageFormat.format(CONTRIBUTOR_ERROR, 160 new Object [] {id}); 161 IStatus status = new Status(IStatus.ERROR, TabbedPropertyViewPlugin 162 .getPlugin().getBundle().getSymbolicName(), 163 TabbedPropertyViewStatusCodes.CONTRIBUTOR_ERROR, message, exception); 164 TabbedPropertyViewPlugin.getPlugin().getLog().log(status); 165 } 166 167 171 protected ISectionDescriptor[] readSectionDescriptors() { 172 List result = new ArrayList (); 173 IConfigurationElement[] extensions = getConfigurationElements(EXTPT_SECTIONS); 174 for (int i = 0; i < extensions.length; i++) { 175 IConfigurationElement extension = extensions[i]; 176 IConfigurationElement[] sections = extension 177 .getChildren(ELEMENT_SECTION); 178 for (int j = 0; j < sections.length; j++) { 179 IConfigurationElement section = sections[j]; 180 ISectionDescriptor descriptor = new SectionDescriptor(section, 181 typeMapper); 182 result.add(descriptor); 183 } 184 } 185 return (ISectionDescriptor[]) result 186 .toArray(new ISectionDescriptor[result.size()]); 187 } 188 189 194 protected IConfigurationElement[] getConfigurationElements( 195 String extensionPointId) { 196 if (contributorId == null) { 197 return new IConfigurationElement[0]; 198 } 199 IExtensionPoint point = Platform.getExtensionRegistry() 200 .getExtensionPoint( 201 TabbedPropertyViewPlugin.getPlugin().getBundle() 202 .getSymbolicName(), extensionPointId); 203 IConfigurationElement[] extensions = point.getConfigurationElements(); 204 List unordered = new ArrayList (extensions.length); 205 for (int i = 0; i < extensions.length; i++) { 206 IConfigurationElement extension = extensions[i]; 207 if (!extension.getName().equals(extensionPointId)) { 208 continue; 209 } 210 String contributor = extension.getAttribute(ATT_CONTRIBUTOR_ID); 211 if (!contributorId.equals(contributor)) { 212 continue; 213 } 214 unordered.add(extension); 215 } 216 return (IConfigurationElement[]) unordered 217 .toArray(new IConfigurationElement[unordered.size()]); 218 } 219 220 223 private int getIndex(Object [] array, Object target) { 224 for (int i = 0; i < array.length; i++) { 225 if (array[i].equals(target)) { 226 return i; 227 } 228 } 229 return -1; } 231 232 236 public TabDescriptor[] getTabDescriptors(IWorkbenchPart part, 237 ISelection selection) { 238 if (selection == null || selection.isEmpty()) { 239 return EMPTY_DESCRIPTOR_ARRAY; 240 } 241 242 TabDescriptor[] allDescriptors = getAllTabDescriptors(); 243 TabDescriptor[] result = filterTabDescriptors(allDescriptors, part, 244 selection); 245 return result; 246 } 247 248 252 protected TabDescriptor[] filterTabDescriptors(TabDescriptor[] descriptors, 253 IWorkbenchPart part, ISelection selection) { 254 List result = new ArrayList (); 255 for (int i = 0; i < descriptors.length; i++) { 256 TabDescriptor descriptor = adaptDescriptorFor(descriptors[i], part, 257 selection); 258 if (!descriptor.getSectionDescriptors().isEmpty()) { 259 result.add(descriptor); 260 } 261 } 262 if (result.size() == 0) { 263 return EMPTY_DESCRIPTOR_ARRAY; 264 } 265 return (TabDescriptor[]) result 266 .toArray(new TabDescriptor[result.size()]); 267 } 268 269 273 protected TabDescriptor adaptDescriptorFor(TabDescriptor target, 274 IWorkbenchPart part, ISelection selection) { 275 List filteredSectionDescriptors = new ArrayList (); 276 List descriptors = target.getSectionDescriptors(); 277 for (Iterator iter = descriptors.iterator(); iter.hasNext();) { 278 ISectionDescriptor descriptor = (ISectionDescriptor) iter.next(); 279 if (descriptor.appliesTo(part, selection)) { 280 filteredSectionDescriptors.add(descriptor); 281 } 282 } 283 TabDescriptor result = (TabDescriptor) target.clone(); 284 result.setSectionDescriptors(filteredSectionDescriptors); 285 return result; 286 } 287 288 292 protected TabDescriptor[] getAllTabDescriptors() { 293 if (tabDescriptors == null) { 294 List temp = readTabDescriptors(); 295 populateWithSectionDescriptors(temp); 296 temp = sortTabDescriptorsByCategory(temp); 297 temp = sortTabDescriptorsByAfterTab(temp); 298 tabDescriptors = (TabDescriptor[]) temp 299 .toArray(new TabDescriptor[temp.size()]); 300 } 301 return tabDescriptors; 302 } 303 304 308 protected List readTabDescriptors() { 309 List result = new ArrayList (); 310 IConfigurationElement[] extensions = getConfigurationElements(EXTPT_TABS); 311 for (int i = 0; i < extensions.length; i++) { 312 IConfigurationElement extension = extensions[i]; 313 IConfigurationElement[] tabs = extension.getChildren(ELEMENT_TAB); 314 for (int j = 0; j < tabs.length; j++) { 315 IConfigurationElement tab = tabs[j]; 316 TabDescriptor descriptor = new TabDescriptor(tab); 317 result.add(descriptor); 318 } 319 } 320 return result; 321 } 322 323 326 protected void populateWithSectionDescriptors(List aTabDescriptors) { 327 ISectionDescriptor[] sections = null; 328 if (sectionDescriptorProvider != null) { 329 sections = sectionDescriptorProvider.getSectionDescriptors(); 330 } else { 331 sections = readSectionDescriptors(); 332 } 333 for (int i = 0; i < sections.length; i++) { 334 ISectionDescriptor section = sections[i]; 335 appendToTabDescriptor(section, aTabDescriptors); 336 } 337 } 338 339 342 protected void appendToTabDescriptor(ISectionDescriptor section, 343 List aTabDescriptors) { 344 for (Iterator i = aTabDescriptors.iterator(); i.hasNext();) { 345 TabDescriptor tab = (TabDescriptor) i.next(); 346 if (tab.append(section)) { 347 return; 348 } 349 } 350 String message = MessageFormat.format(NO_TAB_ERROR, new Object [] { 352 section.getId(), section.getTargetTab()}); 353 IStatus status = new Status(IStatus.ERROR, TabbedPropertyViewPlugin 354 .getPlugin().getBundle().getSymbolicName(), 355 TabbedPropertyViewStatusCodes.NO_TAB_ERROR, message, null); 356 TabbedPropertyViewPlugin.getPlugin().getLog().log(status); 357 } 358 359 362 protected List sortTabDescriptorsByCategory(List descriptors) { 363 Collections.sort(descriptors, new Comparator () { 364 365 public int compare(Object arg0, Object arg1) { 366 TabDescriptor one = (TabDescriptor) arg0; 367 TabDescriptor two = (TabDescriptor) arg1; 368 String categoryOne = one.getCategory(); 369 String categoryTwo = two.getCategory(); 370 int categoryOnePosition = getIndex(propertyCategories.toArray(), 371 categoryOne); 372 int categoryTwoPosition = getIndex(propertyCategories.toArray(), 373 categoryTwo); 374 return categoryOnePosition - categoryTwoPosition; 375 } 376 }); 377 return descriptors; 378 } 379 380 383 protected List sortTabDescriptorsByAfterTab(List tabs) { 384 if (tabs.size() == 0 || propertyCategories == null) { 385 return tabs; 386 } 387 List sorted = new ArrayList (); 388 int categoryIndex = 0; 389 for (int i = 0; i < propertyCategories.size(); i++) { 390 List categoryList = new ArrayList (); 391 String category = (String )propertyCategories.get(i); 392 int topOfCategory = categoryIndex; 393 int endOfCategory = categoryIndex; 394 while (endOfCategory < tabs.size() 395 && ((TabDescriptor) tabs.get(endOfCategory)).getCategory() 396 .equals(category)) { 397 endOfCategory++; 398 } 399 for (int j = topOfCategory; j < endOfCategory; j++) { 400 TabDescriptor tab = (TabDescriptor) tabs.get(j); 401 if (tab.getAfterTab().equals(TOP)) { 402 categoryList.add(0, tabs.get(j)); 403 } else { 404 categoryList.add(tabs.get(j)); 405 } 406 } 407 Collections.sort(categoryList, new Comparator () { 408 409 public int compare(Object arg0, Object arg1) { 410 TabDescriptor one = (TabDescriptor) arg0; 411 TabDescriptor two = (TabDescriptor) arg1; 412 if (two.getAfterTab().equals(one.getId())) { 413 return -1; 414 } else if (one.getAfterTab().equals(two.getId())) { 415 return 1; 416 } else { 417 return 0; 418 } 419 } 420 }); 421 for (int j = 0; j < categoryList.size(); j++) { 422 sorted.add(categoryList.get(j)); 423 } 424 categoryIndex = endOfCategory; 425 } 426 return sorted; 427 } 428 429 434 public ITypeMapper getTypeMapper() { 435 return typeMapper; 436 } 437 438 443 public ILabelProvider getLabelProvider() { 444 return labelProvider; 445 } 446 447 452 public IActionProvider getActionProvider() { 453 return actionProvider; 454 } 455 456 462 public void setSectionDescriptorProvider( 463 ISectionDescriptorProvider sectionDescriptorProvider) { 464 this.sectionDescriptorProvider = sectionDescriptorProvider; 465 } 466 } 467 | Popular Tags |