KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > menus > MenuPersistence


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.menus;
13
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.IExtensionDelta;
18 import org.eclipse.core.runtime.IExtensionRegistry;
19 import org.eclipse.core.runtime.IRegistryChangeEvent;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
23 import org.eclipse.ui.internal.services.RegistryPersistence;
24
25 /**
26  * <p>
27  * A static class for accessing the registry.
28  * </p>
29  * <p>
30  * This class is not intended for use outside of the
31  * <code>org.eclipse.ui.workbench</code> plug-in.
32  * </p>
33  *
34  * @since 3.2
35  */

36 final class MenuPersistence extends RegistryPersistence {
37
38     
39     private final WorkbenchMenuService menuService;
40     
41     /**
42      * Constructs a new instance of {@link MenuPersistence}.
43      * @param workbenchMenuService
44      *
45      * @param workbenchMenuService
46      * The menu service which should be populated with the values
47      * from the registry; must not be <code>null</code>.
48      */

49     MenuPersistence(final WorkbenchMenuService workbenchMenuService) {
50         if (workbenchMenuService == null) {
51             throw new NullPointerException JavaDoc("The menu service cannot be null"); //$NON-NLS-1$
52
}
53
54         this.menuService = workbenchMenuService;
55     }
56
57     public final void dispose() {
58         super.dispose();
59     }
60
61     protected final boolean isChangeImportant(final IRegistryChangeEvent event) {
62         /*
63          * TODO Menus will need to be re-read (i.e., re-verified) if any of the
64          * menu extensions change (i.e., menus), or if any of the command
65          * extensions change (i.e., action definitions).
66          */

67         final IExtensionDelta[] menuDeltas = event.getExtensionDeltas(
68                 PlatformUI.PLUGIN_ID, IWorkbenchRegistryConstants.PL_MENUS);
69         if (menuDeltas.length == 0) {
70             return false;
71         }
72
73         return true;
74     }
75
76     /**
77      * <p>
78      * Reads all of the menu elements and action sets from the registry.
79      * </p>
80      * <p>
81      * TODO Add support for modifications.
82      * </p>
83      */

84     protected final void read() {
85         super.read();
86         
87         // Read legacy 3.2 'trim' additions
88
readTrimAdditions();
89         
90         // read the 3.3 menu additions
91
readAdditions();
92     }
93     
94     //
95
// 3.3 menu extension code
96
//
97

98     public void readTrimAdditions() {
99         if (menuService == null)
100             return;
101         
102         final IExtensionRegistry registry = Platform.getExtensionRegistry();
103         final IConfigurationElement[] configElements = registry
104                 .getConfigurationElementsFor(EXTENSION_MENUS);
105
106         // Create a cache entry for every menu addition
107
for (int i = 0; i < configElements.length; i++) {
108             // Only process 'group' entries
109
if (!TAG_GROUP.equals(configElements[i].getName()))
110                 continue;
111             
112             String JavaDoc id = configElements[i].getAttribute(IWorkbenchRegistryConstants.ATT_ID);
113             
114             // Define the initial URI spec
115
String JavaDoc uriSpec = "toolbar:" + id; //$NON-NLS-1$
116
if (configElements[i].getChildren(TAG_LOCATION).length > 0) {
117                 IConfigurationElement location = configElements[i].getChildren(TAG_LOCATION)[0];
118                 if (location.getChildren(TAG_ORDER).length > 0) {
119                     IConfigurationElement order = location.getChildren(TAG_ORDER)[0];
120                     
121                     String JavaDoc pos = order.getAttribute(IWorkbenchRegistryConstants.ATT_POSITION);
122                     String JavaDoc relTo = order.getAttribute(IWorkbenchRegistryConstants.ATT_RELATIVE_TO);
123                     uriSpec += "?" + pos + "=" + relTo; //$NON-NLS-1$ //$NON-NLS-2$
124

125                     // HACK! We expect that the new trim group is -always- relative to
126
// one of the 'default' groups; indicating which trim area they're in
127
MenuLocationURI uri = new MenuLocationURI("toolbar:" + relTo); //$NON-NLS-1$
128
List JavaDoc trimAdditions = menuService.getAdditionsForURI(uri);
129                     
130                     //
131
// TODO convert the TrimAdditionCacheEntry over to use the
132
// new MenuCacheEntry and addCacheForURI(*)
133
// OK, add the addition to this area
134
uri = new MenuLocationURI(uriSpec);
135                     trimAdditions.add(new TrimAdditionCacheEntry(configElements[i], uri, menuService));
136                 }
137                 else {
138                     // Must be a default group; make a new entry cache
139
MenuLocationURI uri = new MenuLocationURI(uriSpec);
140                     
141                     // NOTE: 'getAdditionsForURI' forces creation
142
menuService.getAdditionsForURI(uri);
143                 }
144             }
145         }
146     }
147     
148     public void readAdditions() {
149         final IExtensionRegistry registry = Platform.getExtensionRegistry();
150         final IConfigurationElement[] menusExtensionPoint = registry
151                 .getConfigurationElementsFor(EXTENSION_MENUS);
152
153         // Create a cache entry for every menu addition
154
for (int i = 0; i < menusExtensionPoint.length; i++) {
155             if (PL_MENU_CONTRIBUTION.equals(menusExtensionPoint[i].getName())) {
156                 // Determine the insertion location by parsing the URI
157
String JavaDoc location = menusExtensionPoint[i]
158                         .getAttribute(TAG_LOCATION_URI);
159
160                 menuService.addContributionFactory(new MenuAdditionCacheEntry(menuService,
161                         menusExtensionPoint[i], location,
162                         menusExtensionPoint[i].getNamespaceIdentifier()));
163             }
164         }
165     }
166 }
167
Popular Tags