1 11 package org.eclipse.ui.internal.dialogs; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.Collections ; 16 import java.util.Comparator ; 17 import java.util.Hashtable ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 import org.eclipse.core.runtime.IConfigurationElement; 22 import org.eclipse.core.runtime.IExtension; 23 import org.eclipse.core.runtime.Platform; 24 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; 25 import org.eclipse.ui.internal.IWorkbenchConstants; 26 import org.eclipse.ui.internal.ObjectContributorManager; 27 import org.eclipse.ui.internal.WorkbenchPlugin; 28 import org.eclipse.ui.internal.registry.PropertyPagesRegistryReader; 29 30 import com.ibm.icu.text.Collator; 31 32 36 37 public class PropertyPageContributorManager extends ObjectContributorManager { 38 private static PropertyPageContributorManager sharedInstance = null; 39 40 private class CategorizedPageNode { 41 RegistryPageContributor contributor; 42 43 CategorizedPageNode parent; 44 45 String qualifiedName; 46 47 CategorizedPageNode(RegistryPageContributor page) { 48 contributor = page; 49 } 50 51 void setParent(CategorizedPageNode node) { 52 parent = node; 53 } 54 55 String getQualifiedName() { 56 57 if (qualifiedName == null) { 58 if (parent == null) { 59 qualifiedName = contributor.getPageName(); 60 } else { 61 StringBuffer nameBuffer = new StringBuffer (); 62 nameBuffer.append(parent.getQualifiedName()); 63 nameBuffer 64 .append(WorkbenchPlugin.PREFERENCE_PAGE_CATEGORY_SEPARATOR); 65 nameBuffer.append(contributor.getPageName()); 66 qualifiedName = nameBuffer.toString(); 67 } 68 } 69 return qualifiedName; 70 71 } 72 } 73 74 private static final Comparator comparer = new Comparator () { 75 private Collator collator = Collator.getInstance(); 76 77 public int compare(Object arg0, Object arg1) { 78 CategorizedPageNode c1 = (CategorizedPageNode) arg0; 80 CategorizedPageNode c2 = (CategorizedPageNode) arg1; 81 if (IWorkbenchConstants.WORKBENCH_PROPERTIES_PAGE_INFO.equals(c1 82 .contributor.getPageId())) { 83 if (IWorkbenchConstants.WORKBENCH_PROPERTIES_PAGE_INFO 85 .equals(c2.contributor.getPageId())) { 86 return 0; 88 } 89 return -1; 91 } 92 if (IWorkbenchConstants.WORKBENCH_PROPERTIES_PAGE_INFO.equals(c2 93 .contributor.getPageId())) { 94 return 1; 96 } 97 String s1 = c1.getQualifiedName(); 99 String s2 = c2.getQualifiedName(); 100 return collator.compare(s1, s2); 101 } 102 }; 103 104 107 public PropertyPageContributorManager() { 108 super(); 109 loadContributors(); 112 } 113 114 123 public boolean contribute(PropertyPageManager manager, Object object) { 124 125 List result = getContributors(object); 126 127 if (result == null || result.size() == 0) { 128 return false; 129 } 130 131 List sortedResult = buildNodeList(result); 133 Collections.sort(sortedResult, comparer); 134 135 Iterator resultIterator = sortedResult.iterator(); 136 137 boolean actualContributions = false; 139 while(resultIterator.hasNext()) { 140 for (int i = 0; i < sortedResult.size(); i++) { 141 CategorizedPageNode next = (CategorizedPageNode) resultIterator.next(); 142 IPropertyPageContributor ppcont = next.contributor; 143 if (!ppcont.isApplicableTo(object)) { 144 continue; 145 } 146 if (ppcont.contributePropertyPages(manager, object)) { 147 actualContributions = true; 148 } 149 } 150 } 151 return actualContributions; 152 } 153 154 159 private List buildNodeList(List nodes) { 160 Hashtable mapping = new Hashtable (); 161 162 Iterator nodesIterator = nodes.iterator(); 163 while(nodesIterator.hasNext()){ 164 RegistryPageContributor page = (RegistryPageContributor) nodesIterator.next(); 165 mapping.put(page.getPageId(),new CategorizedPageNode(page)); 166 } 167 168 Iterator values = mapping.values().iterator(); 169 List returnValue = new ArrayList (); 170 while(values.hasNext()){ 171 CategorizedPageNode next = (CategorizedPageNode) values.next(); 172 returnValue.add(next); 173 if(next.contributor.getCategory() == null) { 174 continue; 175 } 176 Object parent = mapping.get(next.contributor.getCategory()); 177 if(parent != null) { 178 next.setParent((CategorizedPageNode) parent); 179 } 180 } 181 return returnValue; 182 } 183 184 189 public static PropertyPageContributorManager getManager() { 190 if (sharedInstance == null) { 191 sharedInstance = new PropertyPageContributorManager(); 192 } 193 return sharedInstance; 194 } 195 196 199 private void loadContributors() { 200 PropertyPagesRegistryReader reader = new PropertyPagesRegistryReader( 201 this); 202 reader.registerPropertyPages(Platform.getExtensionRegistry()); 203 } 204 205 208 public void addExtension(IExtensionTracker tracker, IExtension extension) { 209 IConfigurationElement[] addedElements = extension.getConfigurationElements(); 210 for (int i = 0; i < addedElements.length; i++) { 211 PropertyPagesRegistryReader reader = new PropertyPagesRegistryReader(this); 212 reader.readElement(addedElements[i]); 213 } 214 } 215 216 223 public Collection getApplicableContributors(Object element) { 224 Collection contributors = getContributors(element); 225 Collection result = new ArrayList (); 226 for (Iterator iter = contributors.iterator(); iter.hasNext();) { 227 RegistryPageContributor contributor = (RegistryPageContributor) iter.next(); 228 if(contributor.isApplicableTo(element)) 229 result.add(contributor); 230 231 } 232 return result; 233 } 234 } 235 | Popular Tags |