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