KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 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.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.CoolBar;
20 import org.eclipse.swt.widgets.Menu;
21 import org.eclipse.swt.widgets.ToolBar;
22 import org.eclipse.ui.IWorkbenchWindow;
23 import org.eclipse.ui.internal.WorkbenchPlugin;
24 import org.eclipse.ui.menus.AbstractWorkbenchTrimWidget;
25 import org.eclipse.ui.menus.IWorkbenchWidget;
26
27 /**
28  * <p>
29  * A proxy for a widget that has been defined in XML. This delays the class
30  * loading until the widget is really asked to fill a menu collection. Asking
31  * the widget for anything will instantiate the class.
32  * </p>
33  *
34  * @since 3.2
35  */

36 final class WidgetProxy implements IWorkbenchWidget {
37
38     /**
39      * Used to determine whether the load has been tried to
40      * prevent multiple retries at a failed load.
41      */

42     private boolean firstLoad = true;
43     
44     /**
45      * The configuration element from which the widget can be created. This
46      * value will exist until the element is converted into a real class -- at
47      * which point this value will be set to <code>null</code>.
48      */

49     private IConfigurationElement configurationElement;
50
51     /**
52      * The real widget. This value is <code>null</code> until the proxy is
53      * forced to load the real widget. At this point, the configuration element
54      * is converted, nulled out, and this widget gains a reference.
55      */

56     private IWorkbenchWidget widget = null;
57
58     /**
59      * The name of the configuration element attribute which contains the
60      * information necessary to instantiate the real widget.
61      */

62     private final String JavaDoc widgetAttributeName;
63
64     /**
65      * Constructs a new instance of <code>WidgetProxy</code> with all the
66      * information it needs to try to load the class at a later point in time.
67      *
68      * @param configurationElement
69      * The configuration element from which the real class can be
70      * loaded at run-time; must not be <code>null</code>.
71      * @param widgetAttributeName
72      * The name of the attibute or element containing the widget
73      * executable extension; must not be <code>null</code>.
74      */

75     public WidgetProxy(final IConfigurationElement configurationElement,
76             final String JavaDoc widgetAttributeName) {
77         if (configurationElement == null) {
78             throw new NullPointerException JavaDoc(
79                     "The configuration element backing a widget proxy cannot be null"); //$NON-NLS-1$
80
}
81
82         if (widgetAttributeName == null) {
83             throw new NullPointerException JavaDoc(
84                     "The attribute containing the widget class must be known"); //$NON-NLS-1$
85
}
86
87         this.configurationElement = configurationElement;
88         this.widgetAttributeName = widgetAttributeName;
89     }
90
91     public final void dispose() {
92         if (loadWidget()) {
93             widget.dispose();
94         }
95     }
96
97     public final void fill(final Composite parent) {
98         if (loadWidget()) {
99             widget.fill(parent);
100         }
101     }
102
103     public final void fill(final CoolBar parent, final int index) {
104         if (loadWidget()) {
105             widget.fill(parent, index);
106         }
107     }
108
109     public final void fill(final Menu parent, final int index) {
110         if (loadWidget()) {
111             widget.fill(parent, index);
112         }
113     }
114
115     public final void fill(final ToolBar parent, final int index) {
116         if (loadWidget()) {
117             widget.fill(parent, index);
118         }
119     }
120
121     /* (non-Javadoc)
122      * @see org.eclipse.ui.menus.IWorkbenchWidget#init(org.eclipse.ui.IWorkbenchWindow)
123      */

124     public void init(IWorkbenchWindow workbenchWindow) {
125         if (loadWidget()) {
126             widget.init(workbenchWindow);
127         }
128     }
129
130     /**
131      * Convenience method that allows the trim layout manager to
132      * inform widgets if they have changed locations. If the IWidget
133      * implementation does not support the method then we default
134      * to using the simpler <code>fill(final Composite parent)</code>.
135      *
136      * @param parent The composite to create the controls in
137      * @param oldSide The side the trim was previously displayed on
138      * @param newSide The new side that the trim will be displayed on
139      */

140     public final void fill(Composite parent, int oldSide, int newSide) {
141         if (loadWidget()) {
142             if (isMoveableTrimWidget()) {
143                 ((AbstractWorkbenchTrimWidget) widget).fill(parent, oldSide, newSide);
144             } else {
145                 widget.fill(parent);
146             }
147         }
148     }
149
150     /**
151      * Loads the widget, if possible. If the widget is loaded, then the member
152      * variables are updated accordingly.
153      *
154      * @return <code>true</code> if the widget is now non-null;
155      * <code>false</code> otherwise.
156      */

157     private final boolean loadWidget() {
158         if (firstLoad) {
159             // Load the handler.
160
try {
161                 widget = (IWorkbenchWidget) configurationElement
162                         .createExecutableExtension(widgetAttributeName);
163                 configurationElement = null;
164             } catch (final ClassCastException JavaDoc e) {
165                 final String JavaDoc message = "The proxied widget was the wrong class"; //$NON-NLS-1$
166
final IStatus status = new Status(IStatus.ERROR,
167                         WorkbenchPlugin.PI_WORKBENCH, 0, message, e);
168                 WorkbenchPlugin.log(message, status);
169
170             } catch (final CoreException e) {
171                 final String JavaDoc message = "The proxied widget for '" + configurationElement.getAttribute(widgetAttributeName) //$NON-NLS-1$
172
+ "' could not be loaded"; //$NON-NLS-1$
173
IStatus status = new Status(IStatus.ERROR,
174                         WorkbenchPlugin.PI_WORKBENCH, 0, message, e);
175                 WorkbenchPlugin.log(message, status);
176             }
177         }
178
179         // We're througth the first load
180
firstLoad = false;
181         
182         // the load only succeeded if there's a widget..
183
return widget != null;
184     }
185
186     /**
187      * Determine if the widget knows how to respond to changes in the
188      * workbench 'side' that it is being displayed on.
189      *
190      * @return <code>true</code> iff the <code>IWidget</code> implementation
191      * is actually based on <code>AbstractTrimWidget</code>
192      */

193     private final boolean isMoveableTrimWidget() {
194         if (loadWidget()) {
195             return widget instanceof AbstractWorkbenchTrimWidget;
196         }
197         
198         return false;
199     }
200     
201     public final String JavaDoc toString() {
202         if (widget == null) {
203             return configurationElement.getAttribute(widgetAttributeName);
204         }
205
206         return widget.toString();
207     }
208 }
209
Popular Tags