KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > commands > ParameterValueConverterProxy


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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 package org.eclipse.ui.internal.commands;
12
13 import org.eclipse.core.commands.AbstractParameterValueConverter;
14 import org.eclipse.core.commands.ParameterValueConversionException;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
18
19 /**
20  * A proxy for a parameter value converter that has been defined in the regisry.
21  * This delays the class loading until the converter is really asked to do
22  * string/object conversions.
23  *
24  * @since 3.2
25  */

26 public final class ParameterValueConverterProxy extends
27         AbstractParameterValueConverter {
28
29     /**
30      * The configuration element providing the executable extension that will
31      * extend <code>AbstractParameterValueConverter</code>. This value will
32      * not be <code>null</code>.
33      */

34     private final IConfigurationElement converterConfigurationElement;
35
36     /**
37      * The real parameter value converter instance. This will be
38      * <code>null</code> until one of the conversion methods are used.
39      */

40     private AbstractParameterValueConverter parameterValueConverter;
41
42     /**
43      * Constructs a <code>ParameterValueConverterProxy</code> to represent the
44      * real converter until it is needed.
45      *
46      * @param converterConfigurationElement
47      * The configuration element from which the real converter can be
48      * loaded.
49      */

50     public ParameterValueConverterProxy(
51             final IConfigurationElement converterConfigurationElement) {
52         if (converterConfigurationElement == null) {
53             throw new NullPointerException JavaDoc(
54                     "converterConfigurationElement must not be null"); //$NON-NLS-1$
55
}
56
57         this.converterConfigurationElement = converterConfigurationElement;
58     }
59
60     public final Object JavaDoc convertToObject(final String JavaDoc parameterValue)
61             throws ParameterValueConversionException {
62         return getConverter().convertToObject(parameterValue);
63     }
64
65     public final String JavaDoc convertToString(final Object JavaDoc parameterValue)
66             throws ParameterValueConversionException {
67         return getConverter().convertToString(parameterValue);
68     }
69
70     /**
71      * Returns the real parameter value converter for this proxy or throws an
72      * exception indicating the converter could not be obtained.
73      *
74      * @return the real converter for this proxy; never <code>null</code>.
75      * @throws ParameterValueConversionException
76      * if the converter could not be obtained
77      */

78     private AbstractParameterValueConverter getConverter()
79             throws ParameterValueConversionException {
80         if (parameterValueConverter == null) {
81             try {
82                 parameterValueConverter = (AbstractParameterValueConverter) converterConfigurationElement
83                         .createExecutableExtension(IWorkbenchRegistryConstants.ATT_CONVERTER);
84             } catch (final CoreException e) {
85                 throw new ParameterValueConversionException(
86                         "Problem creating parameter value converter", e); //$NON-NLS-1$
87
} catch (final ClassCastException JavaDoc e) {
88                 throw new ParameterValueConversionException(
89                         "Parameter value converter was not a subclass of AbstractParameterValueConverter", e); //$NON-NLS-1$
90
}
91         }
92         return parameterValueConverter;
93     }
94 }
95
Popular Tags