001: /*******************************************************************************
002: * Copyright (c) 2005, 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.menus;
011:
012: import java.util.List;
013:
014: import org.eclipse.core.runtime.IConfigurationElement;
015: import org.eclipse.core.runtime.IExtensionDelta;
016: import org.eclipse.core.runtime.IExtensionRegistry;
017: import org.eclipse.core.runtime.IRegistryChangeEvent;
018: import org.eclipse.core.runtime.Platform;
019: import org.eclipse.ui.PlatformUI;
020: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
021: import org.eclipse.ui.internal.services.RegistryPersistence;
022:
023: /**
024: * <p>
025: * A static class for accessing the registry.
026: * </p>
027: * <p>
028: * This class is not intended for use outside of the
029: * <code>org.eclipse.ui.workbench</code> plug-in.
030: * </p>
031: *
032: * @since 3.2
033: */
034: final class MenuPersistence extends RegistryPersistence {
035:
036: private final WorkbenchMenuService menuService;
037:
038: /**
039: * Constructs a new instance of {@link MenuPersistence}.
040: * @param workbenchMenuService
041: *
042: * @param workbenchMenuService
043: * The menu service which should be populated with the values
044: * from the registry; must not be <code>null</code>.
045: */
046: MenuPersistence(final WorkbenchMenuService workbenchMenuService) {
047: if (workbenchMenuService == null) {
048: throw new NullPointerException(
049: "The menu service cannot be null"); //$NON-NLS-1$
050: }
051:
052: this .menuService = workbenchMenuService;
053: }
054:
055: public final void dispose() {
056: super .dispose();
057: }
058:
059: protected final boolean isChangeImportant(
060: final IRegistryChangeEvent event) {
061: /*
062: * TODO Menus will need to be re-read (i.e., re-verified) if any of the
063: * menu extensions change (i.e., menus), or if any of the command
064: * extensions change (i.e., action definitions).
065: */
066: final IExtensionDelta[] menuDeltas = event.getExtensionDeltas(
067: PlatformUI.PLUGIN_ID,
068: IWorkbenchRegistryConstants.PL_MENUS);
069: if (menuDeltas.length == 0) {
070: return false;
071: }
072:
073: return true;
074: }
075:
076: /**
077: * <p>
078: * Reads all of the menu elements and action sets from the registry.
079: * </p>
080: * <p>
081: * TODO Add support for modifications.
082: * </p>
083: */
084: protected final void read() {
085: super .read();
086:
087: // Read legacy 3.2 'trim' additions
088: readTrimAdditions();
089:
090: // read the 3.3 menu additions
091: readAdditions();
092: }
093:
094: //
095: // 3.3 menu extension code
096: //
097:
098: public void readTrimAdditions() {
099: if (menuService == null)
100: return;
101:
102: final IExtensionRegistry registry = Platform
103: .getExtensionRegistry();
104: final IConfigurationElement[] configElements = registry
105: .getConfigurationElementsFor(EXTENSION_MENUS);
106:
107: // Create a cache entry for every menu addition
108: for (int i = 0; i < configElements.length; i++) {
109: // Only process 'group' entries
110: if (!TAG_GROUP.equals(configElements[i].getName()))
111: continue;
112:
113: String id = configElements[i]
114: .getAttribute(IWorkbenchRegistryConstants.ATT_ID);
115:
116: // Define the initial URI spec
117: String uriSpec = "toolbar:" + id; //$NON-NLS-1$
118: if (configElements[i].getChildren(TAG_LOCATION).length > 0) {
119: IConfigurationElement location = configElements[i]
120: .getChildren(TAG_LOCATION)[0];
121: if (location.getChildren(TAG_ORDER).length > 0) {
122: IConfigurationElement order = location
123: .getChildren(TAG_ORDER)[0];
124:
125: String pos = order
126: .getAttribute(IWorkbenchRegistryConstants.ATT_POSITION);
127: String relTo = order
128: .getAttribute(IWorkbenchRegistryConstants.ATT_RELATIVE_TO);
129: uriSpec += "?" + pos + "=" + relTo; //$NON-NLS-1$ //$NON-NLS-2$
130:
131: // HACK! We expect that the new trim group is -always- relative to
132: // one of the 'default' groups; indicating which trim area they're in
133: MenuLocationURI uri = new MenuLocationURI(
134: "toolbar:" + relTo); //$NON-NLS-1$
135: List trimAdditions = menuService
136: .getAdditionsForURI(uri);
137:
138: //
139: // TODO convert the TrimAdditionCacheEntry over to use the
140: // new MenuCacheEntry and addCacheForURI(*)
141: // OK, add the addition to this area
142: uri = new MenuLocationURI(uriSpec);
143: trimAdditions.add(new TrimAdditionCacheEntry(
144: configElements[i], uri, menuService));
145: } else {
146: // Must be a default group; make a new entry cache
147: MenuLocationURI uri = new MenuLocationURI(uriSpec);
148:
149: // NOTE: 'getAdditionsForURI' forces creation
150: menuService.getAdditionsForURI(uri);
151: }
152: }
153: }
154: }
155:
156: public void readAdditions() {
157: final IExtensionRegistry registry = Platform
158: .getExtensionRegistry();
159: final IConfigurationElement[] menusExtensionPoint = registry
160: .getConfigurationElementsFor(EXTENSION_MENUS);
161:
162: // Create a cache entry for every menu addition
163: for (int i = 0; i < menusExtensionPoint.length; i++) {
164: if (PL_MENU_CONTRIBUTION.equals(menusExtensionPoint[i]
165: .getName())) {
166: // Determine the insertion location by parsing the URI
167: String location = menusExtensionPoint[i]
168: .getAttribute(TAG_LOCATION_URI);
169:
170: menuService
171: .addContributionFactory(new MenuAdditionCacheEntry(
172: menuService, menusExtensionPoint[i],
173: location, menusExtensionPoint[i]
174: .getNamespaceIdentifier()));
175: }
176: }
177: }
178: }
|