KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IConfigurationElement;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.ui.internal.WorkbenchPlugin;
19
20 /**
21  * <p>
22  * A proxy for a dynamic menu that has been defined in XML. This delays the
23  * class loading until the dynamic menu is really asked to modify a menu
24  * collection. Asking a proxy for anything but the attributes defined publicly
25  * in this class will cause the proxy to instantiate the proxied handler.
26  * </p>
27  * <p>
28  * This class is not intended for use outside of the
29  * <code>org.eclipse.ui.workbench</code> plug-in.
30  * </p>
31  *
32  * @since 3.2
33  */

34 final class DynamicMenuProxy implements IDynamicMenu {
35
36     /**
37      * The configuration element from which the dynamic menu can be created.
38      * This value will exist until the element is converted into a real class --
39      * at which point this value will be set to <code>null</code>.
40      */

41     private IConfigurationElement configurationElement;
42
43     /**
44      * The real dynamic menu. This value is <code>null</code> until the proxy
45      * is forced to load the real dynamic menu. At this point, the configuration
46      * element is converted, nulled out, and this dynamic menu gains a
47      * reference.
48      */

49     private IDynamicMenu dynamicMenu = null;
50
51     /**
52      * The name of the configuration element attribute which contains the
53      * information necessary to instantiate the real dynamic menu.
54      */

55     private final String JavaDoc dynamicMenuAttributeName;
56
57     /**
58      * Constructs a new instance of <code>DynamicMenuProxy</code> with all the
59      * information it needs to try to load the class at a later point in time.
60      *
61      * @param configurationElement
62      * The configuration element from which the real class can be
63      * loaded at run-time; must not be <code>null</code>.
64      * @param dynamicMenuAttributeName
65      * The name of the attibute or element containing the dynamic
66      * menu executable extension; must not be <code>null</code>.
67      */

68     public DynamicMenuProxy(final IConfigurationElement configurationElement,
69             final String JavaDoc dynamicMenuAttributeName) {
70         if (configurationElement == null) {
71             throw new NullPointerException JavaDoc(
72                     "The configuration element backing a dynamic menu proxy cannot be null"); //$NON-NLS-1$
73
}
74
75         if (dynamicMenuAttributeName == null) {
76             throw new NullPointerException JavaDoc(
77                     "The attribute containing the dynamic menu class must be known"); //$NON-NLS-1$
78
}
79
80         this.configurationElement = configurationElement;
81         this.dynamicMenuAttributeName = dynamicMenuAttributeName;
82     }
83
84     public final void aboutToShow(final IMenuCollection menu) {
85         if (loadDynamicMenu()) {
86             dynamicMenu.aboutToShow(menu);
87         }
88     }
89
90     /**
91      * Loads the dynamic menu, if possible. If the dynamic menu is loaded, then
92      * the member variables are updated accordingly.
93      *
94      * @return <code>true</code> if the dynamic menu is now non-null;
95      * <code>false</code> otherwise.
96      */

97     private final boolean loadDynamicMenu() {
98         if (dynamicMenu == null) {
99             // Load the handler.
100
try {
101                 dynamicMenu = (IDynamicMenu) configurationElement
102                         .createExecutableExtension(dynamicMenuAttributeName);
103                 configurationElement = null;
104                 return true;
105
106             } catch (final ClassCastException JavaDoc e) {
107                 final String JavaDoc message = "The proxied dynamic menu was the wrong class"; //$NON-NLS-1$
108
final IStatus status = new Status(IStatus.ERROR,
109                         WorkbenchPlugin.PI_WORKBENCH, 0, message, e);
110                 WorkbenchPlugin.log(message, status);
111                 return false;
112
113             } catch (final CoreException e) {
114                 final String JavaDoc message = "The proxied dynamic menu for '" + configurationElement.getAttribute(dynamicMenuAttributeName) //$NON-NLS-1$
115
+ "' could not be loaded"; //$NON-NLS-1$
116
final IStatus status = new Status(IStatus.ERROR,
117                         WorkbenchPlugin.PI_WORKBENCH, 0, message, e);
118                 WorkbenchPlugin.log(message, status);
119                 return false;
120
121             }
122         }
123
124         return true;
125     }
126
127     public final String JavaDoc toString() {
128         if (dynamicMenu == null) {
129             return configurationElement.getAttribute(dynamicMenuAttributeName);
130         }
131
132         return dynamicMenu.toString();
133     }
134 }
135
Popular Tags