001: /*******************************************************************************
002: * Copyright (c) 2000, 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 org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IConfigurationElement;
014: import org.eclipse.core.runtime.IStatus;
015: import org.eclipse.core.runtime.Status;
016: import org.eclipse.swt.widgets.Composite;
017: import org.eclipse.swt.widgets.CoolBar;
018: import org.eclipse.swt.widgets.Menu;
019: import org.eclipse.swt.widgets.ToolBar;
020: import org.eclipse.ui.IWorkbenchWindow;
021: import org.eclipse.ui.internal.WorkbenchPlugin;
022: import org.eclipse.ui.menus.AbstractWorkbenchTrimWidget;
023: import org.eclipse.ui.menus.IWorkbenchWidget;
024:
025: /**
026: * <p>
027: * A proxy for a widget that has been defined in XML. This delays the class
028: * loading until the widget is really asked to fill a menu collection. Asking
029: * the widget for anything will instantiate the class.
030: * </p>
031: *
032: * @since 3.2
033: */
034: final class WidgetProxy implements IWorkbenchWidget {
035:
036: /**
037: * Used to determine whether the load has been tried to
038: * prevent multiple retries at a failed load.
039: */
040: private boolean firstLoad = true;
041:
042: /**
043: * The configuration element from which the widget can be created. This
044: * value will exist until the element is converted into a real class -- at
045: * which point this value will be set to <code>null</code>.
046: */
047: private IConfigurationElement configurationElement;
048:
049: /**
050: * The real widget. This value is <code>null</code> until the proxy is
051: * forced to load the real widget. At this point, the configuration element
052: * is converted, nulled out, and this widget gains a reference.
053: */
054: private IWorkbenchWidget widget = null;
055:
056: /**
057: * The name of the configuration element attribute which contains the
058: * information necessary to instantiate the real widget.
059: */
060: private final String widgetAttributeName;
061:
062: /**
063: * Constructs a new instance of <code>WidgetProxy</code> with all the
064: * information it needs to try to load the class at a later point in time.
065: *
066: * @param configurationElement
067: * The configuration element from which the real class can be
068: * loaded at run-time; must not be <code>null</code>.
069: * @param widgetAttributeName
070: * The name of the attibute or element containing the widget
071: * executable extension; must not be <code>null</code>.
072: */
073: public WidgetProxy(
074: final IConfigurationElement configurationElement,
075: final String widgetAttributeName) {
076: if (configurationElement == null) {
077: throw new NullPointerException(
078: "The configuration element backing a widget proxy cannot be null"); //$NON-NLS-1$
079: }
080:
081: if (widgetAttributeName == null) {
082: throw new NullPointerException(
083: "The attribute containing the widget class must be known"); //$NON-NLS-1$
084: }
085:
086: this .configurationElement = configurationElement;
087: this .widgetAttributeName = widgetAttributeName;
088: }
089:
090: public final void dispose() {
091: if (loadWidget()) {
092: widget.dispose();
093: }
094: }
095:
096: public final void fill(final Composite parent) {
097: if (loadWidget()) {
098: widget.fill(parent);
099: }
100: }
101:
102: public final void fill(final CoolBar parent, final int index) {
103: if (loadWidget()) {
104: widget.fill(parent, index);
105: }
106: }
107:
108: public final void fill(final Menu parent, final int index) {
109: if (loadWidget()) {
110: widget.fill(parent, index);
111: }
112: }
113:
114: public final void fill(final ToolBar parent, final int index) {
115: if (loadWidget()) {
116: widget.fill(parent, index);
117: }
118: }
119:
120: /* (non-Javadoc)
121: * @see org.eclipse.ui.menus.IWorkbenchWidget#init(org.eclipse.ui.IWorkbenchWindow)
122: */
123: public void init(IWorkbenchWindow workbenchWindow) {
124: if (loadWidget()) {
125: widget.init(workbenchWindow);
126: }
127: }
128:
129: /**
130: * Convenience method that allows the trim layout manager to
131: * inform widgets if they have changed locations. If the IWidget
132: * implementation does not support the method then we default
133: * to using the simpler <code>fill(final Composite parent)</code>.
134: *
135: * @param parent The composite to create the controls in
136: * @param oldSide The side the trim was previously displayed on
137: * @param newSide The new side that the trim will be displayed on
138: */
139: public final void fill(Composite parent, int oldSide, int newSide) {
140: if (loadWidget()) {
141: if (isMoveableTrimWidget()) {
142: ((AbstractWorkbenchTrimWidget) widget).fill(parent,
143: 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 e) {
165: final String 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 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 toString() {
202: if (widget == null) {
203: return configurationElement
204: .getAttribute(widgetAttributeName);
205: }
206:
207: return widget.toString();
208: }
209: }
|