01: /*******************************************************************************
02: * Copyright (c) 2005 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.commands;
11:
12: import java.util.Collections;
13: import java.util.HashMap;
14: import java.util.Hashtable;
15: import java.util.Map;
16: import java.util.StringTokenizer;
17:
18: import org.eclipse.core.commands.IParameterValues;
19: import org.eclipse.core.runtime.IConfigurationElement;
20: import org.eclipse.core.runtime.IExecutableExtension;
21:
22: /**
23: * <p>
24: * A generic implementation of <code>IParameterValues</code> that takes advantage
25: * of the <code>IExecutableExtension</code> mechanism. The parameter values and names can be
26: * specified purely in XML. This can be done as follows:
27: * </p>
28: * <p><pre><code>
29: * <command
30: * name="%name"
31: * description="%description"
32: * categoryId="categoryId"
33: * id="commandId">
34: * <parameter
35: * id="parameterId"
36: * name="%parameterName">
37: * <values class="org.eclipse.ui.commands.ExtensionParameterValues">
38: * <parameter name="%parameterName1" value="parameterValue1" />
39: * <parameter name="%parameterName2" value="parameterValue2" />
40: * <parameter name="%parameterName3" value="parameterValue3" />
41: * </values>
42: * </parameter>
43: * </command>
44: * </code></pre></p>
45: *
46: * @since 3.1
47: */
48: public final class ExtensionParameterValues implements
49: IParameterValues, IExecutableExtension {
50:
51: /**
52: * The delimiter between elements if the name-value pairs are specified in a
53: * single string.
54: */
55: public static final String DELIMITER = ","; //$NON-NLS-1$
56:
57: /**
58: * The parameter values for this instance. This is initialization when the
59: * executable extension is created. For example,
60: */
61: private Map parameterValues = null;
62:
63: public Map getParameterValues() {
64: return parameterValues;
65: }
66:
67: public final void setInitializationData(
68: final IConfigurationElement config,
69: final String propertyName, final Object data) {
70: if (data == null) {
71: parameterValues = Collections.EMPTY_MAP;
72:
73: } else if (data instanceof String) {
74: parameterValues = new HashMap();
75: final StringTokenizer tokenizer = new StringTokenizer(
76: (String) data, DELIMITER);
77: while (tokenizer.hasMoreTokens()) {
78: final String name = tokenizer.nextToken();
79: if (tokenizer.hasMoreTokens()) {
80: final String value = tokenizer.nextToken();
81: parameterValues.put(name, value);
82: }
83: }
84: parameterValues = Collections
85: .unmodifiableMap(parameterValues);
86:
87: } else if (data instanceof Hashtable) {
88: parameterValues = Collections
89: .unmodifiableMap((Hashtable) data);
90:
91: }
92:
93: }
94: }
|