KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > WorkbenchPreferenceManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.dialogs;
12
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
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 /**
33  * The WorkbenchPreferenceManager is the manager that can handle categories and
34  * preference nodes.
35  */

36 public class WorkbenchPreferenceManager extends PreferenceManager implements
37         IExtensionChangeHandler {
38
39     /**
40      * Create a new instance of the receiver with the specified seperatorChar
41      *
42      * @param separatorChar
43      */

44     public WorkbenchPreferenceManager(char separatorChar) {
45         super(separatorChar);
46         
47         IExtensionTracker tracker = PlatformUI.getWorkbench().getExtensionTracker();
48         tracker.registerHandler(this, ExtensionTracker.createExtensionPointFilter(getExtensionPointFilter()));
49
50         // add a listener for keyword deltas. If any occur clear all page caches
51
Platform.getExtensionRegistry().addRegistryChangeListener(
52                 new IRegistryChangeListener() {
53
54                     /*
55                      * (non-Javadoc)
56                      *
57                      * @see org.eclipse.core.runtime.IRegistryChangeListener#registryChanged(org.eclipse.core.runtime.IRegistryChangeEvent)
58                      */

59                     public void registryChanged(IRegistryChangeEvent event) {
60                         if (event.getExtensionDeltas(PlatformUI.PLUGIN_ID,
61                                 IWorkbenchRegistryConstants.PL_KEYWORDS).length > 0) {
62                             for (Iterator JavaDoc j = getElements(
63                                     PreferenceManager.POST_ORDER).iterator(); j
64                                     .hasNext();) {
65                                 ((WorkbenchPreferenceNode) j.next())
66                                         .clearKeywords();
67                             }
68                         }
69                     }
70                 });
71     }
72
73     /**
74      * Add the pages and the groups to the receiver.
75      *
76      * @param pageContributions
77      */

78     public void addPages(Collection JavaDoc pageContributions) {
79
80         // Add the contributions to the manager
81
Iterator JavaDoc iterator = pageContributions.iterator();
82         while (iterator.hasNext()) {
83             Object JavaDoc 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     /**
94      * Register a node with the extension tracker.
95      *
96      * @param node
97      * register the given node and its subnodes with the extension
98      * tracker
99      */

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     /* (non-Javadoc)
112      * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
113      */

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 JavaDoc category = node.getCategory();
124             if (category == null) {
125                 addToRoot(node);
126             } else {
127                 WorkbenchPreferenceNode parent = null;
128                 for (Iterator JavaDoc 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                     // Could not find the parent - log
139
WorkbenchPlugin
140                             .log("Invalid preference page path: " + category); //$NON-NLS-1$
141
addToRoot(node);
142                 } else {
143                     parent.add(node);
144                 }
145             }
146         }
147     }
148
149     /*
150      * (non-Javadoc)
151      *
152      * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionAdditionHandler#getExtensionPointFilter()
153      */

154     private IExtensionPoint getExtensionPointFilter() {
155         return Platform.getExtensionRegistry().getExtensionPoint(
156                 PlatformUI.PLUGIN_ID, IWorkbenchRegistryConstants.PL_PREFERENCES);
157     }
158
159     /* (non-Javadoc)
160      * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
161      */

162     public void removeExtension(IExtension extension, Object JavaDoc[] 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     /**
173      * Removes the node from the manager, searching through all subnodes.
174      *
175      * @param parent
176      * the node to search
177      * @param nodeToRemove
178      * the node to remove
179      * @return whether the node was removed
180      */

181     private boolean deepRemove(IPreferenceNode parent,
182             IPreferenceNode nodeToRemove) {
183         if (parent == nodeToRemove) {
184             if (parent == getRoot()) {
185                 removeAll(); // we're removing the root
186
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