1 11 package org.eclipse.ui.internal.dialogs; 12 13 import java.util.Collection ; 14 import java.util.Iterator ; 15 16 import org.eclipse.core.runtime.IConfigurationElement; 17 import org.eclipse.core.runtime.IExtension; 18 import org.eclipse.core.runtime.IExtensionPoint; 19 import org.eclipse.core.runtime.IRegistryChangeEvent; 20 import org.eclipse.core.runtime.IRegistryChangeListener; 21 import org.eclipse.core.runtime.Platform; 22 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker; 23 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler; 24 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; 25 import org.eclipse.jface.preference.IPreferenceNode; 26 import org.eclipse.jface.preference.PreferenceManager; 27 import org.eclipse.ui.PlatformUI; 28 import org.eclipse.ui.internal.WorkbenchPlugin; 29 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 30 import org.eclipse.ui.internal.registry.PreferencePageRegistryReader; 31 32 36 public class WorkbenchPreferenceManager extends PreferenceManager implements 37 IExtensionChangeHandler { 38 39 44 public WorkbenchPreferenceManager(char separatorChar) { 45 super(separatorChar); 46 47 IExtensionTracker tracker = PlatformUI.getWorkbench().getExtensionTracker(); 48 tracker.registerHandler(this, ExtensionTracker.createExtensionPointFilter(getExtensionPointFilter())); 49 50 Platform.getExtensionRegistry().addRegistryChangeListener( 52 new IRegistryChangeListener() { 53 54 59 public void registryChanged(IRegistryChangeEvent event) { 60 if (event.getExtensionDeltas(PlatformUI.PLUGIN_ID, 61 IWorkbenchRegistryConstants.PL_KEYWORDS).length > 0) { 62 for (Iterator j = getElements( 63 PreferenceManager.POST_ORDER).iterator(); j 64 .hasNext();) { 65 ((WorkbenchPreferenceNode) j.next()) 66 .clearKeywords(); 67 } 68 } 69 } 70 }); 71 } 72 73 78 public void addPages(Collection pageContributions) { 79 80 Iterator iterator = pageContributions.iterator(); 82 while (iterator.hasNext()) { 83 Object next = iterator.next(); 84 if (next instanceof WorkbenchPreferenceNode) { 85 WorkbenchPreferenceNode wNode = (WorkbenchPreferenceNode) next; 86 addToRoot(wNode); 87 registerNode(wNode); 88 } 89 } 90 91 } 92 93 100 private void registerNode(WorkbenchPreferenceNode node) { 101 PlatformUI.getWorkbench().getExtensionTracker().registerObject( 102 node.getConfigurationElement().getDeclaringExtension(), node, 103 IExtensionTracker.REF_WEAK); 104 IPreferenceNode[] subNodes = node.getSubNodes(); 105 for (int i = 0; i < subNodes.length; i++) { 106 registerNode((WorkbenchPreferenceNode) subNodes[i]); 107 } 108 109 } 110 111 114 public void addExtension(IExtensionTracker tracker, IExtension extension) { 115 IConfigurationElement[] elements = extension.getConfigurationElements(); 116 for (int i = 0; i < elements.length; i++) { 117 WorkbenchPreferenceNode node = PreferencePageRegistryReader 118 .createNode(elements[i]); 119 if (node == null) { 120 continue; 121 } 122 registerNode(node); 123 String category = node.getCategory(); 124 if (category == null) { 125 addToRoot(node); 126 } else { 127 WorkbenchPreferenceNode parent = null; 128 for (Iterator j = getElements(PreferenceManager.POST_ORDER) 129 .iterator(); j.hasNext();) { 130 WorkbenchPreferenceNode element = (WorkbenchPreferenceNode) j 131 .next(); 132 if (category.equals(element.getId())) { 133 parent = element; 134 break; 135 } 136 } 137 if (parent == null) { 138 WorkbenchPlugin 140 .log("Invalid preference page path: " + category); addToRoot(node); 142 } else { 143 parent.add(node); 144 } 145 } 146 } 147 } 148 149 154 private IExtensionPoint getExtensionPointFilter() { 155 return Platform.getExtensionRegistry().getExtensionPoint( 156 PlatformUI.PLUGIN_ID, IWorkbenchRegistryConstants.PL_PREFERENCES); 157 } 158 159 162 public void removeExtension(IExtension extension, Object [] objects) { 163 for (int i = 0; i < objects.length; i++) { 164 if (objects[i] instanceof WorkbenchPreferenceNode) { 165 WorkbenchPreferenceNode wNode = (WorkbenchPreferenceNode) objects[i]; 166 wNode.disposeResources(); 167 deepRemove(getRoot(), wNode); 168 } 169 } 170 } 171 172 181 private boolean deepRemove(IPreferenceNode parent, 182 IPreferenceNode nodeToRemove) { 183 if (parent == nodeToRemove) { 184 if (parent == getRoot()) { 185 removeAll(); return true; 187 } 188 } 189 190 if (parent.remove(nodeToRemove)) { 191 return true; 192 } 193 194 IPreferenceNode[] subNodes = parent.getSubNodes(); 195 for (int i = 0; i < subNodes.length; i++) { 196 if (deepRemove(subNodes[i], nodeToRemove)) { 197 return true; 198 } 199 } 200 return false; 201 } 202 } 203 | Popular Tags |