001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.registry;
011:
012: import java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.IConfigurationElement;
017: import org.eclipse.core.runtime.IExtensionRegistry;
018: import org.eclipse.jface.preference.IPreferenceNode;
019: import org.eclipse.ui.IWorkbench;
020: import org.eclipse.ui.PlatformUI;
021: import org.eclipse.ui.internal.Workbench;
022: import org.eclipse.ui.internal.dialogs.WorkbenchPreferenceNode;
023:
024: /**
025: * Instances access the registry that is provided at creation time in order
026: * to determine the contributed preference pages
027: */
028: public class PreferencePageRegistryReader extends
029: CategorizedPageRegistryReader {
030:
031: private static final String TAG_PAGE = "page"; //$NON-NLS-1$
032:
033: private List nodes;
034:
035: private IWorkbench workbench;
036:
037: class PreferencesCategoryNode extends CategoryNode {
038:
039: WorkbenchPreferenceNode node;
040:
041: /**
042: * Create a new instance of the receiver.
043: * @param reader
044: * @param nodeToCategorize
045: */
046: public PreferencesCategoryNode(
047: CategorizedPageRegistryReader reader,
048: WorkbenchPreferenceNode nodeToCategorize) {
049: super (reader);
050: this .node = nodeToCategorize;
051: }
052:
053: /* (non-Javadoc)
054: * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader.CategoryNode#getLabelText()
055: */
056: String getLabelText() {
057: return node.getLabelText();
058: }
059:
060: /* (non-Javadoc)
061: * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader.CategoryNode#getLabelText(java.lang.Object)
062: */
063: String getLabelText(Object element) {
064: return ((WorkbenchPreferenceNode) element).getLabelText();
065: }
066:
067: /* (non-Javadoc)
068: * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader.CategoryNode#getNode()
069: */
070: Object getNode() {
071: return node;
072: }
073: }
074:
075: /**
076: * Create a new instance configured with the workbench
077: *
078: * @param newWorkbench the workbench
079: */
080: public PreferencePageRegistryReader(IWorkbench newWorkbench) {
081: workbench = newWorkbench;
082: }
083:
084: /* (non-Javadoc)
085: * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader#findNode(java.lang.String)
086: */
087: Object findNode(String id) {
088: for (int i = 0; i < nodes.size(); i++) {
089: WorkbenchPreferenceNode node = (WorkbenchPreferenceNode) nodes
090: .get(i);
091: if (node.getId().equals(id)) {
092: return node;
093: }
094: }
095: return null;
096: }
097:
098: /* (non-Javadoc)
099: * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader#findNode(java.lang.Object, java.lang.String)
100: */
101: Object findNode(Object parent, String currentToken) {
102: IPreferenceNode[] subNodes = ((WorkbenchPreferenceNode) parent)
103: .getSubNodes();
104: for (int i = 0; i < subNodes.length; i++) {
105: WorkbenchPreferenceNode node = (WorkbenchPreferenceNode) subNodes[i];
106: if (node.getId().equals(currentToken)) {
107: return node;
108: }
109: }
110: return null;
111: }
112:
113: /* (non-Javadoc)
114: * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader#add(java.lang.Object, java.lang.Object)
115: */
116: void add(Object parent, Object node) {
117: ((IPreferenceNode) parent).add((IPreferenceNode) node);
118: }
119:
120: /* (non-Javadoc)
121: * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader#createCategoryNode(org.eclipse.ui.internal.registry.CategorizedPageRegistryReader, java.lang.Object)
122: */
123: CategoryNode createCategoryNode(
124: CategorizedPageRegistryReader reader, Object object) {
125: return new PreferencesCategoryNode(reader,
126: (WorkbenchPreferenceNode) object);
127: }
128:
129: /* (non-Javadoc)
130: * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader#getCategory(java.lang.Object)
131: */
132: String getCategory(Object node) {
133: return ((WorkbenchPreferenceNode) node).getCategory();
134: }
135:
136: /* (non-Javadoc)
137: * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader#getNodes()
138: */
139: Collection getNodes() {
140: return nodes;
141: }
142:
143: /**
144: * Load the preference page contirbutions from the registry and
145: * organize preference node contributions by category into hierarchies
146: * If there is no page for a given node in the hierarchy then a blank
147: * page will be created.
148: * If no category has been specified or category information
149: * is incorrect, page will appear at the root level. workbench
150: * log entry will be created for incorrect category information.
151: *
152: * @param registry the extension registry
153: */
154: public void loadFromRegistry(IExtensionRegistry registry) {
155: nodes = new ArrayList();
156:
157: readRegistry(registry, PlatformUI.PLUGIN_ID,
158: IWorkbenchRegistryConstants.PL_PREFERENCES);
159:
160: processNodes();
161:
162: }
163:
164: /**
165: * Read preference page element.
166: */
167: protected boolean readElement(IConfigurationElement element) {
168: if (element.getName().equals(TAG_PAGE) == false) {
169: return false;
170: }
171: WorkbenchPreferenceNode node = createNode(element);
172: if (node != null) {
173: nodes.add(node);
174: }
175: return true;
176: }
177:
178: /**
179: * Create a workbench preference node.
180: * @param element
181: * @return WorkbenchPreferenceNode
182: */
183: public static WorkbenchPreferenceNode createNode(
184: IConfigurationElement element) {
185: boolean nameMissing = element
186: .getAttribute(IWorkbenchRegistryConstants.ATT_NAME) == null;
187: String id = element
188: .getAttribute(IWorkbenchRegistryConstants.ATT_ID);
189: boolean classMissing = getClassValue(element,
190: IWorkbenchRegistryConstants.ATT_CLASS) == null;
191:
192: if (nameMissing) {
193: logMissingAttribute(element,
194: IWorkbenchRegistryConstants.ATT_NAME);
195: }
196: if (id == null) {
197: logMissingAttribute(element,
198: IWorkbenchRegistryConstants.ATT_ID);
199: }
200: if (classMissing) {
201: logMissingAttribute(element,
202: IWorkbenchRegistryConstants.ATT_CLASS);
203: }
204:
205: if (nameMissing || id == null || classMissing) {
206: return null;
207: }
208:
209: WorkbenchPreferenceNode node = new WorkbenchPreferenceNode(id,
210: element);
211: return node;
212: }
213:
214: /**
215: * Return the top level IPreferenceNodes.
216: * @return Collection of IPreferenceNode.
217: */
218: public Collection getTopLevelNodes() {
219: return topLevelNodes;
220: }
221:
222: /* (non-Javadoc)
223: * @see org.eclipse.ui.internal.registry.CategorizedPageRegistryReader#getFavoriteNodeId()
224: */
225: String getFavoriteNodeId() {
226: return ((Workbench) workbench).getMainPreferencePageId();
227: }
228: }
|