001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.dialogs;
011:
012: import java.util.Collection;
013: import java.util.Iterator;
014:
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.core.runtime.IExtension;
017: import org.eclipse.core.runtime.IExtensionPoint;
018: import org.eclipse.core.runtime.IRegistryChangeEvent;
019: import org.eclipse.core.runtime.IRegistryChangeListener;
020: import org.eclipse.core.runtime.Platform;
021: import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
022: import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
023: import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
024: import org.eclipse.jface.preference.IPreferenceNode;
025: import org.eclipse.jface.preference.PreferenceManager;
026: import org.eclipse.ui.PlatformUI;
027: import org.eclipse.ui.internal.WorkbenchPlugin;
028: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
029: import org.eclipse.ui.internal.registry.PreferencePageRegistryReader;
030:
031: /**
032: * The WorkbenchPreferenceManager is the manager that can handle categories and
033: * preference nodes.
034: */
035: public class WorkbenchPreferenceManager extends PreferenceManager
036: implements IExtensionChangeHandler {
037:
038: /**
039: * Create a new instance of the receiver with the specified seperatorChar
040: *
041: * @param separatorChar
042: */
043: public WorkbenchPreferenceManager(char separatorChar) {
044: super (separatorChar);
045:
046: IExtensionTracker tracker = PlatformUI.getWorkbench()
047: .getExtensionTracker();
048: tracker.registerHandler(this , ExtensionTracker
049: .createExtensionPointFilter(getExtensionPointFilter()));
050:
051: // add a listener for keyword deltas. If any occur clear all page caches
052: Platform.getExtensionRegistry().addRegistryChangeListener(
053: new IRegistryChangeListener() {
054:
055: /*
056: * (non-Javadoc)
057: *
058: * @see org.eclipse.core.runtime.IRegistryChangeListener#registryChanged(org.eclipse.core.runtime.IRegistryChangeEvent)
059: */
060: public void registryChanged(
061: IRegistryChangeEvent event) {
062: if (event
063: .getExtensionDeltas(
064: PlatformUI.PLUGIN_ID,
065: IWorkbenchRegistryConstants.PL_KEYWORDS).length > 0) {
066: for (Iterator j = getElements(
067: PreferenceManager.POST_ORDER)
068: .iterator(); j.hasNext();) {
069: ((WorkbenchPreferenceNode) j.next())
070: .clearKeywords();
071: }
072: }
073: }
074: });
075: }
076:
077: /**
078: * Add the pages and the groups to the receiver.
079: *
080: * @param pageContributions
081: */
082: public void addPages(Collection pageContributions) {
083:
084: // Add the contributions to the manager
085: Iterator iterator = pageContributions.iterator();
086: while (iterator.hasNext()) {
087: Object next = iterator.next();
088: if (next instanceof WorkbenchPreferenceNode) {
089: WorkbenchPreferenceNode wNode = (WorkbenchPreferenceNode) next;
090: addToRoot(wNode);
091: registerNode(wNode);
092: }
093: }
094:
095: }
096:
097: /**
098: * Register a node with the extension tracker.
099: *
100: * @param node
101: * register the given node and its subnodes with the extension
102: * tracker
103: */
104: private void registerNode(WorkbenchPreferenceNode node) {
105: PlatformUI.getWorkbench().getExtensionTracker().registerObject(
106: node.getConfigurationElement().getDeclaringExtension(),
107: node, IExtensionTracker.REF_WEAK);
108: IPreferenceNode[] subNodes = node.getSubNodes();
109: for (int i = 0; i < subNodes.length; i++) {
110: registerNode((WorkbenchPreferenceNode) subNodes[i]);
111: }
112:
113: }
114:
115: /* (non-Javadoc)
116: * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
117: */
118: public void addExtension(IExtensionTracker tracker,
119: IExtension extension) {
120: IConfigurationElement[] elements = extension
121: .getConfigurationElements();
122: for (int i = 0; i < elements.length; i++) {
123: WorkbenchPreferenceNode node = PreferencePageRegistryReader
124: .createNode(elements[i]);
125: if (node == null) {
126: continue;
127: }
128: registerNode(node);
129: String category = node.getCategory();
130: if (category == null) {
131: addToRoot(node);
132: } else {
133: WorkbenchPreferenceNode parent = null;
134: for (Iterator j = getElements(
135: PreferenceManager.POST_ORDER).iterator(); j
136: .hasNext();) {
137: WorkbenchPreferenceNode element = (WorkbenchPreferenceNode) j
138: .next();
139: if (category.equals(element.getId())) {
140: parent = element;
141: break;
142: }
143: }
144: if (parent == null) {
145: // Could not find the parent - log
146: WorkbenchPlugin
147: .log("Invalid preference page path: " + category); //$NON-NLS-1$
148: addToRoot(node);
149: } else {
150: parent.add(node);
151: }
152: }
153: }
154: }
155:
156: /*
157: * (non-Javadoc)
158: *
159: * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionAdditionHandler#getExtensionPointFilter()
160: */
161: private IExtensionPoint getExtensionPointFilter() {
162: return Platform.getExtensionRegistry().getExtensionPoint(
163: PlatformUI.PLUGIN_ID,
164: IWorkbenchRegistryConstants.PL_PREFERENCES);
165: }
166:
167: /* (non-Javadoc)
168: * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
169: */
170: public void removeExtension(IExtension extension, Object[] objects) {
171: for (int i = 0; i < objects.length; i++) {
172: if (objects[i] instanceof WorkbenchPreferenceNode) {
173: WorkbenchPreferenceNode wNode = (WorkbenchPreferenceNode) objects[i];
174: wNode.disposeResources();
175: deepRemove(getRoot(), wNode);
176: }
177: }
178: }
179:
180: /**
181: * Removes the node from the manager, searching through all subnodes.
182: *
183: * @param parent
184: * the node to search
185: * @param nodeToRemove
186: * the node to remove
187: * @return whether the node was removed
188: */
189: private boolean deepRemove(IPreferenceNode parent,
190: IPreferenceNode nodeToRemove) {
191: if (parent == nodeToRemove) {
192: if (parent == getRoot()) {
193: removeAll(); // we're removing the root
194: return true;
195: }
196: }
197:
198: if (parent.remove(nodeToRemove)) {
199: return true;
200: }
201:
202: IPreferenceNode[] subNodes = parent.getSubNodes();
203: for (int i = 0; i < subNodes.length; i++) {
204: if (deepRemove(subNodes[i], nodeToRemove)) {
205: return true;
206: }
207: }
208: return false;
209: }
210: }
|