KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > handlers > LegacyHandlerProxy


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.handlers;
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  * <p>
28  * A proxy for a handler that has been defined in XML. This delays the class
29  * loading until the handler is really asked for information (besides the
30  * priority or the command identifier). Asking a proxy for anything but the
31  * attributes defined publicly in this class will cause the proxy to instantiate
32  * the proxied handler.
33  * </p>
34  *
35  * @since 3.0
36  */

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

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

45     /**
46      * The configuration element from which the handler can be created. This
47      * value will exist until the element is converted into a real class -- at
48      * which point this value will be set to <code>null</code>.
49      */

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

57     private IHandler handler;
58
59     /**
60      * Constructs a new instance of <code>HandlerProxy</code> with all the
61      * information it needs to try to avoid loading until it is needed.
62      *
63      * @param newConfigurationElement
64      * The configuration element from which the real class can be
65      * loaded at run-time.
66      */

67     public LegacyHandlerProxy(
68             final IConfigurationElement newConfigurationElement) {
69         configurationElement = newConfigurationElement;
70         handler = null;
71     }
72
73     /**
74      * Passes the dipose on to the proxied handler, if it has been loaded.
75      */

76     public void dispose() {
77         if (handler != null) {
78             handler.dispose();
79         }
80     }
81
82     /**
83      * @see IHandler#execute(Map)
84      */

85     public Object JavaDoc execute(Map JavaDoc parameters) throws ExecutionException {
86         if (loadHandler()) {
87             return handler.execute(parameters);
88         }
89
90         return null;
91     }
92
93     /**
94      * @see IHandler#getAttributeValuesByName()
95      */

96     public Map JavaDoc getAttributeValuesByName() {
97         if (loadHandler()) {
98             return handler.getAttributeValuesByName();
99         } else {
100             return Collections.EMPTY_MAP;
101         }
102     }
103
104     /**
105      * Loads the handler, if possible. If the handler is loaded, then the member
106      * variables are updated accordingly.
107      *
108      * @return <code>true</code> if the handler is now non-null;
109      * <code>false</code> otherwise.
110      */

111     private final boolean loadHandler() {
112         if (handler == null) {
113             // Load the handler.
114
try {
115                 handler = (IHandler) configurationElement
116                         .createExecutableExtension(HANDLER_ATTRIBUTE_NAME);
117                 configurationElement = null;
118                 return true;
119             } catch (final CoreException e) {
120                 /*
121                  * TODO If it can't be instantiated, should future attempts to
122                  * instantiate be blocked?
123                  */

124                 final String JavaDoc message = "The proxied handler for '" + configurationElement.getAttribute(HANDLER_ATTRIBUTE_NAME) //$NON-NLS-1$
125
+ "' could not be loaded"; //$NON-NLS-1$
126
IStatus status = new Status(IStatus.ERROR,
127                         WorkbenchPlugin.PI_WORKBENCH, 0, message, e);
128                 WorkbenchPlugin.log(message, status);
129                 return false;
130             }
131         }
132
133         return true;
134     }
135
136     public final String JavaDoc toString() {
137         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
138
139         buffer.append("LegacyProxy("); //$NON-NLS-1$
140
if (handler == null) {
141             final String JavaDoc className = configurationElement
142                     .getAttribute(HANDLER_ATTRIBUTE_NAME);
143             buffer.append(className);
144         } else {
145             buffer.append(handler);
146         }
147         buffer.append(')');
148
149         return buffer.toString();
150     }
151 }
152
Popular Tags