001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 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.Map;
013: import java.util.TreeMap;
014:
015: import org.eclipse.core.commands.IParameterValues;
016: import org.eclipse.core.runtime.IRegistryChangeEvent;
017: import org.eclipse.core.runtime.IRegistryChangeListener;
018: import org.eclipse.core.runtime.Platform;
019: import org.eclipse.jface.preference.IPreferenceNode;
020: import org.eclipse.jface.preference.PreferenceManager;
021: import org.eclipse.ui.PlatformUI;
022: import org.eclipse.ui.internal.WorkbenchMessages;
023:
024: /**
025: * <p>
026: * Provides the parameter values for the show preferences command.
027: * </p>
028: * <p>
029: * To disambiguate preference pages with the same local label, names are
030: * constructed incorporating the full path of preference page labels. For
031: * instance names like <code>General > Appearance</code> and
032: * <code>Java > Appearance</code> avoid the problem of trying to put two
033: * <code>Appearance</code> keys into the parameter values map.
034: * </p>
035: * <p>
036: * This is only intended for use within the
037: * <code>org.eclipse.ui.workbench</code> plug-in.
038: * </p>
039: *
040: * @since 3.2
041: */
042: public final class PreferencePageParameterValues implements
043: IParameterValues {
044:
045: public PreferencePageParameterValues() {
046: Platform.getExtensionRegistry().addRegistryChangeListener(
047: new IRegistryChangeListener() {
048:
049: /*
050: * (non-Javadoc)
051: *
052: * @see org.eclipse.core.runtime.IRegistryChangeListener#registryChanged(org.eclipse.core.runtime.IRegistryChangeEvent)
053: */
054: public void registryChanged(
055: IRegistryChangeEvent event) {
056: if (event
057: .getExtensionDeltas(
058: PlatformUI.PLUGIN_ID,
059: IWorkbenchRegistryConstants.PL_PREFERENCES).length > 0) {
060: preferenceMap = null;
061: }
062: }
063: });
064: }
065:
066: private Map preferenceMap;
067:
068: /**
069: * Iterate through the preference page and build the map of preference page
070: * names to ids.
071: *
072: * @param values
073: * The Map being populated with parameter values.
074: * @param preferenceNodes
075: * An array of <code>IPreferenceNode</code> to process.
076: * @param namePrefix
077: * A string incorporating the names of each parent
078: * <code>IPreferenceNode</code> up to the root of the
079: * preference page tree. This will be <code>null</code> for the
080: * root level preference page nodes.
081: */
082: private final void collectParameterValues(final Map values,
083: final IPreferenceNode[] preferenceNodes,
084: final String namePrefix) {
085:
086: for (int i = 0; i < preferenceNodes.length; i++) {
087: final IPreferenceNode preferenceNode = preferenceNodes[i];
088:
089: final String name;
090: if (namePrefix == null) {
091: name = preferenceNode.getLabelText();
092: } else {
093: name = namePrefix
094: + WorkbenchMessages.PreferencePageParameterValues_pageLabelSeparator
095: + preferenceNode.getLabelText();
096: }
097: final String value = preferenceNode.getId();
098: values.put(name, value);
099:
100: collectParameterValues(values,
101: preferenceNode.getSubNodes(), name);
102: }
103: }
104:
105: public final Map getParameterValues() {
106: if (preferenceMap == null) {
107: preferenceMap = new TreeMap();
108:
109: final PreferenceManager preferenceManager = PlatformUI
110: .getWorkbench().getPreferenceManager();
111: collectParameterValues(preferenceMap, preferenceManager
112: .getRootSubNodes(), null);
113: }
114:
115: return preferenceMap;
116: }
117:
118: }
|