01: /*******************************************************************************
02: * Copyright (c) 2006, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.quickaccess;
11:
12: import java.util.ArrayList;
13: import java.util.HashMap;
14: import java.util.List;
15: import java.util.Map;
16:
17: import org.eclipse.jface.preference.IPreferenceNode;
18: import org.eclipse.jface.resource.ImageDescriptor;
19: import org.eclipse.ui.PlatformUI;
20: import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
21: import org.eclipse.ui.internal.WorkbenchImages;
22:
23: /**
24: * @since 3.3
25: *
26: */
27: public class PreferenceProvider extends QuickAccessProvider {
28:
29: private QuickAccessElement[] cachedElements;
30: private Map idToElement = new HashMap();
31:
32: public String getId() {
33: return "org.eclipse.ui.preferences"; //$NON-NLS-1$
34: }
35:
36: public QuickAccessElement getElementForId(String id) {
37: getElements();
38: return (PreferenceElement) idToElement.get(id);
39: }
40:
41: public QuickAccessElement[] getElements() {
42: if (cachedElements == null) {
43: List list = new ArrayList();
44: collectElements(
45: "", PlatformUI.getWorkbench().getPreferenceManager().getRootSubNodes(), list); //$NON-NLS-1$
46: cachedElements = new PreferenceElement[list.size()];
47: for (int i = 0; i < list.size(); i++) {
48: PreferenceElement preferenceElement = (PreferenceElement) list
49: .get(i);
50: cachedElements[i] = preferenceElement;
51: idToElement.put(preferenceElement.getId(),
52: preferenceElement);
53: }
54: }
55: return cachedElements;
56: }
57:
58: /**
59: * @param subNodes
60: * @return
61: */
62: private void collectElements(String prefix,
63: IPreferenceNode[] subNodes, List result) {
64: for (int i = 0; i < subNodes.length; i++) {
65: PreferenceElement preferenceElement = new PreferenceElement(
66: subNodes[i], prefix, this );
67: result.add(preferenceElement);
68: String nestedPrefix = prefix.length() == 0 ? subNodes[i]
69: .getLabelText()
70: : (prefix + "/" + subNodes[i].getLabelText()); //$NON-NLS-1$
71: collectElements(nestedPrefix, subNodes[i].getSubNodes(),
72: result);
73: }
74: }
75:
76: public ImageDescriptor getImageDescriptor() {
77: return WorkbenchImages
78: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJ_NODE);
79: }
80:
81: public String getName() {
82: return QuickAccessMessages.QuickAccess_Preferences;
83: }
84: }
|