Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 731168a2c38e37125c68ba4135a4f0ea63d7118a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tm.te.runtime.extensions;

import java.util.Hashtable;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tm.te.runtime.activator.CoreBundleActivator;
import org.eclipse.tm.te.runtime.interfaces.extensions.IExecutableExtension;
import org.eclipse.tm.te.runtime.nls.Messages;

/**
 * Executable extension implementation.
 */
public class ExecutableExtension extends PlatformObject implements IExecutableExtension {
	// The mandatory id of the extension
	private String id = null;
	// The label of the extension
	private String label = null;
	// The description of the extension
	private String description = null;

	/**
	 * Clone the initialization data to the given executable extension instance.
	 *
	 * @param other The destination executable extension instance. Must not be <code>null</code>.
	 */
	public void cloneInitializationData(ExecutableExtension other) {
		Assert.isNotNull(other);
		other.id = id;
		other.label = label;
		other.description = description;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement, java.lang.String, java.lang.Object)
	 */
	@Override
	public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
		if (config != null) doSetInitializationData(config, propertyName, data);
	}

	/**
	 * Executes the {@link #setInitializationData(IConfigurationElement, String, Object)}.
	 *
	 * @param config The configuration element. Must not be <code>null</code>.
	 * @param propertyName The name of an attribute of the configuration element used on the <code>createExecutableExtension(String)<code> call.
	 *                     This argument can be used in the cases where a single configuration element is used to define multiple
	 *                     executable extensions.
 	 * @param data Adapter data in the form of a String, a {@link Hashtable}, or <code>null</code>.
 	 *
	 * @throws CoreException - if error(s) detected during initialization processing
	 */
	public void doSetInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
		Assert.isNotNull(config);

		// Initialize the id field by reading the <id> extension attribute.
		// Throws an exception if the id is empty or null.
		id = config != null ? config.getAttribute("id") : null; //$NON-NLS-1$
		if (id == null || (id != null && "".equals(id.trim()))) { //$NON-NLS-1$
			throw createMissingMandatoryAttributeException("id", config.getContributor().getName()); //$NON-NLS-1$
		}

		// Try the "label" attribute first
		String label = config != null ? config.getAttribute("label") : null; //$NON-NLS-1$
		// If "label" is not found or empty, try the "name" attribute as fallback
		if (label == null || "".equals(label.trim())) { //$NON-NLS-1$
			label = config != null ? config.getAttribute("name") : null; //$NON-NLS-1$
		}

		// Read the description text from the "<description>" child element
		IConfigurationElement[] children = config != null ? config.getChildren("description") : null; //$NON-NLS-1$
		// Only one description element is allow. All other will be ignored
		if (children != null && children.length > 0) {
			IConfigurationElement element = children[0];
			description = element.getValue();
		}
	}

	/**
	 * Creates a new {@link CoreException} to be thrown if a mandatory extension attribute
	 * is missing.
	 *
	 * @param attributeName The attribute name. Must not be <code>null</code>.
	 * @param extensionId The extension id. Must not be <code>null</code>.
	 *
	 * @return The {@link CoreException} instance.
	 */
	protected CoreException createMissingMandatoryAttributeException(String attributeName, String extensionId) {
		Assert.isNotNull(attributeName);
		Assert.isNotNull(extensionId);

		return new CoreException(new Status(IStatus.ERROR,
				CoreBundleActivator.getUniqueIdentifier(),
				0,
				NLS.bind(Messages.Extension_error_missingRequiredAttribute, attributeName, extensionId),
				null));
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tm.te.runtime.interfaces.extensions.IExecutableExtension#getId()
	 */
	@Override
	public String getId() {
		return id;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tm.te.runtime.interfaces.extensions.IExecutableExtension#getLabel()
	 */
	@Override
	public String getLabel() {
		return label != null ? label.trim() : ""; //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tm.te.runtime.interfaces.extensions.IExecutableExtension#getDescription()
	 */
	@Override
	public String getDescription() {
		return description != null ? description.trim() : ""; //$NON-NLS-1$
	}
}

Back to the top