KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > registry > PreferencePageRegistryReader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.registry;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IConfigurationElement;
18 import org.eclipse.core.runtime.IExtensionRegistry;
19 import org.eclipse.jface.preference.IPreferenceNode;
20 import org.eclipse.ui.IWorkbench;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.internal.Workbench;
23 import org.eclipse.ui.internal.dialogs.WorkbenchPreferenceNode;
24
25 /**
26  * Instances access the registry that is provided at creation time in order
27  * to determine the contributed preference pages
28  */

29 public class PreferencePageRegistryReader extends CategorizedPageRegistryReader {
30
31     private static final String JavaDoc TAG_PAGE = "page"; //$NON-NLS-1$
32

33     private List JavaDoc nodes;
34
35     private IWorkbench workbench;
36     
37
38     class PreferencesCategoryNode extends CategoryNode {
39
40         WorkbenchPreferenceNode node;
41
42         /**
43          * Create a new instance of the receiver.
44          * @param reader
45          * @param nodeToCategorize
46          */

47         public PreferencesCategoryNode(CategorizedPageRegistryReader reader,
48                 WorkbenchPreferenceNode nodeToCategorize) {
49             super(reader);
50             this.node = nodeToCategorize;
51         }
52
53         /* (non-Javadoc)
54          * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader.CategoryNode#getLabelText()
55          */

56         String JavaDoc getLabelText() {
57             return node.getLabelText();
58         }
59
60         /* (non-Javadoc)
61          * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader.CategoryNode#getLabelText(java.lang.Object)
62          */

63         String JavaDoc getLabelText(Object JavaDoc element) {
64             return ((WorkbenchPreferenceNode) element).getLabelText();
65         }
66
67         /* (non-Javadoc)
68          * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader.CategoryNode#getNode()
69          */

70         Object JavaDoc getNode() {
71             return node;
72         }
73     }
74
75     /**
76      * Create a new instance configured with the workbench
77      *
78      * @param newWorkbench the workbench
79      */

80     public PreferencePageRegistryReader(IWorkbench newWorkbench) {
81         workbench = newWorkbench;
82     }
83
84     /* (non-Javadoc)
85      * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader#findNode(java.lang.String)
86      */

87     Object JavaDoc findNode(String JavaDoc id) {
88         for (int i = 0; i < nodes.size(); i++) {
89             WorkbenchPreferenceNode node = (WorkbenchPreferenceNode) nodes.get(i);
90             if (node.getId().equals(id)) {
91                 return node;
92             }
93         }
94         return null;
95     }
96
97     /* (non-Javadoc)
98      * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader#findNode(java.lang.Object, java.lang.String)
99      */

100     Object JavaDoc findNode(Object JavaDoc parent, String JavaDoc currentToken) {
101         IPreferenceNode[] subNodes = ((WorkbenchPreferenceNode) parent).getSubNodes();
102         for (int i = 0; i < subNodes.length; i++) {
103             WorkbenchPreferenceNode node = (WorkbenchPreferenceNode) subNodes[i];
104             if (node.getId().equals(currentToken)) {
105                 return node;
106             }
107         }
108         return null;
109     }
110
111     /* (non-Javadoc)
112      * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader#add(java.lang.Object, java.lang.Object)
113      */

114     void add(Object JavaDoc parent, Object JavaDoc node) {
115         ((IPreferenceNode) parent).add((IPreferenceNode) node);
116     }
117
118     /* (non-Javadoc)
119      * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader#createCategoryNode(org.eclipse.ui.internal.registry.CategorizedPageRegistryReader, java.lang.Object)
120      */

121     CategoryNode createCategoryNode(CategorizedPageRegistryReader reader, Object JavaDoc object) {
122         return new PreferencesCategoryNode(reader, (WorkbenchPreferenceNode) object);
123     }
124
125     /* (non-Javadoc)
126      * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader#getCategory(java.lang.Object)
127      */

128     String JavaDoc getCategory(Object JavaDoc node) {
129         return ((WorkbenchPreferenceNode) node).getCategory();
130     }
131
132     /* (non-Javadoc)
133      * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader#getNodes()
134      */

135     Collection JavaDoc getNodes() {
136         return nodes;
137     }
138
139     /**
140      * Load the preference page contirbutions from the registry and
141      * organize preference node contributions by category into hierarchies
142      * If there is no page for a given node in the hierarchy then a blank
143      * page will be created.
144      * If no category has been specified or category information
145      * is incorrect, page will appear at the root level. workbench
146      * log entry will be created for incorrect category information.
147      *
148      * @param registry the extension registry
149      */

150     public void loadFromRegistry(IExtensionRegistry registry) {
151         nodes = new ArrayList JavaDoc();
152
153         readRegistry(registry, PlatformUI.PLUGIN_ID, IWorkbenchRegistryConstants.PL_PREFERENCES);
154
155         processNodes();
156
157     }
158
159     /**
160      * Read preference page element.
161      */

162     protected boolean readElement(IConfigurationElement element) {
163         if (element.getName().equals(TAG_PAGE) == false) {
164             return false;
165         }
166         WorkbenchPreferenceNode node = createNode(element);
167         if (node != null) {
168             nodes.add(node);
169         }
170         return true;
171     }
172
173     /**
174      * Create a workbench preference node.
175      * @param element
176      * @return WorkbenchPreferenceNode
177      */

178     public static WorkbenchPreferenceNode createNode(IConfigurationElement element) {
179         boolean nameMissing = element.getAttribute(IWorkbenchRegistryConstants.ATT_NAME) == null;
180         String JavaDoc id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
181         boolean classMissing = getClassValue(element, IWorkbenchRegistryConstants.ATT_CLASS) == null;
182
183         if (nameMissing) {
184             logMissingAttribute(element, IWorkbenchRegistryConstants.ATT_NAME);
185         }
186         if (id == null) {
187             logMissingAttribute(element, IWorkbenchRegistryConstants.ATT_ID);
188         }
189         if (classMissing) {
190             logMissingAttribute(element, IWorkbenchRegistryConstants.ATT_CLASS);
191         }
192         
193         if (nameMissing || id == null || classMissing) {
194             return null;
195         }
196
197         WorkbenchPreferenceNode node = new WorkbenchPreferenceNode(id, element);
198         return node;
199     }
200
201     /**
202      * Return the top level IPreferenceNodes.
203      * @return Collection of IPreferenceNode.
204      */

205     public Collection JavaDoc getTopLevelNodes() {
206         return topLevelNodes;
207     }
208
209     /* (non-Javadoc)
210      * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader#getFavoriteNodeId()
211      */

212     String JavaDoc getFavoriteNodeId() {
213         return ((Workbench) workbench).getMainPreferencePageId();
214     }
215 }
216
Popular Tags