KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > commands > ws > HandlerProxy


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.commands.ws;
13
14 import java.util.Collections JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.ui.commands.AbstractHandler;
22 import org.eclipse.ui.commands.ExecutionException;
23 import org.eclipse.ui.commands.IHandler;
24 import org.eclipse.ui.internal.WorkbenchPlugin;
25
26 /**
27  * A proxy for a handler that has been defined in XML. This delays the class
28  * loading until the handler is really asked for information (besides the
29  * priority or the command identifier). Asking a proxy for anything but the
30  * attributes defined publicly in this class will cause the proxy to instantiate
31  * the proxied handler.
32  *
33  * @since 3.0
34  */

35 public final class HandlerProxy extends AbstractHandler {
36
37     /**
38      * The name of the configuration element attribute which contains the
39      * information necessary to instantiate the real handler.
40      */

41     private static final String JavaDoc HANDLER_ATTRIBUTE_NAME = "handler"; //$NON-NLS-1$
42

43     /**
44      * The identifier for the command to which this proxy should be associated.
45      * This value should never be <code>null</code>.
46      */

47     private final String JavaDoc commandId;
48
49     /**
50      * The configuration element from which the handler can be created. This
51      * value will exist until the element is converted into a real class -- at
52      * which point this value will be set to <code>null</code>.
53      */

54     private IConfigurationElement configurationElement;
55
56     /**
57      * The real handler. This value is <code>null</code> until the proxy is
58      * forced to load the real handler. At this point, the configuration element
59      * is converted, nulled out, and this handler gains a reference.
60      */

61     private IHandler handler;
62
63     /**
64      * Constructs a new instance of <code>HandlerProxy</code> with all the
65      * information it needs to try to avoid loading until it is needed.
66      *
67      * @param newCommandId
68      * The identifier for the command to which this proxy should be
69      * associated; must not be <code>null</code>.
70      * @param newConfigurationElement
71      * The configuration element from which the real class can be
72      * loaded at run-time.
73      */

74     public HandlerProxy(final String JavaDoc newCommandId,
75             final IConfigurationElement newConfigurationElement) {
76         commandId = newCommandId;
77         configurationElement = newConfigurationElement;
78         handler = null;
79     }
80     
81     /**
82      * Passes the dipose on to the proxied handler, if it has been loaded.
83      */

84     public void dispose() {
85         if (handler != null) {
86             handler.dispose();
87         }
88     }
89
90     /**
91      * @see IHandler#execute(Map)
92      */

93     public Object JavaDoc execute(Map JavaDoc parameterValuesByName) throws ExecutionException {
94         if (loadHandler()) { return handler.execute(parameterValuesByName); }
95
96         return null;
97     }
98
99     /**
100      * An accessor for the identifier of the command to which the proxied
101      * handler should be associated.
102      *
103      * @return The command identifier; should never be <code>null</code>.
104      */

105     final String JavaDoc getCommandId() {
106         return commandId;
107     }
108
109     /**
110      * @see IHandler#getAttributeValuesByName()
111      */

112     public Map JavaDoc getAttributeValuesByName() {
113         if (loadHandler())
114             return handler.getAttributeValuesByName();
115         else
116             return Collections.EMPTY_MAP;
117     }
118
119     /**
120      * Loads the handler, if possible. If the handler is loaded, then the member
121      * variables are updated accordingly.
122      *
123      * @return <code>true</code> if the handler is now non-null;
124      * <code>false</code> otherwise.
125      */

126     private final boolean loadHandler() {
127         if (handler == null) {
128             // Load the handler.
129
try {
130                 handler = (IHandler) configurationElement
131                         .createExecutableExtension(HANDLER_ATTRIBUTE_NAME);
132                 configurationElement = null;
133                 return true;
134             } catch (final CoreException e) {
135                 /*
136                  * TODO If it can't be instantiated, should future attempts to
137                  * instantiate be blocked?
138                  */

139                 final String JavaDoc message = "The proxied handler for '" + commandId //$NON-NLS-1$
140
+ "' could not be loaded"; //$NON-NLS-1$
141
IStatus status = new Status(IStatus.ERROR,
142                         WorkbenchPlugin.PI_WORKBENCH, 0, message, e);
143                 WorkbenchPlugin.log(message, status);
144                 return false;
145             }
146         }
147
148         return true;
149     }
150 }
151
Popular Tags