01: /*******************************************************************************
02: * Copyright (c) 2005, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.commands;
11:
12: import org.eclipse.core.commands.AbstractParameterValueConverter;
13: import org.eclipse.core.commands.ParameterValueConversionException;
14: import org.eclipse.core.runtime.CoreException;
15: import org.eclipse.core.runtime.IConfigurationElement;
16: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
17:
18: /**
19: * A proxy for a parameter value converter that has been defined in the regisry.
20: * This delays the class loading until the converter is really asked to do
21: * string/object conversions.
22: *
23: * @since 3.2
24: */
25: public final class ParameterValueConverterProxy extends
26: AbstractParameterValueConverter {
27:
28: /**
29: * The configuration element providing the executable extension that will
30: * extend <code>AbstractParameterValueConverter</code>. This value will
31: * not be <code>null</code>.
32: */
33: private final IConfigurationElement converterConfigurationElement;
34:
35: /**
36: * The real parameter value converter instance. This will be
37: * <code>null</code> until one of the conversion methods are used.
38: */
39: private AbstractParameterValueConverter parameterValueConverter;
40:
41: /**
42: * Constructs a <code>ParameterValueConverterProxy</code> to represent the
43: * real converter until it is needed.
44: *
45: * @param converterConfigurationElement
46: * The configuration element from which the real converter can be
47: * loaded.
48: */
49: public ParameterValueConverterProxy(
50: final IConfigurationElement converterConfigurationElement) {
51: if (converterConfigurationElement == null) {
52: throw new NullPointerException(
53: "converterConfigurationElement must not be null"); //$NON-NLS-1$
54: }
55:
56: this .converterConfigurationElement = converterConfigurationElement;
57: }
58:
59: public final Object convertToObject(final String parameterValue)
60: throws ParameterValueConversionException {
61: return getConverter().convertToObject(parameterValue);
62: }
63:
64: public final String convertToString(final Object parameterValue)
65: throws ParameterValueConversionException {
66: return getConverter().convertToString(parameterValue);
67: }
68:
69: /**
70: * Returns the real parameter value converter for this proxy or throws an
71: * exception indicating the converter could not be obtained.
72: *
73: * @return the real converter for this proxy; never <code>null</code>.
74: * @throws ParameterValueConversionException
75: * if the converter could not be obtained
76: */
77: private AbstractParameterValueConverter getConverter()
78: throws ParameterValueConversionException {
79: if (parameterValueConverter == null) {
80: try {
81: parameterValueConverter = (AbstractParameterValueConverter) converterConfigurationElement
82: .createExecutableExtension(IWorkbenchRegistryConstants.ATT_CONVERTER);
83: } catch (final CoreException e) {
84: throw new ParameterValueConversionException(
85: "Problem creating parameter value converter", e); //$NON-NLS-1$
86: } catch (final ClassCastException e) {
87: throw new ParameterValueConversionException(
88: "Parameter value converter was not a subclass of AbstractParameterValueConverter", e); //$NON-NLS-1$
89: }
90: }
91: return parameterValueConverter;
92: }
93: }
|