KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > quickaccess > PreferenceProvider


1 /*******************************************************************************
2  * Copyright (c) 2006, 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
12 package org.eclipse.ui.internal.quickaccess;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.jface.preference.IPreferenceNode;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
23 import org.eclipse.ui.internal.WorkbenchImages;
24
25 /**
26  * @since 3.3
27  *
28  */

29 public class PreferenceProvider extends QuickAccessProvider {
30
31     private QuickAccessElement[] cachedElements;
32     private Map JavaDoc idToElement = new HashMap JavaDoc();
33
34     public String JavaDoc getId() {
35         return "org.eclipse.ui.preferences"; //$NON-NLS-1$
36
}
37
38     public QuickAccessElement getElementForId(String JavaDoc id) {
39         getElements();
40         return (PreferenceElement) idToElement.get(id);
41     }
42
43     public QuickAccessElement[] getElements() {
44         if (cachedElements == null) {
45             List JavaDoc list = new ArrayList JavaDoc();
46             collectElements("", PlatformUI.getWorkbench().getPreferenceManager().getRootSubNodes(), list); //$NON-NLS-1$
47
cachedElements = new PreferenceElement[list.size()];
48             for (int i = 0; i < list.size(); i++) {
49                 PreferenceElement preferenceElement = (PreferenceElement) list.get(i);
50                 cachedElements[i] = preferenceElement;
51                 idToElement.put(preferenceElement.getId(), preferenceElement);
52             }
53         }
54         return cachedElements;
55     }
56
57     /**
58      * @param subNodes
59      * @return
60      */

61     private void collectElements(String JavaDoc prefix, IPreferenceNode[] subNodes, List JavaDoc result) {
62         for (int i = 0; i < subNodes.length; i++) {
63             PreferenceElement preferenceElement = new PreferenceElement(
64                     subNodes[i], prefix, this);
65             result.add(preferenceElement);
66             String JavaDoc nestedPrefix = prefix.length() == 0 ? subNodes[i].getLabelText() : (prefix + "/" + subNodes[i].getLabelText()); //$NON-NLS-1$
67
collectElements(nestedPrefix, subNodes[i].getSubNodes(), result);
68         }
69     }
70
71     public ImageDescriptor getImageDescriptor() {
72         return WorkbenchImages
73                 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJ_NODE);
74     }
75
76     public String JavaDoc getName() {
77         return QuickAccessMessages.QuickAccess_Preferences;
78     }
79 }
80
Popular Tags